misc_pterodactyl-panel/resources/scripts/api/swr/getServerBackups.ts

33 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-11-25 20:25:03 +00:00
import { createContext, useContext } from 'react';
import useSWR from 'swr';
2022-11-25 20:25:03 +00:00
import type { PaginatedResult } from '@/api/http';
import http, { getPaginationSet } from '@/api/http';
import type { ServerBackup } from '@/api/server/types';
2020-08-22 22:46:13 +00:00
import { rawDataToServerBackup } from '@/api/transformers';
2020-08-26 04:39:00 +00:00
import { ServerContext } from '@/state/server';
interface ctx {
page: number;
setPage: (value: number | ((s: number) => number)) => void;
}
export const Context = createContext<ctx>({ page: 1, setPage: () => 1 });
type BackupResponse = PaginatedResult<ServerBackup> & { backupCount: number };
export default () => {
const { page } = useContext(Context);
2022-11-25 20:25:03 +00:00
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
return useSWR<BackupResponse>(['server:backups', uuid, page], async () => {
const { data } = await http.get(`/api/client/servers/${uuid}/backups`, { params: { page } });
return {
items: (data.data || []).map(rawDataToServerBackup),
pagination: getPaginationSet(data.meta.pagination),
backupCount: data.meta.backup_count,
};
});
};