2022-11-25 20:25:03 +00:00
|
|
|
import type { AxiosError } from 'axios';
|
|
|
|
import type { SWRConfiguration } from 'swr';
|
|
|
|
import useSWR from 'swr';
|
|
|
|
|
|
|
|
import type { PaginatedResult, QueryBuilderParams } from '@/api/http';
|
|
|
|
import http, { withQueryBuilderParams } from '@/api/http';
|
2022-06-12 19:16:48 +00:00
|
|
|
import { toPaginatedSet } from '@definitions/helpers';
|
2022-11-25 20:25:03 +00:00
|
|
|
import type { ActivityLog } from '@definitions/user';
|
|
|
|
import { Transformers } from '@definitions/user';
|
2022-06-12 19:16:48 +00:00
|
|
|
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'>;
|
|
|
|
|
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>,
|
|
|
|
) => {
|
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data?.uuid);
|
2022-07-04 22:22:58 +00:00
|
|
|
const key = useServerSWRKey(['activity', useFilteredObject(filters || {})]);
|
2022-06-12 19:16:48 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
return useSWR<PaginatedResult<ActivityLog>>(
|
2022-07-04 22:22:58 +00:00
|
|
|
key,
|
2022-06-26 19:13:52 +00:00
|
|
|
async () => {
|
|
|
|
const { data } = await http.get(`/api/client/servers/${uuid}/activity`, {
|
|
|
|
params: {
|
|
|
|
...withQueryBuilderParams(filters),
|
|
|
|
include: ['actor'],
|
|
|
|
},
|
|
|
|
});
|
2022-06-12 19:16:48 +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-12 19:16:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export { useActivityLogs };
|