misc_pterodactyl-panel/resources/scripts/api/server/activity.ts

36 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-06-12 19:16:48 +00:00
import useSWR, { ConfigInterface, responseInterface } from 'swr';
import { ActivityLog, Transformers } from '@definitions/user';
import { AxiosError } from 'axios';
import http, { PaginatedResult, QueryBuilderParams, withQueryBuilderParams } from '@/api/http';
import { toPaginatedSet } from '@definitions/helpers';
import useFilteredObject from '@/plugins/useFilteredObject';
2022-07-04 22:22:58 +00:00
import { useServerSWRKey } from '@/plugins/useSWRKey';
2022-06-12 19:16:48 +00:00
import { ServerContext } from '@/state/server';
export type ActivityLogFilters = QueryBuilderParams<'ip' | 'event', 'timestamp'>;
const useActivityLogs = (
filters?: ActivityLogFilters,
config?: ConfigInterface<PaginatedResult<ActivityLog>, AxiosError>
): responseInterface<PaginatedResult<ActivityLog>, AxiosError> => {
2022-07-04 22:22:58 +00:00
const uuid = ServerContext.useStoreState((state) => state.server.data?.uuid);
const key = useServerSWRKey(['activity', useFilteredObject(filters || {})]);
2022-06-12 19:16:48 +00:00
return useSWR<PaginatedResult<ActivityLog>>(
2022-07-04 22:22:58 +00:00
key,
async () => {
const { data } = await http.get(`/api/client/servers/${uuid}/activity`, {
params: {
...withQueryBuilderParams(filters),
include: ['actor'],
},
});
2022-06-12 19:16:48 +00:00
return toPaginatedSet(data, Transformers.toActivityLog);
},
{ revalidateOnMount: false, ...(config || {}) }
);
2022-06-12 19:16:48 +00:00
};
export { useActivityLogs };