Skip to content

refactor: lift MediaRecord state into useMediaRecord so the record panel reads it - #877

Draft
nabalone wants to merge 2 commits into
resource-upload-contextfrom
record-panel-had-take
Draft

nabalone wants to merge 2 commits into
resource-upload-contextfrom
record-panel-had-take

Conversation

@nabalone

@nabalone nabalone commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #875 (resource-upload-context); retarget to develop once #875 merges.

Resolves the TODO cleanup in next PR on MediaRecord's setHasTake effect, which only copied a derived value up to the parent (a CODING_STANDARDS "Effects" finding).

Approach

The first commit replaced the effect with hadTake bookkeeping in the panel (set on upload, cleared on new recording). Review showed that was fragile: it missed a deleted take, and any other way a take leaves the waveform would be the same bug. The second commit replaces it by lifting the recorder state up instead:

  • MediaRecord is split, following the existing AudioDownload / useAudioDownload / AudioDownloadView pattern:
    • useMediaRecord (new file useMediaRecord.ts): all of MediaRecord's state and save logic, moved unchanged. It computes needsSave and hasTake during render and returns them.
    • MediaRecordView: the markup, rendered from the hook's return value.
    • MediaRecord: a two-line wrapper (hook + view), so every other host is unchanged.
  • The add-resource Record tab (ResourceRecordTab, mounted only while that tab shows, so it still starts fresh on each switch) calls useMediaRecord itself and renders MediaRecordView. Its button reads onNext ? rec.hasTake : rec.needsSave directly: no effect, no copied state, no event tracking. Deleting a take, editing it, etc. are all covered because hasTake is derived from the waveform state.
  • setHasTake is gone. setCanSave is now optional and its effect only runs for hosts that pass it; the TODO there says to move those hosts to the hook + view pattern and drop the effect.

Test plan

  • npm run typecheck clean; prettier and eslint clean on changed files.
  • Jest: related tests pass (148 suites). ResourceRecordPanel.test.tsx covers the button rule (wizard Next on hasTake, plain Save on needsSave).
  • Cypress CT MediaRecord.recording.cy.tsx / MediaRecord.deleteRegion.cy.tsx (not run locally).
  • Manual: Add audio resource as a general resource → Record → Next → Upload in Select Sections → Back: Next still enabled. Delete the take: Next disabled. Re-record: disabled until the take is ready. Also record in another host (e.g. passage Record step) to confirm the wrapper is unchanged.

🤖 Generated with Claude Code

…aRecord's setHasTake effect

MediaRecord's setHasTake effect only copied a derived value up to the
parent (a CODING_STANDARDS "Effects" finding). Drop the prop and effect;
ResourceRecordPanel now marks hadTake from afterUploadCb on a successful
upload and clears it when a new recording starts or the Record tab is
re-entered, so Next still carries an already-uploaded take forward after
Back. Adds ResourceRecordPanel tests for the Back cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.

🟡 Changes recommended

The change still contains an active test issue where controlsRef is forcibly cast with as never, bypassing the intended RefObject<MediaUploadControlsRef> typing contract and conflicting with the project's typing standards, so the test setup should be corrected to provide a properly shaped controlsRef.current instead.

1 open issue
What changed in this PR

Refactors the add-resource wizard recording flow so ResourceRecordPanel owns the “uploaded take exists” state (hadTake) instead of relying on MediaRecord to push derived state upward via an effect.

Changes:

  • Removed setHasTake and its derived-state syncing effect from MediaRecord.
  • Added hadTake state to ResourceRecordPanel, set on successful upload and cleared when a new recording starts or when re-entering the record step.
  • Added Jest coverage to verify wizard Back/Next behavior after upload, handling of failed uploads, and non-wizard Save gating.
File Description
web/​src/​components/​PassageDetail/​Internalization/​ResourceRecordPanel.tsx Moves “uploaded take exists” tracking into the panel (hadTake) and updates button enablement logic to preserve wizard Next behavior after an upload while keeping non-wizard Save gated on unsaved changes.
web/​src/​components/​PassageDetail/​Internalization/​ResourceRecordPanel.test.tsx Adds tests covering Next/Save enablement across upload success/failure, Back navigation, and starting a new recording resetting the uploaded-take state.
web/​src/​components/​MediaRecord.tsx Removes the setHasTake prop and the effect that copied derived hasTake state upward, leaving the remaining setCanSave effect TODO for later cleanup.

Review effort: Balanced

…nel reads it

Replaces the hadTake bookkeeping, which missed a deleted take. MediaRecord
is split into useMediaRecord (state + save logic, computing needsSave and
hasTake during render), MediaRecordView (markup) and a MediaRecord wrapper
for existing hosts, following the AudioDownload / useAudioDownload pattern.
The add-resource Record tab owns the hook, so its button reads needsSave
and hasTake directly: no effect copies them up and no events are tracked.
setCanSave is now optional and only fires for hosts that pass it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nabalone nabalone changed the title refactor: ResourceRecordPanel tracks an uploaded take instead of MediaRecord's setHasTake effect refactor: lift MediaRecord state into useMediaRecord so the record panel reads it Nov 16, 2026
@nabalone
nabalone requested a balanced review from Copilot November 16, 2026 20:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.

🟡 Changes recommended

Outstanding review findings note missing required justifications for exhaustive-deps suppressions and a maintainability concern with a 748-line hook module that exceeds project size guidelines.

3 open issues
1 resolved since last review

Review effort: Balanced

controlsRef.current.handleAddOrSave = () => startSave(myToolId);
controlsRef.current.handleAddOrSave = () => startSave(RECORD_TOOL_ID);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Comment on lines +77 to +80
/** The recorder's state and save logic; render it with MediaRecordView. */
export function useMediaRecord(props: IMediaRecordStateProps) {
const {
toolId,
Comment on lines +260 to +263
useEffect(() => {
if (mediaId !== mediaState.id) fetchMediaUrl({ id: mediaId ?? '' });
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [mediaId]);
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.

2 participants