Skip to content

Commit aad0285

Browse files
committed
Get rid of CannedResponse collection
1 parent 94ed072 commit aad0285

File tree

7 files changed

+87
-77
lines changed

7 files changed

+87
-77
lines changed

apps/meteor/app/canned-responses/client/collections/CannedResponse.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

apps/meteor/app/canned-responses/client/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

apps/meteor/app/canned-responses/client/startup/responses.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

apps/meteor/client/importPackages.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import '../app/cors/client';
22
import '../app/apple/client';
33
import '../app/authorization/client';
44
import '../app/autotranslate/client';
5-
import '../app/canned-responses/client';
65
import '../app/custom-sounds/client';
76
import '../app/emoji/client';
87
import '../app/emoji-emojione/client';

apps/meteor/client/lib/queryKeys.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ export const subscriptionsQueryKeys = {
1414
all: ['subscriptions'] as const,
1515
subscription: (rid: IRoom['_id']) => [...subscriptionsQueryKeys.all, { rid }] as const,
1616
};
17+
18+
export const cannedResponsesQueryKeys = {
19+
all: ['canned-responses'] as const,
20+
};

apps/meteor/client/views/room/providers/ComposerPopupProvider.tsx

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ import { isOmnichannelRoom } from '@rocket.chat/core-typings';
33
import { useLocalStorage } from '@rocket.chat/fuselage-hooks';
44
import { escapeRegExp } from '@rocket.chat/string-helpers';
55
import { useMethod, useSetting, useUserPreference } from '@rocket.chat/ui-contexts';
6+
import { useQueryClient } from '@tanstack/react-query';
67
import { useMemo, useState } from 'react';
78
import type { ReactNode } from 'react';
89
import { useTranslation } from 'react-i18next';
910

1011
import { hasAtLeastOnePermission } from '../../../../app/authorization/client';
11-
import { CannedResponse } from '../../../../app/canned-responses/client/collections/CannedResponse';
1212
import { emoji } from '../../../../app/emoji/client';
1313
import { Subscriptions } from '../../../../app/models/client';
1414
import { usersFromRoomMessages } from '../../../../app/ui-message/client/popup/messagePopupConfig';
1515
import { slashCommands } from '../../../../app/utils/client';
16+
import { cannedResponsesQueryKeys } from '../../../lib/queryKeys';
1617
import ComposerBoxPopupCannedResponse from '../composer/ComposerBoxPopupCannedResponse';
1718
import type { ComposerBoxPopupEmojiProps } from '../composer/ComposerBoxPopupEmoji';
1819
import ComposerBoxPopupEmoji from '../composer/ComposerBoxPopupEmoji';
@@ -24,6 +25,9 @@ import ComposerBoxPopupUser from '../composer/ComposerBoxPopupUser';
2425
import type { ComposerBoxPopupUserProps } from '../composer/ComposerBoxPopupUser';
2526
import type { ComposerPopupContextValue } from '../contexts/ComposerPopupContext';
2627
import { ComposerPopupContext, createMessageBoxPopupConfig } from '../contexts/ComposerPopupContext';
28+
import useCannedResponsesQuery from './hooks/useCannedResponsesQuery';
29+
30+
export type CannedResponse = { _id: string; shortcut: string; text: string };
2731

2832
type ComposerPopupProviderProps = {
2933
children: ReactNode;
@@ -32,6 +36,11 @@ type ComposerPopupProviderProps = {
3236

3337
const ComposerPopupProvider = ({ children, room }: ComposerPopupProviderProps) => {
3438
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+
3544
const userSpotlight = useMethod('spotlight');
3645
const suggestionsCount = useSetting('Number_of_users_autocomplete_suggestions', 5);
3746
const cannedResponseEnabled = useSetting('Canned_Responses_Enable', true);
@@ -43,6 +52,7 @@ const ComposerPopupProvider = ({ children, room }: ComposerPopupProviderProps) =
4352
const e2eEnabled = useSetting('E2E_Enable', false);
4453
const unencryptedMessagesAllowed = useSetting('E2E_Allow_Unencrypted_Messages', false);
4554
const encrypted = isRoomEncrypted && e2eEnabled && !unencryptedMessagesAllowed;
55+
const queryClient = useQueryClient();
4656

4757
const call = useMethod('getSlashCommandPreviews');
4858
const value: ComposerPopupContextValue = useMemo(() => {
@@ -334,28 +344,20 @@ const ComposerPopupProvider = ({ children, room }: ComposerPopupProviderProps) =
334344
renderItem: ({ item }) => <ComposerBoxPopupCannedResponse {...item} />,
335345
getItemsFromLocal: async (filter: string) => {
336346
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)
349353
.map((record) => ({
350354
_id: record._id,
351355
text: record.text,
352356
shortcut: record.shortcut,
353357
}));
354358
},
355359
getItemsFromServer: async () => [],
356-
getValue: (item) => {
357-
return item.text;
358-
},
360+
getValue: (item) => item.text,
359361
}),
360362
createMessageBoxPopupConfig({
361363
title: previewTitle,
@@ -388,8 +390,8 @@ const ComposerPopupProvider = ({ children, room }: ComposerPopupProviderProps) =
388390
rid,
389391
recentEmojis,
390392
i18n,
393+
queryClient,
391394
call,
392-
setPreviewTitle,
393395
]);
394396

395397
return <ComposerPopupContext.Provider value={value} children={children} />;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import type { IRoom } from '@rocket.chat/core-typings';
2+
import { isOmnichannelRoom } from '@rocket.chat/core-typings';
3+
import { useEndpoint, usePermission, useSetting, useStream, useUserId } from '@rocket.chat/ui-contexts';
4+
import { useQuery, useQueryClient } from '@tanstack/react-query';
5+
import { useEffect } from 'react';
6+
7+
import { cannedResponsesQueryKeys } from '../../../../lib/queryKeys';
8+
9+
type CannedResponse = { _id: string; shortcut: string; text: string };
10+
11+
const useCannedResponsesQuery = (room: IRoom) => {
12+
const isOmnichannel = isOmnichannelRoom(room);
13+
const uid = useUserId();
14+
const isCannedResponsesEnabled = useSetting('Canned_Responses_Enable', true);
15+
const canViewCannedResponses = usePermission('view-canned-responses');
16+
const subscribeToCannedResponses = useStream('canned-responses');
17+
18+
const enabled = isOmnichannel && !!uid && isCannedResponsesEnabled && canViewCannedResponses;
19+
20+
const getCannedResponses = useEndpoint('GET', '/v1/canned-responses.get');
21+
22+
const queryClient = useQueryClient();
23+
24+
useEffect(() => {
25+
if (!enabled) return;
26+
27+
return subscribeToCannedResponses('canned-responses', (...[response, options]) => {
28+
const { agentsId } = options || {};
29+
if (Array.isArray(agentsId) && !agentsId.includes(uid)) {
30+
return;
31+
}
32+
33+
switch (response.type) {
34+
case 'changed': {
35+
const { _id, shortcut, text } = response;
36+
queryClient.setQueryData<CannedResponse[]>(cannedResponsesQueryKeys.all, (responses) =>
37+
responses?.filter((response) => response._id !== _id).concat([{ _id, shortcut, text }]),
38+
);
39+
break;
40+
}
41+
42+
case 'removed': {
43+
const { _id } = response;
44+
queryClient.setQueryData<CannedResponse[]>(cannedResponsesQueryKeys.all, (responses) =>
45+
responses?.filter((response) => response._id !== _id),
46+
);
47+
break;
48+
}
49+
}
50+
});
51+
}, [enabled, getCannedResponses, queryClient, subscribeToCannedResponses, uid]);
52+
53+
return useQuery<CannedResponse[]>({
54+
queryKey: cannedResponsesQueryKeys.all,
55+
queryFn: async () => {
56+
const { responses } = await getCannedResponses();
57+
return responses.map(({ _id, shortcut, text }) => ({ _id, shortcut, text }));
58+
},
59+
enabled,
60+
staleTime: Infinity,
61+
});
62+
};
63+
64+
export default useCannedResponsesQuery;

0 commit comments

Comments
 (0)