Open History to see the last commit
1import type { Action } from "expo-quick-actions";
2import type { NavigationState } from "@react-navigation/native";
3import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
4
5import type { RecentThreadShortcut } from "../../persistence/imperative";
6
7export const MAX_RECENT_THREAD_SHORTCUTS = 3;
8
9export const NEW_TASK_SHORTCUT_ID = "new-task";
10const NEW_TASK_SHORTCUT_HREF = "/new";
11
12const SHORTCUT_ICON = "shortcut_icon";
13
14const THREAD_SHORTCUT_HREF_PATTERN = /^\/threads\/[^/?#]+\/[^/?#]+$/;
15
16function threadShortcutHref(thread: RecentThreadShortcut): string {
17 return `/threads/${encodeURIComponent(thread.environmentId)}/${encodeURIComponent(thread.threadId)}`;
18}
19
20function firstRouteParam(value: string | string[] | undefined): string | null {
21 if (Array.isArray(value)) {
22 return value[0] ?? null;
23 }
24
25 return value ?? null;
26}
27
28/**
29 * Never throws: route params can arrive malformed from external deep links or restored state, the
30 * branded id constructors throw on values failing their schema, and this runs during render of
31 * the root stack layout — an uncaught throw here would take down the whole navigation tree.
32 */
33export function activeThreadRef(state: NavigationState): ScopedThreadRef | null {
34 const route = state.routes[state.index];
35 if (route?.name !== "Thread") {
36 return null;
37 }
38
39 try {
40 const params = route.params as
41 | {
42 readonly environmentId?: string | string[];
43 readonly threadId?: string | string[];
44 }
45 | undefined;
46 const environmentId = firstRouteParam(params?.environmentId)?.trim();
47 const threadId = firstRouteParam(params?.threadId)?.trim();
48 if (!environmentId || !threadId) {
49 return null;
50 }
51
52 return {
53 environmentId: EnvironmentId.make(environmentId),
54 threadId: ThreadId.make(threadId),
55 };
56 } catch {
57 return null;
58 }
59}
60
61function threadShortcutLabel(thread: RecentThreadShortcut): string {
62 const title = thread.title.trim();
63 return title.length > 0 ? title : "Thread";
64}
65
66/**
67 * In-app navigation path carried by a shortcut, or null for anything
68 * malformed — shortcuts persist in the launcher across app updates, so this
69 * allowlists the exact destinations shortcuts can produce rather than
70 * trusting any path-shaped string (`//host`, arbitrary routes, stale
71 * persisted junk all navigate nowhere).
72 */
73export function shortcutHref(action: Action): string | null {
74 const href = action.params?.href;
75 if (typeof href !== "string") {
76 return null;
77 }
78
79 return href === NEW_TASK_SHORTCUT_HREF || THREAD_SHORTCUT_HREF_PATTERN.test(href) ? href : null;
80}
81
82export function withRecentThreadShortcut(
83 current: ReadonlyArray<RecentThreadShortcut>,
84 opened: RecentThreadShortcut,
85): ReadonlyArray<RecentThreadShortcut> {
86 const existing = current.find(
87 (thread) =>
88 thread.environmentId === opened.environmentId && thread.threadId === opened.threadId,
89 );
90 const title = opened.title.trim().length > 0 ? opened.title : (existing?.title ?? opened.title);
91 if (current[0] === existing && existing !== undefined && existing.title === title) {
92 return current;
93 }
94
95 return [
96 { environmentId: opened.environmentId, threadId: opened.threadId, title },
97 ...current.filter((thread) => thread !== existing),
98 ].slice(0, MAX_RECENT_THREAD_SHORTCUTS);
99}
100
101export function buildShortcutActions(recents: ReadonlyArray<RecentThreadShortcut>): Action[] {
102 return [
103 {
104 id: NEW_TASK_SHORTCUT_ID,
105 title: "New task",
106 icon: SHORTCUT_ICON,
107 params: { href: NEW_TASK_SHORTCUT_HREF },
108 },
109 ...recents.slice(0, MAX_RECENT_THREAD_SHORTCUTS).map((thread): Action => ({
110 // The encoded href doubles as the launcher id: URI-encoding makes the
111 // env/thread join unambiguous (a plain `-` join lets different pairs
112 // collide and overwrite each other's launcher slots).
113 id: `thread:${threadShortcutHref(thread)}`,
114 title: threadShortcutLabel(thread),
115 icon: SHORTCUT_ICON,
116 params: { href: threadShortcutHref(thread) },
117 })),
118 ];
119}