1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

Revert "fix: laggy switch" (#3815)

Reverts Unleash/unleash#3814 forcing merge to fix issue in demo instance: https://github.com/Unleash/unleash/pull/3815#issuecomment-1554712970
This commit is contained in:
Gastón Fournier 2023-05-19 16:55:14 +02:00 committed by GitHub
parent 0c538f070a
commit 6e847d0015
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
import { useCallback, useState, VFC } from 'react';
import { useState, VFC } from 'react';
import { Box, styled } from '@mui/material';
import PermissionSwitch from 'component/common/PermissionSwitch/PermissionSwitch';
import { UPDATE_FEATURE_ENVIRONMENT } from 'component/providers/AccessProvider/permissions';
@ -68,8 +68,9 @@ export const FeatureToggleSwitch: VFC<IFeatureToggleSwitchProps> = ({
onToggle(projectId, feature.name, environmentName, !isChecked);
};
const handleToggleEnvironmentOn = useCallback(
async (shouldActivateDisabled = false) => {
const handleToggleEnvironmentOn = async (
shouldActivateDisabled = false
) => {
try {
setIsChecked(!isChecked);
await toggleFeatureEnvironmentOn(
@ -95,18 +96,9 @@ export const FeatureToggleSwitch: VFC<IFeatureToggleSwitchProps> = ({
}
rollbackIsChecked();
}
},
[
rollbackIsChecked,
setToastApiError,
showInfoBox,
setToastData,
toggleFeatureEnvironmentOn,
setIsChecked,
]
);
};
const handleToggleEnvironmentOff = useCallback(async () => {
const handleToggleEnvironmentOff = async () => {
try {
setIsChecked(!isChecked);
await toggleFeatureEnvironmentOff(
@ -124,27 +116,9 @@ export const FeatureToggleSwitch: VFC<IFeatureToggleSwitchProps> = ({
setToastApiError(formatUnknownError(error));
rollbackIsChecked();
}
}, [
toggleFeatureEnvironmentOff,
setToastData,
setToastApiError,
rollbackIsChecked,
setIsChecked,
]);
};
const featureHasOnlyDisabledStrategies = useCallback(() => {
const featureEnvironment = feature?.environments?.find(
env => env.name === environmentName
);
return (
featureEnvironment?.strategies &&
featureEnvironment?.strategies?.length > 0 &&
featureEnvironment?.strategies?.every(strategy => strategy.disabled)
);
}, [environmentName]);
const onClick = useCallback(
async (e: React.MouseEvent) => {
const onClick = async (e: React.MouseEvent) => {
if (isChangeRequestConfigured(environmentName)) {
e.preventDefault();
if (featureHasOnlyDisabledStrategies()) {
@ -169,41 +143,36 @@ export const FeatureToggleSwitch: VFC<IFeatureToggleSwitchProps> = ({
} else {
await handleToggleEnvironmentOn();
}
},
[
isChangeRequestConfigured,
onChangeRequestToggle,
handleToggleEnvironmentOff,
setShowEnabledDialog,
]
);
};
const onActivateStrategies = useCallback(async () => {
const onActivateStrategies = async () => {
if (isChangeRequestConfigured(environmentName)) {
onChangeRequestToggle(feature.name, environmentName, !value, true);
} else {
await handleToggleEnvironmentOn(true);
}
setShowEnabledDialog(false);
}, [
handleToggleEnvironmentOn,
setShowEnabledDialog,
isChangeRequestConfigured,
onChangeRequestToggle,
]);
};
const onAddDefaultStrategy = useCallback(async () => {
const onAddDefaultStrategy = async () => {
if (isChangeRequestConfigured(environmentName)) {
onChangeRequestToggle(feature.name, environmentName, !value, false);
} else {
await handleToggleEnvironmentOn();
}
setShowEnabledDialog(false);
}, [
isChangeRequestConfigured,
onChangeRequestToggle,
handleToggleEnvironmentOn,
]);
};
const featureHasOnlyDisabledStrategies = () => {
const featureEnvironment = feature?.environments?.find(
env => env.name === environmentName
);
return (
featureEnvironment?.strategies &&
featureEnvironment?.strategies?.length > 0 &&
featureEnvironment?.strategies?.every(strategy => strategy.disabled)
);
};
const key = `${feature.name}-${environmentName}`;
@ -219,7 +188,7 @@ export const FeatureToggleSwitch: VFC<IFeatureToggleSwitchProps> = ({
? `Disable feature in ${environmentName}`
: `Enable feature in ${environmentName}`
}
checked={isChecked}
checked={value}
environmentId={environmentName}
projectId={projectId}
permission={UPDATE_FEATURE_ENVIRONMENT}