Chats: fix crash in emoji autosuggest
This commit is contained in:
parent
67ffe9609f
commit
f8b20858a3
4 changed files with 35 additions and 32 deletions
|
@ -110,7 +110,7 @@ const ChatComposer = React.forwardRef<HTMLTextAreaElement | null, IChatComposer>
|
||||||
);
|
);
|
||||||
|
|
||||||
if (token && tokenStart) {
|
if (token && tokenStart) {
|
||||||
const results = emojiSearch(token.replace(':', ''), { maxResults: 5 } as any);
|
const results = emojiSearch(token.replace(':', ''), { maxResults: 5 });
|
||||||
setSuggestions({
|
setSuggestions({
|
||||||
list: results,
|
list: results,
|
||||||
token,
|
token,
|
||||||
|
|
|
@ -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 data from './data';
|
||||||
|
|
||||||
import type { Emoji } from './index';
|
import type { Emoji } from './index';
|
||||||
// import type { Emoji as EmojiMart, CustomEmoji } from 'emoji-mart';
|
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
const index = new Index({
|
const index = new Index({
|
||||||
tokenize: 'full',
|
tokenize: 'full',
|
||||||
optimize: true,
|
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'
|
// 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<ImmutableMap<string, string>>,
|
||||||
|
): Emoji[] => {
|
||||||
return index.search(str, maxResults)
|
return index.search(str, maxResults)
|
||||||
.flatMap((id: string) => {
|
.flatMap((id) => {
|
||||||
if (id[0] === 'c') {
|
if (typeof id !== 'string') return;
|
||||||
const { shortcode, static_url } = custom_emojis.get((id as string).slice(1)).toJS();
|
|
||||||
|
|
||||||
|
if (id[0] === 'c' && custom_emojis) {
|
||||||
|
const index = Number(id.slice(1));
|
||||||
|
const custom = custom_emojis.get(index);
|
||||||
|
|
||||||
|
if (custom) {
|
||||||
return {
|
return {
|
||||||
id: shortcode,
|
id: custom.get('shortcode', ''),
|
||||||
colons: ':' + shortcode + ':',
|
colons: ':' + custom.get('shortcode', '') + ':',
|
||||||
custom: true,
|
custom: true,
|
||||||
imageUrl: static_url,
|
imageUrl: custom.get('static_url', ''),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const { skins } = data.emojis[(id as string).slice(1)];
|
const skins = data.emojis[id.slice(1)]?.skins;
|
||||||
|
|
||||||
|
if (skins) {
|
||||||
return {
|
return {
|
||||||
id: (id as string).slice(1),
|
id: id.slice(1),
|
||||||
colons: ':' + id.slice(1) + ':',
|
colons: ':' + id.slice(1) + ':',
|
||||||
unified: skins[0].unified,
|
unified: skins[0].unified,
|
||||||
native: skins[0].native,
|
native: skins[0].native,
|
||||||
};
|
};
|
||||||
});
|
}
|
||||||
|
}).filter(Boolean) as Emoji[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export default search;
|
export default search;
|
||||||
|
|
|
@ -73,7 +73,6 @@
|
||||||
"@tanstack/react-query": "^4.0.10",
|
"@tanstack/react-query": "^4.0.10",
|
||||||
"@testing-library/react": "^14.0.0",
|
"@testing-library/react": "^14.0.0",
|
||||||
"@types/escape-html": "^1.0.1",
|
"@types/escape-html": "^1.0.1",
|
||||||
"@types/flexsearch": "^0.7.3",
|
|
||||||
"@types/http-link-header": "^1.0.3",
|
"@types/http-link-header": "^1.0.3",
|
||||||
"@types/jest": "^29.0.0",
|
"@types/jest": "^29.0.0",
|
||||||
"@types/leaflet": "^1.8.0",
|
"@types/leaflet": "^1.8.0",
|
||||||
|
@ -118,7 +117,7 @@
|
||||||
"emoji-mart": "^5.5.2",
|
"emoji-mart": "^5.5.2",
|
||||||
"escape-html": "^1.0.3",
|
"escape-html": "^1.0.3",
|
||||||
"exif-js": "^2.3.0",
|
"exif-js": "^2.3.0",
|
||||||
"flexsearch": "^0.7.31",
|
"flexsearch-ts": "^0.7.31",
|
||||||
"fork-ts-checker-webpack-plugin": "^8.0.0",
|
"fork-ts-checker-webpack-plugin": "^8.0.0",
|
||||||
"graphemesplit": "^2.4.4",
|
"graphemesplit": "^2.4.4",
|
||||||
"html-webpack-harddisk-plugin": "^2.0.0",
|
"html-webpack-harddisk-plugin": "^2.0.0",
|
||||||
|
|
11
yarn.lock
11
yarn.lock
|
@ -4143,11 +4143,6 @@
|
||||||
resolved "https://registry.yarnpkg.com/@types/filewriter/-/filewriter-0.0.29.tgz#a48795ecadf957f6c0d10e0c34af86c098fa5bee"
|
resolved "https://registry.yarnpkg.com/@types/filewriter/-/filewriter-0.0.29.tgz#a48795ecadf957f6c0d10e0c34af86c098fa5bee"
|
||||||
integrity sha512-BsPXH/irW0ht0Ji6iw/jJaK8Lj3FJemon2gvEqHKpCdDCeemHa+rI3WBGq5z7cDMZgoLjY40oninGxqk+8NzNQ==
|
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":
|
"@types/fs-extra@^9.0.1":
|
||||||
version "9.0.13"
|
version "9.0.13"
|
||||||
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-9.0.13.tgz#7594fbae04fe7f1918ce8b3d213f74ff44ac1f45"
|
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"
|
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.2.tgz#64bfed5cb68fe3ca78b3eb214ad97b63bedce561"
|
||||||
integrity sha512-JaTY/wtrcSyvXJl4IMFHPKyFur1sE9AUqc0QnhOaJ0CxHtAoIV8pYDzeEfAaNEtGkOfq4gr3LBFmdXW5mOQFnA==
|
integrity sha512-JaTY/wtrcSyvXJl4IMFHPKyFur1sE9AUqc0QnhOaJ0CxHtAoIV8pYDzeEfAaNEtGkOfq4gr3LBFmdXW5mOQFnA==
|
||||||
|
|
||||||
flexsearch@^0.7.31:
|
flexsearch-ts@^0.7.31:
|
||||||
version "0.7.31"
|
version "0.7.31"
|
||||||
resolved "https://registry.yarnpkg.com/flexsearch/-/flexsearch-0.7.31.tgz#065d4110b95083110b9b6c762a71a77cc52e4702"
|
resolved "https://registry.yarnpkg.com/flexsearch-ts/-/flexsearch-ts-0.7.31.tgz#0353f51789ad8e7660c3df157534dcf2d346a20f"
|
||||||
integrity sha512-XGozTsMPYkm+6b5QL3Z9wQcJjNYxp0CYn3U1gO7dwD6PAqU1SVWZxI9CCg3z+ml3YfqdPnrBehaBrnH2AGKbNA==
|
integrity sha512-Z3geBbHiPw/JALe/thvxTd1LAgDcUNvQuHWGjhO4lG7gOR5IVVPsyS8tRt/qmme9HgXj3zdtHC4yJ3anGW1Xmw==
|
||||||
|
|
||||||
flush-write-stream@^1.0.0:
|
flush-write-stream@^1.0.0:
|
||||||
version "1.1.1"
|
version "1.1.1"
|
||||||
|
|
Loading…
Reference in a new issue