From f8b20858a391c3ca057217dad35a483d5e53c148 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 19 Mar 2023 19:56:01 -0500 Subject: [PATCH] Chats: fix crash in emoji autosuggest --- .../chats/components/chat-composer.tsx | 2 +- app/soapbox/features/emoji/search.ts | 51 +++++++++++-------- package.json | 3 +- yarn.lock | 11 ++-- 4 files changed, 35 insertions(+), 32 deletions(-) diff --git a/app/soapbox/features/chats/components/chat-composer.tsx b/app/soapbox/features/chats/components/chat-composer.tsx index 11b22ba5d..c5e4900e2 100644 --- a/app/soapbox/features/chats/components/chat-composer.tsx +++ b/app/soapbox/features/chats/components/chat-composer.tsx @@ -110,7 +110,7 @@ const ChatComposer = React.forwardRef ); if (token && tokenStart) { - const results = emojiSearch(token.replace(':', ''), { maxResults: 5 } as any); + const results = emojiSearch(token.replace(':', ''), { maxResults: 5 }); setSuggestions({ list: results, token, diff --git a/app/soapbox/features/emoji/search.ts b/app/soapbox/features/emoji/search.ts index 119636a29..dbcb29756 100644 --- a/app/soapbox/features/emoji/search.ts +++ b/app/soapbox/features/emoji/search.ts @@ -1,11 +1,10 @@ -import { Index } from 'flexsearch'; +import { Index } from 'flexsearch-ts'; +import { Map as ImmutableMap, List as ImmutableList } from 'immutable'; import data from './data'; import type { Emoji } from './index'; -// import type { Emoji as EmojiMart, CustomEmoji } from 'emoji-mart'; -// @ts-ignore const index = new Index({ tokenize: 'full', optimize: true, @@ -37,29 +36,39 @@ export const addCustomToPool = (customEmojis: any[]) => { }; // we can share an index by prefixing custom emojis with 'c' and native with 'n' -const search = (str: string, { maxResults = 5, custom }: searchOptions = {}, custom_emojis?: any): Emoji[] => { +const search = ( + str: string, { maxResults = 5 }: searchOptions = {}, + custom_emojis?: ImmutableList>, +): Emoji[] => { return index.search(str, maxResults) - .flatMap((id: string) => { - if (id[0] === 'c') { - const { shortcode, static_url } = custom_emojis.get((id as string).slice(1)).toJS(); + .flatMap((id) => { + if (typeof id !== 'string') return; - return { - id: shortcode, - colons: ':' + shortcode + ':', - custom: true, - imageUrl: static_url, - }; + if (id[0] === 'c' && custom_emojis) { + const index = Number(id.slice(1)); + const custom = custom_emojis.get(index); + + if (custom) { + return { + id: custom.get('shortcode', ''), + colons: ':' + custom.get('shortcode', '') + ':', + custom: true, + imageUrl: custom.get('static_url', ''), + }; + } } - const { skins } = data.emojis[(id as string).slice(1)]; + const skins = data.emojis[id.slice(1)]?.skins; - return { - id: (id as string).slice(1), - colons: ':' + id.slice(1) + ':', - unified: skins[0].unified, - native: skins[0].native, - }; - }); + if (skins) { + return { + id: id.slice(1), + colons: ':' + id.slice(1) + ':', + unified: skins[0].unified, + native: skins[0].native, + }; + } + }).filter(Boolean) as Emoji[]; }; export default search; diff --git a/package.json b/package.json index f49c8f883..079f2173f 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,6 @@ "@tanstack/react-query": "^4.0.10", "@testing-library/react": "^14.0.0", "@types/escape-html": "^1.0.1", - "@types/flexsearch": "^0.7.3", "@types/http-link-header": "^1.0.3", "@types/jest": "^29.0.0", "@types/leaflet": "^1.8.0", @@ -118,7 +117,7 @@ "emoji-mart": "^5.5.2", "escape-html": "^1.0.3", "exif-js": "^2.3.0", - "flexsearch": "^0.7.31", + "flexsearch-ts": "^0.7.31", "fork-ts-checker-webpack-plugin": "^8.0.0", "graphemesplit": "^2.4.4", "html-webpack-harddisk-plugin": "^2.0.0", diff --git a/yarn.lock b/yarn.lock index 65e08987f..12eb37b1c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4143,11 +4143,6 @@ resolved "https://registry.yarnpkg.com/@types/filewriter/-/filewriter-0.0.29.tgz#a48795ecadf957f6c0d10e0c34af86c098fa5bee" integrity sha512-BsPXH/irW0ht0Ji6iw/jJaK8Lj3FJemon2gvEqHKpCdDCeemHa+rI3WBGq5z7cDMZgoLjY40oninGxqk+8NzNQ== -"@types/flexsearch@^0.7.3": - version "0.7.3" - resolved "https://registry.yarnpkg.com/@types/flexsearch/-/flexsearch-0.7.3.tgz#ee79b1618035c82284278e05652e91116765b634" - integrity sha512-HXwADeHEP4exXkCIwy2n1+i0f1ilP1ETQOH5KDOugjkTFZPntWo0Gr8stZOaebkxsdx+k0X/K6obU/+it07ocg== - "@types/fs-extra@^9.0.1": version "9.0.13" resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-9.0.13.tgz#7594fbae04fe7f1918ce8b3d213f74ff44ac1f45" @@ -9045,10 +9040,10 @@ flatted@^3.1.0: resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.2.tgz#64bfed5cb68fe3ca78b3eb214ad97b63bedce561" integrity sha512-JaTY/wtrcSyvXJl4IMFHPKyFur1sE9AUqc0QnhOaJ0CxHtAoIV8pYDzeEfAaNEtGkOfq4gr3LBFmdXW5mOQFnA== -flexsearch@^0.7.31: +flexsearch-ts@^0.7.31: version "0.7.31" - resolved "https://registry.yarnpkg.com/flexsearch/-/flexsearch-0.7.31.tgz#065d4110b95083110b9b6c762a71a77cc52e4702" - integrity sha512-XGozTsMPYkm+6b5QL3Z9wQcJjNYxp0CYn3U1gO7dwD6PAqU1SVWZxI9CCg3z+ml3YfqdPnrBehaBrnH2AGKbNA== + resolved "https://registry.yarnpkg.com/flexsearch-ts/-/flexsearch-ts-0.7.31.tgz#0353f51789ad8e7660c3df157534dcf2d346a20f" + integrity sha512-Z3geBbHiPw/JALe/thvxTd1LAgDcUNvQuHWGjhO4lG7gOR5IVVPsyS8tRt/qmme9HgXj3zdtHC4yJ3anGW1Xmw== flush-write-stream@^1.0.0: version "1.1.1"