2023-03-15 15:06:25 +01:00
|
|
|
import useUiConfig from './api/getters/useUiConfig/useUiConfig';
|
2023-03-17 03:35:55 +01:00
|
|
|
import { SWRConfiguration } from 'swr';
|
2023-03-15 15:06:25 +01:00
|
|
|
import { useCallback } from 'react';
|
|
|
|
import handleErrorResponses from './api/getters/httpErrorResponseHandler';
|
2023-03-17 03:35:55 +01:00
|
|
|
import { useConditionalSWR } from './api/getters/useConditionalSWR/useConditionalSWR';
|
2023-04-04 10:46:28 +02:00
|
|
|
import { ProjectMode } from 'component/project/Project/hooks/useProjectForm';
|
2023-03-23 15:13:28 +01:00
|
|
|
import { formatApiPath } from 'utils/formatPath';
|
2023-03-15 15:06:25 +01:00
|
|
|
|
2023-03-17 13:41:59 +01:00
|
|
|
export interface ISettingsResponse {
|
2023-04-04 10:46:28 +02:00
|
|
|
defaultStickiness?: string;
|
2023-03-17 13:41:59 +01:00
|
|
|
mode?: ProjectMode;
|
2023-03-15 15:06:25 +01:00
|
|
|
}
|
|
|
|
const DEFAULT_STICKINESS = 'default';
|
|
|
|
export const useDefaultProjectSettings = (
|
|
|
|
projectId?: string,
|
|
|
|
options?: SWRConfiguration
|
|
|
|
) => {
|
|
|
|
const { uiConfig } = useUiConfig();
|
|
|
|
|
2023-03-23 15:13:28 +01:00
|
|
|
const PATH = `api/admin/projects/${projectId}/settings`;
|
2023-03-15 15:06:25 +01:00
|
|
|
const { projectScopedStickiness } = uiConfig.flags;
|
|
|
|
|
2023-04-04 10:46:28 +02:00
|
|
|
const { data, isLoading, error, mutate } =
|
|
|
|
useConditionalSWR<ISettingsResponse>(
|
|
|
|
Boolean(projectId) && Boolean(projectScopedStickiness),
|
|
|
|
{},
|
|
|
|
['useDefaultProjectSettings', PATH],
|
|
|
|
() => fetcher(formatApiPath(PATH)),
|
|
|
|
options
|
|
|
|
);
|
2023-03-15 15:06:25 +01:00
|
|
|
|
2023-04-04 10:46:28 +02:00
|
|
|
const defaultStickiness = (): string => {
|
|
|
|
if (!isLoading) {
|
|
|
|
if (data?.defaultStickiness) {
|
|
|
|
return data?.defaultStickiness;
|
|
|
|
}
|
|
|
|
return DEFAULT_STICKINESS;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
};
|
2023-03-15 15:06:25 +01:00
|
|
|
|
|
|
|
const refetch = useCallback(() => {
|
|
|
|
mutate().catch(console.warn);
|
|
|
|
}, [mutate]);
|
|
|
|
return {
|
2023-04-04 10:46:28 +02:00
|
|
|
defaultStickiness: defaultStickiness(),
|
2023-03-15 15:06:25 +01:00
|
|
|
refetch,
|
2023-04-04 10:46:28 +02:00
|
|
|
loading: isLoading,
|
2023-03-15 15:06:25 +01:00
|
|
|
error,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-03-17 03:35:55 +01:00
|
|
|
const fetcher = (path: string) => {
|
|
|
|
return fetch(path)
|
|
|
|
.then(handleErrorResponses('Project stickiness data'))
|
|
|
|
.then(res => res.json());
|
2023-03-15 15:06:25 +01:00
|
|
|
};
|