Files
beat-hound/src/components/InstrumentSelector.tsx
T

180 lines
5.9 KiB
TypeScript

import { useMemo, useRef, useState } from 'react';
import {
IonAccordion,
IonAccordionGroup,
IonButton,
IonContent,
IonIcon,
IonItem,
IonLabel,
IonModal,
IonSearchbar,
} from '@ionic/react';
import { add } from 'ionicons/icons';
import {
INSTRUMENT_BY_ID,
INSTRUMENT_CATEGORIES,
isInstrumentId,
} from '../domain/instruments';
import type { InstrumentId } from '../domain/instruments';
const PICKER_SHEET_BREAKPOINTS = [0, 0.82, 1];
interface InstrumentSelectorProps {
onSelectInstrument: (instrumentId: InstrumentId) => void;
}
export function InstrumentSelector({ onSelectInstrument }: InstrumentSelectorProps) {
const [searchQuery, setSearchQuery] = useState('');
const [expandedCategoryIds, setExpandedCategoryIds] = useState<string[]>([]);
const [isPickerOpen, setIsPickerOpen] = useState(false);
const modalRef = useRef<HTMLIonModalElement>(null);
const filteredCategories = useMemo(() => {
const query = searchQuery.trim().toLowerCase();
return INSTRUMENT_CATEGORIES.flatMap((category) => {
const categoryMatches = category.label.toLowerCase().includes(query);
const instruments = category.instrumentIds
.map((instrumentId) => INSTRUMENT_BY_ID[instrumentId])
.filter((instrument) => (
!query
|| categoryMatches
|| `${instrument.label} ${instrument.description}`.toLowerCase().includes(query)
));
return instruments.length > 0 ? [{ category, instruments }] : [];
});
}, [searchQuery]);
function handleSearchInput(value: string | null | undefined): void {
const nextQuery = value ?? '';
const normalizedQuery = nextQuery.trim().toLowerCase();
setSearchQuery(nextQuery);
if (!normalizedQuery) {
setExpandedCategoryIds([]);
return;
}
setExpandedCategoryIds(
INSTRUMENT_CATEGORIES
.filter((category) => (
category.label.toLowerCase().includes(normalizedQuery)
|| category.instrumentIds.some((instrumentId) => {
const instrument = INSTRUMENT_BY_ID[instrumentId];
return `${instrument.label} ${instrument.description}`
.toLowerCase()
.includes(normalizedQuery);
})
))
.map((category) => category.id),
);
}
function handleAccordionChange(value: unknown): void {
if (Array.isArray(value)) {
setExpandedCategoryIds(value.filter((categoryId): categoryId is string => (
typeof categoryId === 'string'
)));
return;
}
setExpandedCategoryIds(typeof value === 'string' ? [value] : []);
}
function handlePickerDidDismiss(): void {
setIsPickerOpen(false);
setSearchQuery('');
setExpandedCategoryIds([]);
}
function addInstrument(value: unknown): void {
if (!isInstrumentId(value)) return;
void modalRef.current?.dismiss();
onSelectInstrument(value);
}
return (
<div className="instrument-selector-group">
<IonButton
id="instrument-add-trigger"
className="instrument-add-button"
fill="solid"
onClick={() => setIsPickerOpen(true)}
aria-label="Open samples"
aria-haspopup="dialog"
aria-expanded={isPickerOpen}
>
<IonIcon icon={add} slot="start" />
Samples
</IonButton>
<IonModal
ref={modalRef}
className="instrument-picker-modal"
isOpen={isPickerOpen}
breakpoints={PICKER_SHEET_BREAKPOINTS}
initialBreakpoint={1}
expandToScroll={false}
handle
onDidDismiss={handlePickerDidDismiss}
>
<IonContent className="instrument-picker-content" scrollY={false}>
<div className="instrument-picker-menu">
<IonSearchbar
className="instrument-picker-search"
aria-label="Search samples"
placeholder="Search samples"
value={searchQuery}
debounce={0}
showClearButton="always"
onIonInput={(event) => handleSearchInput(event.detail.value)}
onIonClear={() => handleSearchInput('')}
/>
{filteredCategories.length > 0 ? (
<IonAccordionGroup
className="instrument-picker-accordion-group"
multiple
value={expandedCategoryIds}
onIonChange={(event) => handleAccordionChange(event.detail.value)}
>
{filteredCategories.map(({ category, instruments }) => (
<IonAccordion
className="instrument-picker-category"
key={category.id}
value={category.id}
>
<IonItem className="instrument-category-header" slot="header" lines="none">
<IonLabel>{category.label}</IonLabel>
</IonItem>
<div className="instrument-category-content" slot="content">
{instruments.map((instrument) => (
<IonButton
className="instrument-picker-option"
fill="solid"
key={instrument.id}
aria-label={`Add ${instrument.label}`}
onClick={() => addInstrument(instrument.id)}
>
<span className="instrument-picker-option-copy">
<span>{instrument.label}</span>
<span>{instrument.description}</span>
</span>
</IonButton>
))}
</div>
</IonAccordion>
))}
</IonAccordionGroup>
) : (
<p className="instrument-picker-empty" role="status">No matching samples.</p>
)}
</div>
</IonContent>
</IonModal>
</div>
);
}