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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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