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/client/hooks/useMethodData.ts

40 lines
1.3 KiB

import { useCallback, useEffect } from 'react';
import type { Awaited } from '../../definition/utils';
import { ServerMethodFunction, ServerMethodParameters, ServerMethods, useMethod } from '../contexts/ServerContext';
import { useToastMessageDispatch } from '../contexts/ToastMessagesContext';
import { AsyncState, useAsyncState } from './useAsyncState';
export const useMethodData = <MethodName extends keyof ServerMethods, Result = Awaited<ReturnType<ServerMethodFunction<MethodName>>>>(
methodName: MethodName,
args: ServerMethodParameters<MethodName>,
initialValue?: Result | (() => Result),
): AsyncState<Result> & { reload: () => void } => {
const { resolve, reject, reset, ...state } = useAsyncState<Result>(initialValue);
const dispatchToastMessage = useToastMessageDispatch();
const getData: ServerMethodFunction<MethodName> = useMethod(methodName);
const fetchData = useCallback(() => {
reset();
getData(...args)
.then(resolve)
.catch((error) => {
console.error(error);
dispatchToastMessage({
type: 'error',
message: error,
});
reject(error);
});
}, [reset, getData, args, resolve, dispatchToastMessage, reject]);
useEffect(() => {
console.log('as');
fetchData();
}, [fetchData]);
return {
...state,
reload: fetchData,
};
};