diff --git a/resources/scripts/api/account/activity.ts b/resources/scripts/api/account/activity.ts index 12740b39f..eef215696 100644 --- a/resources/scripts/api/account/activity.ts +++ b/resources/scripts/api/account/activity.ts @@ -1,10 +1,10 @@ -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 { useUserSWRKey } from '@/plugins/useSWRKey'; export type ActivityLogFilters = QueryBuilderParams<'ip' | 'event', 'timestamp'>; @@ -12,7 +12,7 @@ const useActivityLogs = ( filters?: ActivityLogFilters, config?: ConfigInterface, AxiosError> ): responseInterface, AxiosError> => { - const key = useUserSWRContentKey(['account', 'activity', JSON.stringify(useFilteredObject(filters || {}))]); + const key = useUserSWRKey(['account', 'activity', JSON.stringify(useFilteredObject(filters || {}))]); return useSWR>( key, diff --git a/resources/scripts/api/account/ssh-keys.ts b/resources/scripts/api/account/ssh-keys.ts index e85f9b65f..f35ff1e12 100644 --- a/resources/scripts/api/account/ssh-keys.ts +++ b/resources/scripts/api/account/ssh-keys.ts @@ -1,11 +1,11 @@ import useSWR, { ConfigInterface } from 'swr'; -import useUserSWRContentKey from '@/plugins/useUserSWRContentKey'; import http, { FractalResponseList } from '@/api/http'; import { SSHKey, Transformers } from '@definitions/user'; import { AxiosError } from 'axios'; +import { useUserSWRKey } from '@/plugins/useSWRKey'; const useSSHKeys = (config?: ConfigInterface) => { - const key = useUserSWRContentKey(['account', 'ssh-keys']); + const key = useUserSWRKey(['account', 'ssh-keys']); return useSWR( key, diff --git a/resources/scripts/api/server/activity.ts b/resources/scripts/api/server/activity.ts index 76ca9e97c..a7fa8d31b 100644 --- a/resources/scripts/api/server/activity.ts +++ b/resources/scripts/api/server/activity.ts @@ -4,6 +4,7 @@ import { AxiosError } from 'axios'; import http, { PaginatedResult, QueryBuilderParams, withQueryBuilderParams } from '@/api/http'; import { toPaginatedSet } from '@definitions/helpers'; import useFilteredObject from '@/plugins/useFilteredObject'; +import { useServerSWRKey } from '@/plugins/useSWRKey'; import { ServerContext } from '@/state/server'; export type ActivityLogFilters = QueryBuilderParams<'ip' | 'event', 'timestamp'>; @@ -12,10 +13,11 @@ const useActivityLogs = ( filters?: ActivityLogFilters, config?: ConfigInterface, AxiosError> ): responseInterface, AxiosError> => { - const uuid = ServerContext.useStoreState((state) => state.server.data!.uuid); + const uuid = ServerContext.useStoreState((state) => state.server.data?.uuid); + const key = useServerSWRKey(['activity', useFilteredObject(filters || {})]); return useSWR>( - ['server:activty', uuid, useFilteredObject(filters || {})], + key, async () => { const { data } = await http.get(`/api/client/servers/${uuid}/activity`, { params: { diff --git a/resources/scripts/plugins/useSWRKey.ts b/resources/scripts/plugins/useSWRKey.ts new file mode 100644 index 000000000..78e84fd77 --- /dev/null +++ b/resources/scripts/plugins/useSWRKey.ts @@ -0,0 +1,33 @@ +import { useDeepCompareMemo } from '@/plugins/useDeepCompareMemo'; +import { ServerContext } from '@/state/server'; +import { useStoreState } from '@/state/hooks'; + +// eslint-disable-next-line @typescript-eslint/ban-types +type Context = string | string[] | (string | number | null | {})[]; + +function useSWRKey(context: Context, prefix: string | null = null): string { + const key = useDeepCompareMemo((): string => { + return (Array.isArray(context) ? context : [context]).map((value) => JSON.stringify(value)).join(':'); + }, [context]); + + if (!key.trim().length) { + throw new Error('Must provide a valid context key to "useSWRKey".'); + } + + return `swr::${prefix ? `${prefix}:` : ''}${key.trim()}`; +} + +function useServerSWRKey(context: Context): string { + const uuid = ServerContext.useStoreState((state) => state.server.data?.uuid); + + return useSWRKey(context, `server:${uuid}`); +} + +function useUserSWRKey(context: Context): string { + const uuid = useStoreState((state) => state.user.data?.uuid); + + return useSWRKey(context, `user:${uuid}`); +} + +export default useSWRKey; +export { useServerSWRKey, useUserSWRKey }; diff --git a/resources/scripts/plugins/useUserSWRContentKey.ts b/resources/scripts/plugins/useUserSWRContentKey.ts deleted file mode 100644 index 8ae005d30..000000000 --- a/resources/scripts/plugins/useUserSWRContentKey.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { useStoreState } from '@/state/hooks'; -import { useDeepCompareMemo } from '@/plugins/useDeepCompareMemo'; - -// eslint-disable-next-line @typescript-eslint/ban-types -export default (context: string | string[] | (string | number | null | {})[]) => { - const uuid = useStoreState((state) => state.user.data?.uuid); - const key = useDeepCompareMemo((): string => { - return (Array.isArray(context) ? context : [context]).map((value) => JSON.stringify(value)).join(':'); - }, [context]); - - if (!key.trim().length) { - throw new Error('Must provide a valid context key to "useUserSWRContextKey".'); - } - - return `swr::${uuid || 'unknown'}:${key.trim()}`; -};