@@ -10,27 +10,29 @@ import {
1010 replaceWordWithEntity ,
1111} from 'stream-chat' ;
1212import 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' ;
1617import type { TextComposerSuggestion } from 'stream-chat' ;
1718import type { MiddlewareParams } from 'stream-chat' ;
1819
19- init ( { data } ) ;
20-
2120class 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 - z A - Z 0 - 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: '
7779export 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