mirror of
https://github.com/Unleash/unleash.git
synced 2025-07-02 01:17:58 +02:00
Fix(1-3804)/store flag creation form state (#10089)
Adds caching via localstorage to the flag creation form, so that if you
(accidentally) close the form before submitting it, you'll retain (most)
of the same data when you reopen it.
Specifically, we'll store:
- name
- description
- type
- tags
- impression data
We can't store the project as it is now, because it gets overridden by
whatever is in the URL. However, this is probably a good thing. It means
that if you navigate to a different project and open the feature
creation form there, it'll retain everything from the last one, but
it'll use the current project.
The stored data is cleared when you successfully create a feature, so
that you don't get dangling data.
The data is also stored in a shared cache for all projects, so that you
don't have different caches per project.
The behavior of seeding the form is hidden behind a flag (that doesn't
exist yet). We'll still read and write to the cache if the flag is off,
but we won't use it to populate the feature form, so it has no
discernible impact on the user.
## Bug detected 🐛 ... and squashed
Working on this, I came to realize that there was a bug in how the
config button and use feature form hooks interacted. We (in this case
probably me) have assumed that it's fine to use a set for any option
checking in the config buttons. Also, we're using a set to store tags in
the feature form. But objects aren't compared by value in JS, so the set
will happily accept multiple instances of the same tag. Likewise, these
tags won't show up as selected in the dropdown because when the dropdown
checks if the set `has` the value, it's using reference equality.
To get around this, I have normalized the values of the Tags set to
strings (`<type>:<value>`), which are easily comparable.
We can iterate on this later if we need to.
## `useLocalStorageState`
In doing this, I have also made a change to the useLocalStorageState
hook:
the exposed "setState" function now writes to the localstorage
immediately. This is because the useEffect method might not have time to
save the data if the component unmounts (this was the case with the flag
dialog).
However, I have kept the useEffect because it gets run on component
mount and then only when it changes. This means that we will get double
saves to localstorage, but it'll be with the same data, so it's benign.
I've tried out two other uses of the hook (event timeline props and
environment columns in the project flags table) and see no discernible
difference in behavior.
## `useFeatureForm`
I have also made a change to the useFeatureForm hook and removed a
`useEffect` that would reset the name to whatever you passed in as the
initial name if you cleared it out. This essentially meant that you
couldn't clear the name completely, because it would just refill with
the initial name.
As far as I can tell, there is no need to have this sticking around
anymore. The hook is only used in two places: the flag creation dialog
and the flag edit page. The flag edit page doesn't allow you to change
the name anyway and it was causing issues in the dialog. It's likely a
holdover from the way something worked 3 years ago. Both the dialog and
the edit screen seem to work just fine with this change.
I have also changed the function parameters from ordered parameters to
an object. There's so many of them that even you don't think it's a good
idea to use objects when you have multiple params with the same type,
it's just nigh-impossible to remember the order by now.
## Minor changes
Additionally, I came across three issues that were causing react errors,
and have fixed them.
1. we'd forgotten to interpolate a variable and just used the variable
name in a string instead
2. an html attribute that doesn't exist (`aria-role` instead of `role`)
3. Providing a disabled button inside a tooltip. I've seen this one
around for ages and it prevented tooltips from working on disabled
buttons. The solution was wrapping it in a span.
This commit is contained in:
parent
bdb763c9d5
commit
c739ea71cf
@ -107,7 +107,7 @@ export function DropdownList<T = string>({
|
||||
return (
|
||||
<StyledListItem
|
||||
aria-describedby={labelId}
|
||||
key={`${option.label}@index`}
|
||||
key={`${option.label}@${index}`}
|
||||
dense
|
||||
disablePadding
|
||||
tabIndex={0}
|
||||
|
@ -36,7 +36,7 @@ export function ToggleConfigButton({
|
||||
variant='custom'
|
||||
>
|
||||
<Button
|
||||
aria-role='switch'
|
||||
role='switch'
|
||||
aria-checked={currentValue}
|
||||
variant={currentValue ? 'contained' : 'outlined'}
|
||||
color='primary'
|
||||
|
@ -109,18 +109,20 @@ const BasePermissionButton = React.forwardRef<
|
||||
title={formatAccessText(access, tooltipProps?.title)}
|
||||
arrow
|
||||
>
|
||||
<StyledButton
|
||||
ref={ref}
|
||||
onClick={onClick}
|
||||
disabled={disabled || !access}
|
||||
aria-labelledby={id}
|
||||
variant={variant}
|
||||
color={color}
|
||||
{...rest}
|
||||
endIcon={endIcon}
|
||||
>
|
||||
{children}
|
||||
</StyledButton>
|
||||
<span>
|
||||
<StyledButton
|
||||
ref={ref}
|
||||
onClick={onClick}
|
||||
disabled={disabled || !access}
|
||||
aria-labelledby={id}
|
||||
variant={variant}
|
||||
color={color}
|
||||
{...rest}
|
||||
endIcon={endIcon}
|
||||
>
|
||||
{children}
|
||||
</StyledButton>
|
||||
</span>
|
||||
</TooltipResolver>
|
||||
);
|
||||
},
|
||||
|
@ -32,13 +32,13 @@ const EditFeature = () => {
|
||||
impressionData,
|
||||
setImpressionData,
|
||||
clearErrors,
|
||||
} = useFeatureForm(
|
||||
feature?.name,
|
||||
feature?.type,
|
||||
feature?.project,
|
||||
feature?.description,
|
||||
feature?.impressionData,
|
||||
);
|
||||
} = useFeatureForm({
|
||||
name: feature?.name,
|
||||
type: feature?.type,
|
||||
project: feature?.project,
|
||||
description: feature?.description,
|
||||
impressionData: feature?.impressionData,
|
||||
});
|
||||
|
||||
const createPatch = () => {
|
||||
const comparison = { ...feature, type, description, impressionData };
|
||||
|
@ -6,19 +6,29 @@ import { formatUnknownError } from 'utils/formatUnknownError';
|
||||
import type { ITag } from 'interfaces/tags';
|
||||
import type { CreateFeatureSchema, CreateFeatureSchemaType } from 'openapi';
|
||||
|
||||
const useFeatureForm = (
|
||||
initialName: string = '',
|
||||
initialType: CreateFeatureSchemaType = 'release',
|
||||
initialProject: string = 'default',
|
||||
initialDescription: string = '',
|
||||
initialImpressionData: boolean = false,
|
||||
) => {
|
||||
export type FeatureFormInitialData = Partial<{
|
||||
name: string;
|
||||
type: CreateFeatureSchemaType;
|
||||
project: string;
|
||||
description: string;
|
||||
impressionData: boolean;
|
||||
tags: Set<ITag>;
|
||||
}>;
|
||||
|
||||
const useFeatureForm = ({
|
||||
name: initialName = '',
|
||||
type: initialType = 'release',
|
||||
project: initialProject = 'default',
|
||||
description: initialDescription = '',
|
||||
impressionData: initialImpressionData = false,
|
||||
tags: initialTags = new Set(),
|
||||
}: FeatureFormInitialData) => {
|
||||
const projectId = useRequiredPathParam('projectId');
|
||||
const params = useQueryParams();
|
||||
const { validateFeatureToggleName } = useFeatureApi();
|
||||
const toggleQueryName = params.get('name');
|
||||
const [type, setType] = useState(initialType);
|
||||
const [tags, setTags] = useState<Set<ITag>>(new Set());
|
||||
const [tags, setTags] = useState<Set<ITag>>(initialTags);
|
||||
const [name, setName] = useState(toggleQueryName || initialName);
|
||||
const [project, setProject] = useState(projectId || initialProject);
|
||||
const [description, setDescription] = useState(initialDescription);
|
||||
@ -31,12 +41,6 @@ const useFeatureForm = (
|
||||
setType(initialType);
|
||||
}, [initialType]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!name) {
|
||||
setName(toggleQueryName || initialName);
|
||||
}
|
||||
}, [name, initialName, toggleQueryName]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!projectId) setProject(initialProject);
|
||||
else setProject(projectId);
|
||||
|
@ -9,7 +9,9 @@ import { Dialog, styled } from '@mui/material';
|
||||
import useProjects from 'hooks/api/getters/useProjects/useProjects';
|
||||
import { Limit } from 'component/common/Limit/Limit';
|
||||
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
|
||||
import useFeatureForm from 'component/feature/hooks/useFeatureForm';
|
||||
import useFeatureForm, {
|
||||
type FeatureFormInitialData,
|
||||
} from 'component/feature/hooks/useFeatureForm';
|
||||
import useFeatureApi from 'hooks/api/actions/useFeatureApi/useFeatureApi';
|
||||
import FlagIcon from '@mui/icons-material/Flag';
|
||||
import ImpressionDataIcon from '@mui/icons-material/AltRoute';
|
||||
@ -26,11 +28,12 @@ import useAllTags from 'hooks/api/getters/useAllTags/useAllTags';
|
||||
import Label from '@mui/icons-material/Label';
|
||||
import { ProjectIcon } from 'component/common/ProjectIcon/ProjectIcon';
|
||||
import { MultiSelectConfigButton } from 'component/common/DialogFormTemplate/ConfigButtons/MultiSelectConfigButton';
|
||||
import type { ITag } from 'interfaces/tags';
|
||||
import { ToggleConfigButton } from 'component/common/DialogFormTemplate/ConfigButtons/ToggleConfigButton';
|
||||
import { useFlagLimits } from './useFlagLimits.tsx';
|
||||
import { useFeatureCreatedFeedback } from './hooks/useFeatureCreatedFeedback.ts';
|
||||
import { formatTag } from 'utils/format-tag';
|
||||
import { useLocalStorageState } from 'hooks/useLocalStorageState.ts';
|
||||
import { useUiFlag } from 'hooks/useUiFlag.ts';
|
||||
|
||||
interface ICreateFeatureDialogProps {
|
||||
open: boolean;
|
||||
@ -103,6 +106,16 @@ const CreateFeatureDialogContent = ({
|
||||
const navigate = useNavigate();
|
||||
const openFeatureCreatedFeedback = useFeatureCreatedFeedback();
|
||||
|
||||
const [storedFlagConfig, setStoredFlagConfig] =
|
||||
useLocalStorageState<FeatureFormInitialData>(
|
||||
'flag-creation-dialog',
|
||||
{},
|
||||
60 * 60 * 1000, // <- 1 hour
|
||||
);
|
||||
const useFlagCreationCache = useUiFlag('createFlagDialogCache');
|
||||
|
||||
const initialData = useFlagCreationCache ? storedFlagConfig : {};
|
||||
|
||||
const {
|
||||
type,
|
||||
setType,
|
||||
@ -120,7 +133,7 @@ const CreateFeatureDialogContent = ({
|
||||
getTogglePayload,
|
||||
clearErrors,
|
||||
errors,
|
||||
} = useFeatureForm();
|
||||
} = useFeatureForm(initialData);
|
||||
const { createFeatureToggle, loading } = useFeatureApi();
|
||||
|
||||
const generalDocumentation: {
|
||||
@ -170,6 +183,7 @@ const CreateFeatureDialogContent = ({
|
||||
});
|
||||
onClose();
|
||||
onSuccess?.();
|
||||
setStoredFlagConfig({});
|
||||
openFeatureCreatedFeedback();
|
||||
} catch (error: unknown) {
|
||||
setToastApiError(formatUnknownError(error));
|
||||
@ -210,8 +224,19 @@ const CreateFeatureDialogContent = ({
|
||||
return projectObject?.name;
|
||||
}, [project, projects]);
|
||||
|
||||
const onDialogClose = () => {
|
||||
setStoredFlagConfig({
|
||||
name,
|
||||
tags,
|
||||
impressionData,
|
||||
type,
|
||||
description,
|
||||
});
|
||||
onClose();
|
||||
};
|
||||
|
||||
return (
|
||||
<StyledDialog open={open} onClose={onClose}>
|
||||
<StyledDialog open={open} onClose={onDialogClose}>
|
||||
<FormTemplate
|
||||
compact
|
||||
disablePadding
|
||||
@ -289,17 +314,32 @@ const CreateFeatureDialogContent = ({
|
||||
/>
|
||||
}
|
||||
/>
|
||||
<MultiSelectConfigButton<ITag>
|
||||
<MultiSelectConfigButton
|
||||
tooltip={{
|
||||
header: 'Select tags',
|
||||
}}
|
||||
description={configButtonData.tags.text}
|
||||
selectedOptions={tags}
|
||||
selectedOptions={
|
||||
new Set(
|
||||
Array.from(tags).map(
|
||||
(tag) => `${tag.type}:${tag.value}`,
|
||||
),
|
||||
)
|
||||
}
|
||||
options={allTags.map((tag) => ({
|
||||
label: formatTag(tag),
|
||||
value: tag,
|
||||
value: `${tag.type}:${tag.value}`,
|
||||
}))}
|
||||
onChange={setTags}
|
||||
onChange={(strings) => {
|
||||
const normalized = Array.from(strings).map(
|
||||
(string) => {
|
||||
const [type, value] =
|
||||
string.split(':');
|
||||
return { type, value };
|
||||
},
|
||||
);
|
||||
setTags(new Set(normalized));
|
||||
}}
|
||||
button={{
|
||||
label:
|
||||
tags.size > 0
|
||||
|
@ -73,31 +73,33 @@ exports[`renders an empty list correctly 1`] = `
|
||||
<hr
|
||||
className="MuiDivider-root MuiDivider-middle MuiDivider-vertical css-1cqsk4j-MuiDivider-root"
|
||||
/>
|
||||
<button
|
||||
aria-labelledby="useId-0"
|
||||
className="MuiButtonBase-root MuiButton-root MuiButton-contained MuiButton-containedPrimary MuiButton-sizeMedium MuiButton-containedSizeMedium MuiButton-root MuiButton-contained MuiButton-containedPrimary MuiButton-sizeMedium MuiButton-containedSizeMedium css-1ind840-MuiButtonBase-root-MuiButton-root"
|
||||
disabled={false}
|
||||
onBlur={[Function]}
|
||||
onClick={[Function]}
|
||||
onContextMenu={[Function]}
|
||||
onDragLeave={[Function]}
|
||||
onFocus={[Function]}
|
||||
onKeyDown={[Function]}
|
||||
onKeyUp={[Function]}
|
||||
onMouseDown={[Function]}
|
||||
onMouseLeave={[Function]}
|
||||
onMouseUp={[Function]}
|
||||
onTouchEnd={[Function]}
|
||||
onTouchMove={[Function]}
|
||||
onTouchStart={[Function]}
|
||||
tabIndex={0}
|
||||
type="button"
|
||||
>
|
||||
New tag type
|
||||
<span
|
||||
className="MuiTouchRipple-root css-8je8zh-MuiTouchRipple-root"
|
||||
/>
|
||||
</button>
|
||||
<span>
|
||||
<button
|
||||
aria-labelledby="useId-0"
|
||||
className="MuiButtonBase-root MuiButton-root MuiButton-contained MuiButton-containedPrimary MuiButton-sizeMedium MuiButton-containedSizeMedium MuiButton-root MuiButton-contained MuiButton-containedPrimary MuiButton-sizeMedium MuiButton-containedSizeMedium css-1ind840-MuiButtonBase-root-MuiButton-root"
|
||||
disabled={false}
|
||||
onBlur={[Function]}
|
||||
onClick={[Function]}
|
||||
onContextMenu={[Function]}
|
||||
onDragLeave={[Function]}
|
||||
onFocus={[Function]}
|
||||
onKeyDown={[Function]}
|
||||
onKeyUp={[Function]}
|
||||
onMouseDown={[Function]}
|
||||
onMouseLeave={[Function]}
|
||||
onMouseUp={[Function]}
|
||||
onTouchEnd={[Function]}
|
||||
onTouchMove={[Function]}
|
||||
onTouchStart={[Function]}
|
||||
tabIndex={0}
|
||||
type="button"
|
||||
>
|
||||
New tag type
|
||||
<span
|
||||
className="MuiTouchRipple-root css-8je8zh-MuiTouchRipple-root"
|
||||
/>
|
||||
</button>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -13,7 +13,13 @@ export const useLocalStorageState = <T extends object | string>(
|
||||
|
||||
useEffect(() => {
|
||||
setStoredValue(localValue);
|
||||
}, [localValue]);
|
||||
}, [localValue, setStoredValue]);
|
||||
|
||||
return [localValue, setLocalValue] as const;
|
||||
return [
|
||||
localValue,
|
||||
(value: T | ((prevState: T) => T)) => {
|
||||
setStoredValue(value);
|
||||
setLocalValue(value);
|
||||
},
|
||||
] as const;
|
||||
};
|
||||
|
@ -90,6 +90,7 @@ export type UiFlags = {
|
||||
registerFrontendClient?: boolean;
|
||||
customMetrics?: boolean;
|
||||
lifecycleMetrics?: boolean;
|
||||
createFlagDialogCache?: boolean;
|
||||
sideMenuCleanup?: boolean;
|
||||
};
|
||||
|
||||
|
@ -63,6 +63,7 @@ export type IFlagKey =
|
||||
| 'newGettingStartedEmail'
|
||||
| 'lifecycleMetrics'
|
||||
| 'customMetrics'
|
||||
| 'createFlagDialogCache'
|
||||
| 'sideMenuCleanup';
|
||||
|
||||
export type IFlags = Partial<{ [key in IFlagKey]: boolean | Variant }>;
|
||||
@ -292,6 +293,10 @@ const flags: IFlags = {
|
||||
process.env.UNLEASH_EXPERIMENTAL_LIFECYCLE_METRICS,
|
||||
false,
|
||||
),
|
||||
createFlagDialogCache: parseEnvVarBoolean(
|
||||
process.env.UNLEASH_EXPERIMENTAL_CREATE_FLAG_DIALOG_CACHE,
|
||||
false,
|
||||
),
|
||||
sideMenuCleanup: parseEnvVarBoolean(
|
||||
process.env.UNLEASH_EXPERIMENTAL_SIDE_MENU_CLEANUP,
|
||||
false,
|
||||
|
Loading…
Reference in New Issue
Block a user