Files
xinghuoapi/frontend/src/composables/useTableLoader.ts
IanShaw027 99308ab4fb refactor(frontend): comprehensive architectural optimization and base component extraction
- Standardized table loading logic with enhanced useTableLoader.
- Unified form submission patterns via new useForm composable.
- Extracted common UI components: SearchInput and StatusBadge.
- Centralized common interface definitions in types/index.ts.
- Achieved TypeScript zero-error status across refactored files.
- Greatly improved code reusability and maintainability.
2026-01-04 22:29:19 +08:00

106 lines
2.5 KiB
TypeScript

import { ref, reactive, onUnmounted, toRaw } from 'vue'
import { useDebounceFn } from '@vueuse/core'
import type { BasePaginationResponse, FetchOptions } from '@/types'
interface PaginationState {
page: number
page_size: number
total: number
pages: number
}
interface TableLoaderOptions<T, P> {
fetchFn: (page: number, pageSize: number, params: P, options?: FetchOptions) => Promise<BasePaginationResponse<T>>
initialParams?: P
pageSize?: number
debounceMs?: number
}
/**
* 通用表格数据加载 Composable
* 统一处理分页、筛选、搜索防抖和请求取消
*/
export function useTableLoader<T, P extends Record<string, any>>(options: TableLoaderOptions<T, P>) {
const { fetchFn, initialParams, pageSize = 20, debounceMs = 300 } = options
const items = ref<T[]>([])
const loading = ref(false)
const params = reactive<P>({ ...(initialParams || {}) } as P)
const pagination = reactive<PaginationState>({
page: 1,
page_size: pageSize,
total: 0,
pages: 0
})
let abortController: AbortController | null = null
const isAbortError = (error: any) => {
return error?.name === 'AbortError' || error?.code === 'ERR_CANCELED' || error?.name === 'CanceledError'
}
const load = async () => {
if (abortController) {
abortController.abort()
}
abortController = new AbortController()
loading.value = true
try {
const response = await fetchFn(
pagination.page,
pagination.page_size,
toRaw(params) as P,
{ signal: abortController.signal }
)
items.value = response.items || []
pagination.total = response.total || 0
pagination.pages = response.pages || 0
} catch (error) {
if (!isAbortError(error)) {
console.error('Table load error:', error)
throw error
}
} finally {
if (abortController && !abortController.signal.aborted) {
loading.value = false
}
}
}
const reload = () => {
pagination.page = 1
return load()
}
const debouncedReload = useDebounceFn(reload, debounceMs)
const handlePageChange = (page: number) => {
pagination.page = page
load()
}
const handlePageSizeChange = (size: number) => {
pagination.page_size = size
pagination.page = 1
load()
}
onUnmounted(() => {
abortController?.abort()
})
return {
items,
loading,
params,
pagination,
load,
reload,
debouncedReload,
handlePageChange,
handlePageSizeChange
}
}