feat: require at least two characters to autocomplete emoji (#298)

This commit is contained in:
bree 2021-10-31 18:36:38 -04:00 committed by GitHub
parent 3ef3f84877
commit 1800aace43
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,19 +14,19 @@ import UserIcon from "./user/UserIcon";
export type AutoCompleteState = export type AutoCompleteState =
| { type: "none" } | { type: "none" }
| ({ selected: number; within: boolean } & ( | ({ selected: number; within: boolean } & (
| { | {
type: "emoji"; type: "emoji";
matches: string[]; matches: string[];
} }
| { | {
type: "user"; type: "user";
matches: User[]; matches: User[];
} }
| { | {
type: "channel"; type: "channel";
matches: Channel[]; matches: Channel[];
} }
)); ));
export type SearchClues = { export type SearchClues = {
users?: { type: "channel"; id: string } | { type: "all" }; users?: { type: "channel"; id: string } | { type: "all" };
@ -79,13 +79,15 @@ export function useAutoComplete(
if (current === ":" || current === "@" || current === "#") { if (current === ":" || current === "@" || current === "#") {
const search = content.slice(j + 1, content.length); const search = content.slice(j + 1, content.length);
if (search.length > 0) { const minLen = current === ":" ? 2 : 1
if (search.length >= minLen) {
return [ return [
current === "#" current === "#"
? "channel" ? "channel"
: current === ":" : current === ":"
? "emoji" ? "emoji"
: "user", : "user",
search.toLowerCase(), search.toLowerCase(),
j + 1, j + 1,
]; ];
@ -165,8 +167,8 @@ export function useAutoComplete(
const matches = ( const matches = (
search.length > 0 search.length > 0
? users.filter((user) => ? users.filter((user) =>
user.username.toLowerCase().match(regex), user.username.toLowerCase().match(regex),
) )
: users : users
) )
.splice(0, 5) .splice(0, 5)
@ -197,8 +199,8 @@ export function useAutoComplete(
const matches = ( const matches = (
search.length > 0 search.length > 0
? channels.filter((channel) => ? channels.filter((channel) =>
channel.name!.toLowerCase().match(regex), channel.name!.toLowerCase().match(regex),
) )
: channels : channels
) )
.splice(0, 5) .splice(0, 5)
@ -415,7 +417,7 @@ export default function AutoComplete({
<Emoji <Emoji
emoji={ emoji={
(emojiDictionary as Record<string, string>)[ (emojiDictionary as Record<string, string>)[
match match
] ]
} }
size={20} size={20}