import { computed, onMounted, reactive, unref } from "vue"; import type { TableInstance } from "@arco-design/web-vue"; import type { BaseResponse } from "@chatgpt-plus/packages/type"; export type TableOriginalProps = TableInstance["$props"]; export type TableRequest> = ( params?: any ) => Promise>; export type TableReturn = [TableOriginalProps, () => Promise]; function useAsyncTable>( request: TableRequest, params?: Record ): TableReturn { const tableState = reactive<{ loading: Boolean; data: T[] }>({ loading: false, data: [], }); const tableConfig = computed(() => { return { ...tableState, rowKey: "id", }; }); const getTableData = async () => { tableState.loading = true; try { const { data } = await request({ ...unref(params ?? {}), }); tableState.data = data as any; } finally { tableState.loading = false; } }; onMounted(getTableData); return [tableConfig, getTableData] as TableReturn; } export default useAsyncTable;