xref: /aosp_15_r20/external/pigweed/pw_web/log-viewer/src/shared/state.ts (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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  viewTitle?: string;
26  wordWrap?: boolean;
27}
28
29interface StateStorage {
30  loadState(): LogViewerState | undefined;
31  saveState(state: LogViewerState): void;
32}
33
34export class LocalStateStorage implements StateStorage {
35  private readonly STATE_STORAGE_KEY = 'logViewerState';
36
37  loadState(): LogViewerState | undefined {
38    const storedStateString = localStorage.getItem(this.STATE_STORAGE_KEY);
39    if (storedStateString) {
40      return JSON.parse(storedStateString);
41    }
42    return undefined;
43  }
44
45  saveState(state: LogViewerState) {
46    localStorage.setItem(this.STATE_STORAGE_KEY, JSON.stringify(state));
47  }
48}
49
50export class StateService {
51  private store: StateStorage;
52
53  constructor(store: StateStorage) {
54    this.store = store;
55  }
56
57  loadState(): LogViewerState | undefined {
58    return this.store.loadState();
59  }
60
61  saveState(state: LogViewerState) {
62    this.store.saveState(state);
63  }
64}
65