2022-06-12 19:16:48 +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';
|
|
|
|
import { toPaginatedSet } from '@definitions/helpers';
|
|
|
|
import useFilteredObject from '@/plugins/useFilteredObject';
|
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
|
|
|
|
export type ActivityLogFilters = QueryBuilderParams<'ip' | 'event', 'timestamp'>;
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
const useActivityLogs = (
|
|
|
|
filters?: ActivityLogFilters,
|
|
|
|
config?: ConfigInterface<PaginatedResult<ActivityLog>, AxiosError>
|
|
|
|
): responseInterface<PaginatedResult<ActivityLog>, AxiosError> => {
|
|
|
|
const uuid = ServerContext.useStoreState((state) => state.server.data?.uuid);
|
|
|
|
const key = useUserSWRContentKey(['server', 'activity', useFilteredObject(filters || {})]);
|
2022-06-12 19:16:48 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
return useSWR<PaginatedResult<ActivityLog>>(
|
|
|
|
key,
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
{ revalidateOnMount: false, ...(config || {}) }
|
|
|
|
);
|
2022-06-12 19:16:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export { useActivityLogs };
|