import React, { useState } from 'react'; import { Accordion, Grid, Paper, Stack, useMantineColorScheme } from '@mantine/core'; import { closestCenter, DndContext, DragOverlay, MouseSensor, TouchSensor, useSensor, useSensors, } from '@dnd-kit/core'; import { arrayMove, SortableContext } from '@dnd-kit/sortable'; import { useLocalStorage } from '@mantine/hooks'; import { useTranslation } from 'next-i18next'; import { useConfig } from '../../tools/state'; import { SortableAppShelfItem, AppShelfItem } from './AppShelfItem'; import { ModuleMenu, ModuleWrapper } from '../../modules/moduleWrapper'; import { DownloadsModule } from '../../modules'; import DownloadComponent from '../../modules/downloads/DownloadsModule'; const AppShelf = (props: any) => { const { config, setConfig } = useConfig(); // Extract all the categories from the services in config const categoryList = config.services.reduce((acc, cur) => { if (cur.category && !acc.includes(cur.category)) { acc.push(cur.category); } return acc; }, [] as string[]); const [toggledCategories, setToggledCategories] = useLocalStorage({ key: 'app-shelf-toggled', // This is a bit of a hack to toggle the categories on the first load, return a string[] of the categories defaultValue: categoryList, }); const [activeId, setActiveId] = useState(null); const { colorScheme } = useMantineColorScheme(); const { t } = useTranslation('layout/app-shelf'); const sensors = useSensors( useSensor(TouchSensor, { activationConstraint: { delay: 500, tolerance: 5, }, }), useSensor(MouseSensor, { // Require the mouse to move by 10 pixels before activating activationConstraint: { delay: 500, tolerance: 5, }, }) ); function handleDragStart(event: any) { const { active } = event; setActiveId(active.id); } function handleDragEnd(event: any) { const { active, over } = event; if (active.id !== over.id) { const newConfig = { ...config }; const activeIndex = newConfig.services.findIndex((e) => e.id === active.id); const overIndex = newConfig.services.findIndex((e) => e.id === over.id); newConfig.services = arrayMove(newConfig.services, activeIndex, overIndex); setConfig(newConfig); } setActiveId(null); } const getItems = (filter?: string) => { // If filter is not set, return all the services without a category or a null category let filtered = config.services; if (!filter) { filtered = config.services.filter((e) => !e.category || e.category === null); } if (filter) { filtered = config.services.filter((e) => e.category === filter); } return ( {filtered.map((service) => ( ))} {activeId ? ( e.id === activeId)} id={activeId} /> ) : null} ); }; if (categoryList.length > 0) { const noCategory = config.services.filter( (e) => e.category === undefined || e.category === null ); const downloadEnabled = config.modules?.[DownloadsModule.id]?.enabled ?? false; // Create an item with 0: true, 1: true, 2: true... For each category return ( // TODO: Style accordion so that the bar is transparent to the user settings { setToggledCategories([...state]); }} > {categoryList.map((category, idx) => ( {category} {getItems(category)} ))} {/* Return the item for all services without category */} {noCategory && noCategory.length > 0 ? ( {t('accordions.others.text')} {getItems()} ) : null} {downloadEnabled ? ( {t('accordions.downloads.text')} ) : null} ); } return ( {getItems()} ); }; export default AppShelf;