File size: 1,080 Bytes
6111b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
export const ACTIVE_ONLY_STORAGE_KEY = "omniroute-api-manager-active-only";

interface StorageReader {
  getItem(key: string): string | null;
}

interface StorageWriter extends StorageReader {
  setItem(key: string, value: string): void;
  removeItem(key: string): void;
}

function getBrowserStorage(): StorageWriter | null {
  try {
    return globalThis.localStorage ?? null;
  } catch {
    return null;
  }
}

export function parseActiveOnlyPreference(value: string | null | undefined): boolean {
  return value === "true";
}

export function readActiveOnlyPreference(

  storage: StorageReader | null = getBrowserStorage()

): boolean {
  if (!storage) return false;
  return parseActiveOnlyPreference(storage.getItem(ACTIVE_ONLY_STORAGE_KEY));
}

export function writeActiveOnlyPreference(

  enabled: boolean,

  storage: StorageWriter | null = getBrowserStorage()

): void {
  if (!storage) return;
  if (enabled) {
    storage.setItem(ACTIVE_ONLY_STORAGE_KEY, "true");
    return;
  }
  storage.removeItem(ACTIVE_ONLY_STORAGE_KEY);
}