Skip to content

Commit 49391d8

Browse files
tellahonpub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w
andauthored
fix(composer): address image-editor follow-up nits on #1491 (#1565)
Signed-off-by: Taylor Ho <[email protected]> Co-authored-by: npub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w <52a228d6edf316ec6812ac3c9fc0d696ab59fc7954d77e7be31eedcddf91335b@sprout-oss.stage.blox.sqprod.co>
1 parent 10444d2 commit 49391d8

6 files changed

Lines changed: 53 additions & 12 deletions

File tree

‎desktop/src-tauri/src/commands/media_download.rs‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,21 @@ pub async fn download_file(
138138
///
139139
/// Same SSRF validation, size cap, and content policy as the download
140140
/// commands above.
141+
///
142+
/// Returns `tauri::ipc::Response` so the bytes cross IPC as a raw buffer
143+
/// instead of a JSON number array (which would be ~3x the size to
144+
/// serialize and deserialize at the 50 MiB cap).
141145
#[tauri::command]
142-
pub async fn fetch_media_bytes(url: String, state: State<'_, AppState>) -> Result<Vec<u8>, String> {
146+
pub async fn fetch_media_bytes(
147+
url: String,
148+
state: State<'_, AppState>,
149+
) -> Result<tauri::ipc::Response, String> {
143150
let relay_base = relay_api_base_url_with_override(&state);
144151
validate_download_url(&url, &relay_base)?;
145152

146153
let bytes = fetch_blob_bytes(&url, &state).await?;
147154
detect_and_validate_mime(&bytes)?;
148-
Ok(bytes)
155+
Ok(tauri::ipc::Response::new(bytes))
149156
}
150157

151158
/// Fetch blob bytes from a (pre-validated) relay media URL through the app's

‎desktop/src/features/messages/lib/useMediaUpload.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,9 @@ export function useMediaUpload() {
208208
/**
209209
* Pre-edit originals of annotated attachments, keyed by the annotated
210210
* attachment's URL. Powers "revert to original" in the composer lightbox.
211-
* In-memory only — cleared implicitly when the attachment leaves the
212-
* composer (send, remove, draft switch).
211+
* In-memory only, by design — cleared implicitly when the attachment
212+
* leaves the composer (send, remove, draft switch). Persisting revert
213+
* across a draft round-trip is an explicit non-goal (#1491 review).
213214
*/
214215
const [originalsByUrl, setOriginalsByUrl] = React.useState<
215216
Map<string, BlobDescriptor>

‎desktop/src/features/messages/ui/ComposerAttachments.tsx‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,17 @@ const MediaAttachmentItem = React.forwardRef<
126126
// stable Escape listener, so the handler would otherwise see a stale mode.
127127
const modeRef = React.useRef(mode);
128128
modeRef.current = mode;
129+
// Gates Escape while the editor's save upload is in flight: dismissing
130+
// edit mode mid-save would look like a cancel while the swap still lands.
131+
const editorSavingRef = React.useRef(false);
132+
const handleEditorSavingChange = React.useCallback((saving: boolean) => {
133+
editorSavingRef.current = saving;
134+
}, []);
129135
const handleEscapeKeyDown = React.useCallback((event: KeyboardEvent) => {
130136
if (modeRef.current === "edit") {
131137
// Escape leaves canvas mode but keeps the lightbox open.
132138
event.preventDefault();
133-
setMode("view");
139+
if (!editorSavingRef.current) setMode("view");
134140
}
135141
}, []);
136142

@@ -235,6 +241,7 @@ const MediaAttachmentItem = React.forwardRef<
235241
sourceType={attachment.type}
236242
onCancel={handleEditorCancel}
237243
onSave={handleEditorSave}
244+
onSavingChange={handleEditorSavingChange}
238245
/>
239246
) : isVideo ? (
240247
// biome-ignore lint/a11y/useMediaCaption: user-uploaded video, no captions available
@@ -297,6 +304,14 @@ const MediaAttachmentItem = React.forwardRef<
297304
className={cn(
298305
LIGHTBOX_BUTTON_CLASS,
299306
"h-auto min-w-0",
307+
// Active state driven by component state, not
308+
// Radix's data-state: the TooltipTrigger clobbers
309+
// the Toggle's data-state attribute. Swap the
310+
// circular pill for the shared button radius with
311+
// a visible ring so a spoilered attachment reads
312+
// as "selected" on the dark lightbox backdrop.
313+
isSpoilered &&
314+
"rounded-lg bg-white/25 text-white ring-2 ring-white",
300315
)}
301316
data-testid="composer-attachment-spoiler"
302317
onPressedChange={() =>

‎desktop/src/features/messages/ui/ComposerImageEditor.tsx‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ type ComposerImageEditorProps = {
121121
onCancel: () => void;
122122
/** Upload the annotated PNG; rejection keeps the editor open. */
123123
onSave: (bytes: Uint8Array) => Promise<void>;
124+
/** Notifies the parent while a save is in flight (used to gate Escape). */
125+
onSavingChange?: (saving: boolean) => void;
124126
};
125127

126128
/**
@@ -136,6 +138,7 @@ export function ComposerImageEditor({
136138
sourceType,
137139
onCancel,
138140
onSave,
141+
onSavingChange,
139142
}: ComposerImageEditorProps) {
140143
const canvasRef = React.useRef<HTMLCanvasElement>(null);
141144
const activeStrokeRef = React.useRef<EditorStroke | null>(null);
@@ -159,6 +162,18 @@ export function ComposerImageEditor({
159162
const [saving, setSaving] = React.useState(false);
160163
const [saveError, setSaveError] = React.useState<string | null>(null);
161164

165+
const setSavingState = React.useCallback(
166+
(next: boolean) => {
167+
setSaving(next);
168+
onSavingChange?.(next);
169+
},
170+
[onSavingChange],
171+
);
172+
173+
// Reset the parent's gate if we unmount mid-save (success closes the
174+
// lightbox and unmounts this component before the flag is cleared here).
175+
React.useEffect(() => () => onSavingChange?.(false), [onSavingChange]);
176+
162177
const handleImageLoad = React.useCallback(
163178
(event: React.SyntheticEvent<HTMLImageElement>) => {
164179
const { naturalHeight, naturalWidth } = event.currentTarget;
@@ -280,17 +295,17 @@ export function ComposerImageEditor({
280295

281296
const handleSave = React.useCallback(async () => {
282297
if (saving || strokes.length === 0) return;
283-
setSaving(true);
298+
setSavingState(true);
284299
setSaveError(null);
285300
try {
286301
const bytes = await renderAnnotatedPng(sourceUrl, sourceType, strokes);
287302
await onSave(bytes);
288303
// On success the parent closes the lightbox and unmounts this component.
289304
} catch {
290305
setSaveError("Could not save the drawing. Please try again.");
291-
setSaving(false);
306+
setSavingState(false);
292307
}
293-
}, [onSave, saving, sourceType, sourceUrl, strokes]);
308+
}, [onSave, saving, setSavingState, sourceType, sourceUrl, strokes]);
294309

295310
const hasStrokes = strokes.length > 0;
296311

‎desktop/src/shared/api/tauriMedia.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { invokeTauri } from "./tauri";
1111
export async function fetchMediaBytes(
1212
url: string,
1313
): Promise<Uint8Array<ArrayBuffer>> {
14-
const bytes = await invokeTauri<number[]>("fetch_media_bytes", { url });
14+
// The Rust command replies with `tauri::ipc::Response`, so the bytes
15+
// arrive as a raw ArrayBuffer rather than a JSON number array.
16+
const bytes = await invokeTauri<ArrayBuffer>("fetch_media_bytes", { url });
1517
return new Uint8Array(bytes);
1618
}

‎desktop/src/testing/e2eBridge.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8650,11 +8650,12 @@ export function maybeInstallE2eTauriMocks() {
86508650
case "upload_media_bytes":
86518651
return (await resolveMockUploadDescriptors(activeConfig))[0];
86528652
case "fetch_media_bytes": {
8653-
// The real command fetches relay media through Rust reqwest. In E2E
8654-
// the browser fetch suffices — specs serve the URL via page.route.
8653+
// The real command fetches relay media through Rust reqwest and
8654+
// replies with raw bytes (`tauri::ipc::Response` → ArrayBuffer). In
8655+
// E2E the browser fetch suffices — specs serve the URL via page.route.
86558656
const response = await fetch((payload as { url: string }).url);
86568657
if (!response.ok) throw new Error(`fetch failed: ${response.status}`);
8657-
return Array.from(new Uint8Array(await response.arrayBuffer()));
8658+
return await response.arrayBuffer();
86588659
}
86598660
case "download_image":
86608661
case "download_file":

0 commit comments

Comments
 (0)