Skip to content

Commit

Permalink
feat: process all recordings if none are selected and only one medium…
Browse files Browse the repository at this point in the history
… exists

only warn if there's multiple mediums
  • Loading branch information
dvirtz committed Jan 7, 2025
1 parent ebc56b4 commit da90d30
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 53 deletions.
7 changes: 2 additions & 5 deletions src/acum-work-import/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ main();
function main() {
VM.observe(document.body, () => {
if (location.pathname.startsWith('/release/')) {
const recordingCheckboxes = document.querySelectorAll<HTMLInputElement>(
'input[type=checkbox].recording, input[type=checkbox].medium-recordings, input[type=checkbox].all-recordings'
);
if (recordingCheckboxes.length > 0 && !document.getElementById(releaseEditorContainerId)) {
createReleaseEditorUI(recordingCheckboxes);
if (!document.getElementById(releaseEditorContainerId)) {
createReleaseEditorUI();
return true;
}
} else {
Expand Down
33 changes: 31 additions & 2 deletions src/acum-work-import/import-album.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
map,
merge,
mergeMap,
of,
pipe,
scan,
tap,
Expand All @@ -17,6 +18,7 @@ import {
} from 'rxjs';
import {Setter} from 'solid-js';
import {compareInsensitive} from 'src/common/lib/compare';
import {head} from 'src/common/lib/head';
import {addEditNote} from 'src/common/musicbrainz/edit-note';
import {trackRecordingState} from 'src/common/musicbrainz/track-recording-state';
import {albumUrl, Creator, Creators, IPBaseNumber, trackName, WorkVersion} from './acum';
Expand Down Expand Up @@ -58,8 +60,35 @@ export async function importAlbum(
);
};

const noSelection = MB.relationshipEditor.state.selectedRecordings?.size === 0;

const selectedMediums = new Set(
noSelection
? MB.tree.iterate(MB.relationshipEditor.state.mediums)
: MB.tree
.iterate(MB.relationshipEditor.state.mediums)
.filter(([, recordingStateTree]) =>
MB.tree.iterate(recordingStateTree).some(recording => recording.isSelected)
)
);

switch (selectedMediums.size) {
case 0:
addWarning('select at least one recording');
return false;
case 1:
break;
default:
addWarning('select recordings only from a single medium');
return false;
}

const selectedRecordings = await lastValueFrom(
from(MB.tree.iterate(MB.relationshipEditor.state.mediums)).pipe(
of(head(selectedMediums.values())).pipe(
filter(
(mediumAndRecordings): mediumAndRecordings is [MediumWithRecordingsT, MediumRecordingStateTreeT] =>
mediumAndRecordings != null
),
mergeMap(([medium, recordingStateTree]) => {
return zip(
from(albumBean.tracks),
Expand All @@ -68,7 +97,7 @@ export async function importAlbum(
}),
filter((trackAndRecordingState): trackAndRecordingState is [WorkVersion, MediumRecordingStateT] => {
const [, recordingState] = trackAndRecordingState;
return recordingState != null && recordingState.isSelected;
return recordingState != null && (noSelection || recordingState.isSelected);
}),
toArray()
)
Expand Down
22 changes: 3 additions & 19 deletions src/acum-work-import/ui/release-editor-ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@ import {render} from 'solid-js/web';
import {Toolbox} from 'src/common/musicbrainz/toolbox';
import {importAlbum as tryImportWorks} from '../import-album';
import {submitWorks as trySubmitWorks} from '../submit';
import {SelectionStatus, validateSelection} from '../validate';
import {ImportForm} from './import-form';
import {ProgressBar} from './progressbar';
import {useWarnings, WarningsProvider} from './warnings';

function AcumImporter(props: {recordingCheckboxes: NodeListOf<HTMLInputElement>}) {
const [selectedRecordings, setSelectedRecordings] = createSignal(MB.relationshipEditor.state.selectedRecordings);
props.recordingCheckboxes.forEach(checkbox => {
checkbox.addEventListener('change', () => setSelectedRecordings(MB.relationshipEditor.state.selectedRecordings));
});
const selectionStatus = createMemo(() => validateSelection(selectedRecordings()));
function AcumImporter() {
const {addWarning, clearWarnings} = useWarnings();
const [worksPending, setWorksPending] = createSignal(false);
const [submitting, setSubmitting] = createSignal(false);
Expand All @@ -30,16 +24,6 @@ function AcumImporter(props: {recordingCheckboxes: NodeListOf<HTMLInputElement>}

async function importWorks(albumId: string) {
clearWarnings();
switch (selectionStatus()) {
case SelectionStatus.VALID:
break;
case SelectionStatus.NO_RECORDINGS:
addWarning('select at least one recording');
return;
case SelectionStatus.MULTIPLE_MEDIA:
addWarning('select recordings only from a single medium');
return;
}
try {
await tryImportWorks(albumId, addWarning, setProgress);
setWorksPending(true);
Expand Down Expand Up @@ -98,7 +82,7 @@ function AcumImporter(props: {recordingCheckboxes: NodeListOf<HTMLInputElement>}

export const releaseEditorContainerId = 'acum-release-editor-container';

export function createReleaseEditorUI(recordingCheckboxes: NodeListOf<HTMLInputElement>) {
export function createReleaseEditorUI() {
const container = (<div id={releaseEditorContainerId}></div>) as HTMLDivElement;
const theToolbox = Toolbox(document, 'full-page');
theToolbox.append(container);
Expand All @@ -107,7 +91,7 @@ export function createReleaseEditorUI(recordingCheckboxes: NodeListOf<HTMLInputE
render(
() => (
<WarningsProvider>
<AcumImporter recordingCheckboxes={recordingCheckboxes} />
<AcumImporter />
</WarningsProvider>
),
container
Expand Down
27 changes: 0 additions & 27 deletions src/acum-work-import/validate.ts

This file was deleted.

0 comments on commit da90d30

Please sign in to comment.