"use client"; import { ActionIcon, Avatar, Button, Divider, Group, HoverCard, Menu, Paper, ScrollArea, Stack, Text, Tooltip, UnstyledButton, } from "@mantine/core"; import { IconCheck, IconClipboard, IconCopy, IconFolderShare, IconTrash, IconX } from "@tabler/icons-react"; import { clientApi } from "@homarr/api/client"; import { useCurrentLayout, useRequiredBoard } from "@homarr/boards/context"; import { useEditMode } from "@homarr/boards/edit-mode"; import { getBoardLaneColumnCount, getRootSectionLane, getWidgetName } from "@homarr/definitions"; import { useI18n } from "@homarr/translation/client"; import { widgetCatalogIcons } from "@homarr/ui/widget-icons"; import type { EmptySection } from "~/app/[locale]/boards/_types"; import { useRegisteredGridEditors } from "~/components/board/sections/grid/grid-editor-registry"; import { useBoardSelection } from "./board-selection-context"; const MAX_AVATARS_DISPLAYED = 5; export const BoardSelectionToolbar = () => { const [isEditMode] = useEditMode(); const { selectedItemIds, clearSelection, copySelectedItems, pasteItems, removeSelectedItems, moveSelectedItemsToSection, } = useBoardSelection(); const board = useRequiredBoard(); const registeredGridEditors = useRegisteredGridEditors(); const currentLayoutId = useCurrentLayout(); const currentLayout = board.layouts.find((layout) => layout.id === currentLayoutId); const t = useI18n(); const tSelection = useI18n("item.selection"); const selectedItems = board.items.filter((item) => selectedItemIds.has(item.id)); const hasAppWidgets = selectedItems.some((item) => item.kind === "app"); const { data: selectableApps } = clientApi.app.selectable.useQuery(undefined, { enabled: hasAppWidgets, }); if (!isEditMode || selectedItemIds.size === 0) return null; const appMap = new Map((selectableApps ?? []).map((app) => [app.id, app])); // Sort items so apps appear first const sortedSelectedItems = selectedItems.toSorted((itemA, itemB) => { if (itemA.kind === "app" && itemB.kind !== "app") return -1; if (itemA.kind !== "app" && itemB.kind === "app") return 1; return 0; }); const getItemDetails = (item: (typeof selectedItems)[number]) => { let displayName = item.advancedOptions.title?.trim(); let iconUrl: string | undefined = undefined; const IconComponent = widgetCatalogIcons[item.kind] ?? IconCheck; if (item.kind === "app") { const appId = (item.options as { appId?: string })?.appId; const app = appId ? appMap.get(appId) : undefined; if (!displayName) { displayName = app?.name || getWidgetName("app", t); } if (app?.iconUrl) { iconUrl = app.iconUrl; } } if (!displayName) { displayName = getWidgetName(item.kind, t); } return { displayName, iconUrl, IconComponent, kind: item.kind }; }; const visibleItems = sortedSelectedItems.slice(0, MAX_AVATARS_DISPLAYED); const overflowCount = sortedSelectedItems.length - visibleItems.length; const mainCanvasSection = board.sections .filter( (section): section is EmptySection => section.kind === "empty" && getRootSectionLane(section.xOffset) === "main", ) .toSorted((sectionA, sectionB) => sectionA.yOffset - sectionB.yOffset) .at(0); const placementOptions = [ ...(mainCanvasSection ? [ { value: mainCanvasSection.id, label: t("item.create.destination.mainCanvas"), maxRowCount: null, }, ] : []), ...board.sections.flatMap((section) => { if (section.kind !== "empty") return []; const lane = getRootSectionLane(section.xOffset); if (lane === "main" || !currentLayout || getBoardLaneColumnCount(currentLayout, lane) === 0) return []; return [ { value: section.id, label: t(`item.create.destination.${lane === "left" ? "leftRail" : "rightRail"}`), maxRowCount: registeredGridEditors.get(section.id)?.placementMaxRowCount, }, ]; }), ...board.sections .filter((section) => section.kind === "container") .map((section) => ({ value: section.id, label: section.options.title.trim() || t("section.container.untitled"), maxRowCount: section.layouts.find((layout) => layout.layoutId === currentLayoutId)?.height, })), ]; return ( {/* Avatar Group with HoverCard Popover showing all items */} {visibleItems.map((item) => { const { displayName, iconUrl, IconComponent } = getItemDetails(item); return ( {!iconUrl && } ); })} {overflowCount > 0 && ( +{overflowCount} )} {tSelection("selected", { count: sortedSelectedItems.length })} {sortedSelectedItems.map((item) => { const { displayName, iconUrl, IconComponent } = getItemDetails(item); return ( {!iconUrl && } {displayName} ); })} {/* Actions */} {placementOptions.length > 1 && ( {tSelection("moveTo")} {placementOptions.map((opt) => ( moveSelectedItemsToSection(opt.value, opt.maxRowCount)} > {opt.label} ))} )} ); };