Skip to content

Commit

Permalink
Add persistent history via localStorage (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbuezas authored Dec 5, 2023
1 parent 332edf2 commit bbc2e65
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/components/Helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
} from './arrays'
import { dispatchToExtensions } from './html'
import { sortedFilesList, filterResultFiles } from './filters'
import { useStoredState } from './storedState'

export {
beautifyJSONString,
Expand Down Expand Up @@ -83,4 +84,5 @@ export {
removeObjectItem,
isFloat,
BitsArray,
useStoredState,
}
33 changes: 33 additions & 0 deletions src/components/Helpers/storedState.js
Original file line number Diff line number Diff line change
@@ -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]
}
4 changes: 2 additions & 2 deletions src/contexts/DatasContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = () => {
Expand Down

0 comments on commit bbc2e65

Please sign in to comment.