Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | // lib/awareness/collective/context/PolicyContext.tsx import React, { createContext, useContext, useEffect, useMemo, useState } from 'react'; import { policyManager } from '@/lib/core/policies'; import type { PolicyContextState, Policy } from '@/lib/core/policies/types'; import { detectDeviceKind } from '@/lib/core/policies/detectors'; import { logPolicyApplied } from '@/lib/telemetry/policy.telemetry'; const Ctx = createContext<PolicyContextState | null>(null); export const PolicyProvider: React.FC<React.PropsWithChildren> = ({ children }) => { const initial = policyManager.getCached() ?? policyManager.compute(); const [policy, setPolicy] = useState<Policy>(initial); const [device, setDevice] = useState(detectDeviceKind()); useEffect(() => { const onResize = () => { const nextDevice = detectDeviceKind(); setDevice(nextDevice); const nextPolicy = policyManager.compute(nextDevice); setPolicy(nextPolicy); logPolicyApplied(nextPolicy); }; window.addEventListener('resize', onResize); // أول تسجيل للحالة الحالية logPolicyApplied(initial); return () => window.removeEventListener('resize', onResize); }, []); const api = useMemo<PolicyContextState>(() => ({ policy, device, setOverrides: (o) => { policyManager.setOverrides(o); const next = policyManager.compute(device); setPolicy(next); logPolicyApplied(next); }, clearOverrides: () => { policyManager.clearOverrides(); const next = policyManager.compute(device); setPolicy(next); logPolicyApplied(next); } }), [policy, device]); return <Ctx.Provider value={api}>{children}</Ctx.Provider>; }; export function usePolicyContext() { const v = useContext(Ctx); if (!v) throw new Error('usePolicyContext must be used within PolicyProvider'); return v; } |