1// Copyright 2023 The Pigweed Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); you may not 4// use this file except in compliance with the License. You may obtain a copy of 5// the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12// License for the specific language governing permissions and limitations under 13// the License. 14 15import { TableColumn } from './interfaces'; 16import { ViewNode } from './view-node'; 17 18export interface LogViewerState { 19 rootNode: ViewNode; 20} 21 22export interface LogViewState { 23 searchText: string; 24 columnData: TableColumn[]; 25} 26 27interface StateStorage { 28 loadState(): LogViewerState | undefined; 29 saveState(state: LogViewerState): void; 30} 31 32export class LocalStateStorage implements StateStorage { 33 private readonly STATE_STORAGE_KEY = 'logViewerState'; 34 35 loadState(): LogViewerState | undefined { 36 const storedStateString = localStorage.getItem(this.STATE_STORAGE_KEY); 37 if (storedStateString) { 38 return JSON.parse(storedStateString); 39 } 40 return undefined; 41 } 42 43 saveState(state: LogViewerState) { 44 localStorage.setItem(this.STATE_STORAGE_KEY, JSON.stringify(state)); 45 } 46} 47 48export class StateService { 49 private store: StateStorage; 50 51 constructor(store: StateStorage) { 52 this.store = store; 53 } 54 55 loadState(): LogViewerState | undefined { 56 return this.store.loadState(); 57 } 58 59 saveState(state: LogViewerState) { 60 this.store.saveState(state); 61 } 62} 63