PromucFlow_constructor/app/client/src/utils/hooks/useFeatureFlagOverride.ts
Valera Melnikov a2bfe450b6
chore: enable no-explicit-any rule (#35321)
## Description
-  Enabled the rule `@typescript-eslint/no-explicit-any`
- Suppressed errors with comment
```
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
```

Fixes #35308 

## Automation

/ok-to-test tags="@tag.All"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/10181176984>
> Commit: 7fc604e24fa234da7ab2ff56e0b1c715268796ee
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=10181176984&attempt=2"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.All`
> Spec:
> <hr>Wed, 31 Jul 2024 15:00:45 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [x] No
2024-07-31 18:41:28 +03:00

81 lines
2.6 KiB
TypeScript

import type { FeatureFlag } from "@appsmith/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",
"release_anvil_toggle_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]);
};