@@ -12,6 +12,7 @@ import { |
12 | 12 | type SharePayload, |
13 | 13 | } from "expo-sharing"; |
14 | 14 | import { msg } from "gt-react-native"; |
| 15 | +import { Atom } from "effect/reactivity"; |
15 | 16 | import { useSyncExternalStore } from "react"; |
16 | 17 | import { Platform } from "react-native"; |
17 | 18 | import { CommonActions } from "@react-navigation/native"; |
@@ -32,7 +33,7 @@ import { rootNavigationRef } from "../../lib/rootNavigation"; |
32 | 33 | import { selectWorkspaceDetail } from "../layout/workspace/workspace-detail-selection"; |
33 | 34 | import { isDraftThreadRoute } from "../threads/thread-route-state"; |
34 | 35 | import { makeIncomingShareImports } from "../threads/incomingShareImport"; |
35 | | -import { liveNewTaskFlowKeys } from "../threads/new-task-flow-state"; |
| 36 | +import { liveNewTaskFlowKeys, newTaskFlowKey } from "../threads/new-task-flow-state"; |
36 | 37 | import { appAtomRegistry } from "../../state/atom-registry"; |
37 | 38 | import { serverEnvironment } from "../../state/server"; |
38 | 39 | import { |
@@ -222,6 +223,42 @@ let snapshot: IncomingShareSnapshot = { drafts: [], isLoading: true }; |
222 | 223 | const snapshotListeners = new Set<() => void>(); |
223 | 224 | let presentation = EMPTY_INCOMING_SHARE_PRESENTATION_STATE; |
224 | 225 |
|
| 226 | +// A share that arrives while a new-task draft is on screen goes into that draft, by its flow key. |
| 227 | +const handedSharesAtom = Atom.make<Readonly<Record<string, string>>>({}).pipe( |
| 228 | + Atom.keepAlive, |
| 229 | + Atom.withLabel("mobile:incoming-share-handed"), |
| 230 | +); |
| 231 | + |
| 232 | +/** The share handed to the open draft of `flowKey`, if any. */ |
| 233 | +export const handedShareIdAtom = Atom.family((flowKey: string) => |
| 234 | + Atom.make((get) => get(handedSharesAtom)[flowKey] ?? null).pipe( |
| 235 | + Atom.withLabel("mobile:incoming-share-handed:flow"), |
| 236 | + ), |
| 237 | +); |
| 238 | + |
| 239 | +export const noHandedShareIdAtom = Atom.make<string | null>(null).pipe( |
| 240 | + Atom.withLabel("mobile:incoming-share-handed:none"), |
| 241 | +); |
| 242 | + |
| 243 | +function isPendingShare(shareId: string | undefined): boolean { |
| 244 | + return shareId !== undefined && snapshot.drafts.some((draft) => draft.id === shareId); |
| 245 | +} |
| 246 | + |
| 247 | +/** Hands the oldest pending share to the draft on screen once that draft's own imports finished. */ |
| 248 | +function handPendingShareToOpenDraft(route: { readonly key: string; readonly params?: object }) { |
| 249 | + const pendingShareId = snapshot.drafts[0]?.id; |
| 250 | + const flowKey = newTaskFlowKey(route); |
| 251 | + if (pendingShareId === undefined || flowKey === undefined) return; |
| 252 | + const draft = (route.params as { draft?: { incomingShareId?: string | string[] } }).draft; |
| 253 | + const routeShareId = Array.isArray(draft?.incomingShareId) |
| 254 | + ? draft.incomingShareId[0] |
| 255 | + : draft?.incomingShareId; |
| 256 | + const handed = appAtomRegistry.get(handedSharesAtom); |
| 257 | + if (isPendingShare(routeShareId) || isPendingShare(handed[flowKey])) return; |
| 258 | + if (Object.values(handed).includes(pendingShareId)) return; |
| 259 | + appAtomRegistry.set(handedSharesAtom, { ...handed, [flowKey]: pendingShareId }); |
| 260 | +} |
| 261 | + |
225 | 262 | function publish(drafts: ReadonlyArray<IncomingShareDraft>) { |
226 | 263 | snapshot = { drafts, isLoading: false }; |
227 | 264 | for (const listener of snapshotListeners) listener(); |
@@ -274,6 +311,8 @@ export function presentPendingIncomingShare(): void { |
274 | 311 | if (!rootNavigationRef.isReady()) return; |
275 | 312 | const state = rootNavigationRef.getRootState(); |
276 | 313 | if (!state) return; |
| 314 | + const route = state.routes[state.index]; |
| 315 | + if (route && isDraftThreadRoute(route)) handPendingShareToOpenDraft(route); |
277 | 316 | const transition = transitionIncomingSharePresentation(presentation, { |
278 | 317 | isShareSheetPresented: isDraftThreadRoute(state.routes[state.index]), |
279 | 318 | pendingShareId: snapshot.drafts[0]?.id ?? null, |
@@ -373,7 +412,15 @@ export const incomingShareImports = makeIncomingShareImports({ |
373 | 412 |
|
374 | 413 | /** Abandons the share imports of draft routes that left the stack. */ |
375 | 414 | export function pruneIncomingShareImports(state: Parameters<typeof liveNewTaskFlowKeys>[0]) { |
376 | | - void incomingShareImports.prune(liveNewTaskFlowKeys(state)); |
| 415 | + const live = liveNewTaskFlowKeys(state); |
| 416 | + void incomingShareImports.prune(live); |
| 417 | + const handed = appAtomRegistry.get(handedSharesAtom); |
| 418 | + if (Object.keys(handed).some((flowKey) => !live.has(flowKey))) { |
| 419 | + appAtomRegistry.set( |
| 420 | + handedSharesAtom, |
| 421 | + Object.fromEntries(Object.entries(handed).filter(([flowKey]) => live.has(flowKey))), |
| 422 | + ); |
| 423 | + } |
377 | 424 | } |
378 | 425 |
|
379 | 426 | export function useIncomingShare() { |
|