Files
meshtastic-web/packages/sdk-react/src/hooks/useDirectChat.ts
T
AustinandGitHub d2cb51d489 Execute oxfmt (#1166)
Ran
pnpm oxfmt .

This should get the web repo aligned so that we can better enforce oxfmt going forward.
2026-06-16 20:30:08 -04:00

41 lines
1.3 KiB
TypeScript

import type {
ConversationKey,
Message,
SendTextError,
SendTextInput,
} from "@meshtastic/sdk";
import type { ResultType } from "better-result";
import { useCallback, useMemo } from "react";
import { useClient } from "../adapters/useClient.ts";
import { useSignal } from "../adapters/useSignal.ts";
export interface UseDirectChatResult {
messages: Message[];
send(input: SendTextInput): Promise<ResultType<number, SendTextError>>;
loadOlder(before: Date, limit?: number): Promise<Message[]>;
}
/**
* Direct-message conversation with a single peer node. The bucket is keyed
* by the peer (so messages from-me-to-peer and from-peer-to-me share one
* bucket). Lazy-hydrates from the configured MessageRepository on first
* subscribe.
*/
export function useDirectChat(peer: number): UseDirectChatResult {
const client = useClient();
const sig = useMemo(() => client.chat.direct(peer), [client, peer]);
const messages = useSignal(sig);
const send = useCallback(
(input: SendTextInput) => client.chat.send(input),
[client],
);
const loadOlder = useCallback(
(before: Date, limit?: number) => {
const conv: ConversationKey = { kind: "direct", peer };
return client.chat.loadOlder(conv, before, limit);
},
[client, peer],
);
return { messages, send, loadOlder };
}