Repository navigation
refactor: share one decodeImage helper for image loading - #141
Merged
Merged
Conversation
Move App.tsx's decodeImage into src/lib/decodeImage.ts and use it in useQuantize, useAppHandlers, DeditherPanel, useSwatches, useBuildWarning and usePrintableFeatureSimulation in place of their local new Image() promises. The helper resolves null (never rejects) on load failure or a zero-size image; callers that used to reject now throw their original error on null so their catch paths are unchanged. CanvasPreview, ThreeDView, StackMatrixCalibrationPanel and PaletteProofImageTargetPicker keep their own loading because they need different handling. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 7, 2026
This was referenced Nov 19, 2026
Merged
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.
Closes #132 once merged.
What
decodeImagemoves fromsrc/App.tsxtosrc/lib/decodeImage.ts, and six localnew Image()promises now use it. Its one documented behavior: it resolves the loadedHTMLImageElement, ornullwhen the browser can't decode the URL or the image has zero width or height. It never rejects.Why
src/libThe helper has no React and no UI state.
src/libalready holds React-free modules that use browser APIs (seo.tsusesdocument, the*Prefs/*Storagemodules uselocalStorage), so it fits there as a small, framework-free utility. It isn't a hook, sosrc/hookswould be the wrong place, and a new top-level folder for one function seemed like too much. Because it lives insrc/lib,tests/decodeImage.test.tscan test it undernode:testwith a fakeImage.Callers switched
App.tsx(handleFiles,resizeCurrentImage)decodeImageuseQuantizenullon errordecodeImage(...), sameif (!img) returnuseAppHandlers.onSwatchApplynullon errorDeditherPanelnullon erroruseSwatches'image load failed'if (!img) throw new Error('image load failed'), so the samecatchpath runsusePrintableFeatureSimulationloadImagerejects with'Could not load the image for printable-detail analysis'nullafter thecancelledcheck. The.catchsees the same error, so the message shown to users doesn't changeuseBuildWarningonload/onerrorsetting dimensionsdecodeImage(...).then(...). No cancellation before or after; I kept the existing stale-load behavior as it wasAbout zero-size images: the only new behavior is the zero-width/height guard from #124. It doesn't change anything users can reach.
handleFilesalready rejects zero-size images at import, and the canvas-export callers (useQuantize,useAppHandlers,DeditherPanel) get blobs fromCanvasPreview, which only builds its canvases whennaturalWidth && naturalHeight. In those callers, a zero-size image would previously have thrownIndexSizeErrorfromgetImageData(0, 0, 0, 0).Object URLs that
useQuantize,useAppHandlersandDeditherPanelcreate are still never revoked, as before. Fixing that would change behavior, so it's left for a separate change.Callers left alone (on purpose)
CanvasPreview: event-driven. It assignsimgRef.currentbefore the load (the export helpers read it), checks a load generation andmountedRef, takes a separateisSelfCommitpath, and handles zero size inline while still restoring the view.ThreeDView: setscrossOrigin = 'anonymous'so the canvas readback isn't tainted. Adding a cross-origin option to the helper would be scope creep.StackMatrixCalibrationPanelphoto picker: event-driven, with generation and active-owner checks and its own object-URL revocation on each exit path.PaletteProofImageTargetPicker: cancelled through effect cleanup, and has its own error messages. A zero-size image currently goes topaletteProofTargetImageSizeand ends on "Could not read the processed image colors". The helper would turn that into the load error, which is a different user-visible message.Notes
useBuildWarning.tsandusePrintableFeatureSimulation.tsimport../lib/decodeImage.tswith the extension.tests/useBuildWarning.test.tsloads that hook directly in Node, which needs the extension;usePrintableFeatureSimulationalready used.tsimports.Testing
npm run lintpassesnpm run buildpassesnpm test: 1105 passed, 0 failed, 1 skipped (the Windows-only NSIS test). This includes 3 newdecodeImagetests.npm run test:e2e(@smoke, Chromium, production build): 76 passed. Another worktree's server was on port 5173, so I ran it with a temporary copy ofplaywright.config.tsthat serves this branch's build on port 5183. That file isn't committed.🤖 Generated with Claude Code