mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 13:03:50 +00:00
perf: paging data and image
This commit is contained in:
@@ -31,7 +31,7 @@ services:
|
|||||||
- /root/fastgpt/mongo/logs:/var/log/mongodb
|
- /root/fastgpt/mongo/logs:/var/log/mongodb
|
||||||
- /etc/localtime:/etc/localtime:ro
|
- /etc/localtime:/etc/localtime:ro
|
||||||
fastgpt:
|
fastgpt:
|
||||||
image: ghcr.io/c121914yu/fast-gpt:latest # github
|
image: ghcr.io/c121914yu/fastgpt:latest # github
|
||||||
# image: c121914yu/fast-gpt:latest # docker hub
|
# image: c121914yu/fast-gpt:latest # docker hub
|
||||||
# image: registry.cn-hangzhou.aliyuncs.com/fastgpt/fastgpt:latest # 阿里云
|
# image: registry.cn-hangzhou.aliyuncs.com/fastgpt/fastgpt:latest # 阿里云
|
||||||
network_mode: host
|
network_mode: host
|
||||||
|
@@ -31,7 +31,7 @@ export const usePagination = <T = any,>({
|
|||||||
...params
|
...params
|
||||||
});
|
});
|
||||||
setPageNum(num);
|
setPageNum(num);
|
||||||
setTotal(res.total);
|
res.total && setTotal(res.total);
|
||||||
setData(res.data);
|
setData(res.data);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
toast({
|
toast({
|
||||||
|
@@ -1,82 +0,0 @@
|
|||||||
import { useState, useCallback, useEffect } from 'react';
|
|
||||||
import type { PagingData } from '../types/index';
|
|
||||||
import { useToast } from './useToast';
|
|
||||||
|
|
||||||
export const usePaging = <T = any>({
|
|
||||||
api,
|
|
||||||
pageSize = 10,
|
|
||||||
params = {}
|
|
||||||
}: {
|
|
||||||
api: (data: any) => any;
|
|
||||||
pageSize?: number;
|
|
||||||
params?: Record<string, any>;
|
|
||||||
}) => {
|
|
||||||
const { toast } = useToast();
|
|
||||||
const [data, setData] = useState<T[]>([]);
|
|
||||||
const [pageNum, setPageNum] = useState(1);
|
|
||||||
const [total, setTotal] = useState(0);
|
|
||||||
const [isLoadAll, setIsLoadAll] = useState(false);
|
|
||||||
const [requesting, setRequesting] = useState(false);
|
|
||||||
const [initRequesting, setInitRequesting] = useState(false);
|
|
||||||
|
|
||||||
const getData = useCallback(
|
|
||||||
async (num: number, init = false) => {
|
|
||||||
if (requesting) return;
|
|
||||||
if (!init && isLoadAll) return;
|
|
||||||
if (init) {
|
|
||||||
setInitRequesting(true);
|
|
||||||
}
|
|
||||||
setRequesting(true);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const res: PagingData<T> = await api({
|
|
||||||
pageNum: num,
|
|
||||||
pageSize,
|
|
||||||
...params
|
|
||||||
});
|
|
||||||
setData((state) => {
|
|
||||||
const data = init ? res.data : state.concat(res.data);
|
|
||||||
if (data.length >= res.total) {
|
|
||||||
setIsLoadAll(true);
|
|
||||||
}
|
|
||||||
setTotal(res.total);
|
|
||||||
setPageNum(num);
|
|
||||||
return data;
|
|
||||||
});
|
|
||||||
} catch (error: any) {
|
|
||||||
toast({
|
|
||||||
title: error?.message || '获取数据异常',
|
|
||||||
status: 'error'
|
|
||||||
});
|
|
||||||
console.log(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
setRequesting(false);
|
|
||||||
setInitRequesting(false);
|
|
||||||
return null;
|
|
||||||
},
|
|
||||||
[api, isLoadAll, pageSize, params, requesting, toast]
|
|
||||||
);
|
|
||||||
|
|
||||||
const nextPage = useCallback(() => {
|
|
||||||
if (requesting || isLoadAll) return;
|
|
||||||
getData(pageNum + 1);
|
|
||||||
}, [getData, isLoadAll, pageNum, requesting]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
getData(1, true);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return {
|
|
||||||
pageNum,
|
|
||||||
pageSize,
|
|
||||||
total,
|
|
||||||
data,
|
|
||||||
getData,
|
|
||||||
requesting,
|
|
||||||
isLoadAll,
|
|
||||||
nextPage,
|
|
||||||
initRequesting,
|
|
||||||
setData
|
|
||||||
};
|
|
||||||
};
|
|
@@ -47,7 +47,7 @@ try {
|
|||||||
BillSchema.index({ time: -1 });
|
BillSchema.index({ time: -1 });
|
||||||
BillSchema.index({ userId: 1 });
|
BillSchema.index({ userId: 1 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error;
|
console.log(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Bill: Model<BillType> = models['bill'] || model('bill', BillSchema);
|
export const Bill: Model<BillType> = models['bill'] || model('bill', BillSchema);
|
||||||
|
@@ -86,7 +86,7 @@ try {
|
|||||||
ModelSchema.index({ updateTime: -1 });
|
ModelSchema.index({ updateTime: -1 });
|
||||||
ModelSchema.index({ 'share.collection': -1 });
|
ModelSchema.index({ 'share.collection': -1 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error;
|
console.log(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Model: MongoModel<ModelType> = models['model'] || model('model', ModelSchema);
|
export const Model: MongoModel<ModelType> = models['model'] || model('model', ModelSchema);
|
||||||
|
6
src/types/index.d.ts
vendored
6
src/types/index.d.ts
vendored
@@ -20,10 +20,10 @@ declare global {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type PagingData<T> = {
|
export type PagingData<T> = {
|
||||||
pageNum;
|
pageNum: number;
|
||||||
pageSize;
|
pageSize: number;
|
||||||
data: T[];
|
data: T[];
|
||||||
total;
|
total?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type RequestPaging = { pageNum: number; pageSize: number; [key]: any };
|
export type RequestPaging = { pageNum: number; pageSize: number; [key]: any };
|
||||||
|
Reference in New Issue
Block a user