Restore sound-specific rings and refine studio UI
This commit is contained in:
1 parent
02eb8911e8
commit
e7b6c0bb3e
16 files changed
+318
-524
No files matched your search
@@ -1,50 +1,54 @@
|
||||
import { useCallback, useMemo, useState } from 'react';
|
||||
import { INSTRUMENT_BY_ID, INSTRUMENT_CATEGORIES } from '../domain/instruments';
|
||||
import { INSTRUMENT_BY_ID } from '../domain/instruments';
|
||||
import type { InstrumentCategoryId, InstrumentId } from '../domain/instruments';
|
||||
import { generateUniqueSoundColor } from '../domain/soundColors';
|
||||
|
||||
export interface PaletteCategory {
|
||||
id: InstrumentCategoryId;
|
||||
export interface PaletteSound {
|
||||
id: InstrumentId;
|
||||
color: string;
|
||||
}
|
||||
|
||||
export interface SoundPalette {
|
||||
activeCategories: PaletteCategory[];
|
||||
activeSounds: PaletteSound[];
|
||||
colorBySoundId: Partial<Record<InstrumentId, string>>;
|
||||
colorByCategoryId: Partial<Record<InstrumentCategoryId, string>>;
|
||||
activateSounds: (instrumentIds: readonly InstrumentId[]) => void;
|
||||
}
|
||||
|
||||
export function useSoundPalette(): SoundPalette {
|
||||
const [activeCategories, setActiveCategories] = useState<PaletteCategory[]>([]);
|
||||
const [activeSounds, setActiveSounds] = useState<PaletteSound[]>([]);
|
||||
|
||||
const activateSounds = useCallback((instrumentIds: readonly InstrumentId[]): void => {
|
||||
setActiveCategories((currentCategories) => {
|
||||
const nextCategories = [...currentCategories];
|
||||
const currentCount = nextCategories.length;
|
||||
const categoryIds = instrumentIds.map(
|
||||
(instrumentId) => INSTRUMENT_BY_ID[instrumentId].categoryId,
|
||||
);
|
||||
INSTRUMENT_CATEGORIES
|
||||
.filter((category) => categoryIds.includes(category.id))
|
||||
.forEach((category) => {
|
||||
if (nextCategories.some((active) => active.id === category.id)) return;
|
||||
setActiveSounds((currentSounds) => {
|
||||
const nextSounds = [...currentSounds];
|
||||
instrumentIds.forEach((instrumentId) => {
|
||||
if (nextSounds.some((sound) => sound.id === instrumentId)) return;
|
||||
|
||||
nextCategories.push({
|
||||
id: category.id,
|
||||
color: generateUniqueSoundColor(nextCategories.map((active) => active.color)),
|
||||
});
|
||||
nextSounds.push({
|
||||
id: instrumentId,
|
||||
color: generateUniqueSoundColor(nextSounds.map((sound) => sound.color)),
|
||||
});
|
||||
return nextCategories.length === currentCount ? currentCategories : nextCategories;
|
||||
});
|
||||
return nextSounds.length === currentSounds.length ? currentSounds : nextSounds;
|
||||
});
|
||||
}, []);
|
||||
|
||||
const colorByCategoryId = useMemo(() => {
|
||||
const colors: Partial<Record<InstrumentCategoryId, string>> = {};
|
||||
activeCategories.forEach((category) => {
|
||||
colors[category.id] = category.color;
|
||||
const colorBySoundId = useMemo(() => {
|
||||
const colors: Partial<Record<InstrumentId, string>> = {};
|
||||
activeSounds.forEach((sound) => {
|
||||
colors[sound.id] = sound.color;
|
||||
});
|
||||
return colors;
|
||||
}, [activeCategories]);
|
||||
}, [activeSounds]);
|
||||
|
||||
return { activeCategories, colorByCategoryId, activateSounds };
|
||||
const colorByCategoryId = useMemo(() => {
|
||||
const colors: Partial<Record<InstrumentCategoryId, string>> = {};
|
||||
activeSounds.forEach((sound) => {
|
||||
const categoryId = INSTRUMENT_BY_ID[sound.id].categoryId;
|
||||
if (colors[categoryId] === undefined) colors[categoryId] = sound.color;
|
||||
});
|
||||
return colors;
|
||||
}, [activeSounds]);
|
||||
|
||||
return { activeSounds, colorBySoundId, colorByCategoryId, activateSounds };
|
||||
}
|
||||
Reference in new issue
Block a user