Merge pull request #3149 from spotify/rugvip/ff
core-api: refactor and slim down the FeatureFlagsApi
This commit is contained in:
@@ -55,9 +55,7 @@ import { useSubtleTypographyStyles } from '../../utils/styles';
|
||||
|
||||
export const CostInsightsPage = () => {
|
||||
const classes = useSubtleTypographyStyles();
|
||||
const flags = useApi(featureFlagsApiRef).getFlags();
|
||||
// There is not currently a UI to set feature flags
|
||||
// flags.set('cost-insights-currencies', FeatureFlagState.On);
|
||||
const featureFlags = useApi(featureFlagsApiRef);
|
||||
const client = useApi(costInsightsApiRef);
|
||||
const config = useConfig();
|
||||
const groups = useGroups();
|
||||
@@ -211,7 +209,7 @@ export const CostInsightsPage = () => {
|
||||
</Typography>
|
||||
</Box>
|
||||
<Box minHeight={40} maxHeight={60} display="flex">
|
||||
{!!flags.get('cost-insights-currencies') && (
|
||||
{featureFlags.isActive('cost-insights-currencies') && (
|
||||
<Box mr={1}>
|
||||
<CurrencySelect
|
||||
currency={currency}
|
||||
|
||||
@@ -16,9 +16,7 @@
|
||||
|
||||
import React, { useCallback, useState } from 'react';
|
||||
import {
|
||||
FeatureFlagName,
|
||||
featureFlagsApiRef,
|
||||
FeatureFlagsRegistryItem,
|
||||
FeatureFlagState,
|
||||
InfoCard,
|
||||
useApi,
|
||||
@@ -30,29 +28,28 @@ import { FlagItem } from './FeatureFlagsItem';
|
||||
export const FeatureFlags = () => {
|
||||
const featureFlagsApi = useApi(featureFlagsApiRef);
|
||||
const featureFlags = featureFlagsApi.getRegisteredFlags();
|
||||
const initialFlagState = featureFlags.reduce(
|
||||
(result, featureFlag: FeatureFlagsRegistryItem) => {
|
||||
const state = featureFlagsApi.getFlags().get(featureFlag.name);
|
||||
|
||||
result[featureFlag.name] = state;
|
||||
return result;
|
||||
},
|
||||
{} as Record<FeatureFlagName, FeatureFlagState>,
|
||||
const initialFlagState = Object.fromEntries(
|
||||
featureFlags.map(({ name }) => [name, featureFlagsApi.isActive(name)]),
|
||||
);
|
||||
|
||||
const [state, setState] = useState<Record<FeatureFlagName, FeatureFlagState>>(
|
||||
initialFlagState,
|
||||
);
|
||||
const [state, setState] = useState<Record<string, boolean>>(initialFlagState);
|
||||
|
||||
const toggleFlag = useCallback(
|
||||
(flagName: FeatureFlagName) => {
|
||||
const newState = featureFlagsApi.getFlags().toggle(flagName);
|
||||
(flagName: string) => {
|
||||
const newState = featureFlagsApi.isActive(flagName)
|
||||
? FeatureFlagState.None
|
||||
: FeatureFlagState.Active;
|
||||
|
||||
featureFlagsApi.save({
|
||||
states: { [flagName]: newState },
|
||||
merge: true,
|
||||
});
|
||||
|
||||
setState(prevState => ({
|
||||
...prevState,
|
||||
[flagName]: newState,
|
||||
[flagName]: newState === FeatureFlagState.Active,
|
||||
}));
|
||||
featureFlagsApi.getFlags().save();
|
||||
},
|
||||
[featureFlagsApi],
|
||||
);
|
||||
|
||||
@@ -22,10 +22,10 @@ import {
|
||||
Switch,
|
||||
Tooltip,
|
||||
} from '@material-ui/core';
|
||||
import { FeatureFlagsRegistryItem } from '@backstage/core';
|
||||
import { FeatureFlag } from '@backstage/core';
|
||||
|
||||
type Props = {
|
||||
flag: FeatureFlagsRegistryItem;
|
||||
flag: FeatureFlag;
|
||||
enabled: boolean;
|
||||
toggleHandler: Function;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user