From 02805a2c1258422940cdab54881baa838da62b4e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Loi=CC=88c=20Correnson?= <loic.correnson@cea.fr>
Date: Fri, 11 Sep 2020 11:20:20 +0200
Subject: [PATCH] [ivette] use local storage for console and custom views

---
 ivette/src/dome/src/renderer/data/json.ts |  7 +++++++
 ivette/src/frama-c/LabViews.tsx           | 11 ++++++-----
 ivette/src/renderer/Controller.tsx        |  4 ++--
 3 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/ivette/src/dome/src/renderer/data/json.ts b/ivette/src/dome/src/renderer/data/json.ts
index 56ef9117e0f..76f304999bf 100644
--- a/ivette/src/dome/src/renderer/data/json.ts
+++ b/ivette/src/dome/src/renderer/data/json.ts
@@ -14,6 +14,8 @@ export type json =
   undefined | null | boolean | number | string |
   json[] | { [key: string]: json };
 
+export type jobject = { [key: string]: json };
+
 /**
    Parse without _revivals_.
    Returned data is guaranteed to have only [[json]] type.
@@ -84,6 +86,11 @@ export const jNull: Safe<undefined> = () => undefined;
 /** Identity. */
 export const jAny: Safe<json> = (js: json) => js;
 
+/** JSON Object. */
+export const jObj: Loose<jobject> = (js: json) => (
+  typeof js === 'object' && !Array.isArray(js) && js !== null ? js : undefined
+);
+
 /** Primitive JSON number or `undefined`. */
 export const jNumber: Loose<number> = (js: json) => (
   typeof js === 'number' && !Number.isNaN(js) ? js : undefined
diff --git a/ivette/src/frama-c/LabViews.tsx b/ivette/src/frama-c/LabViews.tsx
index 70f63b7f694..54b4bc4f0e8 100644
--- a/ivette/src/frama-c/LabViews.tsx
+++ b/ivette/src/frama-c/LabViews.tsx
@@ -352,10 +352,11 @@ const makeGridItem = (customize: any, onClose: any) => (comp: any) => {
 
 function CustomViews({ settings, shape, setShape, views: libViews }: any) {
   const [local, setLocal] = Settings.useWindowSettings(
-    settings, Json.jAny, {},
+    settings, Json.jObj, {},
   ) as any;
-  const [customs, setCustoms] =
-    Dome.useGlobalSettings<any>('frama-c.labview', Json.jAny, {});
+  const [customs, setCustoms] = Settings.useLocalStorage(
+    'frama-c.labview', Json.jObj, {},
+  );
   const [edited, setEdited]: any = React.useState();
   const triggerDefault = React.useRef();
   const { current, shapes = {} } = local;
@@ -374,7 +375,7 @@ function CustomViews({ settings, shape, setShape, views: libViews }: any) {
       { id, order, label, title, builtin: true, defaultView, origin };
   });
 
-  _.forEach(customs, (view) => {
+  _.forEach(customs as any, (view) => {
     const { id, order, label = '(Custom View)', title, origin } = view;
     if (id && !theViews[id]) {
       theViews[id] = { id, order, label, title, builtin: false, origin };
@@ -463,7 +464,7 @@ function CustomViews({ settings, shape, setShape, views: libViews }: any) {
     if (edited === id) {
       const RENAMED = (newLabel: string) => {
         if (newLabel) {
-          const custom = customs[id];
+          const custom = Json.jObj(customs[id]) || {};
           if (custom) custom.label = newLabel;
           setCustoms(customs);
         }
diff --git a/ivette/src/renderer/Controller.tsx b/ivette/src/renderer/Controller.tsx
index 853f8b06ccd..0f51b37cc00 100644
--- a/ivette/src/renderer/Controller.tsx
+++ b/ivette/src/renderer/Controller.tsx
@@ -99,7 +99,7 @@ function insertConfig(hs: string[], cfg: Server.Configuration) {
 let reloadCommand: string | undefined;
 
 Dome.reload.on(() => {
-  const [lastCmd] = Settings.getWindowSettings(
+  const [lastCmd] = Settings.getLocalStorage(
     'Controller.history', Json.jList(Json.jString), [],
   );
   reloadCommand = lastCmd;
@@ -175,7 +175,7 @@ const RenderConsole = () => {
   const [cursor, setCursor] = React.useState(-1);
   const [isEmpty, setEmpty] = React.useState(true);
   const [noTrash, setNoTrash] = React.useState(true);
-  const [history, setHistory] = Settings.useWindowSettings(
+  const [history, setHistory] = Settings.useLocalStorage(
     'Controller.history', Json.jList(Json.jString), [],
   );
 
-- 
GitLab