refactor: 移除调试日志并优化代码结构
feat(components): 新增设备管理相关组件和仪表盘组件 feat(hooks): 添加自定义hooks用于API调用和数据管理 style: 优化滚动条样式和模态框布局 chore: 清理无用脚本和调试文件 docs: 更新组件导出文件
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import useSWR, { SWRConfig } from 'swr';
|
||||
import api from '../api';
|
||||
|
||||
const fetcher = url => api.get(url).then(res => res);
|
||||
|
||||
export const swrConfig = {
|
||||
fetcher,
|
||||
revalidateOnFocus: false,
|
||||
revalidateOnReconnect: true,
|
||||
shouldRetryOnError: false,
|
||||
dedupingInterval: 5000,
|
||||
errorRetryCount: 2,
|
||||
};
|
||||
|
||||
export const useSWRConfig = () => {
|
||||
return {
|
||||
mutate: useSWR().mutate,
|
||||
};
|
||||
};
|
||||
|
||||
export const useFetch = (key, options = {}) => {
|
||||
const { data, error, isLoading, isValidating, mutate } = useSWR(key, options);
|
||||
|
||||
return {
|
||||
data,
|
||||
error,
|
||||
isLoading,
|
||||
isValidating,
|
||||
mutate,
|
||||
isError: !!error,
|
||||
};
|
||||
};
|
||||
|
||||
export const useFetchList = (key, options = {}) => {
|
||||
const { data, error, isLoading, mutate } = useSWR(key, {
|
||||
...options,
|
||||
onSuccess: data => {
|
||||
if (options.onSuccess) {
|
||||
options.onSuccess(data);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
list: data?.list || data?.devices || data?.racks || data?.rooms || data || [],
|
||||
total: data?.total || data?.count || 0,
|
||||
error,
|
||||
isLoading,
|
||||
mutate,
|
||||
};
|
||||
};
|
||||
|
||||
export { SWRConfig, useSWR };
|
||||
Reference in New Issue
Block a user