Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useAppShortcuts.ts

125 lines3.8 KB

1import * as QuickActions from "expo-quick-actions";
2import { useEffect, useMemo, useRef, useState } from "react";
3import { Platform } from "react-native";
4import { useLinkTo, type NavigationState } from "@react-navigation/native";
5​
6import {
7 loadRecentThreadShortcuts,
8 saveRecentThreadShortcuts,
9 type RecentThreadShortcut,
10} from "../../persistence/imperative";
11import { useThreadShell } from "../../state/entities";
12import {
13 activeThreadRef,
14 buildShortcutActions,
15 shortcutHref,
16 withRecentThreadShortcut,
17} from "./appShortcuts";
18​
19export function useAppShortcuts(state: NavigationState): void {
20 useShortcutNavigation();
21 useRecentThreadShortcutSync(state);
23​
24function useShortcutNavigation(): void {
25 const linkTo = useLinkTo();
26 const handledInitialAction = useRef(false);
27​
28 useEffect(() => {
29 // Handle the launch action before listeners; push over Home so Back returns there.
30 if (!handledInitialAction.current) {
31 handledInitialAction.current = true;
32 const initialHref = QuickActions.initial ? shortcutHref(QuickActions.initial) : null;
33 if (initialHref !== null) {
34 linkTo(initialHref);
35 }
36 }
37​
38 const subscription = QuickActions.addListener((action) => {
39 const href = shortcutHref(action);
40 if (href !== null) {
41 linkTo(href);
42 }
43 });
44 return () => subscription.remove();
45 }, [linkTo]);
47​
48function useRecentThreadShortcutSync(state: NavigationState): void {
49 // iOS subscriptions for Android-only shortcuts would re-render every screen on active-thread updates.
50 const threadRef = useMemo(
51 () => (Platform.OS === "android" ? activeThreadRef(state) : null),
52 [state],
53 );
54 const threadShell = useThreadShell(threadRef);
55 // Wait for persisted recents before recording visits, or cold starts overwrite older entries.
56 const [recents, setRecents] = useState<ReadonlyArray<RecentThreadShortcut> | null>(null);
57 // Do not persist failed-load fallbacks; only a real visit makes the list authoritative.
58 const persistableRef = useRef(false);
59 // Serialize saves so older lists cannot overwrite newer ones.
60 const saveQueueRef = useRef<Promise<unknown>>(Promise.resolve());
61​
62 useEffect(() => {
63 if (Platform.OS !== "android") {
64 return;
65 }
66​
67 let cancelled = false;
68 void loadRecentThreadShortcuts()
69 .then((threads) => {
70 if (!cancelled) {
71 persistableRef.current = true;
72 setRecents(threads);
73 }
74 })
75 .catch((error) => {
76 console.warn("[app-shortcuts] failed to load recent threads", error);
77 if (!cancelled) {
78 setRecents([]);
79 }
80 });
81 return () => {
82 cancelled = true;
83 };
84 }, []);
85​
86 const loaded = recents !== null;
87 const environmentId = threadRef?.environmentId ?? null;
88 const threadId = threadRef?.threadId ?? null;
89 const title = threadShell?.title ?? "";
90 useEffect(() => {
91 if (!loaded || environmentId === null || threadId === null) {
92 return;
93 }
94​
95 setRecents((current) => {
96 if (current === null) {
97 return current;
98 }
99 const next = withRecentThreadShortcut(current, { environmentId, threadId, title });
100 if (next !== current) {
101 persistableRef.current = true;
102 }
103 return next;
104 });
105 }, [loaded, environmentId, threadId, title]);
106​
107 useEffect(() => {
108 if (recents === null) {
109 return;
110 }
111​
112 if (persistableRef.current) {
113 saveQueueRef.current = saveQueueRef.current.then(
114 () =>
115 saveRecentThreadShortcuts(recents).catch((error) => {
116 console.warn("[app-shortcuts] failed to persist recent threads", error);
117 }),
118 () => undefined,
119 );
120 }
121 void QuickActions.setItems(buildShortcutActions(recents)).catch((error) => {
122 console.warn("[app-shortcuts] failed to update launcher shortcuts", error);
123 });
124 }, [recents]);