import { createContext, useContext, useEffect, useMemo, useState } from 'react'; import type { PropsWithChildren } from 'react'; interface OperationalContextValue { areaId: string; companyId: string; setAreaId: (areaId: string) => void; setCompanyId: (companyId: string) => void; setContext: (areaId: string, companyId?: string) => void; clearContext: () => void; } const LEGACY_STORAGE_KEY = 'dhv2.operational-context'; const OperationalContext = createContext(null); export function OperationalContextProvider({ children }: PropsWithChildren) { const [areaId, setAreaState] = useState(''); const [companyId, setCompanyState] = useState(''); useEffect(() => { // F6.2 retiró la barra global Área/Operadora. Un contexto persistido sin // controles visibles podía seguir filtrando pantallas de forma inesperada. window.localStorage.removeItem(LEGACY_STORAGE_KEY); }, []); const value = useMemo(() => ({ areaId, companyId, setAreaId: (nextAreaId) => { setAreaState(nextAreaId); setCompanyState(''); }, setCompanyId: setCompanyState, setContext: (nextAreaId, nextCompanyId = '') => { setAreaState(nextAreaId); setCompanyState(nextAreaId ? nextCompanyId : ''); }, clearContext: () => { setAreaState(''); setCompanyState(''); }, }), [areaId, companyId]); return {children}; } export function useOperationalContext() { const value = useContext(OperationalContext); if (!value) throw new Error('useOperationalContext debe usarse dentro de OperationalContextProvider'); return value; }