import { useEffect, useRef, useState, type ReactNode } from 'react'; import type { InstanceContext } from '../app-shell.js'; /** Only these bounded primitives may cross the injected read boundary into the UI. */ export type DeviceNetworkValue = string | number | boolean | null; export interface WlanStatus { readonly enabled?: boolean | null; readonly radioState?: string | null; readonly connectionState?: string | null; readonly activeProfile?: string | null; readonly ssid?: string | null; } export interface WlanProfile { readonly name?: string | null; readonly ssid?: string | null; readonly security?: string | null; readonly enabled?: boolean | null; readonly priority?: number | null; } export interface NetworkAddress { readonly family?: string | null; readonly address?: string | null; readonly prefixLength?: number | null; readonly scope?: string | null; } export interface NetworkInterface { readonly name?: string | null; readonly kind?: string | null; readonly state?: string | null; readonly macAddress?: string | null; readonly mtu?: number | null; readonly addresses: readonly NetworkAddress[]; } export interface DdnsStatus { readonly enabled?: boolean | null; readonly state?: string | null; readonly lastUpdateAt?: string | null; } /** Credentials are intentionally absent. Do not add password, token, secret, or username fields. */ export interface DdnsConfig { readonly provider?: string | null; readonly hostname?: string | null; readonly updateIntervalSeconds?: number | null; } /** This is aggregate metadata only; raw DDNS log messages are intentionally unsupported. */ export interface DdnsLogSummary { readonly totalEntries?: number | null; readonly successfulUpdates?: number | null; readonly failedUpdates?: number | null; readonly lastEventAt?: string | null; } export interface DeviceNetworkSnapshot { readonly observedAt?: string; readonly wlan: { readonly status: WlanStatus; readonly profiles: readonly WlanProfile[]; }; readonly interfaces: readonly NetworkInterface[]; readonly ddns: { readonly status: DdnsStatus; readonly config: DdnsConfig; readonly logSummary: DdnsLogSummary; }; } export interface DeviceNetworkDataSource { /** Implementations are injected by the owner; this module defines no production endpoint. */ load(instanceId: string, signal: AbortSignal): Promise; } export interface DeviceNetworkModuleProps { readonly instance: InstanceContext; readonly dataSource?: DeviceNetworkDataSource; /** Change this owner-provided value to request another read. */ readonly refreshSignal?: unknown; } type ReadState = | { kind: 'idle'; ownerId: string } | { kind: 'loading'; ownerId: string; snapshot?: DeviceNetworkSnapshot } | { kind: 'ready'; ownerId: string; snapshot: DeviceNetworkSnapshot } | { kind: 'error'; ownerId: string; snapshot?: DeviceNetworkSnapshot }; const SAFE_LOAD_ERROR = 'Device Network data could not be loaded.'; function display(value: DeviceNetworkValue | undefined): string { return value == null ? 'Unavailable' : String(value); } function Fields({ values, }: { values: readonly (readonly [label: string, value: DeviceNetworkValue | undefined])[]; }) { return (
{values.map(([label, value]) => (
{label}
{display(value)}
))}
); } function Section({ label, children }: { label: string; children: ReactNode }) { return (

{label}

{children}
); } function SnapshotView({ snapshot }: { snapshot: DeviceNetworkSnapshot }) { const status = snapshot.wlan.status; const ddnsStatus = snapshot.ddns.status; const config = snapshot.ddns.config; const logs = snapshot.ddns.logSummary; return ( <> {snapshot.observedAt ?

Observed {snapshot.observedAt}

: null}
{snapshot.wlan.profiles.length ? ( snapshot.wlan.profiles.map((profile, index) => (
)) ) : (

No WLAN profiles were supplied.

)}
{snapshot.interfaces.length ? ( snapshot.interfaces.map((networkInterface, index) => (

Addresses

{networkInterface.addresses.length ? (
    {networkInterface.addresses.map((address, addressIndex) => (
  • ))}
) : (

No addresses were supplied.

)}
)) ) : (

No network interfaces were supplied.

)}

Device Network actions

R1 configuration actions are unavailable until executable backend support exists.

R2 operational actions are unavailable until executable backend support exists.

); } export function DeviceNetworkModule({ instance, dataSource, refreshSignal, }: DeviceNetworkModuleProps) { const requestOwner = useRef(0); const [retry, setRetry] = useState(0); const [state, setState] = useState({ kind: 'idle', ownerId: instance.id }); useEffect(() => { const request = ++requestOwner.current; const ownerId = instance.id; const controller = new AbortController(); if (instance.authentication !== 'authenticated' || !dataSource) { setState({ kind: 'idle', ownerId }); return () => controller.abort(); } setState((current) => ({ kind: 'loading', ownerId, ...(current.ownerId === ownerId && (current.kind === 'ready' || current.kind === 'error') && current.snapshot ? { snapshot: current.snapshot } : {}), })); void dataSource.load(ownerId, controller.signal).then( (snapshot) => { if (request === requestOwner.current && !controller.signal.aborted) setState({ kind: 'ready', ownerId, snapshot }); }, (_reason: unknown) => { void _reason; if (request === requestOwner.current && !controller.signal.aborted) setState((current) => ({ kind: 'error', ownerId, ...(current.ownerId === ownerId && current.kind === 'loading' && current.snapshot ? { snapshot: current.snapshot } : {}), })); }, ); return () => controller.abort(); }, [dataSource, instance.authentication, instance.id, refreshSignal, retry]); if (instance.authentication !== 'authenticated') { return (
Authentication is required before Device Network data can be read for this instance.
); } if (!dataSource) return (
No safe Device Network read data source is available. This console will not invent or call an uncontracted production endpoint.
); const retainedSnapshot = state.ownerId === instance.id && (state.kind === 'loading' || state.kind === 'error') ? state.snapshot : undefined; if ((state.kind === 'idle' || state.kind === 'loading') && !retainedSnapshot) return (

Loading Device Network…

); if (state.kind === 'error' && !retainedSnapshot) return (

Unable to load Device Network: {SAFE_LOAD_ERROR}

); const currentSnapshot = state.kind === 'ready' && state.ownerId === instance.id ? state.snapshot : retainedSnapshot; if (!currentSnapshot) return null; return (
{state.kind === 'loading' ?

Refreshing Device Network…

: null} {state.kind === 'error' ? (

Refresh failed; showing the last known Device Network data.

) : null} {instance.freshness !== 'fresh' ? (

Device Network data is {instance.freshness}; verify freshness before relying on these values.

) : null}
); }