-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TS migration] Migrate 'EmojiSuggestions.js' component to TypeScript #30416
Changes from all commits
d4095f6
8c93470
8b38fe8
18fc871
e4245e4
05b718e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,20 @@ import CONST from '@src/CONST'; | |
import Timing from './actions/Timing'; | ||
import Trie from './Trie'; | ||
|
||
type Emoji = { | ||
type HeaderEmoji = { | ||
code: string; | ||
header?: boolean; | ||
icon?: React.FC<SvgProps>; | ||
name?: string; | ||
header: boolean; | ||
icon: React.FC<SvgProps>; | ||
}; | ||
|
||
type SimpleEmoji = { | ||
code: string; | ||
name: string; | ||
types?: string[]; | ||
}; | ||
|
||
type Emoji = HeaderEmoji | SimpleEmoji; | ||
|
||
type LocalizedEmoji = { | ||
name?: string; | ||
keywords: string[]; | ||
|
@@ -51,7 +57,7 @@ type EmojiTrie = { | |
* @param name The localized name of the emoji. | ||
* @param shouldPrependKeyword Prepend the keyword (instead of append) to the suggestions | ||
*/ | ||
function addKeywordsToTrie(trie: Trie<EmojiMetaData>, keywords: string[], item: Emoji, name: string, shouldPrependKeyword = false) { | ||
function addKeywordsToTrie(trie: Trie<EmojiMetaData>, keywords: string[], item: SimpleEmoji, name: string, shouldPrependKeyword = false) { | ||
keywords.forEach((keyword) => { | ||
const keywordNode = trie.search(keyword); | ||
if (!keywordNode) { | ||
|
@@ -84,37 +90,35 @@ function createTrie(lang: SupportedLanguage = CONST.LOCALES.DEFAULT): Trie<Emoji | |
const defaultLangEmojis: LocalizedEmojis = localeEmojis[CONST.LOCALES.DEFAULT]; | ||
const isDefaultLocale = lang === CONST.LOCALES.DEFAULT; | ||
|
||
emojis.forEach((item: Emoji) => { | ||
if (!item.name) { | ||
return; | ||
} | ||
|
||
const englishName = item.name; | ||
const localeName = langEmojis?.[item.code]?.name ?? englishName; | ||
|
||
const node = trie.search(localeName); | ||
if (!node) { | ||
trie.add(localeName, {code: item.code, types: item.types, name: localeName, suggestions: []}); | ||
} else { | ||
trie.update(localeName, {code: item.code, types: item.types, name: localeName, suggestions: node.metaData.suggestions}); | ||
} | ||
|
||
const nameParts = getNameParts(localeName).slice(1); // We remove the first part because we already index the full name. | ||
addKeywordsToTrie(trie, nameParts, item, localeName); | ||
|
||
// Add keywords for both the locale language and English to enable users to search using either language. | ||
const keywords = (langEmojis?.[item.code]?.keywords ?? []).concat(isDefaultLocale ? [] : defaultLangEmojis?.[item.code]?.keywords ?? []); | ||
addKeywordsToTrie(trie, keywords, item, localeName); | ||
|
||
/** | ||
* If current language isn't the default, prepend the English name of the emoji in the suggestions as well. | ||
* We do this because when the user types the english name of the emoji, we want to show the emoji in the suggestions before all the others. | ||
*/ | ||
if (!isDefaultLocale) { | ||
const englishNameParts = getNameParts(englishName); | ||
addKeywordsToTrie(trie, englishNameParts, item, localeName, true); | ||
} | ||
}); | ||
emojis | ||
.filter((item: Emoji): item is SimpleEmoji => !(item as HeaderEmoji).header) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you explain the purpose of this filter()? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fabioh8010 removing of the
and other usual emojis. Header emoji is not used in emoji suggestions and was filtered out before with a check.
I replaced it with a filter instead to add type narrowing to identify that only simple emojis are used in the suggestions. Btw, before the migration of the EmojiTrie file it also checked for the header field instead of name to filter header emoji out. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for explanation @VickyStash ! |
||
.forEach((item: SimpleEmoji) => { | ||
const englishName = item.name; | ||
const localeName = langEmojis?.[item.code]?.name ?? englishName; | ||
|
||
const node = trie.search(localeName); | ||
if (!node) { | ||
trie.add(localeName, {code: item.code, types: item.types, name: localeName, suggestions: []}); | ||
} else { | ||
trie.update(localeName, {code: item.code, types: item.types, name: localeName, suggestions: node.metaData.suggestions}); | ||
} | ||
|
||
const nameParts = getNameParts(localeName).slice(1); // We remove the first part because we already index the full name. | ||
addKeywordsToTrie(trie, nameParts, item, localeName); | ||
|
||
// Add keywords for both the locale language and English to enable users to search using either language. | ||
const keywords = (langEmojis?.[item.code]?.keywords ?? []).concat(isDefaultLocale ? [] : defaultLangEmojis?.[item.code]?.keywords ?? []); | ||
addKeywordsToTrie(trie, keywords, item, localeName); | ||
|
||
/** | ||
* If current language isn't the default, prepend the English name of the emoji in the suggestions as well. | ||
* We do this because when the user types the english name of the emoji, we want to show the emoji in the suggestions before all the others. | ||
*/ | ||
if (!isDefaultLocale) { | ||
const englishNameParts = getNameParts(englishName); | ||
addKeywordsToTrie(trie, englishNameParts, item, localeName, true); | ||
} | ||
}); | ||
|
||
return trie; | ||
} | ||
|
@@ -124,3 +128,4 @@ const emojiTrie: EmojiTrie = supportedLanguages.reduce((prev, cur) => ({...prev, | |
Timing.end(CONST.TIMING.TRIE_INITIALIZATION); | ||
|
||
export default emojiTrie; | ||
export type {SimpleEmoji}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this condition removed?