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-03-17 13:41:59 +01:00
|
|
|
import {
|
|
|
|
DefaultStickiness,
|
|
|
|
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 {
|
|
|
|
defaultStickiness?: DefaultStickiness;
|
|
|
|
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-03-17 13:41:59 +01:00
|
|
|
const { data, error, mutate } = useConditionalSWR<ISettingsResponse>(
|
2023-03-17 03:35:55 +01:00
|
|
|
Boolean(projectId) && Boolean(projectScopedStickiness),
|
|
|
|
{},
|
2023-03-15 15:06:25 +01:00
|
|
|
['useDefaultProjectSettings', PATH],
|
2023-03-23 15:13:28 +01:00
|
|
|
() => fetcher(formatApiPath(PATH)),
|
2023-03-15 15:06:25 +01:00
|
|
|
options
|
|
|
|
);
|
|
|
|
|
2023-03-17 13:41:59 +01:00
|
|
|
const defaultStickiness: DefaultStickiness =
|
|
|
|
data?.defaultStickiness ?? DEFAULT_STICKINESS;
|
2023-03-15 15:06:25 +01:00
|
|
|
|
|
|
|
const refetch = useCallback(() => {
|
|
|
|
mutate().catch(console.warn);
|
|
|
|
}, [mutate]);
|
|
|
|
return {
|
|
|
|
defaultStickiness,
|
|
|
|
refetch,
|
|
|
|
loading: !error && !data,
|
|
|
|
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
|
|
|
};
|