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

34 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

2022-06-05 22:35:53 +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';
2022-06-05 22:35:53 +00:00
import { toPaginatedSet } from '@definitions/helpers';
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,
config?: ConfigInterface<PaginatedResult<ActivityLog>, AxiosError>
): responseInterface<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);
},
{ revalidateOnMount: false, ...(config || {}) }
);
2022-06-05 22:35:53 +00:00
};
export { useActivityLogs };