The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/apps/meteor/client/hooks/useAppSlashCommands.ts

44 lines
1.2 KiB

import { useDebouncedCallback } from '@rocket.chat/fuselage-hooks';
import { useEndpoint, useStream, useUserId } from '@rocket.chat/ui-contexts';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useEffect } from 'react';
import { slashCommands } from '../../app/utils/lib/slashCommand';
export const useAppSlashCommands = () => {
const queryClient = useQueryClient();
const apps = useStream('apps');
const uid = useUserId();
const invalidate = useDebouncedCallback(
() => {
queryClient.invalidateQueries(['apps', 'slashCommands']);
},
100,
[],
);
useEffect(() => {
if (!uid) {
return;
}
return apps('apps', ([key, [command]]) => {
if (['command/added', 'command/updated', 'command/disabled', 'command/removed'].includes(key)) {
if (typeof command === 'string') {
delete slashCommands.commands[command];
}
invalidate();
}
});
}, [apps, uid, invalidate]);
const getSlashCommands = useEndpoint('GET', '/v1/commands.list');
useQuery(['apps', 'slashCommands'], () => getSlashCommands(), {
enabled: !!uid,
onSuccess(data) {
data.commands.forEach((command) => slashCommands.add(command));
},
});
};