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

36 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-11-25 20:25:03 +00:00
import type { AxiosError } from 'axios';
import type { SWRConfiguration } from 'swr';
import useSWR from 'swr';
import http, { PaginatedResult, QueryBuilderParams, withQueryBuilderParams } from '@/api/http';
2022-06-05 22:35:53 +00:00
import { toPaginatedSet } from '@definitions/helpers';
2022-11-25 20:25:03 +00:00
import { ActivityLog, Transformers } from '@definitions/user';
import useFilteredObject from '@/plugins/useFilteredObject';
2022-07-04 22:22:58 +00:00
import { useUserSWRKey } from '@/plugins/useSWRKey';
2022-06-05 22:35:53 +00:00
export type ActivityLogFilters = QueryBuilderParams<'ip' | 'event', 'timestamp'>;
const useActivityLogs = (
filters?: ActivityLogFilters,
2022-11-25 20:25:03 +00:00
config?: SWRConfiguration<PaginatedResult<ActivityLog>, AxiosError>,
) => {
2022-07-04 22:22:58 +00:00
const key = useUserSWRKey(['account', 'activity', JSON.stringify(useFilteredObject(filters || {}))]);
2022-06-05 22:35:53 +00:00
return useSWR<PaginatedResult<ActivityLog>>(
key,
async () => {
const { data } = await http.get('/api/client/account/activity', {
params: {
...withQueryBuilderParams(filters),
include: ['actor'],
},
});
2022-06-05 22:35:53 +00:00
return toPaginatedSet(data, Transformers.toActivityLog);
},
2022-11-25 20:25:03 +00:00
{ revalidateOnMount: false, ...(config || {}) },
);
2022-06-05 22:35:53 +00:00
};
export { useActivityLogs };