• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CBIIT / crdc-datahub-ui / 16006182009

01 Jul 2025 05:32PM UTC coverage: 62.703% (-8.6%) from 71.278%
16006182009

Pull #756

github

web-flow
Merge pull request #755 from CBIIT/revert-omb-date

revert: OMB expiration update
Pull Request #756: Sync 3.4.0 with 3.3.0

3560 of 6102 branches covered (58.34%)

Branch coverage included in aggregate %.

4920 of 7422 relevant lines covered (66.29%)

227.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.88
/src/hooks/useLocalStorage.ts
1
import { useEffect, useState } from "react";
2
import { Logger, safeParse } from "../utils";
3

4
/**
5
 * Custom hook to manage state synchronized with localStorage.
6
 * @param key The key in localStorage.
7
 * @param initialValue The initial value to use if none is found in localStorage.
8
 * @returns A stateful value and a function to update it.
9
 */
10
export const useLocalStorage = <T>(key: string, initialValue: T): [T, (value: T) => void] => {
4✔
11
  const [storedValue, setStoredValue] = useState<T>(() => {
12✔
12
    if (typeof window === "undefined") {
10!
13
      return initialValue;
×
14
    }
15
    try {
10✔
16
      const item = window.localStorage.getItem(key);
10✔
17
      return safeParse<T>(item, initialValue);
8✔
18
    } catch (error) {
19
      Logger.error(error?.toString());
2✔
20
      return initialValue;
2✔
21
    }
22
  });
23

24
  /**
25
   * Sets the state and updates localStorage.
26
   * @param value The new value to store.
27
   */
28
  const setValue = (value: T): void => {
12✔
29
    try {
4✔
30
      setStoredValue(value);
4✔
31
      if (typeof window !== "undefined") {
4!
32
        window.localStorage.setItem(key, JSON.stringify(value));
4✔
33
      }
34
    } catch (error) {
35
      Logger.error(error?.toString());
×
36
    }
37
  };
38

39
  useEffect(() => {
12✔
40
    const handleStorageChange = (event: StorageEvent) => {
10✔
41
      if (event.key === key) {
×
42
        setStoredValue(JSON.parse(event.newValue));
×
43
      }
44
    };
45
    window.addEventListener("storage", handleStorageChange);
10✔
46
    return () => {
10✔
47
      window.removeEventListener("storage", handleStorageChange);
10✔
48
    };
49
  }, [key, initialValue]);
50

51
  return [storedValue, setValue];
12✔
52
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc