import type { CredentialSnapshot } from '@astrale-os/shell' import type { ReactElement, ReactNode } from 'react' import { Component, Fragment } from 'react' import type { GraphResource } from '../graph/resource.js' import { useConnection } from '../session/connection/connection.hook.js' export function ViewBoundary(props: { readonly resource: GraphResource readonly children: (value: T) => ReactNode readonly loading?: ReactNode readonly empty?: ReactNode readonly error?: (error: unknown) => ReactNode }): ReactElement { const { resource } = props if (resource.state === 'failed') { if (props.error !== undefined) return <>{props.error(resource.error)} throw resource.error } if (resource.data === undefined) return <>{props.loading ?? null} if (resource.data === null) return <>{props.empty ?? null} return <>{props.children(resource.data)} } export const GraphBoundary = ViewBoundary export interface ViewErrorBoundaryProps { /** A render function receives the retry generation and can recreate rejected lazy resources. */ readonly children: ReactNode | ((generation: number) => ReactNode) readonly fallback?: (error: unknown, retry: () => void) => ReactNode readonly onError?: (error: unknown) => void } /** Catch Shell View render/lazy failures and remount the failed subtree on explicit retry. */ export class ViewErrorBoundary extends Component< ViewErrorBoundaryProps, { readonly error: unknown; readonly generation: number } > { override state = { error: undefined as unknown, generation: 0 } static getDerivedStateFromError(error: unknown) { return { error } } override componentDidCatch(error: unknown): void { this.props.onError?.(error) } private readonly retry = (): void => { this.setState((state) => ({ error: undefined, generation: state.generation + 1 })) } override render(): ReactNode { if (this.state.error !== undefined) { return ( this.props.fallback?.(this.state.error, this.retry) ?? defaultViewError(this.state.error, this.retry) ) } const content = typeof this.props.children === 'function' ? this.props.children(this.state.generation) : this.props.children return {content} } } export function defaultViewLoading(): ReactElement { return (
Loading view…
) } export function defaultViewError(_error: unknown, retry: () => void): ReactElement { return (

View could not load.

) } export function ConnectionBoundary(props: { readonly children: ReactNode readonly fallback?: (status: ReturnType) => ReactNode }): ReactElement { const connection = useConnection() return connection.status === 'ready' ? ( <>{props.children} ) : ( <>{props.fallback?.(connection) ?? null} ) } export function CredentialBoundary(props: { readonly credential: CredentialSnapshot readonly children: ReactNode readonly fallback?: (credential: CredentialSnapshot) => ReactNode }): ReactElement { return props.credential.state === 'valid' || props.credential.state === 'refreshing' || props.credential.state === 'degraded' ? ( <>{props.children} ) : ( <>{props.fallback?.(props.credential) ?? null} ) }