forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseSWRKey.ts
More file actions
33 lines (24 loc) · 1.13 KB
/
useSWRKey.ts
File metadata and controls
33 lines (24 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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 };