Skip to content

Commit

Permalink
chore: fetch all slashcommands on startup (#34167)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustrb authored Dec 16, 2024
1 parent 7f22793 commit b32c629
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions apps/meteor/client/hooks/useAppSlashCommands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SlashCommand } from '@rocket.chat/core-typings';
import { useDebouncedCallback } from '@rocket.chat/fuselage-hooks';
import { useEndpoint, useStream, useUserId } from '@rocket.chat/ui-contexts';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useQueryClient, useQuery } from '@tanstack/react-query';
import { useEffect } from 'react';

import { slashCommands } from '../../app/utils/client/slashCommand';
Expand Down Expand Up @@ -35,10 +36,29 @@ export const useAppSlashCommands = () => {

const getSlashCommands = useEndpoint('GET', '/v1/commands.list');

useQuery(['apps', 'slashCommands'], () => getSlashCommands(), {
enabled: !!uid,
onSuccess(data) {
data.commands.forEach((command) => slashCommands.add(command));
useQuery(
['apps', 'slashCommands'],
async () => {
let allCommands: Pick<SlashCommand, 'clientOnly' | 'command' | 'description' | 'params' | 'providesPreview' | 'appId'>[] = [];
let hasMore = true;
let offset = 0;
const count = 50;

while (hasMore) {
// eslint-disable-next-line no-await-in-loop
const { commands, total } = await getSlashCommands({ offset, count });
allCommands = allCommands.concat(commands);
hasMore = allCommands.length < total;
offset += count;
}

return allCommands;
},
});
{
enabled: !!uid,
onSuccess(data) {
data.forEach((command) => slashCommands.add(command));
},
},
);
};

0 comments on commit b32c629

Please sign in to comment.