import { useEffect, useState } from 'react' import { getD2Account, getD2Csrf, onD2Change, type D2Account } from '../api/identity' interface D2SessionState { account: D2Account | null csrf: string | null authed: boolean isOperator: boolean } function snapshot(): D2SessionState { const account = getD2Account() return { account, csrf: getD2Csrf(), authed: account !== null, isOperator: account?.role === 'operator', } } /** Subscribes to the in-memory D2 account/csrf store (api/identity.ts). */ export function useD2Session(): D2SessionState { const [state, setState] = useState(snapshot) useEffect(() => onD2Change(() => setState(snapshot())), []) return state }