52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
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<OperationalContextValue | null>(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<OperationalContextValue>(() => ({
|
|
areaId,
|
|
companyId,
|
|
setAreaId: (nextAreaId) => {
|
|
setAreaState(nextAreaId);
|
|
setCompanyState('');
|
|
},
|
|
setCompanyId: setCompanyState,
|
|
setContext: (nextAreaId, nextCompanyId = '') => {
|
|
setAreaState(nextAreaId);
|
|
setCompanyState(nextAreaId ? nextCompanyId : '');
|
|
},
|
|
clearContext: () => {
|
|
setAreaState('');
|
|
setCompanyState('');
|
|
},
|
|
}), [areaId, companyId]);
|
|
|
|
return <OperationalContext.Provider value={value}>{children}</OperationalContext.Provider>;
|
|
}
|
|
|
|
export function useOperationalContext() {
|
|
const value = useContext(OperationalContext);
|
|
if (!value) throw new Error('useOperationalContext debe usarse dentro de OperationalContextProvider');
|
|
return value;
|
|
}
|