1 import { type Context, useRef } from 'react';
3 import type { MaybeNull } from '@proton/pass/types';
5 import { createUseContext } from './useContextFactory';
7 /** Creates a proxy to a React `Context` that always reflects the latest
8 * context value, even when accessed outside of the React lifecycle. This
9 * hook addresses scenarios where closures might capture stale context values,
10 * ensuring that you always have access to the most up-to-date context data. */
11 export const useContextProxy = <T extends {}>(context: Context<MaybeNull<T>>) => {
12 const ctx = createUseContext(context)();
13 const ref = useRef(ctx);
16 return new Proxy<T>({} as T, {
17 get: (_, prop) => ref.current[prop as keyof T],