mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
20 lines
516 B
TypeScript
20 lines
516 B
TypeScript
import type { DependencyList } from 'react';
|
|
import { showErrorToasts } from '../manager/ui';
|
|
import type { FunctionReturningPromise } from '../types';
|
|
import { useAsyncFn } from './useAsyncFn';
|
|
|
|
export function useAsyncRequest<T extends FunctionReturningPromise>(
|
|
fn: T,
|
|
deps: DependencyList = []
|
|
) {
|
|
const [{ loading }, call] = useAsyncFn(async (...args) => {
|
|
try {
|
|
await fn(...args);
|
|
} catch (err) {
|
|
showErrorToasts(err);
|
|
}
|
|
}, deps);
|
|
|
|
return [{ loading }, call as T] as const;
|
|
}
|