wip: fix stacked dynamic sections menu

This commit is contained in:
Meier Lukas committed 2024-09-25 21:39:56 +02:00
1 parent 78230b1fe9
commit 0386449ec1
3 files changed
+153 -14

No files matched your search

@@ -2,6 +2,7 @@ import { Box, Card } from "@mantine/core";
import type { DynamicSection } from "~/app/[locale]/boards/_types";
import { useRequiredBoard } from "~/app/[locale]/boards/(content)/_context";
import { DynamicSectionProvider } from "./dynamic/dynamic-context";
import { BoardDynamicSectionMenu } from "./dynamic/dynamic-menu";
import { GridStack } from "./gridstack/gridstack";
import classes from "./item.module.css";
@@ -12,6 +13,7 @@ interface Props {
export const BoardDynamicSection = ({ section }: Props) => {
const board = useRequiredBoard();
return (
<Box className="grid-stack-item-content">
<Card
@@ -27,7 +29,9 @@ export const BoardDynamicSection = ({ section }: Props) => {
}}
p={0}
>
<GridStack section={section} className="min-row" />
<DynamicSectionProvider section={section}>
<GridStack section={section} className="min-row" />
</DynamicSectionProvider>
</Card>
<BoardDynamicSectionMenu section={section} />
</Box>
@@ -0,0 +1,54 @@
"use client";
import type { PropsWithChildren } from "react";
import { createContext, useContext } from "react";
import type { DynamicSection } from "~/app/[locale]/boards/_types";
import { useRequiredBoard } from "~/app/[locale]/boards/(content)/_context";
interface DynamicSectionContextProps {
sectionIds: string[];
}
const DynamicSectionContext = createContext<DynamicSectionContextProps>({
sectionIds: [],
});
interface DynamicSectionProviderProps {
section: DynamicSection;
}
export const DynamicSectionProvider = ({ section, children }: PropsWithChildren<DynamicSectionProviderProps>) => {
const aboveIds = useAboveDynamicSectionIds();
const resetPrevious = usePlacedTopRightAndInDynamicSection({ sectionOrItem: section, previousId: aboveIds.at(-1) });
return (
<DynamicSectionContext.Provider value={{ sectionIds: resetPrevious ? [section.id] : [...aboveIds, section.id] }}>
{children}
</DynamicSectionContext.Provider>
);
};
export const useAboveDynamicSectionIds = () => {
return useContext(DynamicSectionContext).sectionIds;
};
interface UsePlacedTopRightAndInDynamicSectionProps {
sectionOrItem: { width: number; xOffset: number; yOffset: number };
previousId: string | undefined;
}
export const usePlacedTopRightAndInDynamicSection = ({
sectionOrItem,
previousId,
}: UsePlacedTopRightAndInDynamicSectionProps) => {
const board = useRequiredBoard();
const aboveSection = board.sections.find(
(section): section is DynamicSection => section.id === previousId && section.kind === "dynamic",
);
// When not on top, no above section or not reaching the end of the above section, we want to reset the previous sections
return (
sectionOrItem.yOffset >= 1 || !aboveSection || sectionOrItem.xOffset + sectionOrItem.width !== aboveSection.width
);
};
@@ -1,39 +1,120 @@
import { ActionIcon, Menu } from "@mantine/core";
import type { PropsWithChildren } from "react";
import { Fragment, useEffect } from "react";
import { ActionIcon, Group, Indicator, Menu, Popover, Stack, UnstyledButton } from "@mantine/core";
import { useHover } from "@mantine/hooks";
import { IconDotsVertical, IconTrash } from "@tabler/icons-react";
import { useConfirmModal } from "@homarr/modals";
import { useI18n, useScopedI18n } from "@homarr/translation/client";
import type { DynamicSection } from "~/app/[locale]/boards/_types";
import { useEditMode } from "~/app/[locale]/boards/(content)/_context";
import { useEditMode, useRequiredBoard } from "~/app/[locale]/boards/(content)/_context";
import { useDynamicSectionActions } from "./dynamic-actions";
import { useAboveDynamicSectionIds } from "./dynamic-context";
export const BoardDynamicSectionMenu = ({ section }: { section: DynamicSection }) => {
const [isEditMode] = useEditMode();
const aboveIds = useAboveDynamicSectionIds();
const board = useRequiredBoard();
const hasItemInTopRightCorner =
section.items.some((item) => item.xOffset + item.width === section.width && item.yOffset === 0) ||
board.sections.some(
(innerSection) =>
innerSection.kind === "dynamic" &&
innerSection.parentSectionId === section.id &&
innerSection.xOffset + innerSection.width == section.width &&
innerSection.yOffset == 0,
);
if (!isEditMode) return null;
if (aboveIds.length >= 1 && !hasItemInTopRightCorner) {
return (
<Popover width="auto" position="right" withArrow shadow="md">
<Popover.Target>
<UnstyledButton pos="absolute" top={4} right={4} style={{ zIndex: 10 }}>
<Indicator label={aboveIds.length + 1} styles={{ indicator: { height: "1rem" } }} offset={4}>
<ActionIcon component="div" variant="default" radius={"xl"}>
<IconDotsVertical size={"1rem"} />
</ActionIcon>
</Indicator>
</UnstyledButton>
</Popover.Target>
<Popover.Dropdown>
<Stack>
{[...aboveIds, section.id].map((id, index) => (
<Fragment key={id}>
<Group hiddenFrom="md"></Group>
<DesktopButton id={id} index={index} />
</Fragment>
))}
</Stack>
</Popover.Dropdown>
</Popover>
);
}
if (aboveIds.length >= 1 && !hasItemInTopRightCorner) {
return (
<InnerMenu id={section.id}>
<ActionIcon variant="default" radius={"xl"} pos="absolute" top={4} right={4} style={{ zIndex: 10 }}>
<IconDotsVertical size={"1rem"} />
</ActionIcon>
</InnerMenu>
);
}
return null;
};
interface DesktopButtonProps {
index: number;
id: string;
}
const DesktopButton = ({ index, id }: DesktopButtonProps) => {
const { hovered, ref } = useHover<HTMLButtonElement>();
useEffect(() => {
const sectionRef = document.querySelector<HTMLDivElement>(`[data-section-id="${id}"]`);
if (!sectionRef) return;
if (hovered) {
const previousBackground = sectionRef.style.backgroundColor;
sectionRef.style.backgroundColor = "rgba(255, 0, 0, 0.1)";
return () => {
sectionRef.style.backgroundColor = previousBackground;
};
}
}, [hovered]);
return (
<InnerMenu id={id} trigger="hover">
<UnstyledButton visibleFrom="md" ref={ref}>
Section {index + 1}
</UnstyledButton>
</InnerMenu>
);
};
const InnerMenu = ({ children, id, trigger }: PropsWithChildren<{ id: string; trigger?: "hover" }>) => {
const t = useI18n();
const tDynamic = useScopedI18n("section.dynamic");
const { removeDynamicSection } = useDynamicSectionActions();
const { openConfirmModal } = useConfirmModal();
const [isEditMode] = useEditMode();
if (!isEditMode) return null;
const openRemoveModal = () => {
openConfirmModal({
title: tDynamic("remove.title"),
children: tDynamic("remove.message"),
onConfirm: () => {
removeDynamicSection({ id: section.id });
removeDynamicSection({ id });
},
});
};
return (
<Menu withinPortal withArrow position="right-start" arrowPosition="center">
<Menu.Target>
<ActionIcon variant="default" radius={"xl"} pos="absolute" top={4} right={4} style={{ zIndex: 10 }}>
<IconDotsVertical size={"1rem"} />
</ActionIcon>
</Menu.Target>
<Menu withinPortal withArrow position="right-start" arrowPosition="center" trigger={trigger}>
<Menu.Target>{children}</Menu.Target>
<Menu.Dropdown miw={128}>
<Menu.Label c="red.6">{t("common.dangerZone")}</Menu.Label>
<Menu.Item c="red.6" leftSection={<IconTrash size={16} />} onClick={openRemoveModal}>