|
| 1 | +import type { HostGrouping } from './flow-data' |
| 2 | + |
| 3 | +export const FLOW_FILTER_PREFS_LS_KEY = 'homer_callflow_prefs' |
| 4 | + |
| 5 | +const HOST_GROUPINGS: HostGrouping[] = ['ungrouped', 'group-by-ip', 'group-by-alias'] |
| 6 | + |
| 7 | +export interface FlowFilters { |
| 8 | + isSimplify: boolean |
| 9 | + isAbsoluteTime: boolean |
| 10 | + isHighContrast: boolean |
| 11 | + hostGrouping: HostGrouping |
| 12 | + ipExcluded: Set<string> |
| 13 | + methodExcluded: Set<string> |
| 14 | + payloadTypeExcluded: Set<string> |
| 15 | + callIdExcluded: Set<string> |
| 16 | +} |
| 17 | + |
| 18 | +export const DEFAULT_FILTERS: FlowFilters = { |
| 19 | + isSimplify: false, |
| 20 | + isAbsoluteTime: false, |
| 21 | + isHighContrast: false, |
| 22 | + hostGrouping: 'ungrouped', |
| 23 | + ipExcluded: new Set(), |
| 24 | + methodExcluded: new Set(), |
| 25 | + payloadTypeExcluded: new Set(), |
| 26 | + callIdExcluded: new Set(), |
| 27 | +} |
| 28 | + |
| 29 | +export interface StoredFlowPrefs { |
| 30 | + hostGrouping?: HostGrouping |
| 31 | + isSimplify?: boolean |
| 32 | + isAbsoluteTime?: boolean |
| 33 | + isHighContrast?: boolean |
| 34 | +} |
| 35 | + |
| 36 | +export function isHostGrouping(value: unknown): value is HostGrouping { |
| 37 | + return typeof value === 'string' && (HOST_GROUPINGS as string[]).includes(value) |
| 38 | +} |
| 39 | + |
| 40 | +export function loadStoredFlowPrefs(): StoredFlowPrefs { |
| 41 | + if (typeof localStorage === 'undefined') return {} |
| 42 | + try { |
| 43 | + const raw = localStorage.getItem(FLOW_FILTER_PREFS_LS_KEY) |
| 44 | + if (!raw) return {} |
| 45 | + const parsed = JSON.parse(raw) as StoredFlowPrefs |
| 46 | + if (!parsed || typeof parsed !== 'object') return {} |
| 47 | + const out: StoredFlowPrefs = {} |
| 48 | + if (isHostGrouping(parsed.hostGrouping)) out.hostGrouping = parsed.hostGrouping |
| 49 | + if (typeof parsed.isSimplify === 'boolean') out.isSimplify = parsed.isSimplify |
| 50 | + if (typeof parsed.isAbsoluteTime === 'boolean') out.isAbsoluteTime = parsed.isAbsoluteTime |
| 51 | + if (typeof parsed.isHighContrast === 'boolean') out.isHighContrast = parsed.isHighContrast |
| 52 | + return out |
| 53 | + } catch { |
| 54 | + return {} |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +export function saveStoredFlowPrefs(filters: FlowFilters): void { |
| 59 | + if (typeof localStorage === 'undefined') return |
| 60 | + const payload: StoredFlowPrefs = { |
| 61 | + hostGrouping: filters.hostGrouping, |
| 62 | + isSimplify: filters.isSimplify, |
| 63 | + isAbsoluteTime: filters.isAbsoluteTime, |
| 64 | + isHighContrast: filters.isHighContrast, |
| 65 | + } |
| 66 | + try { |
| 67 | + localStorage.setItem(FLOW_FILTER_PREFS_LS_KEY, JSON.stringify(payload)) |
| 68 | + } catch { |
| 69 | + /* ignore quota / private mode */ |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/** UI prefs from localStorage + fresh per-call exclusion sets. */ |
| 74 | +export function initialFlowFilters(): FlowFilters { |
| 75 | + const stored = loadStoredFlowPrefs() |
| 76 | + return { |
| 77 | + ...DEFAULT_FILTERS, |
| 78 | + ...stored, |
| 79 | + ipExcluded: new Set(), |
| 80 | + methodExcluded: new Set(), |
| 81 | + payloadTypeExcluded: new Set(), |
| 82 | + callIdExcluded: new Set(), |
| 83 | + } |
| 84 | +} |
0 commit comments