Skip to content

Commit d14523c

Browse files
committed
improve api cache
1 parent fd0a3f7 commit d14523c

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

src/pages/manage/components/deps-table.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const DepsTable = ({
1414
name?: string;
1515
}) => {
1616
const { packages, appId } = useManageContext();
17-
const { versions } = useVersions({ appId, limit: 1000 });
17+
const { allVersions: versions } = useVersions({ appId });
1818
const [diffs, setDiffs] = useState<{
1919
oldDeps?: Record<string, string>;
2020
newDeps?: Record<string, string>;

src/services/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const api = {
9090
getVersions: ({
9191
appId,
9292
offset = 0,
93-
limit = 10,
93+
limit = 1000,
9494
}: {
9595
appId: number;
9696
offset?: number;

src/services/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export default async function request<T extends Record<any, any>>(
7272
}
7373

7474
message.error(json.message);
75-
throw new Error(`${response.status}: ${json.message}`);
75+
throw Error(`${response.status}: ${json.message}`);
7676
} catch (err) {
7777
if ((err as Error).message.includes("Unauthorized")) {
7878
logout();

src/utils/hooks.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,34 @@ export const usePackages = (appId: number) => {
6464

6565
export const useVersions = ({
6666
appId,
67-
offset,
68-
limit,
67+
offset = 0,
68+
limit = 10,
6969
}: {
7070
appId: number;
7171
offset?: number;
7272
limit?: number;
7373
}) => {
74+
// Fetch all versions (up to 1000) from backend and cache them
7475
const { data, isLoading } = useQuery({
75-
queryKey: ["versions", appId, offset, limit],
76-
staleTime: 0,
77-
queryFn: () => api.getVersions({ appId, offset, limit }),
76+
queryKey: ["versions", appId],
77+
staleTime: 3000,
78+
queryFn: () => api.getVersions({ appId, offset: 0, limit: 1000 }),
7879
});
79-
return { versions: data?.data ?? [], count: data?.count ?? 0, isLoading };
80+
81+
// Implement frontend pagination
82+
const allVersions = data?.data ?? [];
83+
const totalCount = data?.count ?? 0;
84+
85+
// Calculate pagination
86+
const startIndex = offset;
87+
const endIndex = offset + limit;
88+
const paginatedVersions = allVersions.slice(startIndex, endIndex);
89+
90+
return {
91+
versions: paginatedVersions,
92+
count: totalCount,
93+
isLoading,
94+
// Also return all versions for components that might need them
95+
allVersions,
96+
};
8097
};

0 commit comments

Comments
 (0)