Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persistent terminal history #378

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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