2022-11-25 20:25:03 +00:00
|
|
|
import type { AxiosError } from 'axios';
|
|
|
|
import type { SWRConfiguration } from 'swr';
|
|
|
|
import useSWR from 'swr';
|
|
|
|
|
2022-06-05 23:23:25 +00:00
|
|
|
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';
|
2022-06-11 18:52:33 +00:00
|
|
|
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
|
|
|
|
2022-06-05 23:23:25 +00:00
|
|
|
export type ActivityLogFilters = QueryBuilderParams<'ip' | 'event', 'timestamp'>;
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
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
|
|
|
|
2022-06-26 19:13:52 +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
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
return toPaginatedSet(data, Transformers.toActivityLog);
|
|
|
|
},
|
2022-11-25 20:25:03 +00:00
|
|
|
{ revalidateOnMount: false, ...(config || {}) },
|
2022-06-26 19:13:52 +00:00
|
|
|
);
|
2022-06-05 22:35:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export { useActivityLogs };
|