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

27 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-06-05 22:35:53 +00:00
import useUserSWRContentKey from '@/plugins/useUserSWRContentKey';
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-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> => {
const key = useUserSWRContentKey([ '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),
2022-06-05 22:35:53 +00:00
include: [ 'actor' ],
},
});
return toPaginatedSet(data, Transformers.toActivityLog);
}, { revalidateOnMount: false, ...(config || {}) });
};
export { useActivityLogs };