Add app info and instrument controls
This commit is contained in:
1 parent
e23825cdb1
commit
9d8f277971
38 files changed
+4001
-257
No files matched your search
@@ -0,0 +1,97 @@
|
||||
import { useRef, useState } from 'react';
|
||||
import { IonButton, IonIcon, IonPopover, IonSelect, IonSelectOption } from '@ionic/react';
|
||||
import { add } from 'ionicons/icons';
|
||||
import {
|
||||
INSTRUMENT_BY_ID,
|
||||
INSTRUMENT_CATEGORIES,
|
||||
isInstrumentId,
|
||||
} from '../domain/instruments';
|
||||
import type { InstrumentId } from '../domain/instruments';
|
||||
import { usePopoverDisclosure } from '../hooks/usePopoverDisclosure';
|
||||
|
||||
interface InstrumentSelectorProps {
|
||||
onSelectInstrument: (instrumentId: InstrumentId) => void;
|
||||
}
|
||||
|
||||
export function InstrumentSelector({ onSelectInstrument }: InstrumentSelectorProps) {
|
||||
const [selectedCategoryId, setSelectedCategoryId] = useState('drums');
|
||||
const popoverRef = useRef<HTMLIonPopoverElement>(null);
|
||||
const popover = usePopoverDisclosure();
|
||||
const selectedCategory = INSTRUMENT_CATEGORIES.find(
|
||||
(category) => category.id === selectedCategoryId,
|
||||
) ?? INSTRUMENT_CATEGORIES[0];
|
||||
|
||||
function handleCategoryChange(value: unknown): void {
|
||||
if (typeof value !== 'string') return;
|
||||
const category = INSTRUMENT_CATEGORIES.find((candidate) => candidate.id === value);
|
||||
if (category) setSelectedCategoryId(category.id);
|
||||
}
|
||||
|
||||
function addInstrument(value: unknown): void {
|
||||
if (!isInstrumentId(value)) return;
|
||||
|
||||
void popoverRef.current?.dismiss();
|
||||
onSelectInstrument(value);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="instrument-selector-group">
|
||||
<IonSelect
|
||||
className="instrument-selector"
|
||||
fill="solid"
|
||||
interface="popover"
|
||||
aria-label="Choose an instrument category"
|
||||
value={selectedCategoryId}
|
||||
onIonChange={(event) => handleCategoryChange(event.detail.value)}
|
||||
>
|
||||
{INSTRUMENT_CATEGORIES.map((category) => (
|
||||
<IonSelectOption key={category.id} value={category.id}>
|
||||
{category.label}
|
||||
</IonSelectOption>
|
||||
))}
|
||||
</IonSelect>
|
||||
|
||||
<IonButton
|
||||
id="instrument-add-trigger"
|
||||
className="instrument-add-button"
|
||||
fill="solid"
|
||||
aria-label={`Show ${selectedCategory.label} instruments`}
|
||||
aria-haspopup="dialog"
|
||||
aria-expanded={popover.isOpen}
|
||||
>
|
||||
<IonIcon icon={add} />
|
||||
</IonButton>
|
||||
|
||||
<IonPopover
|
||||
ref={popoverRef}
|
||||
className="instrument-picker-popover"
|
||||
trigger="instrument-add-trigger"
|
||||
triggerAction="click"
|
||||
side="top"
|
||||
alignment="start"
|
||||
onDidPresent={popover.onDidPresent}
|
||||
onDidDismiss={popover.onDidDismiss}
|
||||
>
|
||||
<div className="instrument-picker-menu" role="group" aria-label={`${selectedCategory.label} instruments`}>
|
||||
{selectedCategory.instrumentIds.map((instrumentId) => {
|
||||
const instrument = INSTRUMENT_BY_ID[instrumentId];
|
||||
return (
|
||||
<IonButton
|
||||
className="instrument-picker-option"
|
||||
fill="clear"
|
||||
key={instrumentId}
|
||||
aria-label={`Add ${instrument.label}`}
|
||||
onClick={() => addInstrument(instrumentId)}
|
||||
>
|
||||
<span className="instrument-picker-option-copy">
|
||||
<span>{instrument.label}</span>
|
||||
<span>{instrument.description}</span>
|
||||
</span>
|
||||
</IonButton>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</IonPopover>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user