Skip to content

Commit 393fbb2

Browse files
committed
Improve search in manager
1 parent 822e39e commit 393fbb2

File tree

2 files changed

+80
-50
lines changed

2 files changed

+80
-50
lines changed

src/components/UI/modals/TuningPackManagerModal.jsx

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,11 @@ import { useLatest, useWindowSize } from "react-use";
44
import { findSystemByEdo, getSystemLabel } from "@/lib/theory/tuning";
55
import { memoWithPick } from "@/utils/memo";
66
import ModalFrame from "@/components/UI/modals/ModalFrame";
7-
8-
function normalizePack(pack) {
9-
if (!pack || typeof pack !== "object") return null;
10-
const edo = Number(pack?.system?.edo);
11-
const metaSystemId =
12-
typeof pack?.meta?.systemId === "string" ? pack.meta.systemId : null;
13-
const rawName = typeof pack?.name === "string" ? pack.name : "";
14-
const displayName = rawName.trim().length ? rawName : "Untitled pack";
15-
const strings = Array.isArray(pack?.tuning?.strings)
16-
? pack.tuning.strings
17-
: [];
18-
const stringsCount = strings.length;
19-
return {
20-
raw: pack,
21-
rawName,
22-
displayName,
23-
edo: Number.isFinite(edo) ? edo : null,
24-
metaSystemId,
25-
strings,
26-
stringsCount,
27-
};
28-
}
29-
30-
function formatStringsCount(stringsCount) {
31-
if (!Number.isFinite(stringsCount) || stringsCount <= 0) {
32-
return "Unknown string count";
33-
}
34-
return `${stringsCount} string${stringsCount === 1 ? "" : "s"}`;
35-
}
7+
import {
8+
formatStringsCount,
9+
normalizePack,
10+
packMatchesQuery,
11+
} from "@/components/UI/modals/tuningPackSearch";
3612

3713
function TuningPackManagerModal({
3814
isOpen,
@@ -57,7 +33,6 @@ function TuningPackManagerModal({
5733
const groups = useMemo(() => {
5834
if (!Array.isArray(tunings) || tunings.length === 0) return [];
5935

60-
const hasQuery = normalizedQuery.length > 0;
6136
const grouped = new Map();
6237

6338
tunings.forEach((entry) => {
@@ -80,20 +55,12 @@ function TuningPackManagerModal({
8055

8156
const formattedStringsCount = formatStringsCount(normalized.stringsCount);
8257

83-
const matchesQuery = !hasQuery
84-
? true
85-
: [
86-
normalized.displayName,
87-
normalized.rawName,
88-
String(normalized.stringsCount ?? ""),
89-
formattedStringsCount,
90-
systemLabel,
91-
].some(
92-
(value) =>
93-
typeof value === "string" &&
94-
value.toLowerCase().includes(normalizedQuery),
95-
);
96-
58+
const matchesQuery = packMatchesQuery({
59+
normalizedPack: normalized,
60+
systemLabel,
61+
normalizedQuery,
62+
formattedStringsCount,
63+
});
9764
if (!matchesQuery) {
9865
return;
9966
}
@@ -180,7 +147,7 @@ function TuningPackManagerModal({
180147
type="search"
181148
value={query}
182149
onChange={(event) => setQuery(event.target.value)}
183-
placeholder="Search by name, system, or string count"
150+
placeholder="Search by name, system, string count, or notes"
184151
aria-controls="tuning-pack-manager-sections"
185152
autoComplete="off"
186153
/>
@@ -211,10 +178,6 @@ function TuningPackManagerModal({
211178
</header>
212179
<ul className="tv-modal__manager-list">
213180
{group.packs.map((pack, index) => {
214-
const preview = pack.strings
215-
.map((string) => string?.note || string?.label || "•")
216-
.filter(Boolean)
217-
.join(" · ");
218181
return (
219182
<li
220183
key={`${pack.rawName || pack.displayName}__${index}`}
@@ -226,7 +189,9 @@ function TuningPackManagerModal({
226189
</span>
227190
<span className="tv-modal__manager-pack-preview">
228191
{formatStringsCount(pack.stringsCount)}
229-
{preview ? ` · ${preview}` : ""}
192+
{pack.stringPreview
193+
? ` · ${pack.stringPreview}`
194+
: ""}
230195
</span>
231196
</div>
232197
<div className="tv-modal__manager-actions">
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
export function buildStringPreview(strings = []) {
2+
if (!Array.isArray(strings)) return "";
3+
4+
return strings
5+
.map((string) => string?.note || string?.label || "")
6+
.filter(Boolean)
7+
.join(" · ");
8+
}
9+
10+
export function normalizePack(pack) {
11+
if (!pack || typeof pack !== "object") return null;
12+
const edo = Number(pack?.system?.edo);
13+
const metaSystemId =
14+
typeof pack?.meta?.systemId === "string" ? pack.meta.systemId : null;
15+
const rawName = typeof pack?.name === "string" ? pack.name : "";
16+
const displayName = rawName.trim().length ? rawName : "Untitled pack";
17+
const strings = Array.isArray(pack?.tuning?.strings)
18+
? pack.tuning.strings
19+
: [];
20+
const stringsCount = strings.length;
21+
const stringPreview = buildStringPreview(strings);
22+
return {
23+
raw: pack,
24+
rawName,
25+
displayName,
26+
edo: Number.isFinite(edo) ? edo : null,
27+
metaSystemId,
28+
strings,
29+
stringsCount,
30+
stringPreview,
31+
};
32+
}
33+
34+
export function formatStringsCount(stringsCount) {
35+
if (!Number.isFinite(stringsCount) || stringsCount <= 0) {
36+
return "Unknown string count";
37+
}
38+
return `${stringsCount} string${stringsCount === 1 ? "" : "s"}`;
39+
}
40+
41+
export function packMatchesQuery({
42+
normalizedPack,
43+
systemLabel,
44+
normalizedQuery,
45+
formattedStringsCount,
46+
}) {
47+
const searchString =
48+
typeof normalizedQuery === "string" ? normalizedQuery : "";
49+
const countLabel =
50+
formattedStringsCount ?? formatStringsCount(normalizedPack?.stringsCount);
51+
52+
if (searchString.length === 0) return true;
53+
54+
return [
55+
normalizedPack?.displayName,
56+
normalizedPack?.rawName,
57+
String(normalizedPack?.stringsCount ?? ""),
58+
countLabel,
59+
systemLabel,
60+
normalizedPack?.stringPreview,
61+
].some(
62+
(value) =>
63+
typeof value === "string" && value.toLowerCase().includes(searchString),
64+
);
65+
}

0 commit comments

Comments
 (0)