1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-31 00:16:47 +01:00
unleash.unleash/frontend/src/hooks/useCollaborateData.tsx
Fredrik Strand Oseberg 54633500fd
Feat/use collaborate data (#2067)
* feat: initial architecture

* feat: add generic types

* fix: refactor

* feat: style notification

* feat: remove useFeatureImmutable

* fix: remove casting

* fix: ensure data is present

* fix: revive useFeatureImmutable

* Update frontend/src/component/common/StaleDataNotification/StaleDataNotification.tsx

Co-authored-by: Nuno Góis <github@nunogois.com>

* Update frontend/src/component/common/StaleDataNotification/StaleDataNotification.tsx

Co-authored-by: Nuno Góis <github@nunogois.com>

* Update frontend/src/component/common/StaleDataNotification/StaleDataNotification.tsx

Co-authored-by: Nuno Góis <github@nunogois.com>

* Update frontend/src/component/common/StaleDataNotification/StaleDataNotification.tsx

Co-authored-by: Nuno Góis <github@nunogois.com>

* Update frontend/src/component/common/StaleDataNotification/StaleDataNotification.tsx

Co-authored-by: Nuno Góis <github@nunogois.com>

* fix: pr comments

* fix: change order

Co-authored-by: Nuno Góis <github@nunogois.com>
2022-09-16 15:23:08 +02:00

85 lines
2.2 KiB
TypeScript

import { useState, useEffect } from 'react';
import { SWRConfiguration } from 'swr';
import { dequal } from 'dequal';
import { StaleDataNotification } from 'component/common/StaleDataNotification/StaleDataNotification';
interface IFormatUnleashGetterOutput<Type> {
data: Type;
refetch: () => void;
}
const formatUnleashGetter = <Type,>({
unleashGetter,
dataKey = '',
refetchFunctionKey = '',
options = {},
params = [''],
}: IGetterOptions): IFormatUnleashGetterOutput<Type> => {
const result = unleashGetter(...params, { refreshInterval: 5, ...options });
return { data: result[dataKey], refetch: result[refetchFunctionKey] };
};
interface IGetterOptions {
dataKey: string;
unleashGetter: any;
options: SWRConfiguration;
refetchFunctionKey: string;
params: string[];
}
interface ICollaborateDataOutput<Type> {
staleDataNotification: JSX.Element;
data: Type | null;
refetch: () => void;
forceRefreshCache: (data: Type) => void;
}
interface IStaleNotificationOptions {
afterSubmitAction: () => void;
}
export const useCollaborateData = <Type,>(
getterOptions: IGetterOptions,
initialData: Type,
notificationOptions: IStaleNotificationOptions
): ICollaborateDataOutput<Type> => {
const { data, refetch } = formatUnleashGetter<Type>(getterOptions);
const [cache, setCache] = useState<Type | null>(initialData || null);
const [dataModified, setDataModified] = useState(false);
const forceRefreshCache = (data: Type) => {
setDataModified(false);
setCache(data);
};
useEffect(() => {
if (cache === null) {
setCache(initialData);
}
}, [initialData]);
useEffect(() => {
const equal = dequal(data, cache);
if (!equal) {
setDataModified(true);
}
}, [data]);
return {
data: cache,
refetch,
staleDataNotification: (
<StaleDataNotification
cache={cache}
data={data}
refresh={() => forceRefreshCache(data)}
show={dataModified}
afterSubmitAction={notificationOptions.afterSubmitAction}
/>
),
forceRefreshCache,
};
};