2022-05-14 21:31:53 +00:00
|
|
|
import { useStoreState } from '@/state/hooks';
|
2022-06-20 17:19:40 +00:00
|
|
|
import { useDeepCompareMemo } from '@/plugins/useDeepCompareMemo';
|
2022-05-14 21:31:53 +00:00
|
|
|
|
2022-06-20 17:19:40 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
|
|
export default (context: string | string[] | (string | number | null | {})[]) => {
|
2022-05-14 21:31:53 +00:00
|
|
|
const uuid = useStoreState(state => state.user.data?.uuid);
|
2022-06-20 17:19:40 +00:00
|
|
|
const key = useDeepCompareMemo((): string => {
|
|
|
|
return (Array.isArray(context) ? context : [ context ])
|
|
|
|
.map((value) => JSON.stringify(value))
|
|
|
|
.join(':');
|
|
|
|
}, [ context ]);
|
2022-05-14 21:31:53 +00:00
|
|
|
|
|
|
|
if (!key.trim().length) {
|
|
|
|
throw new Error('Must provide a valid context key to "useUserSWRContextKey".');
|
|
|
|
}
|
|
|
|
|
|
|
|
return `swr::${uuid || 'unknown'}:${key.trim()}`;
|
|
|
|
};
|