mirror of
https://github.com/hzrd149/nostrudel.git
synced 2025-10-10 21:03:39 +02:00
15 lines
453 B
TypeScript
15 lines
453 B
TypeScript
import { useToast } from "@chakra-ui/react";
|
|
import { DependencyList, useCallback } from "react";
|
|
|
|
export default function useAsyncErrorHandler<T = any>(fn: () => Promise<T>, deps: DependencyList = []): () => Promise<T | undefined> {
|
|
const toast = useToast();
|
|
|
|
return useCallback(async () => {
|
|
try {
|
|
return await fn();
|
|
} catch (e) {
|
|
if (e instanceof Error) toast({ description: e.message, status: "error" });
|
|
}
|
|
}, deps);
|
|
}
|