Skip to content

Commit 18c07f5

Browse files
authored
Merge branch 'main' into t3/fork-checkout-options-rebased
2 parents 55eb693 + d8e9fd2 commit 18c07f5

87 files changed

Lines changed: 5946 additions & 7609 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/mobile/src/components/ComposerContextAttachment.tsx‎

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
2+
import { AsyncResult } from "effect/reactivity";
13
import * as Dialog from "./ui/Dialog";
24
import type { ComposerContextRecord, EnvironmentId } from "@t3tools/contracts";
35
import { Pressable, View } from "react-native";
46
import { Image } from "expo-image";
5-
import { useEffect, useId, useMemo, useState } from "react";
7+
import { useEffect, useId, useState } from "react";
68
import type { DraftComposerAttachment } from "../lib/composerImages";
79
import { composerAttachmentInlineUri, isFileBackedComposerAttachment } from "../lib/composerImages";
810
import { loadLocalAttachmentPreview } from "../lib/localAttachmentPreview";
@@ -20,14 +22,11 @@ export function ComposerContextAttachment(props: {
2022
}) {
2123
const { record, attachment } = props;
2224
const shareSourceIdentifier = useId();
23-
const resource = useMemo(
24-
() => ({
25-
_tag: "attachment" as const,
26-
attachmentId: record.attachmentId,
27-
fileName: record.name,
28-
}),
29-
[record.attachmentId, record.name],
30-
);
25+
const resource = {
26+
_tag: "attachment" as const,
27+
attachmentId: record.attachmentId,
28+
fileName: record.name,
29+
};
3130
const local = attachment?.fileUri
3231
? (attachment as DraftComposerAttachment & { fileUri: string })
3332
: undefined;
@@ -64,31 +63,33 @@ export function ComposerContextAttachment(props: {
6463
if (sharing) return;
6564
setSharing(true);
6665
const controller = new AbortController();
67-
try {
66+
const result = await settlePromise(async () => {
6867
if (local) {
6968
const preview = await loadLocalAttachmentPreview(local, controller.signal);
70-
try {
71-
await preview?.share(controller.signal, shareSourceIdentifier);
72-
} finally {
73-
preview?.dispose();
74-
}
75-
} else {
76-
const url = await refresh();
77-
if (!url) throw new Error("Reconnect to the environment and try again.");
78-
await downloadAndShareAttachment({
79-
url,
80-
attachment: record,
81-
signal: controller.signal,
82-
sourceIdentifier: shareSourceIdentifier,
83-
});
69+
if (!preview) return;
70+
const shared = await settlePromise(() =>
71+
preview.share(controller.signal, shareSourceIdentifier),
72+
);
73+
preview.dispose();
74+
if (AsyncResult.isFailure(shared)) throw squashAtomCommandFailure(shared);
75+
return;
8476
}
85-
} catch (cause) {
77+
const url = await refresh();
78+
if (!url) throw new Error("Reconnect to the environment and try again.");
79+
await downloadAndShareAttachment({
80+
url,
81+
attachment: record,
82+
signal: controller.signal,
83+
sourceIdentifier: shareSourceIdentifier,
84+
});
85+
});
86+
setSharing(false);
87+
if (AsyncResult.isFailure(result)) {
88+
const cause = squashAtomCommandFailure(result);
8689
Dialog.alert(
8790
"Could not open attachment",
8891
cause instanceof Error ? cause.message : "Try again.",
8992
);
90-
} finally {
91-
setSharing(false);
9293
}
9394
};
9495
return (

‎apps/mobile/src/components/ComposerEditor.tsx‎

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import * as Dialog from "./ui/Dialog";
22
import { ComposerContextId } from "@t3tools/contracts";
33
import { useAtomValue } from "@effect/atom-react";
44
import { AsyncResult } from "effect/reactivity";
5+
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
56
import { useNavigation } from "@react-navigation/native";
6-
import { useEffect, useMemo, useRef, useState } from "react";
7+
import { useEffect, useRef, useState } from "react";
78
import type { EnvironmentId } from "@t3tools/contracts";
89
import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard";
910
import { collectComposerContextReferences } from "@t3tools/shared/composerContextReferences";
@@ -54,7 +55,7 @@ export function ComposerEditor({
5455
const preferredEnterBehavior = AsyncResult.isSuccess(preferencesResult)
5556
? preferencesResult.value.composerEnterBehavior
5657
: undefined;
57-
const contextHistory = useMemo(() => createComposerDraftContextHistory(), [draftKey]);
58+
const contextHistory = createComposerDraftContextHistory();
5859
useEffect(() => () => contextHistory.dispose(), [contextHistory]);
5960
const changeText = (text: string) => {
6061
const restored = contextHistory.restore(
@@ -87,7 +88,7 @@ export function ComposerEditor({
8788
importRef.current = controller;
8889
setImporting(true);
8990
setComposerContextImporting(draftKey, true);
90-
try {
91+
const settled = await settlePromise(async () => {
9192
const retained = getComposerDraftAfterSelection(draftKey, insertion);
9293
const result = await importComposerContextClipboard(
9394
clipboard,
@@ -111,41 +112,36 @@ export function ComposerEditor({
111112
"Some attachments could not be copied",
112113
"Reconnect to the source environment and copy them again. References without their files are marked unavailable.",
113114
);
114-
} catch (error) {
115-
if (!controller.signal.aborted)
116-
Dialog.alert(
117-
"Could not paste context",
118-
error instanceof Error ? error.message : "Try copying again.",
119-
);
120-
} finally {
121-
setComposerContextImporting(draftKey, false);
122-
importRef.current = null;
123-
setImporting(false);
115+
});
116+
setComposerContextImporting(draftKey, false);
117+
importRef.current = null;
118+
setImporting(false);
119+
if (AsyncResult.isFailure(settled) && !controller.signal.aborted) {
120+
const error = squashAtomCommandFailure(settled);
121+
Dialog.alert(
122+
"Could not paste context",
123+
error instanceof Error ? error.message : "Try copying again.",
124+
);
124125
}
125126
};
126-
const clipboardFragment = useMemo(
127-
() =>
128-
environmentId && draft.context
129-
? encodeComposerContextFragment({
130-
version: 1,
131-
source: { environmentId },
132-
records: draft.context.records.map((record) => {
133-
if (!("attachmentId" in record)) return record;
134-
const attachment = draft.attachments.find(
135-
(entry) => entry.id === record.attachmentId,
136-
);
137-
return {
138-
...record,
139-
attachmentId:
140-
attachment?.uploadEnvironmentId === environmentId
141-
? (attachment.uploadedAttachmentId ?? record.attachmentId)
142-
: record.attachmentId,
143-
};
144-
}),
145-
})
146-
: "",
147-
[environmentId, draft.context, draft.attachments],
148-
);
127+
const clipboardFragment =
128+
environmentId && draft.context
129+
? encodeComposerContextFragment({
130+
version: 1,
131+
source: { environmentId },
132+
records: draft.context.records.map((record) => {
133+
if (!("attachmentId" in record)) return record;
134+
const attachment = draft.attachments.find((entry) => entry.id === record.attachmentId);
135+
return {
136+
...record,
137+
attachmentId:
138+
attachment?.uploadEnvironmentId === environmentId
139+
? (attachment.uploadedAttachmentId ?? record.attachmentId)
140+
: record.attachmentId,
141+
};
142+
}),
143+
})
144+
: "";
149145
const navigation = useNavigation();
150146
const selectedReference = selected
151147
? collectComposerContextReferences(selected.source)[0]

‎apps/mobile/src/components/VideoPreviewModal.ios.tsx‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as Dialog from "./ui/Dialog";
22
import { useIsFocused } from "@react-navigation/native";
33
import { requireNativeModule } from "expo";
4+
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
5+
import { AsyncResult } from "effect/reactivity";
46
import { useEffect, useEffectEvent, useId, useState } from "react";
57
import { Keyboard } from "react-native";
68

@@ -76,20 +78,23 @@ function NativeVideoPreview(props: {
7678
? await loadLocalAttachmentPreview(localAttachment, controller.signal)
7779
: null;
7880
if (localAttachment !== null && !file) return;
79-
try {
80-
if (controller.signal.aborted) return;
81-
ready = true;
82-
await NativeControls.presentVideo(
81+
if (controller.signal.aborted) {
82+
file?.dispose();
83+
return;
84+
}
85+
ready = true;
86+
const presented = await settlePromise(() =>
87+
NativeControls.presentVideo(
8388
file?.uri ?? playbackUrl!,
8489
name,
8590
source.sourceIdentifier ?? "",
8691
identifier,
87-
);
88-
if (!controller.signal.aborted) onRequestClose();
89-
} finally {
90-
// Native completion follows dismissal, so local playback keeps its file lease.
91-
file?.dispose();
92-
}
92+
),
93+
);
94+
// Native completion follows dismissal, so local playback keeps its file lease.
95+
file?.dispose();
96+
if (AsyncResult.isFailure(presented)) throw squashAtomCommandFailure(presented);
97+
if (!controller.signal.aborted) onRequestClose();
9398
})().catch((error: unknown) => {
9499
if (controller.signal.aborted) return;
95100
// AVKit gives no retry, so re-mint now; the cached URL may simply have expired.

‎apps/mobile/src/components/VideoPreviewModal.tsx‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useIsFocused } from "@react-navigation/native";
22
import { videoMimeType } from "@t3tools/shared/video";
3-
import { useEffect, useRef, useState } from "react";
3+
import { useEffect, useEffectEvent, useState } from "react";
44
import { Keyboard, Modal, Pressable, View } from "react-native";
55
import { useSafeAreaInsets } from "react-native-safe-area-context";
66

@@ -60,14 +60,15 @@ function useLocalPlayback(source: LocalVideoPreviewSource): PlaybackState {
6060
const [error, setError] = useState<string | null>(null);
6161
// Only a different file needs a new lease; a metadata update on the same
6262
// draft must not dispose the file Android is still playing.
63-
const attachmentRef = useRef(attachment);
64-
attachmentRef.current = attachment;
63+
const loadAttachment = useEffectEvent((signal: AbortSignal) =>
64+
loadLocalAttachmentPreview(attachment, signal),
65+
);
6566
const { id: attachmentId, fileUri } = attachment;
6667
useEffect(() => {
6768
setUri(null);
6869
setError(null);
6970
const controller = new AbortController();
70-
const loading = loadLocalAttachmentPreview(attachmentRef.current, controller.signal);
71+
const loading = loadAttachment(controller.signal);
7172
void loading.then(
7273
(file) => {
7374
if (file === null) return;

‎apps/mobile/src/components/ui/OverlayPortal.tsx‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type ReactNode, useEffect, useRef, useState } from "react";
1+
import { type ReactNode, useEffect, useState } from "react";
22
import { View } from "react-native";
33

44
// AndroidAnchoredMenu projects its dropdown here instead of into an RN Modal: a Modal is a
@@ -8,6 +8,10 @@ type Entries = ReadonlyMap<number, ReactNode>;
88
type Listener = (entries: Entries) => void;
99

1010
let nextKey = 0;
11+
12+
function allocateKey() {
13+
return nextKey++;
14+
}
1115
const entries = new Map<number, ReactNode>();
1216
const listeners = new Set<Listener>();
1317

@@ -19,9 +23,7 @@ function emit() {
1923
}
2024

2125
export function OverlayPortal(props: { readonly children: ReactNode }) {
22-
const keyRef = useRef<number | null>(null);
23-
keyRef.current ??= nextKey++;
24-
const key = keyRef.current;
26+
const [key] = useState(allocateKey);
2527

2628
// No dependency array: re-project after every render so the host always
2729
// shows the current content (menus re-render while open — drill-in, theme).

0 commit comments

Comments
 (0)