PromucFlow_constructor/app/client/src/utils/hooks/useFeatureFlagOverride.ts
Nidhi 919b2907a1
chore: Remove function calling flag (#39790)
## Description
> [!TIP]  
> _Add a TL;DR when the description is longer than 500 words or
extremely technical (helps the content, marketing, and DevRel team)._
>
> _Please also include relevant motivation and context. List any
dependencies that are required for this change. Add links to Notion,
Figma or any other documents that might be relevant to the PR._


Fixes #`Issue Number`  
_or_  
Fixes `Issue URL`
> [!WARNING]  
> _If no issue exists, please create an issue first, and check with the
maintainers if the issue is valid._

## Automation

/ok-to-test tags=""

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!CAUTION]  
> If you modify the content in this section, you are likely to disrupt
the CI result for your PR.

<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Refactor**
- Updated the logic that determines when the visualization view is
enabled, now relying on a refined integration check.
  
- **Chores**
- Removed an outdated configuration toggle from overall feature
settings.
  - Streamlined override options by eliminating an unnecessary flag.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-03-18 21:06:31 +05:30

83 lines
2.6 KiB
TypeScript

import type { FeatureFlag } from "ee/entities/FeatureFlag";
import {
setFeatureFlagOverridesAction,
updateFeatureFlagOverrideAction,
} from "actions/featureFlagActions";
import { isBoolean } from "lodash";
import { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { getFeatureFlagsFetched } from "selectors/usersSelectors";
import {
getFeatureFlagOverrideValues,
setFeatureFlagOverrideValues,
} from "utils/storage";
export const AvailableFeaturesToOverride: FeatureFlag[] = [
"release_anvil_enabled",
"release_layout_conversion_enabled",
];
export type OverriddenFeatureFlags = Partial<Record<FeatureFlag, boolean>>;
export const useFeatureFlagOverride = () => {
const dispatch = useDispatch();
const areFeatureFlagsFetched = useSelector(getFeatureFlagsFetched);
/**
* Fetches the feature flag override values and updates the state.
*/
useEffect(() => {
if (areFeatureFlagsFetched) {
getFeatureFlagOverrideValues().then((flagValues) => {
const filteredFlagValues = (
Object.entries(flagValues) as [FeatureFlag, boolean][]
).reduce((acc, [flagName, flagValue]) => {
if (
AvailableFeaturesToOverride.includes(flagName) &&
isBoolean(flagValue)
) {
acc[flagName] = flagValues[flagName];
}
return acc;
}, {} as OverriddenFeatureFlags);
if (filteredFlagValues) {
dispatch(setFeatureFlagOverridesAction(filteredFlagValues));
}
});
}
}, [areFeatureFlagsFetched, dispatch]);
/**
* Sets up a global function to toggle the feature flag override.
*/
useEffect(() => {
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any).overrideFeatureFlag = (
featureFlagValues: OverriddenFeatureFlags,
) => {
const areAllFlagsValid = (
Object.entries(featureFlagValues) as [FeatureFlag, boolean][]
).every(
([flagName, flagValue]) =>
AvailableFeaturesToOverride.includes(flagName) &&
isBoolean(flagValue),
);
if (areAllFlagsValid) {
dispatch(updateFeatureFlagOverrideAction(featureFlagValues));
setFeatureFlagOverrideValues(featureFlagValues);
window.console.log(
"Feature flag override values set to: ",
featureFlagValues,
);
} else {
window.console.error(
"Invalid feature flag override values. Please check the feature flags being overridden.",
);
}
};
}, [dispatch]);
};