For the complete documentation index, see llms.txt. This page is also available as Markdown.

Phase 4: Download Logic Hook

← Back to Index

Effort: 2 hours (tests) + 2 hours (implementation) = 4 hours total Risk: Medium (mitigated by tests) Priority: Medium Previous: Phase 3: Canvas Data Next: Phase 5: Restriction Status

Goal

Extract download options logic from ViewerTopBar.tsx into a reusable custom hook. This removes ~65 lines of complex business logic from the component and makes it testable in isolation.

Note: The restricted-images merge already added download deduplication and improved ChoiceBody handling. This phase integrates those changes into the hook.

Why Extract to a Hook (Not Context)?

Current problem: ViewerTopBar.tsx has 65+ lines of download option calculations:

  • IIIF image downloads

  • Canvas image downloads from services

  • Canvas rendering downloads (PDFs)

  • Manifest downloads

  • Video/audio downloads

  • ChoiceBody handling

Why a hook instead of context?

  • Not for context: Only used in ViewerTopBar (one component)

  • Perfect for hook: Complex logic (65 lines) worth extracting for testability

  • Potentially reusable: Other components might need download options in the future

Solution: Move all this into useDownloadOptions hook:

  • Easier to test (no component rendering needed)

  • Reusable if other components need download options

  • Clearer component code (ViewerTopBar focuses on presentation)

  • Business logic separate from presentation

Steps

2.1 Write Tests FIRST for useDownloadOptions Hook

File: content/webapp/hooks/useDownloadOptions.test.ts

2.2 Create useDownloadOptions Hook

File: content/webapp/hooks/useDownloadOptions.ts

Note: The restricted-images merge improved getDownloadOptionsFromCanvasRenderingAndSupplementing to properly handle ChoiceBody items:

2.3 Update ViewerTopBar to Use Hook

File: content/webapp/views/pages/works/work/IIIFViewer/ViewerTopBar.refactored.tsx

Result: ~65 lines removed from ViewerTopBar.

2.4 Run Tests

All should pass.

2.5 Manual Testing

Success Criteria

Time Breakdown

  • 2 hours: Write comprehensive tests for hook

  • 1 hour: Extract logic to hook

  • 1 hour: Update ViewerTopBar to use hook

  • 30 mins: Manual testing

Total: ~4 hours


Next: Phase 5: Restriction Status

Last updated