Skip to content

Commit

Permalink
Persist repl history in browser local storage (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dewb authored Aug 2, 2023
1 parent 905413d commit 5587f24
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
22 changes: 22 additions & 0 deletions web/src/model/repl-history-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const localStorageKeyPrefix = "maiden-repl-history-";
const maxLocalStorageHistoryItems = 100;

export const loadHistoryFromLocalStorage = (component) => {
try {
const history = JSON.parse(window.localStorage.getItem(`${localStorageKeyPrefix}${component}`));
return Array.isArray(history) ? history : [];
} catch (e) {
return [];
}
};

export const saveHistoryToLocalStorage = (component, history) => {
try {
window.localStorage.setItem(
`${localStorageKeyPrefix}${component}`,
JSON.stringify(history.slice(0, maxLocalStorageHistoryItems))
);
} catch(e) {
}
};

5 changes: 4 additions & 1 deletion web/src/model/repl-reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
REPL_CLEAR,
REPL_UNIT_MAP_SUCCESS,
} from './repl-actions';
import { loadHistoryFromLocalStorage, saveHistoryToLocalStorage } from './repl-history-storage';

/*
-- shape of the repl connection and scrollback buffer state
Expand Down Expand Up @@ -42,6 +43,8 @@ export const outputAppend = (buffer, limit, line) => {
const handleReplEcho = (action, state, input) => {
// add command to history list
const history = state.history.get(action.component).unshift(input);
saveHistoryToLocalStorage(action.component, history);

// echo command to output buffer
let buffer = state.buffers.get(action.component);
buffer = outputAppend(buffer, state.scrollbackLimit, input);
Expand Down Expand Up @@ -139,7 +142,7 @@ const repl = (state = initialReplState, action) => {
...state,
connections: state.connections.set(action.component, conn.merge(changes)),
buffers: state.buffers.set(action.component, new List()),
history: state.history.set(action.component, new List()),
history: state.history.set(action.component, new List(loadHistoryFromLocalStorage(action.component))),
};

case REPL_RECEIVE:
Expand Down
2 changes: 1 addition & 1 deletion web/src/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ReplOutput extends Component {
class ReplInput extends Component {
constructor(props) {
super(props);
this.histroyIdx = 0;
this.historyIdx = 0;
}

onKeyDown = event => {
Expand Down

0 comments on commit 5587f24

Please sign in to comment.