Repository navigation
Move image import into useImageImport and validate Stack Matrix photos - #142
Merged
Merged
Conversation
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]>
This was referenced Nov 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.tsxkeeps only the commit step (setTouchUpTool(null),invalidate(),setImage), theprocessingActivegating, and the notice JSX.The hook has to be called before
useImageHistory, so that itscancelPendingImportcan be the history'sonBeforeSet. That means it cannot readimageSrcduring render, so the render-time "clear the error whenimageSrcchanges" check is replaced:cancelPendingImportnow also closes the error. EveryimageSrcchange goes throughonBeforeSet(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.keptImage: booleaninstead of the shown image's URL. The URL was only used for the "current image was kept" wording.tests/e2e/image-import-error.spec.tspasses unchanged in Chromium and WebKit.2. Shared
validateImageFile(file)insrc/lib/imageFile.ts. It combinesisImageFile, the<img>decode,isTruncatedImageDataandcreateImageBitmap. It returns{ ok: true, image, url }(the caller owns the URL) or{ ok: false, reason: 'notImage' | 'undecodable' }with no URL left open.decodeImagemoved there as well, becauseresizeCurrentImagestill uses it.3. Stack Matrix photo picker fix.
handlePhotoFilenow usesvalidateImageFile. 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. TheisImageFilepre-check stays synchronous, so a non-image doesn't cancel a photo that is still loading (same as before).Docs and changelog
Tests
tests/imageFile.test.tscoversvalidateImageFilewith 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.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 oldhandlePhotoFilethis spec fails in both Chromium and WebKit at the first truncated photo. Addsdata-testids to the photo input and canvas.Ran locally:
npm run lint,npm run build,npm test(1107 pass, 1 skipped).image-import-error,stack-matrix-photo-import,stack-matrix-settings,stack-matrix-desktop-save(12/12).Desktop Safari):image-import-errorandstack-matrix-photo-import(8/8).Notes for reviewers
handleFiles/decodeImage/isCompleteRasterImagecode. Those helpers now live insrc/lib/imageFile.ts, so their changes would move there.🤖 Generated with Claude Code