import { useEffect, useState } from 'react'; import type { ReactNode } from 'react'; import { tradingKillSwitchClient } from '../lib/runtime'; interface AccessibilityState { status: 'loading' | 'available' | 'maintenance' | 'product_disabled'; message?: string; } const initialState: AccessibilityState = { status: 'loading', }; export function ProductAccessibilityGate({ children }: { children: ReactNode }) { const [state, setState] = useState(initialState); useEffect(() => { let active = true; async function loadAvailability() { try { const result = await tradingKillSwitchClient.check(); if (!active) { return; } if (result.disabled) { setState({ status: 'product_disabled', message: result.message ?? 'Trading access is temporarily disabled.', }); return; } setState({ status: 'available' }); } catch (error) { // Fail open — kill switch service being down should not block users. // The kill switch is a safety net, not an auth gate; degraded availability // is preferable to a hard block when the check service is unreachable. console.warn('[ProductAccessibilityGate] Kill switch check failed, defaulting to available.', error); if (active) { setState({ status: 'available' }); } } } void loadAvailability(); return () => { active = false; }; }, []); if (state.status === 'loading') { return ; } if (state.status !== 'available') { return ( ); } return <>{children}; } function CenteredMessage({ title, body }: { title: string; body?: string }) { return (

{title}

{body ? (

{body}

) : null}
); }