improvemtns again, to the archetype builder, and playback
This commit is contained in:
@@ -1,7 +1,20 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import api from "../../api/client";
|
||||
|
||||
export default function SelectBuiltMelodyModal({ open, melodyId, onClose, onSuccess }) {
|
||||
function computeStepsAndNotes(stepsStr) {
|
||||
if (!stepsStr || !stepsStr.trim()) return { steps: 0, totalNotes: 0 };
|
||||
const tokens = stepsStr.trim().split(",");
|
||||
const bellSet = new Set();
|
||||
for (const token of tokens) {
|
||||
for (const part of token.split("+")) {
|
||||
const n = parseInt(part.trim(), 10);
|
||||
if (!isNaN(n) && n >= 1 && n <= 16) bellSet.add(n);
|
||||
}
|
||||
}
|
||||
return { steps: tokens.length, totalNotes: bellSet.size || 1 };
|
||||
}
|
||||
|
||||
export default function SelectBuiltMelodyModal({ open, melodyId, currentMelody, onClose, onSuccess }) {
|
||||
const [melodies, setMelodies] = useState([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [assigning, setAssigning] = useState(null); // id of the one being assigned
|
||||
@@ -44,7 +57,27 @@ export default function SelectBuiltMelodyModal({ open, melodyId, onClose, onSucc
|
||||
// 3. Mark this built melody as assigned to this Firestore melody
|
||||
await api.post(`/builder/melodies/${builtMelody.id}/assign?firestore_melody_id=${melodyId}`);
|
||||
|
||||
onSuccess({ name: builtMelody.name, pid: builtMelody.pid });
|
||||
// 4. Update the melody's information with archetype_csv, steps, and totalNotes
|
||||
const csv = builtMelody.steps || "";
|
||||
const { steps, totalNotes } = computeStepsAndNotes(csv);
|
||||
if (currentMelody && csv) {
|
||||
const existingInfo = currentMelody.information || {};
|
||||
await api.put(`/melodies/${melodyId}`, {
|
||||
information: {
|
||||
...existingInfo,
|
||||
archetype_csv: csv,
|
||||
steps,
|
||||
totalNotes,
|
||||
},
|
||||
default_settings: currentMelody.default_settings,
|
||||
type: currentMelody.type,
|
||||
url: currentMelody.url,
|
||||
uid: currentMelody.uid,
|
||||
pid: currentMelody.pid,
|
||||
});
|
||||
}
|
||||
|
||||
onSuccess({ name: builtMelody.name, pid: builtMelody.pid, steps, totalNotes, archetype_csv: csv });
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user