Add localized emoji search
This commit is contained in:
parent
ce0fb22041
commit
e90553b3b3
17 changed files with 878 additions and 97 deletions
63
ts/hooks/useEmojiSearch.ts
Normal file
63
ts/hooks/useEmojiSearch.ts
Normal file
|
@ -0,0 +1,63 @@
|
|||
// Copyright 2024 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { useEffect, useCallback, useRef } from 'react';
|
||||
import data from 'emoji-datasource';
|
||||
|
||||
import { createSearch } from '../components/emoji/lib';
|
||||
import type { SearchEmojiListType } from '../components/emoji/lib';
|
||||
import { drop } from '../util/drop';
|
||||
import * as log from '../logging/log';
|
||||
|
||||
const uninitialized: SearchEmojiListType = data.map(
|
||||
({ short_name: shortName, short_names: shortNames }) => {
|
||||
return {
|
||||
shortName,
|
||||
rank: 0,
|
||||
tags: shortNames,
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
const defaultSearch = createSearch(uninitialized);
|
||||
|
||||
export function useEmojiSearch(
|
||||
locale: string
|
||||
): ReturnType<typeof createSearch> {
|
||||
const searchRef = useRef(defaultSearch);
|
||||
|
||||
useEffect(() => {
|
||||
let canceled = false;
|
||||
|
||||
async function run() {
|
||||
let result: SearchEmojiListType | undefined;
|
||||
try {
|
||||
result = await window.SignalContext.getLocalizedEmojiList(locale);
|
||||
} catch (error) {
|
||||
log.error(`Failed to get localized emoji list for ${locale}`, error);
|
||||
}
|
||||
|
||||
// Fallback
|
||||
if (result === undefined) {
|
||||
try {
|
||||
result = await window.SignalContext.getLocalizedEmojiList('en');
|
||||
} catch (error) {
|
||||
log.error('Failed to get fallback localized emoji list');
|
||||
}
|
||||
}
|
||||
|
||||
if (!canceled && result !== undefined) {
|
||||
searchRef.current = createSearch(result);
|
||||
}
|
||||
}
|
||||
drop(run());
|
||||
|
||||
return () => {
|
||||
canceled = true;
|
||||
};
|
||||
}, [locale]);
|
||||
|
||||
return useCallback((...args) => {
|
||||
return searchRef.current?.(...args);
|
||||
}, []);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue