Skip to content

Commit 1346c62

Browse files
committed
Further improve deletion after recent changes
1 parent 2b15409 commit 1346c62

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

src/components/UI/modals/TuningPackManagerModal.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function TuningPackManagerModal({
112112
const handleDelete = useCallback(
113113
(pack) => {
114114
if (!pack) return;
115-
onDeleteRef.current?.(pack);
115+
onDeleteRef.current?.(pack.raw ?? pack, pack.displayName);
116116
},
117117
[onDeleteRef],
118118
);
@@ -181,9 +181,11 @@ function TuningPackManagerModal({
181181
</header>
182182
<ul className="tv-modal__manager-list">
183183
{group.packs.map((pack, index) => {
184+
const packKey =
185+
pack?.raw?.meta?.id || pack.rawName || pack.displayName;
184186
return (
185187
<li
186-
key={`${pack.rawName || pack.displayName}__${index}`}
188+
key={`${packKey || "pack"}__${index}`}
187189
className="tv-modal__manager-item"
188190
>
189191
<div className="tv-modal__manager-pack">
@@ -201,7 +203,7 @@ function TuningPackManagerModal({
201203
<button
202204
type="button"
203205
className="tv-button tv-button--icon tv-button--ghost tv-button--accent"
204-
onClick={() => handleEdit(pack.raw)}
206+
onClick={() => handleEdit(pack)}
205207
aria-label={`Edit ${pack.displayName}`}
206208
title="Edit"
207209
>

src/hooks/useCustomTuningPacks.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ import { useToggle, useLatest, useMountedState } from "react-use";
33
import { slug } from "@/lib/export/scales";
44
import { withToastPromise } from "@/utils/toast";
55

6-
function resolvePackLabel(target) {
6+
function resolvePackLabel(target, fallbackDisplayName = "") {
77
if (target && typeof target === "object") {
88
const name = typeof target?.name === "string" ? target.name.trim() : "";
9+
const displayNameFallback =
10+
typeof fallbackDisplayName === "string"
11+
? fallbackDisplayName.trim()
12+
: "";
913
const displayName =
1014
typeof target?.displayName === "string"
1115
? target.displayName.trim()
12-
: name
13-
? ""
14-
: "Untitled pack";
15-
return name || displayName;
16+
: displayNameFallback || (name ? "" : "Untitled pack");
17+
return name || displayName || displayNameFallback;
1618
}
1719

1820
if (typeof target === "string") {
@@ -125,13 +127,14 @@ export function useCustomTuningPacks({
125127
);
126128

127129
const deletePack = useCallback(
128-
async (target) => {
130+
async (target, fallbackDisplayName = "") => {
129131
if (typeof deleteCustomTuningRef.current !== "function") return false;
130132
if (typeof target === "string" && !target.trim()) return false;
131133
if (!target) return false;
132134

133-
const label = resolvePackLabel(target) || "this tuning pack";
134-
const key = slug(resolvePackKey(target));
135+
const label =
136+
resolvePackLabel(target, fallbackDisplayName) || "this tuning pack";
137+
const key = slug(resolvePackKey(target));
135138

136139
let ok = true;
137140
if (typeof confirmRef.current === "function") {

src/hooks/useTuningIO.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,10 @@ export function useTuningIO({ systemId, strings, TUNINGS }) {
347347
const deleteCustomTuning = useCallback(
348348
(identifier) => {
349349
if (!identifier) return;
350-
const existing = getExistingCustomTunings();
351-
const filtered = removePackByIdentifier(existing, identifier);
350+
const existing = getExistingCustomTunings().map(ensurePackHasId);
351+
const normalizedIdentifier = ensurePackHasId(identifier);
352+
353+
const filtered = removePackByIdentifier(existing, normalizedIdentifier);
352354
setCustomTunings(filtered);
353355
},
354356
[getExistingCustomTunings, setCustomTunings],

src/tests/tuningIO.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,23 @@ test("removePackByIdentifier removes only the targeted duplicate-named pack", ()
167167
["pack-1", "pack-3"],
168168
);
169169
});
170+
171+
test(
172+
"removePackByIdentifier deletes the correct pack when names are empty or duplicated",
173+
() => {
174+
const packs = [
175+
{ name: "", meta: { id: "pack-a" } },
176+
{ name: "Duplicate", meta: { id: "pack-b" } },
177+
{ name: "Duplicate", meta: { id: "pack-c" } },
178+
{ name: "", meta: { id: "pack-d" } },
179+
];
180+
181+
const result = removePackByIdentifier(packs, { meta: { id: "pack-c" } });
182+
183+
assert.equal(result.length, 3);
184+
assert.deepEqual(
185+
result.map((pack) => pack.meta.id),
186+
["pack-a", "pack-b", "pack-d"],
187+
);
188+
},
189+
);

0 commit comments

Comments
 (0)