import { useCallback, useMemo } from "react"; import { Grid, NumberInput, Select, Stack } from "@mantine/core"; import { z } from "zod/v4"; import { useCurrentLayout, useRequiredBoard } from "@homarr/boards/context"; import { getRootSectionLane } from "@homarr/definitions"; import { useZodForm } from "@homarr/form"; import { createModal, ModalFormFooter, modalSizeForm, useModalAction } from "@homarr/modals"; import { useI18n } from "@homarr/translation/client"; import type { Board, ContainerSectionItem, SectionItem } from "~/app/[locale]/boards/_types"; import { getLayoutRowCount } from "../layout"; import { resolvePinnedGridCollisions } from "../sections/grid/dnd"; import type { RegisteredGridEditor } from "../sections/grid/grid-editor-registry"; import { useRegisteredGridEditors } from "../sections/grid/grid-editor-registry"; import { getSectionGridColumnCount, getSectionGridPlacements } from "../sections/grid/section-grid-placements"; import type { CommitSectionGridInput, SectionGridPlacement } from "../sections/grid/use-grid-layout-actions"; import { useGridLayoutActions } from "../sections/grid/use-grid-layout-actions"; export type MovableEntry = Pick< SectionItem | ContainerSectionItem, "id" | "type" | "width" | "height" | "xOffset" | "yOffset" >; export interface OpenItemMoveModalInput { entry: MovableEntry; sourceSectionId: string; } interface InnerProps extends OpenItemMoveModalInput { board: Board; currentLayoutId: string; commitSectionGrids: (snapshots: readonly CommitSectionGridInput[]) => void; registeredGridEditors: ReadonlyMap>; } interface MoveTarget { id: string; label: string; columnCount: number; maxRowCount: number | null; } interface MoveTargetLabels { canvas: string; container: string; leftRail: string; rightRail: string; numbered: (name: string, index: number) => string; located: (name: string, location: string, index: number) => string; } type MoveTargetCandidate = Omit & { name: string; location?: string; }; export const ItemMoveModal = createModal(({ actions, innerProps }) => { const tLandmark = useI18n("board.landmark"); const tContainer = useI18n("section.container"); const tMoveResize = useI18n("item.moveResize"); const { board, commitSectionGrids, currentLayoutId, entry, registeredGridEditors, sourceSectionId } = innerProps; const minimumSize = useMemo( () => getEntryMinimumSize(board, currentLayoutId, entry), [board, currentLayoutId, entry], ); const targets = getMoveTargets( board, currentLayoutId, entry, { canvas: tLandmark("canvas"), container: tContainer("action.create"), leftRail: tLandmark("leftRail"), rightRail: tLandmark("rightRail"), numbered: (name, index) => tMoveResize("target.numbered", { name, index: String(index) }), located: (name, location, index) => tMoveResize("target.located", { name, location, index: String(index), }), }, registeredGridEditors, ); const targetById = useMemo(() => new Map(targets.map((target) => [target.id, target])), [targets]); const initialTarget = targetById.get(sourceSectionId) ?? targets[0]; if (!initialTarget) { throw new Error("No valid dashboard section is available for this item"); } const form = useZodForm( z.object({ sectionId: z.string().min(1), xOffset: z.number().int().min(0).max(32767), yOffset: z.number().int().min(0).max(32767), width: z.number().int().min(1).max(32767), height: z.number().int().min(1).max(32767), }), { initialValues: { sectionId: initialTarget.id, xOffset: entry.xOffset, yOffset: entry.yOffset, width: entry.width, height: entry.height, }, }, ); const selectedTarget = targetById.get(form.values.sectionId) ?? initialTarget; const handleSubmit = useCallback( (values: { sectionId: string; xOffset: number; yOffset: number; width: number; height: number }) => { const target = targetById.get(values.sectionId); if (!target) return; const width = Math.max(minimumSize.width, Math.min(values.width, target.columnCount)); const height = Math.max(minimumSize.height, values.height); const nextPlacement: SectionGridPlacement = { id: entry.id, type: entry.type, x: Math.min(values.xOffset, target.columnCount - width), y: values.yOffset, w: width, h: height, }; const targetPlacements = getSectionGridPlacements(board, currentLayoutId, target.id).filter( (placement) => placement.id !== entry.id, ); const resolvedTarget = resolvePinnedGridCollisions( [...targetPlacements, nextPlacement], nextPlacement, target.columnCount, ); if (target.maxRowCount !== null && getLayoutRowCount(resolvedTarget) > target.maxRowCount) { form.setFieldError("height", tMoveResize("keyboard.boundary")); return; } const snapshots = sourceSectionId === target.id ? [{ layoutId: currentLayoutId, sectionId: target.id, placements: resolvedTarget }] : [ { layoutId: currentLayoutId, sectionId: sourceSectionId, placements: getSectionGridPlacements(board, currentLayoutId, sourceSectionId).filter( (placement) => placement.id !== entry.id, ), }, { layoutId: currentLayoutId, sectionId: target.id, placements: resolvedTarget }, ]; commitSectionGrids(snapshots); actions.closeModal(); focusMovedEntry(entry.id, target.id); }, [ actions, board, commitSectionGrids, currentLayoutId, entry, form, minimumSize, sourceSectionId, targetById, tMoveResize, ], ); return (