Skip to content
Snippets Groups Projects
Commit 85af62c9 authored by Loïc Correnson's avatar Loïc Correnson Committed by David Bühler
Browse files

[ivette] sanitized view management

parent e1f9c49c
No related branches found
No related tags found
No related merge requests found
...@@ -259,19 +259,21 @@ interface CustomViewsProps { ...@@ -259,19 +259,21 @@ interface CustomViewsProps {
views: React.PropsWithChildren<View>[] views: React.PropsWithChildren<View>[]
} }
interface CustomViewsSettings { type CustomShapes = { [id: string]: Shape };
current?: string;
shapes?: { [k: string]: Shape };
}
type CustomViews = { [id: string]: View }; type CustomViews = { [id: string]: View };
function CustomViews(props: CustomViewsProps): JSX.Element { function CustomViews(props: CustomViewsProps): JSX.Element {
const { settings, shape, setShape, views: libViews } = props; const { settings, shape, setShape, views: libViews } = props;
const [local, setLocal] = Settings.useWindowSettingsData<CustomViewsSettings>( const [current, setCurrent] = Settings.useWindowSettingsData<string>(
settings, `${settings}.current`,
Json.identity as Json.Decoder<CustomViewsSettings>, Json.jString,
Json.identity as Json.Encoder<CustomViewsSettings>, Json.identity,
''
);
const [shapes, setShapes] = Settings.useWindowSettingsData<CustomShapes>(
`${settings}.shapes`,
Json.identity as Json.Decoder<CustomShapes>,
Json.identity as Json.Encoder<CustomShapes>,
// Clearly abusive conversion, real encoder/decoder are needed // Clearly abusive conversion, real encoder/decoder are needed
{}, {},
); );
...@@ -284,8 +286,7 @@ function CustomViews(props: CustomViewsProps): JSX.Element { ...@@ -284,8 +286,7 @@ function CustomViews(props: CustomViewsProps): JSX.Element {
); );
const [edited, setEdited] = React.useState<string>(); const [edited, setEdited] = React.useState<string>();
const triggerDefault = React.useRef<View>(); const triggerDefault = React.useRef<View>();
const { current, shapes = {} } = local; const theViews: CustomViews = {};
const theViews: { [id: string]: View } = {};
_.forEach(libViews, (view) => { _.forEach(libViews, (view) => {
const { const {
...@@ -321,9 +322,12 @@ function CustomViews(props: CustomViewsProps): JSX.Element { ...@@ -321,9 +322,12 @@ function CustomViews(props: CustomViewsProps): JSX.Element {
const SELECT = (id: string): void => { const SELECT = (id: string): void => {
if (id && current !== id) { if (id && current !== id) {
if (current) shapes[current] = shape; if (current) {
setLocal({ current: id, shapes }); const newShapes = { ...shapes, [current]: shape };
setShapes(newShapes);
}
setShape(shapes[id] || getDefaultShape(theViews[id])); setShape(shapes[id] || getDefaultShape(theViews[id]));
setCurrent(id);
} }
}; };
...@@ -334,8 +338,9 @@ function CustomViews(props: CustomViewsProps): JSX.Element { ...@@ -334,8 +338,9 @@ function CustomViews(props: CustomViewsProps): JSX.Element {
const isCustom = !view.builtin; const isCustom = !view.builtin;
const DEFAULT = (): void => { const DEFAULT = (): void => {
shapes[id] = undefined; const newShapes = { ...shapes };
setLocal({ current: id, shapes }); delete newShapes[id];
setShapes(newShapes);
setShape(getDefaultShape(view)); setShape(getDefaultShape(view));
}; };
...@@ -365,21 +370,34 @@ function CustomViews(props: CustomViewsProps): JSX.Element { ...@@ -365,21 +370,34 @@ function CustomViews(props: CustomViewsProps): JSX.Element {
setCustoms({ ...customs, [newId]: customView }); setCustoms({ ...customs, [newId]: customView });
const newShape = const newShape =
isCurrent ? shape : (shapes[id] ?? getDefaultShape(view)); isCurrent ? shape : (shapes[id] ?? getDefaultShape(view));
const newShapes = { ...shapes, [newId]: newShape } ; const newShapes = { ...shapes, [newId]: newShape };
setLocal({ current: newId, shapes: newShapes }); setShapes(newShapes);
setShape(newShape); setShape(newShape);
setCurrent(newId);
setEdited(newId); setEdited(newId);
}; };
const REMOVE = (): void => { const REMOVE = (): void => {
delete customs[id]; const newCustoms = { ... customs };
delete shapes[id]; const newShapes = { ... shapes };
setCustoms({ ...customs }); delete newCustoms[id];
const newCurrent = current === id ? undefined : current; delete newShapes[id];
setLocal({ current: newCurrent, shapes }); setCustoms(newCustoms);
setShapes(newShapes);
if (isCurrent) {
const newView =
(view.origin && theViews[view.origin]) ||
_.find(theViews, (v: View) => !!v.defaultView) ||
_.find(theViews, (v: View) => v.id !== current) ||
theViews[0];
const newId = newView.id;
const newShape = shapes[newId] || getDefaultShape(newView);
setCurrent(newId);
setShape(newShape);
}
}; };
const onView = (action: string) => const onView = (action: string): string =>
isCurrent ? `${action} View` : `${action} View (${view.label})`; isCurrent ? `${action} View` : `${action} View (${view.label})`;
const hasRename = !edited && isCustom; const hasRename = !edited && isCustom;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment