import { Dispatch, FC, SetStateAction, useCallback, useMemo, useState } from 'react'; import { Base, Grid, Flex, NitroCardView, NitroCardHeaderView, NitroCardTabsView, NitroCardTabsItemView, NitroCardContentView, Text } from '../../common'; import { useRoom } from '../../hooks'; import { GetConfigurationValue } from '../../api'; interface ItemData { id: number; } interface BackgroundsViewProps { setIsVisible: Dispatch>; selectedBackground: number; setSelectedBackground: Dispatch>; selectedStand: number; setSelectedStand: Dispatch>; selectedOverlay: number; setSelectedOverlay: Dispatch>; selectedCardBackground: number; setSelectedCardBackground: Dispatch>; } const TABS = ['backgrounds', 'stands', 'overlays', 'cards'] as const; type TabType = typeof TABS[number]; export const BackgroundsView: FC = ({ setIsVisible, selectedBackground, setSelectedBackground, selectedStand, setSelectedStand, selectedOverlay, setSelectedOverlay, selectedCardBackground, setSelectedCardBackground }) => { const [activeTab, setActiveTab] = useState('backgrounds'); const { roomSession } = useRoom(); const processData = useCallback((configData: any[], idField: string): ItemData[] => { if (!configData?.length) return []; return configData.map(item => ({ id: item[idField] })); }, []); const allData = useMemo(() => ({ backgrounds: processData(GetConfigurationValue('backgrounds.data'), 'backgroundId'), stands: processData(GetConfigurationValue('stands.data'), 'standId'), overlays: processData(GetConfigurationValue('overlays.data'), 'overlayId'), cards: processData(GetConfigurationValue('cards.data') || GetConfigurationValue('backgrounds.data'), 'backgroundId') }), [processData]); const handleSelection = useCallback((id: number) => { if (!roomSession) return; const setters = { backgrounds: setSelectedBackground, stands: setSelectedStand, overlays: setSelectedOverlay, cards: setSelectedCardBackground }; const currentValues = { backgrounds: selectedBackground, stands: selectedStand, overlays: selectedOverlay, cards: selectedCardBackground }; setters[activeTab](id); const newValues = { ...currentValues, [activeTab]: id }; roomSession.sendBackgroundMessage( newValues.backgrounds, newValues.stands, newValues.overlays, newValues.cards ); }, [activeTab, roomSession, selectedBackground, selectedStand, selectedOverlay, selectedCardBackground, setSelectedBackground, setSelectedStand, setSelectedOverlay, setSelectedCardBackground]); const renderItem = useCallback((item: ItemData, type: string) => ( handleSelection(item.id)} > ), [handleSelection]); return ( setIsVisible(false)} /> {TABS.map(tab => ( setActiveTab(tab)} > {tab.charAt(0).toUpperCase() + tab.slice(1)} ))} Select an Option {allData[activeTab].map(item => renderItem(item, activeTab === 'cards' ? 'card-background' : activeTab.slice(0, -1)))} ); };