import { useCallback, useState } from "react" import { toast } from "sonner" import type { useDataTable } from "@/components/data-table" import type { ApiResponse } from "@/lib/api" export function useStatus() { return useState<"load" | "fail" | "done">("load") } export function useFetch( table: ReturnType, fetchData: (...args: TArgs) => Promise>, messages: { done?: string fail?: string }, ) { return useCallback( async (...args: TArgs) => { try { table.setStatus?.("load") const resp = await fetchData(...args) if (!resp.success) { throw new Error(resp.message) } table.setStatus?.("done") table.refresh(table.pagination.page, table.pagination.size) toast.success(messages.done || "获取数据成功") } catch (e) { table.setStatus?.("fail") toast.error(messages.fail || "获取数据失败", { description: (e as Error).message || "未知错误", }) } }, [ fetchData, table.setStatus, table.pagination.page, table.pagination.size, table.refresh, messages, ], ) } type Action =

(...args: P) => Promise export function useAction(action: Action) { const [status, setStatus] = useStatus() const func = useCallback( async (...args: Parameters) => { try { setStatus("load") await action(...args) setStatus("done") } catch (e) { setStatus("fail") throw e } }, [action, setStatus], ) return [func, status] }