From bd6b8d306b693e894ca309a110e7582e6b5f2980 Mon Sep 17 00:00:00 2001 From: David Buezas Date: Mon, 4 Dec 2023 14:28:46 +0100 Subject: [PATCH] Add persistent history via localStorage --- src/components/Helpers/index.js | 2 ++ src/components/Helpers/storedState.js | 33 +++++++++++++++++++++++++++ src/contexts/DatasContext.js | 4 ++-- 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 src/components/Helpers/storedState.js diff --git a/src/components/Helpers/index.js b/src/components/Helpers/index.js index 46bbb84e..8301cf22 100644 --- a/src/components/Helpers/index.js +++ b/src/components/Helpers/index.js @@ -50,6 +50,7 @@ import { } from './arrays' import { dispatchToExtensions } from './html' import { sortedFilesList, filterResultFiles } from './filters' +import { useStoredState } from './storedState' export { beautifyJSONString, @@ -83,4 +84,5 @@ export { removeObjectItem, isFloat, BitsArray, + useStoredState, } diff --git a/src/components/Helpers/storedState.js b/src/components/Helpers/storedState.js new file mode 100644 index 00000000..cfa52780 --- /dev/null +++ b/src/components/Helpers/storedState.js @@ -0,0 +1,33 @@ +/* + time.js - ESP3D WebUI helpers file + + Copyright (c) 2021 Luc LEBOSSE. All rights reserved. + + This code is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + This code is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + You should have received a copy of the GNU Lesser General Public + License along with This code; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +import { h } from 'preact' +import { useState, useMemo } from 'preact/hooks' + +export const useStoredState = (key, defaultValue) => { + const stored = useMemo(() => { + try { + return JSON.parse(localStorage.getItem(key)) + } catch (e) {} + }) + const [state, setInternalState] = useState(stored ?? defaultValue) + const setState = (newState) => { + setInternalState(newState) + localStorage.setItem(key, JSON.stringify(newState)) + } + return [state, setState] +} \ No newline at end of file diff --git a/src/contexts/DatasContext.js b/src/contexts/DatasContext.js index 0978e29c..a13f0511 100644 --- a/src/contexts/DatasContext.js +++ b/src/contexts/DatasContext.js @@ -18,7 +18,7 @@ */ import { h, createContext } from 'preact' import { useRef, useContext, useState } from 'preact/hooks' -import { limitArr } from '../components/Helpers' +import { limitArr, useStoredState } from '../components/Helpers' /* * Local const @@ -34,7 +34,7 @@ const DatasContextProvider = ({ children }) => { const terminalBuffer = useRef([]) const terminalBufferQuiet = useRef([]) const [terminalContent, setTerminalContent] = useState([]) - const [terminalInputHistory, setTerminalInputHistory] = useState([]) + const [terminalInputHistory, setTerminalInputHistory] = useStoredState("terminalInputHistory", []) const terminalInput = useRef() const clearTerminal = () => {