mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-20 00:24:24 +01:00
Performance sweep (#147)
This commit is contained in:
42
src-web/hooks/useMutation.ts
Normal file
42
src-web/hooks/useMutation.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import type { MutationKey } from '@tanstack/react-query';
|
||||
import { useCallback } from 'react';
|
||||
|
||||
export function useMutation<TData = unknown, TError = unknown, TVariables = void>({
|
||||
mutationKey,
|
||||
mutationFn,
|
||||
onSuccess,
|
||||
onSettled,
|
||||
}: {
|
||||
mutationKey: MutationKey;
|
||||
mutationFn: (vars: TVariables) => Promise<TData>;
|
||||
onSettled?: () => void;
|
||||
onSuccess?: (data: TData) => void;
|
||||
}) {
|
||||
const mutateAsync = useCallback(
|
||||
async (variables: TVariables) => {
|
||||
try {
|
||||
const data = await mutationFn(variables);
|
||||
onSuccess?.(data);
|
||||
} catch (err: unknown) {
|
||||
const e = err as TError;
|
||||
console.log('MUTATION FAILED', mutationKey, e);
|
||||
} finally {
|
||||
onSettled?.();
|
||||
}
|
||||
},
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
mutationKey,
|
||||
);
|
||||
|
||||
const mutate = useCallback(
|
||||
(variables: TVariables) => {
|
||||
setTimeout(() => mutateAsync(variables));
|
||||
},
|
||||
[mutateAsync],
|
||||
);
|
||||
|
||||
return {
|
||||
mutate,
|
||||
mutateAsync,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user