1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/frontend/src/hooks/useChangeRequestsEnabled.ts

36 lines
1.2 KiB
TypeScript
Raw Normal View History

import React from 'react';
import useUiConfig from './api/getters/useUiConfig/useUiConfig';
import { useChangeRequestConfig } from './api/getters/useChangeRequestConfig/useChangeRequestConfig';
export const useChangeRequestsEnabled = (projectId: string) => {
const { uiConfig } = useUiConfig();
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;
},
[JSON.stringify(data)]
);
const isChangeRequestConfiguredInAnyEnv = React.useCallback((): boolean => {
return (
Boolean(uiConfig?.flags.changeRequests) &&
data.some(draft => draft.changeRequestEnabled)
);
}, [JSON.stringify(data)]);
return {
isChangeRequestFlagEnabled: Boolean(uiConfig?.flags.changeRequests),
isChangeRequestConfigured,
isChangeRequestConfiguredInAnyEnv,
};
};