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([]); const [isPickerOpen, setIsPickerOpen] = useState(false); const modalRef = useRef(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 (
setIsPickerOpen(true)} aria-label="Open samples" aria-haspopup="dialog" aria-expanded={isPickerOpen} > Samples
handleSearchInput(event.detail.value)} onIonClear={() => handleSearchInput('')} /> {filteredCategories.length > 0 ? ( handleAccordionChange(event.detail.value)} > {filteredCategories.map(({ category, instruments }) => ( {category.label}
{instruments.map((instrument) => ( addInstrument(instrument.id)} > {instrument.label} {instrument.description} ))}
))}
) : (

No matching samples.

)}
); }