2022-11-14 12:34:38 +01:00
|
|
|
import React from 'react';
|
2022-11-04 09:50:00 +01:00
|
|
|
import useUiConfig from './api/getters/useUiConfig/useUiConfig';
|
2022-11-14 12:34:38 +01:00
|
|
|
import { useChangeRequestConfig } from './api/getters/useChangeRequestConfig/useChangeRequestConfig';
|
2022-11-04 09:50:00 +01:00
|
|
|
|
2022-11-14 12:34:38 +01:00
|
|
|
export const useChangeRequestsEnabled = (projectId: string) => {
|
2022-11-04 09:50:00 +01:00
|
|
|
const { uiConfig } = useUiConfig();
|
2022-11-14 12:34:38 +01:00
|
|
|
const { data } = useChangeRequestConfig(projectId);
|
|
|
|
|
|
|
|
const isChangeRequestConfigured = React.useCallback(
|
|
|
|
(environment: string): boolean => {
|
|
|
|
const enabled = data.some(draft => {
|
|
|
|
return (
|
|
|
|
draft.environment === environment &&
|
|
|
|
draft.changeRequestEnabled
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return Boolean(uiConfig?.flags.changeRequests) && enabled;
|
|
|
|
},
|
2022-11-24 16:16:14 +01:00
|
|
|
[JSON.stringify(data)]
|
2022-11-07 09:16:29 +01:00
|
|
|
);
|
2022-11-14 12:34:38 +01:00
|
|
|
|
|
|
|
const isChangeRequestConfiguredInAnyEnv = React.useCallback((): boolean => {
|
|
|
|
return (
|
|
|
|
Boolean(uiConfig?.flags.changeRequests) &&
|
|
|
|
data.some(draft => draft.changeRequestEnabled)
|
|
|
|
);
|
2022-11-24 16:16:14 +01:00
|
|
|
}, [JSON.stringify(data)]);
|
2022-11-14 12:34:38 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
isChangeRequestFlagEnabled: Boolean(uiConfig?.flags.changeRequests),
|
|
|
|
isChangeRequestConfigured,
|
|
|
|
isChangeRequestConfiguredInAnyEnv,
|
|
|
|
};
|
2022-11-04 09:50:00 +01:00
|
|
|
};
|