@@ -3,16 +3,17 @@ import { isOmnichannelRoom } from '@rocket.chat/core-typings';
3
3
import { useLocalStorage } from '@rocket.chat/fuselage-hooks' ;
4
4
import { escapeRegExp } from '@rocket.chat/string-helpers' ;
5
5
import { useMethod , useSetting , useUserPreference } from '@rocket.chat/ui-contexts' ;
6
+ import { useQueryClient } from '@tanstack/react-query' ;
6
7
import { useMemo , useState } from 'react' ;
7
8
import type { ReactNode } from 'react' ;
8
9
import { useTranslation } from 'react-i18next' ;
9
10
10
11
import { hasAtLeastOnePermission } from '../../../../app/authorization/client' ;
11
- import { CannedResponse } from '../../../../app/canned-responses/client/collections/CannedResponse' ;
12
12
import { emoji } from '../../../../app/emoji/client' ;
13
13
import { Subscriptions } from '../../../../app/models/client' ;
14
14
import { usersFromRoomMessages } from '../../../../app/ui-message/client/popup/messagePopupConfig' ;
15
15
import { slashCommands } from '../../../../app/utils/client' ;
16
+ import { cannedResponsesQueryKeys } from '../../../lib/queryKeys' ;
16
17
import ComposerBoxPopupCannedResponse from '../composer/ComposerBoxPopupCannedResponse' ;
17
18
import type { ComposerBoxPopupEmojiProps } from '../composer/ComposerBoxPopupEmoji' ;
18
19
import ComposerBoxPopupEmoji from '../composer/ComposerBoxPopupEmoji' ;
@@ -24,6 +25,9 @@ import ComposerBoxPopupUser from '../composer/ComposerBoxPopupUser';
24
25
import type { ComposerBoxPopupUserProps } from '../composer/ComposerBoxPopupUser' ;
25
26
import type { ComposerPopupContextValue } from '../contexts/ComposerPopupContext' ;
26
27
import { ComposerPopupContext , createMessageBoxPopupConfig } from '../contexts/ComposerPopupContext' ;
28
+ import useCannedResponsesQuery from './hooks/useCannedResponsesQuery' ;
29
+
30
+ export type CannedResponse = { _id : string ; shortcut : string ; text : string } ;
27
31
28
32
type ComposerPopupProviderProps = {
29
33
children : ReactNode ;
@@ -32,6 +36,11 @@ type ComposerPopupProviderProps = {
32
36
33
37
const ComposerPopupProvider = ( { children, room } : ComposerPopupProviderProps ) => {
34
38
const { _id : rid , encrypted : isRoomEncrypted } = room ;
39
+
40
+ // TODO: this is awful because we are just triggering the query to get the data
41
+ // and we are not using the data itself, we should find a better way to do this
42
+ useCannedResponsesQuery ( room ) ;
43
+
35
44
const userSpotlight = useMethod ( 'spotlight' ) ;
36
45
const suggestionsCount = useSetting ( 'Number_of_users_autocomplete_suggestions' , 5 ) ;
37
46
const cannedResponseEnabled = useSetting ( 'Canned_Responses_Enable' , true ) ;
@@ -43,6 +52,7 @@ const ComposerPopupProvider = ({ children, room }: ComposerPopupProviderProps) =
43
52
const e2eEnabled = useSetting ( 'E2E_Enable' , false ) ;
44
53
const unencryptedMessagesAllowed = useSetting ( 'E2E_Allow_Unencrypted_Messages' , false ) ;
45
54
const encrypted = isRoomEncrypted && e2eEnabled && ! unencryptedMessagesAllowed ;
55
+ const queryClient = useQueryClient ( ) ;
46
56
47
57
const call = useMethod ( 'getSlashCommandPreviews' ) ;
48
58
const value : ComposerPopupContextValue = useMemo ( ( ) => {
@@ -334,28 +344,20 @@ const ComposerPopupProvider = ({ children, room }: ComposerPopupProviderProps) =
334
344
renderItem : ( { item } ) => < ComposerBoxPopupCannedResponse { ...item } /> ,
335
345
getItemsFromLocal : async ( filter : string ) => {
336
346
const exp = new RegExp ( filter , 'i' ) ;
337
- return CannedResponse . find (
338
- {
339
- shortcut : exp ,
340
- } ,
341
- {
342
- limit : 12 ,
343
- sort : {
344
- shortcut : - 1 ,
345
- } ,
346
- } ,
347
- )
348
- . fetch ( )
347
+ // TODO: this is bad, but can only be fixed by refactoring the whole thing
348
+ const cannedResponses = queryClient . getQueryData < CannedResponse [ ] > ( cannedResponsesQueryKeys . all ) ?? [ ] ;
349
+ return cannedResponses
350
+ . filter ( ( record ) => record . shortcut . match ( exp ) )
351
+ . sort ( ( a , b ) => a . shortcut . localeCompare ( b . shortcut ) )
352
+ . slice ( 0 , 11 )
349
353
. map ( ( record ) => ( {
350
354
_id : record . _id ,
351
355
text : record . text ,
352
356
shortcut : record . shortcut ,
353
357
} ) ) ;
354
358
} ,
355
359
getItemsFromServer : async ( ) => [ ] ,
356
- getValue : ( item ) => {
357
- return item . text ;
358
- } ,
360
+ getValue : ( item ) => item . text ,
359
361
} ) ,
360
362
createMessageBoxPopupConfig ( {
361
363
title : previewTitle ,
@@ -388,8 +390,8 @@ const ComposerPopupProvider = ({ children, room }: ComposerPopupProviderProps) =
388
390
rid ,
389
391
recentEmojis ,
390
392
i18n ,
393
+ queryClient ,
391
394
call ,
392
- setPreviewTitle ,
393
395
] ) ;
394
396
395
397
return < ComposerPopupContext . Provider value = { value } children = { children } /> ;
0 commit comments