import { Box, Center, Image } from "@mantine/core"; import { IconLayoutGrid } from "@tabler/icons-react"; import type { BoardPreviewData } from "@homarr/boards/layout-preview"; import { projectBoardLayout } from "@homarr/boards/layout-preview"; import { boardLanes, getBoardLaneColumnCount, getRootSectionLane } from "@homarr/definitions"; import { widgetCatalogIcons } from "@homarr/ui/widget-icons"; import classes from "./board-layout-thumbnail.module.css"; interface BoardLayoutThumbnailProps { preview: BoardPreviewData | null; label: string; previewRowLimit?: number; className?: string; } const compactBoardLayoutThumbnailRows = 12; export const maxBoardLayoutThumbnailRows = 48; export const BoardLayoutThumbnail = ({ preview, label, previewRowLimit = compactBoardLayoutThumbnailRows, className, }: BoardLayoutThumbnailProps) => { const canvasClassName = [classes.canvas, className].filter(Boolean).join(" "); const layout = preview?.layouts.find((candidate) => candidate.role === "base") ?? preview?.layouts.at(0); if (!preview || !layout) { return (
); } const elements = projectBoardLayout(preview, layout, layout); const thumbnailRowLimit = Math.max(1, Math.min(Math.floor(previewRowLimit), maxBoardLayoutThumbnailRows)); const roots = preview.sections .filter((section) => section.kind === "empty") .toSorted((first, second) => (first.xOffset ?? 0) - (second.xOffset ?? 0) || first.id.localeCompare(second.id)); const lanes = boardLanes.flatMap((lane) => { const root = roots.find((section) => getRootSectionLane(section.xOffset) === lane); const columnCount = getBoardLaneColumnCount(layout, lane); return root && columnCount > 0 ? [{ lane, root, columnCount }] : []; }); if (lanes.length === 0) { return (
); } const renderItemIcon = (itemId: string) => { const item = preview.items.find((candidate) => candidate.id === itemId); const WidgetIcon = item?.kind ? widgetCatalogIcons[item.kind] : undefined; return item?.iconUrl ? ( ) : WidgetIcon ? ( ) : null; }; return (
`${columnCount}fr`).join(" ") }} > {lanes.map(({ lane, root, columnCount }) => { const laneElements = elements.filter( (element) => element.sectionId === root.id && element.xOffset >= 0 && element.xOffset < columnCount && element.yOffset >= 0 && element.yOffset < thumbnailRowLimit && element.width > 0 && element.height > 0, ); const rowCount = Math.min( thumbnailRowLimit, Math.max(4, ...laneElements.map((element) => element.yOffset + element.height)), ); return (
{laneElements.map((element) => { const renderedWidth = Math.min(element.width, columnCount - element.xOffset); const renderedHeight = Math.min(element.height, rowCount - element.yOffset); const nestedItems = element.type === "section" ? elements.filter( (candidate) => candidate.type === "item" && candidate.sectionId === element.id && candidate.xOffset >= 0 && candidate.xOffset < renderedWidth && candidate.yOffset >= 0 && candidate.yOffset < renderedHeight && candidate.width > 0 && candidate.height > 0, ) : []; return ( {element.type === "section" ? ( {nestedItems.map((nestedItem) => ( {renderItemIcon(nestedItem.id)} ))} ) : ( renderItemIcon(element.id) )} ); })}
); })}
); };