The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { MessageId } from "@t3tools/contracts"; |
| 2 | |
| 3 | // A palette message result opens its thread at that message. The thread |
| 4 | // screen reads the request while it shows that thread, and finishes it once |
| 5 | // the feed is there or the message cannot be found. A request whose thread |
| 6 | // never showed lapses, so a later plain open does not jump, as on web. |
| 7 | const PENDING_JUMP_MS = 30_000; |
| 8 | let pending: { |
| 9 | readonly threadKey: string; |
| 10 | readonly messageId: MessageId; |
| 11 | readonly at: number; |
| 12 | } | null = null; |
| 13 | const listeners = new Set<() => void>(); |
| 14 | |
| 15 | function notify() { |
| 16 | for (const listener of listeners) listener(); |
| 17 | } |
| 18 | |
| 19 | export function requestThreadMessageJump(threadKey: string, messageId: MessageId): void { |
| 20 | pending = { threadKey, messageId, at: Date.now() }; |
| 21 | notify(); |
| 22 | } |
| 23 | |
| 24 | /** The message this thread should open at, if a palette pick asked for one. */ |
| 25 | export function pendingThreadMessageJump(threadKey: string): MessageId | null { |
| 26 | return pending?.threadKey === threadKey && Date.now() - pending.at < PENDING_JUMP_MS |
| 27 | ? pending.messageId |
| 28 | : null; |
| 29 | } |
| 30 | |
| 31 | export function finishThreadMessageJump(threadKey: string): void { |
| 32 | if (pending?.threadKey !== threadKey) return; |
| 33 | pending = null; |
| 34 | notify(); |
| 35 | } |
| 36 | |
| 37 | export function subscribeThreadMessageJump(listener: () => void): () => void { |
| 38 | listeners.add(listener); |
| 39 | return () => { |
| 40 | listeners.delete(listener); |
| 41 | }; |
| 42 | } |