Skip to content

Commit b458810

Browse files
committed
refactor: remove emojis search index initiation from textComposerEmojiMiddleware
1 parent 47bbd2e commit b458810

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/plugins/Emojis/middleware/textComposerEmojiMiddleware.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,29 @@ import {
1010
replaceWordWithEntity,
1111
} from 'stream-chat';
1212
import mergeWith from 'lodash.mergewith';
13-
import type { EmojiSearchIndexResult } from '../../../components/MessageInput';
14-
import { init, SearchIndex } from 'emoji-mart';
15-
import data from '@emoji-mart/data';
13+
import type {
14+
EmojiSearchIndex,
15+
EmojiSearchIndexResult,
16+
} from '../../../components/MessageInput';
1617
import type { TextComposerSuggestion } from 'stream-chat';
1718
import type { MiddlewareParams } from 'stream-chat';
1819

19-
init({ data });
20-
2120
class EmojiSearchSource<
2221
T extends TextComposerSuggestion<EmojiSearchIndexResult>,
2322
> extends BaseSearchSource<T> {
2423
readonly type: SearchSourceType = 'emoji';
25-
constructor(options?: SearchSourceOptions) {
24+
private emojiSearchIndex: EmojiSearchIndex;
25+
26+
constructor(emojiSearchIndex: EmojiSearchIndex, options?: SearchSourceOptions) {
2627
super(options);
28+
this.emojiSearchIndex = emojiSearchIndex;
2729
}
2830

2931
async query(searchQuery: string) {
3032
if (searchQuery.length === 0 || searchQuery.charAt(0).match(/[^a-zA-Z0-9+-]/)) {
3133
return { items: [] as T[], next: null };
3234
}
33-
const emojis: T[] = (await SearchIndex.search(searchQuery)) ?? [];
35+
const emojis = (await this.emojiSearchIndex.search(searchQuery)) ?? [];
3436

3537
// emojiIndex.search sometimes returns undefined values, so filter those out first
3638
return {
@@ -77,11 +79,11 @@ const DEFAULT_OPTIONS: TextComposerMiddlewareOptions = { minChars: 1, trigger: '
7779
export const createTextComposerEmojiMiddleware = <
7880
T extends EmojiSearchIndexResult = EmojiSearchIndexResult,
7981
>(
80-
searchSource: EmojiSearchSource<T>,
82+
emojiSearchIndex: EmojiSearchIndex,
8183
options?: Partial<TextComposerMiddlewareOptions>,
8284
) => {
8385
const finalOptions = mergeWith(DEFAULT_OPTIONS, options ?? {});
84-
const emojiSearchSource = searchSource ?? new EmojiSearchSource();
86+
const emojiSearchSource = new EmojiSearchSource(emojiSearchIndex);
8587
emojiSearchSource.activate();
8688

8789
return {

0 commit comments

Comments
 (0)