Skip to content

Commit

Permalink
refactor(perf): Use infinite stale time on useAppActionButtons (Roc…
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan authored Aug 16, 2023
1 parent 84ec741 commit aa28bdf
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions apps/meteor/client/hooks/useAppActionButtons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { IUIActionButton, UIActionButtonContext } from '@rocket.chat/apps-e
import { useEndpoint, useStream, useUserId } from '@rocket.chat/ui-contexts';
import type { UseQueryResult } from '@tanstack/react-query';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useEffect, useRef, useMemo } from 'react';
import { useEffect, useMemo } from 'react';

import type { MessageActionConfig, MessageActionContext } from '../../app/ui-utils/client/lib/MessageAction';
import type { MessageBoxAction } from '../../app/ui-utils/client/lib/messageBox';
Expand All @@ -14,33 +14,30 @@ import { useUiKitActionManager } from './useUiKitActionManager';
const getIdForActionButton = ({ appId, actionId }: IUIActionButton): string => `${appId}/${actionId}`;

export const useAppActionButtons = (context?: `${UIActionButtonContext}`) => {
const stream = useRef<() => void>();
const queryClient = useQueryClient();

const apps = useStream('apps');
const uid = useUserId();

useEffect(() => () => stream.current?.(), []);

useQuery(['apps', 'stream', 'actionButtons', uid], () => {
if (!uid) {
return [];
}
stream.current?.();
stream.current = apps('actions/changed', () => {
queryClient.invalidateQueries(['apps', 'actionButtons']);
});

return [];
});

const getActionButtons = useEndpoint('GET', '/apps/actionButtons');

const result = useQuery(['apps', 'actionButtons'], () => getActionButtons(), {
...(context && {
select: (data) => data.filter((button) => button.context === context),
}),
staleTime: Infinity,
});

useEffect(() => {
if (!uid) {
return;
}

return apps('actions/changed', () => {
queryClient.invalidateQueries(['apps', 'actionButtons']);
});
}, [uid, queryClient, apps]);

return result;
};

Expand Down

0 comments on commit aa28bdf

Please sign in to comment.