|
| 1 | +import { createContext, ReactNode, useContext, useState } from "react"; |
| 2 | + |
| 3 | +interface PipelineGraphViewPreferences { |
| 4 | + showNames: boolean; |
| 5 | + setShowNames: (val: boolean) => void; |
| 6 | + showDurations: boolean; |
| 7 | + setShowDurations: (val: boolean) => void; |
| 8 | +} |
| 9 | + |
| 10 | +const defaultPreferences = { |
| 11 | + showNames: false, |
| 12 | + showDurations: false, |
| 13 | +}; |
| 14 | + |
| 15 | +const UserPreferencesContext = createContext< |
| 16 | + PipelineGraphViewPreferences | undefined |
| 17 | +>(undefined); |
| 18 | + |
| 19 | +const makeKey = (setting: string) => `pgv-graph-view.${setting}`; |
| 20 | + |
| 21 | +const parsePreferenceValue = <T,>( |
| 22 | + value: string | undefined, |
| 23 | + fallback: T, |
| 24 | +): T => { |
| 25 | + if (value === undefined) { |
| 26 | + return fallback; |
| 27 | + } |
| 28 | + if (typeof fallback === "boolean") { |
| 29 | + return (value === "true") as typeof fallback; |
| 30 | + } |
| 31 | + return value as unknown as T; |
| 32 | +}; |
| 33 | + |
| 34 | +const loadFromLocalStorage = <T,>(key: string, fallback: T): T => { |
| 35 | + try { |
| 36 | + const value = window.localStorage.getItem(key) ?? undefined; |
| 37 | + return parsePreferenceValue(value, fallback); |
| 38 | + } catch (e) { |
| 39 | + console.error(`Error loading localStorage key "${key}"`, e); |
| 40 | + } |
| 41 | + return fallback; |
| 42 | +}; |
| 43 | + |
| 44 | +const loadFromDOM = <T,>(key: string, fallback: T): T => { |
| 45 | + const preferencesModule = document.querySelector( |
| 46 | + "[data-module='user-preferences']", |
| 47 | + ); |
| 48 | + const value = |
| 49 | + preferencesModule && "dataset" in preferencesModule |
| 50 | + ? (preferencesModule as HTMLElement).dataset[key] |
| 51 | + : undefined; |
| 52 | + return parsePreferenceValue(value, fallback); |
| 53 | +}; |
| 54 | + |
| 55 | +export const UserPreferencesProvider = ({ |
| 56 | + children, |
| 57 | +}: { |
| 58 | + children: ReactNode; |
| 59 | +}) => { |
| 60 | + const stageNamesKey = makeKey("stageNames"); |
| 61 | + const stageDurationsKey = makeKey("stageDurations"); |
| 62 | + |
| 63 | + const [showNames, setShowNames] = useState<boolean>( |
| 64 | + loadFromLocalStorage( |
| 65 | + stageNamesKey, |
| 66 | + loadFromDOM("preferenceShowStageNames", defaultPreferences.showNames), |
| 67 | + ), |
| 68 | + ); |
| 69 | + const [showDurations, setShowDurations] = useState<boolean>( |
| 70 | + loadFromLocalStorage( |
| 71 | + stageDurationsKey, |
| 72 | + loadFromDOM( |
| 73 | + "preferenceShowStageDurations", |
| 74 | + defaultPreferences.showDurations, |
| 75 | + ), |
| 76 | + ), |
| 77 | + ); |
| 78 | + |
| 79 | + const persistShowNames = (val: boolean) => { |
| 80 | + window.localStorage.setItem(stageNamesKey, String(val)); |
| 81 | + setShowNames(val); |
| 82 | + }; |
| 83 | + |
| 84 | + const persistShowDurations = (val: boolean) => { |
| 85 | + window.localStorage.setItem(stageDurationsKey, String(val)); |
| 86 | + setShowDurations(val); |
| 87 | + }; |
| 88 | + |
| 89 | + return ( |
| 90 | + <UserPreferencesContext.Provider |
| 91 | + value={{ |
| 92 | + showNames, |
| 93 | + setShowNames: persistShowNames, |
| 94 | + showDurations, |
| 95 | + setShowDurations: persistShowDurations, |
| 96 | + }} |
| 97 | + > |
| 98 | + {children} |
| 99 | + </UserPreferencesContext.Provider> |
| 100 | + ); |
| 101 | +}; |
| 102 | + |
| 103 | +export const useUserPreferences = (): PipelineGraphViewPreferences => { |
| 104 | + const context = useContext(UserPreferencesContext); |
| 105 | + if (!context) { |
| 106 | + throw new Error( |
| 107 | + "useMonitorPreferences must be used within a UserPreferencesProvider", |
| 108 | + ); |
| 109 | + } |
| 110 | + return context; |
| 111 | +}; |
0 commit comments