The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { Action } from "expo-quick-actions"; |
| 2 | import { msg } from "gt-react-native"; |
| 3 | import type { NavigationState } from "@react-navigation/native"; |
| 4 | import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts"; |
| 5 | |
| 6 | import { m } from "../../i18n/messages"; |
| 7 | import type { RecentThreadShortcut } from "../../persistence/imperative"; |
| 8 | |
| 9 | const MAX_RECENT_THREAD_SHORTCUTS = 3; |
| 10 | |
| 11 | const NEW_TASK_SHORTCUT_ID = "new-task"; |
| 12 | const NEW_TASK_SHORTCUT_HREF = "/new"; |
| 13 | |
| 14 | const SHORTCUT_ICON = "shortcut_icon"; |
| 15 | |
| 16 | const THREAD_SHORTCUT_HREF_PATTERN = /^\/threads\/[^/?#]+\/[^/?#]+$/; |
| 17 | |
| 18 | function threadShortcutHref(thread: RecentThreadShortcut): string { |
| 19 | return `/threads/${encodeURIComponent(thread.environmentId)}/${encodeURIComponent(thread.threadId)}`; |
| 20 | } |
| 21 | |
| 22 | function firstRouteParam(value: string | string[] | undefined): string | null { |
| 23 | if (Array.isArray(value)) { |
| 24 | return value[0] ?? null; |
| 25 | } |
| 26 | |
| 27 | return value ?? null; |
| 28 | } |
| 29 | |
| 30 | /** Never throw: malformed deep-link or restored ids can fail schema constructors during root-stack render. */ |
| 31 | export function activeThreadRef(state: NavigationState): ScopedThreadRef | null { |
| 32 | const route = state.routes[state.index]; |
| 33 | if (route?.name !== "Thread") { |
| 34 | return null; |
| 35 | } |
| 36 | |
| 37 | try { |
| 38 | const params = route.params as |
| 39 | | { |
| 40 | readonly environmentId?: string | string[]; |
| 41 | readonly threadId?: string | string[]; |
| 42 | } |
| 43 | | undefined; |
| 44 | const environmentId = firstRouteParam(params?.environmentId)?.trim(); |
| 45 | const threadId = firstRouteParam(params?.threadId)?.trim(); |
| 46 | if (!environmentId || !threadId) { |
| 47 | return null; |
| 48 | } |
| 49 | |
| 50 | return { |
| 51 | environmentId: EnvironmentId.make(environmentId), |
| 52 | threadId: ThreadId.make(threadId), |
| 53 | }; |
| 54 | } catch { |
| 55 | return null; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | function threadShortcutLabel(thread: RecentThreadShortcut): string { |
| 60 | const title = thread.title.trim(); |
| 61 | return title.length > 0 |
| 62 | ? title |
| 63 | : m(msg("Thread", { $context: "fallback name for an untitled thread" })); |
| 64 | } |
| 65 | |
| 66 | /** Launcher shortcuts survive updates; allowlist destinations so malformed or stale paths cannot navigate. */ |
| 67 | export function shortcutHref(action: Action): string | null { |
| 68 | const href = action.params?.href; |
| 69 | if (typeof href !== "string") { |
| 70 | return null; |
| 71 | } |
| 72 | |
| 73 | return href === NEW_TASK_SHORTCUT_HREF || THREAD_SHORTCUT_HREF_PATTERN.test(href) ? href : null; |
| 74 | } |
| 75 | |
| 76 | export function withRecentThreadShortcut( |
| 77 | current: ReadonlyArray<RecentThreadShortcut>, |
| 78 | opened: RecentThreadShortcut, |
| 79 | ): ReadonlyArray<RecentThreadShortcut> { |
| 80 | const existing = current.find( |
| 81 | (thread) => |
| 82 | thread.environmentId === opened.environmentId && thread.threadId === opened.threadId, |
| 83 | ); |
| 84 | const title = opened.title.trim().length > 0 ? opened.title : (existing?.title ?? opened.title); |
| 85 | if (current[0] === existing && existing !== undefined && existing.title === title) { |
| 86 | return current; |
| 87 | } |
| 88 | |
| 89 | return [ |
| 90 | { environmentId: opened.environmentId, threadId: opened.threadId, title }, |
| 91 | ...current.filter((thread) => thread !== existing), |
| 92 | ].slice(0, MAX_RECENT_THREAD_SHORTCUTS); |
| 93 | } |
| 94 | |
| 95 | export function buildShortcutActions(recents: ReadonlyArray<RecentThreadShortcut>): Action[] { |
| 96 | return [ |
| 97 | { |
| 98 | id: NEW_TASK_SHORTCUT_ID, |
| 99 | title: m(msg("New task")), |
| 100 | icon: SHORTCUT_ICON, |
| 101 | params: { href: NEW_TASK_SHORTCUT_HREF }, |
| 102 | }, |
| 103 | ...recents.slice(0, MAX_RECENT_THREAD_SHORTCUTS).map((thread): Action => ({ |
| 104 | // URI-encoded hrefs avoid env/thread id collisions that plain hyphen joins allow. |
| 105 | id: `thread:${threadShortcutHref(thread)}`, |
| 106 | title: threadShortcutLabel(thread), |
| 107 | icon: SHORTCUT_ICON, |
| 108 | params: { href: threadShortcutHref(thread) }, |
| 109 | })), |
| 110 | ]; |
| 111 | } |