| 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', |
| } |
| } |
|
|
| |
| export function useD2Session(): D2SessionState { |
| const [state, setState] = useState<D2SessionState>(snapshot) |
| useEffect(() => onD2Change(() => setState(snapshot())), []) |
| return state |
| } |
|
|