refactor(mobile): media previews share file leases and close on blur in one place #25527
Mergedjuliusmarmingemerged 4 commits into
main fromrefs/pull/25527/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/mobile/src/components/FilePreviewModal.tsx line 102Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -124,6 +77,16 @@124- const value = resolved[filePreviewKey(item)];125- return value ? [value] : [];126- }),127- [items, resolved],77+ const selectedKey = filePreviewKey(props.source);78+ const [gallery, setGallery] = useState<readonly ResolvedFilePreviewSource[] | null>(() =>79+ items.every((item) => "uri" in item)80+ ? items.map((item) => ({ ...item, name: filePreviewName(item) }))81+ : null,12882 );129- const ready = selected && gallery.length === items.length;83+ const onResolutionError = useEffectEvent((error: PreviewResolutionError) => {84+ if (props.onOpenError) props.onOpenError(error.cause);85+ else Dialog.alert("Could not open preview", error.fallbackMessage);86+ props.onRequestClose();87+ });88+ // Resolves once per presentation and holds local file leases until it closes.89+ useEffect(() => {90+ if (items.every((item) => "uri" in item)) return;91+ const controller = new AbortController();92+ const resolving = Promise.allSettled(apps/mobile/src/state/assets.ts line 139OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -94,0 +119,21 @@119+}120+121+export interface AuthorizedAssetUrl {122+ readonly url: string;123+ readonly authorizedAt: number;124+}125+126+interface FreshAssetUrlKey {127+ readonly environmentId: EnvironmentId | null;128+ readonly resource: AssetResource;129+ /** A reconnect changes the base URL, and any URL minted against the old one is unusable. */130+ readonly httpBaseUrl: string | null;131+ /** Bumped by Retry so a file replaced on disk is reauthorized. */132+ readonly revision: number;133+}134+135+// Plain-object keys compare structurally (Effect `Equal`/`Hash`).136+const freshAssetUrlFamily = Atom.family((key: FreshAssetUrlKey) =>137+ Atom.make(138+ Effect.tryPromise({139+ try: async (): Promise<AuthorizedAssetUrl> => {apps/mobile/src/state/assets.ts line 116OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -92,3 +96,21 @@9296 return state._tag === "Success" ? state.url : null;9397 }, [createUrl, environmentId, httpBaseUrl, resource]);9498 }99+100+/** {@link useRefreshAssetUrl} for async tasks that resolve several resources at once. */101+export async function refreshAssetUrl(102+ environmentId: EnvironmentId,103+ resource: AssetResource,104+): Promise<string | null> {105+ const connection = appAtomRegistry.get(106+ environmentSession.preparedConnectionValueAtom(environmentId),107+ );108+ if (connection._tag === "None") return null;109+ const atom = assetEnvironment.createUrl({ environmentId, input: { resource } });110+ const state = assetUrlStateFromResult(111+ await executeAtomQuery(appAtomRegistry, atom, {112+ label: atom.label?.[0] ?? "asset url",113+ refresh: true,114+ reportFailure: false,115+ }),116+ connection.value.httpBaseUrl,
- apps/mobile/src/state/assets.ts lines 101 to 120Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -94,0 +99,21 @@99+100+/** {@link useRefreshAssetUrl} for async tasks that resolve several resources at once. */101+export async function refreshAssetUrl(102+ environmentId: EnvironmentId,103+ resource: AssetResource,104+): Promise<string | null> {105+ const connection = appAtomRegistry.get(106+ environmentSession.preparedConnectionValueAtom(environmentId),107+ );108+ if (connection._tag === "None") return null;109+ const atom = assetEnvironment.createUrl({ environmentId, input: { resource } });110+ const state = assetUrlStateFromResult(111+ await executeAtomQuery(appAtomRegistry, atom, {112+ label: atom.label?.[0] ?? "asset url",113+ refresh: true,114+ reportFailure: false,115+ }),116+ connection.value.httpBaseUrl,117+ );118+ return state._tag === "Success" ? state.url : null;119+}
- apps/mobile/src/components/FilePreviewModal.tsx line 104Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -129,0 +84,21 @@84+ if (props.onOpenError) props.onOpenError(error.cause);85+ else Dialog.alert("Could not open preview", error.fallbackMessage);86+ props.onRequestClose();87+ });88+ // Resolves once per presentation and holds local file leases until it closes.89+ useEffect(() => {90+ if (items.every((item) => "uri" in item)) return;91+ const controller = new AbortController();92+ // The first failure closes the preview at once and cancels the rest of the gallery.93+ const fail = (error: unknown) => {94+ if (controller.signal.aborted) return;95+ controller.abort();96+ onResolutionError(97+ error instanceof PreviewResolutionError98+ ? error99+ : new PreviewResolutionError(error, "Try again."),100+ );101+ };102+ const resolving = Promise.allSettled(103+ items.map((item) =>104+ resolvePreviewItem(item, controller.signal).catch((error: unknown) => {
juliusmarminge merged this220eb73
juliusmarminge closed this
Sign in to join this conversation.
Thread transfer impact#
⚠️ The latest CI run did not produce a thread transfer result for
0413e8b.This comment will update automatically after the next completed run.