mirror of
https://github.com/meshtastic/web.git
synced 2026-07-30 22:46:30 -04:00
Phase B prep for web migration. Web holds multiple simultaneous device connections keyed by ConnectionId, so per-slice hook migrations need a registry-aware provider. packages/sdk - MeshRegistry: Map<ConnectionId, MeshClient> with signals for list/active/activeId. create()/get()/has()/remove()/setActive(). - First-created client auto-activates. - remove() disconnects the client and rotates active to another entry. - 4 vitest cases covering create, auto-activate, duplicate rejection, and remove. packages/sdk-react - MeshRegistryProvider + MeshRegistryContext. - useMeshRegistry, useOptionalMeshRegistry, useActiveClient, useClientById(id). - useClient now falls back to the registry's active client when no direct <MeshProvider> is present, so existing hooks work unchanged under a registry-backed app. No web-facing changes in this commit; used by follow-up slice migrations.
@meshtastic/sdk-react
React hooks and provider for @meshtastic/sdk.
Install
pnpm add @meshtastic/sdk @meshtastic/sdk-react @meshtastic/transport-web-serial
Quickstart
import { MeshClient } from "@meshtastic/sdk";
import { MeshProvider, useDevice, useChat } from "@meshtastic/sdk-react";
import { TransportWebSerial } from "@meshtastic/transport-web-serial";
import { ChannelNumber } from "@meshtastic/sdk";
const transport = await TransportWebSerial.create({ baudRate: 115200 });
const client = new MeshClient({ transport });
await client.connect();
function App() {
return (
<MeshProvider client={client}>
<Status />
</MeshProvider>
);
}
function Status() {
const { status, myNodeNum } = useDevice();
const { messages, send } = useChat(ChannelNumber.Primary);
return <div>{status} / {myNodeNum} / {messages.length} msgs</div>;
}
All hooks are read-only against a single MeshClient instance supplied through context. Commands are returned as stable functions.
License
GPL-3.0-only.