2020-08-21 04:44:33 +00:00
|
|
|
import useSWR from 'swr';
|
|
|
|
import http, { getPaginationSet, PaginatedResult } from '@/api/http';
|
2022-02-27 17:25:31 +00:00
|
|
|
import { Transformers, ServerBackup } from '@definitions/user';
|
2020-08-26 04:39:00 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2020-12-06 20:01:42 +00:00
|
|
|
import { createContext, useContext } from 'react';
|
2020-08-21 04:44:33 +00:00
|
|
|
|
2020-12-06 20:01:42 +00:00
|
|
|
interface ctx {
|
|
|
|
page: number;
|
|
|
|
setPage: (value: number | ((s: number) => number)) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Context = createContext<ctx>({ page: 1, setPage: () => 1 });
|
|
|
|
|
2021-08-05 03:34:00 +00:00
|
|
|
type BackupResponse = PaginatedResult<ServerBackup> & { backupCount: number };
|
|
|
|
|
2020-12-06 20:01:42 +00:00
|
|
|
export default () => {
|
|
|
|
const { page } = useContext(Context);
|
2020-08-26 04:39:00 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
2020-08-21 04:44:33 +00:00
|
|
|
|
2021-08-05 03:34:00 +00:00
|
|
|
return useSWR<BackupResponse>([ 'server:backups', uuid, page ], async () => {
|
2020-08-21 04:44:33 +00:00
|
|
|
const { data } = await http.get(`/api/client/servers/${uuid}/backups`, { params: { page } });
|
|
|
|
|
|
|
|
return ({
|
2022-02-27 17:25:31 +00:00
|
|
|
items: (data.data || []).map(Transformers.toServerBackup),
|
2020-08-21 04:44:33 +00:00
|
|
|
pagination: getPaginationSet(data.meta.pagination),
|
2021-08-05 03:34:00 +00:00
|
|
|
backupCount: data.meta.backup_count,
|
2020-08-21 04:44:33 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|