Build BeatHound sequencer app
This commit is contained in:
1 parent
226ababcf1
commit
e23825cdb1
49 files changed
+6455
-2
No files matched your search
@@ -0,0 +1,44 @@
|
||||
import { useCallback, useMemo, useState } from 'react';
|
||||
import type { SoundId } from '../domain/sounds';
|
||||
import { generateUniqueSoundColor } from '../domain/soundColors';
|
||||
|
||||
export interface PaletteSound {
|
||||
id: SoundId;
|
||||
color: string;
|
||||
}
|
||||
|
||||
export interface SoundPalette {
|
||||
activeSounds: PaletteSound[];
|
||||
colorBySoundId: Partial<Record<SoundId, string>>;
|
||||
activateSounds: (soundIds: readonly SoundId[]) => void;
|
||||
}
|
||||
|
||||
export function useSoundPalette(): SoundPalette {
|
||||
const [activeSounds, setActiveSounds] = useState<PaletteSound[]>([]);
|
||||
|
||||
const activateSounds = useCallback((soundIds: readonly SoundId[]): void => {
|
||||
setActiveSounds((currentSounds) => {
|
||||
const nextSounds = [...currentSounds];
|
||||
const currentCount = nextSounds.length;
|
||||
soundIds.forEach((soundId) => {
|
||||
if (nextSounds.some((sound) => sound.id === soundId)) return;
|
||||
|
||||
nextSounds.push({
|
||||
id: soundId,
|
||||
color: generateUniqueSoundColor(nextSounds.map((sound) => sound.color)),
|
||||
});
|
||||
});
|
||||
return nextSounds.length === currentCount ? currentSounds : nextSounds;
|
||||
});
|
||||
}, []);
|
||||
|
||||
const colorBySoundId = useMemo(() => {
|
||||
const colors: Partial<Record<SoundId, string>> = {};
|
||||
activeSounds.forEach((sound) => {
|
||||
colors[sound.id] = sound.color;
|
||||
});
|
||||
return colors;
|
||||
}, [activeSounds]);
|
||||
|
||||
return { activeSounds, colorBySoundId, activateSounds };
|
||||
}
|
||||
Reference in new issue
Block a user