Skip to content

Move image import into useImageImport and validate Stack Matrix photos - #142

Merged
vycdev merged 3 commits into
developfrom
refactor/issue-133-image-import-hook
Nov 10, 2026
Merged

vycdev merged 3 commits into
developfrom
refactor/issue-133-image-import-hook

Conversation

@vycdev

@vycdev vycdev commented Nov 7, 2026

Copy link
Copy Markdown
Owner

Refs #133. Leaving the issue open for review.

What changed

1. Image import lifecycle moved into useImageImport (src/hooks/useImageImport.ts). The hook owns the import token, the import error, and the dismiss and cancel functions. App.tsx keeps only the commit step (setTouchUpTool(null), invalidate(), setImage), the processingActive gating, and the notice JSX.

The hook has to be called before useImageHistory, so that its cancelPendingImport can be the history's onBeforeSet. That means it cannot read imageSrc during render, so the render-time "clear the error when imageSrc changes" check is replaced:

  • cancelPendingImport now also closes the error. Every imageSrc change goes through onBeforeSet (set, clear, undo, redo), so the notice still closes on undo, redo, edits, Remove image, and a successful import, and doesn't come back on redo.
  • The error records keptImage: boolean instead of the shown image's URL. The URL was only used for the "current image was kept" wording.

tests/e2e/image-import-error.spec.ts passes unchanged in Chromium and WebKit.

2. Shared validateImageFile(file) in src/lib/imageFile.ts. It combines isImageFile, the <img> decode, isTruncatedImageData and createImageBitmap. It returns { ok: true, image, url } (the caller owns the URL) or { ok: false, reason: 'notImage' | 'undecodable' } with no URL left open. decodeImage moved there as well, because resizeCurrentImage still uses it.

3. Stack Matrix photo picker fix. handlePhotoFile now uses validateImageFile. Before this change, a truncated PNG or JPEG loaded as a partial photo with no error. It now shows the existing "Could not open that photo" message, which is already translated in all 12 locales, so no new strings. The photo already loaded is kept. The existing load-generation and owner checks still run after validation. The isImageFile pre-check stays synchronous, so a non-image doesn't cancel a photo that is still loading (same as before).

Docs and changelog

  • The Calibration Workflows guide ("Load And Align A Photo") gets one sentence about unreadable photos, in all 12 languages, matching the Loading Images guide from Report unreadable image imports instead of failing silently #124.
  • The Stack Matrix fix is folded into the existing unreleased Unreadable image imports entry under Fixed.

Tests

  • Unit: tests/imageFile.test.ts covers validateImageFile with a WebKit-like decoder stub, so the truncated PNG is refused only by the structural check. It also covers decoder and bitmap rejection, the SVG bitmap skip, non-images, and that every refused file's URL is revoked.
  • e2e: tests/e2e/stack-matrix-photo-import.spec.ts (@smoke) runs a truncated photo with no photo loaded, then a valid photo, then truncated, corrupt, and non-image files; the loaded photo is kept each time. On the old handlePhotoFile this spec fails in both Chromium and WebKit at the first truncated photo. Adds data-testids to the photo input and canvas.

Ran locally:

  • npm run lint, npm run build, npm test (1107 pass, 1 skipped).
  • Chromium: image-import-error, stack-matrix-photo-import, stack-matrix-settings, stack-matrix-desktop-save (12/12).
  • WebKit (Desktop Safari): image-import-error and stack-matrix-photo-import (8/8).

Notes for reviewers

🤖 Generated with Claude Code

vycdev and others added 3 commits November 7, 2026 13:35
Move the image import lifecycle out of App.tsx into a useImageImport hook.
It owns the import token, the import error, and the dismiss and cancel
functions. The hook is called before useImageHistory so its
cancelPendingImport can be the history's onBeforeSet. That callback now
also closes the error, which replaces the render-time check against
imageSrc: every image change goes through it, so the notice still closes
on undo, redo, edits, Remove image, and a successful import. The error
records whether an image was kept instead of the shown image's URL.

Extract validateImageFile(file) into src/lib/imageFile.ts, combining
isImageFile, the <img> decode, the truncation check, and
createImageBitmap. It returns the decoded image and its object URL, or
the reason the file was refused.

The Stack Matrix photo picker now uses validateImageFile too. A truncated
PNG or JPEG chosen there used to load as a partial photo without an
error; it now reports "Could not open that photo", which is already
translated, and keeps the photo already loaded. Mention this in the
Calibration Workflows guide in all 12 languages and fold it into the
existing changelog entry.

Add unit tests for validateImageFile with a WebKit-like decoder stub,
and a browser regression for truncated, corrupt, and non-image Stack
Matrix photos, which fails without this change in Chromium and WebKit.

Refs #133

Co-Authored-By: Claude Opus 5.5 <[email protected]>
From self-review:
- validateImageFile now treats a failed byte read (for example, a file
  changed on disk after it was chosen) as unreadable, so it reports an
  error and revokes its object URL instead of rejecting.
- useImageImport clears its error on a successful import itself rather
  than relying on the commit going through useImageHistory.
- The Stack Matrix e2e loads a different valid photo before the corrupt
  file, so that step cannot pass on the previous step's error.

Refs #133

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve conflicts with #132's shared decodeImage helper: validateImageFile
and App's resize now import decodeImage from src/lib/decodeImage.ts, and
the duplicate copy in imageFile.ts is dropped. The validateImageFile tests
now remove their Image and createImageBitmap stubs instead of leaving the
globals defined as undefined.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant