| 1 | import type { ScopedThreadRef } from "@t3tools/contracts"; |
| 2 | |
| 3 | type SettleAndAdvance = (threadRef: ScopedThreadRef) => void; |
| 4 | |
| 5 | // Only the mounted thread sidebar knows the card order that picks the next |
| 6 | // thread, so it registers its settle action here for other entry points. |
| 7 | let registeredSettleAndAdvance: SettleAndAdvance | null = null; |
| 8 | |
| 9 | /** |
| 10 | * Called by the thread sidebar while it is mounted. Returns the unregister |
| 11 | * function, which leaves a newer registration in place. |
| 12 | */ |
| 13 | export function registerSettleAndAdvance(handler: SettleAndAdvance): () => void { |
| 14 | registeredSettleAndAdvance = handler; |
| 15 | return () => { |
| 16 | if (registeredSettleAndAdvance === handler) registeredSettleAndAdvance = null; |
| 17 | }; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Settles the open thread the way the sidebar Settle button does: settle, |
| 22 | * then move to the next thread. Used by the `thread.settle` shortcut. |
| 23 | * Returns false when no sidebar is mounted, so the caller settles in place. |
| 24 | */ |
| 25 | export function settleAndAdvance(threadRef: ScopedThreadRef): boolean { |
| 26 | if (!registeredSettleAndAdvance) return false; |
| 27 | registeredSettleAndAdvance(threadRef); |
| 28 | return true; |
| 29 | } |