File size: 719 Bytes
dc47d57 | 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 | 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<D2SessionState>(snapshot)
useEffect(() => onD2Change(() => setState(snapshot())), [])
return state
}
|