2022-06-12 19:16:48 +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';
|
|
|
|
import { toPaginatedSet } from '@definitions/helpers';
|
|
|
|
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,
|
|
|
|
config?: ConfigInterface<PaginatedResult<ActivityLog>, AxiosError>
|
|
|
|
): responseInterface<PaginatedResult<ActivityLog>, AxiosError> => {
|
2022-07-04 22:22:58 +00:00
|
|
|
const uuid = ServerContext.useStoreState((state) => state.server.data?.uuid);
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
{ revalidateOnMount: false, ...(config || {}) }
|
|
|
|
);
|
2022-06-12 19:16:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export { useActivityLogs };
|