Skip to content

refactor: share one decodeImage helper for image loading - #141

Merged
vycdev merged 2 commits into
developfrom
refactor/issue-132-image-loading-helper
Nov 7, 2026
Merged

vycdev merged 2 commits into
developfrom
refactor/issue-132-image-loading-helper

Conversation

@vycdev

@vycdev vycdev commented Nov 7, 2026

Copy link
Copy Markdown
Owner

Closes #132 once merged.

What

decodeImage moves from src/App.tsx to src/lib/decodeImage.ts, and six local new Image() promises now use it. Its one documented behavior: it resolves the loaded HTMLImageElement, or null when the browser can't decode the URL or the image has zero width or height. It never rejects.

Why src/lib

The helper has no React and no UI state. src/lib already holds React-free modules that use browser APIs (seo.ts uses document, the *Prefs/*Storage modules use localStorage), so it fits there as a small, framework-free utility. It isn't a hook, so src/hooks would be the wrong place, and a new top-level folder for one function seemed like too much. Because it lives in src/lib, tests/decodeImage.test.ts can test it under node:test with a fake Image.

Callers switched

Caller Before Now
App.tsx (handleFiles, resizeCurrentImage) local decodeImage import only. The call sites are unchanged, to keep the diff small next to #131 and #133
useQuantize resolves null on error decodeImage(...), same if (!img) return
useAppHandlers.onSwatchApply resolves null on error same as above
DeditherPanel resolves null on error same as above
useSwatches rejects with 'image load failed' if (!img) throw new Error('image load failed'), so the same catch path runs
usePrintableFeatureSimulation local loadImage rejects with 'Could not load the image for printable-detail analysis' throws the same message on null after the cancelled check. The .catch sees the same error, so the message shown to users doesn't change
useBuildWarning onload/onerror setting dimensions decodeImage(...).then(...). No cancellation before or after; I kept the existing stale-load behavior as it was

About zero-size images: the only new behavior is the zero-width/height guard from #124. It doesn't change anything users can reach. handleFiles already rejects zero-size images at import, and the canvas-export callers (useQuantize, useAppHandlers, DeditherPanel) get blobs from CanvasPreview, which only builds its canvases when naturalWidth && naturalHeight. In those callers, a zero-size image would previously have thrown IndexSizeError from getImageData(0, 0, 0, 0).

Object URLs that useQuantize, useAppHandlers and DeditherPanel create 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 assigns imgRef.current before the load (the export helpers read it), checks a load generation and mountedRef, takes a separate isSelfCommit path, and handles zero size inline while still restoring the view.
  • ThreeDView: sets crossOrigin = 'anonymous' so the canvas readback isn't tainted. Adding a cross-origin option to the helper would be scope creep.
  • StackMatrixCalibrationPanel photo 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 to paletteProofTargetImageSize and 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.ts and usePrintableFeatureSimulation.ts import ../lib/decodeImage.ts with the extension. tests/useBuildWarning.test.ts loads that hook directly in Node, which needs the extension; usePrintableFeatureSimulation already used .ts imports.
  • No changelog or docs change, because this is a behavior-preserving refactor.

Testing

  • npm run lint passes
  • npm run build passes
  • npm test: 1105 passed, 0 failed, 1 skipped (the Windows-only NSIS test). This includes 3 new decodeImage tests.
  • 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 of playwright.config.ts that serves this branch's build on port 5183. That file isn't committed.

🤖 Generated with Claude Code

vycdev and others added 2 commits November 7, 2026 13:31
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]>
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