Files
admin/src/hooks/data.ts

52 lines
1.3 KiB
TypeScript
Raw Normal View History

import {
type Dispatch,
type SetStateAction,
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<TArgs extends unknown[], TResult>(
table: ReturnType<typeof useDataTable>,
fetchData: (...args: TArgs) => Promise<ApiResponse<TResult>>,
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,
],
)
}