mirror of
https://github.com/docmost/docmost.git
synced 2026-05-17 23:14:07 +08:00
d1dc6977ab
* lock/unlock pages * remove using isLocked column - add default page edit state preference * * Move state management to editors (avoids flickers on edit mode switch) * Rename variables * Add strings to translation file * Memoize components in page component * Fix title editor sending update request on editable state change * fixed errors merging main * Fix embed view in read-only mode * remove unused line * sync * fix responsiveness on mobile --------- Co-authored-by: Philipinho <16838612+Philipinho@users.noreply.github.com>
173 lines
4.9 KiB
TypeScript
173 lines
4.9 KiB
TypeScript
import { useAtomValue } from "jotai";
|
|
import { treeDataAtom } from "@/features/page/tree/atoms/tree-data-atom.ts";
|
|
import React, { useCallback, useEffect, useState } from "react";
|
|
import { findBreadcrumbPath } from "@/features/page/tree/utils";
|
|
import {
|
|
Button,
|
|
Anchor,
|
|
Popover,
|
|
Breadcrumbs,
|
|
ActionIcon,
|
|
Text,
|
|
Tooltip,
|
|
} from "@mantine/core";
|
|
import { IconCornerDownRightDouble, IconDots } from "@tabler/icons-react";
|
|
import { Link, useParams } from "react-router-dom";
|
|
import classes from "./breadcrumb.module.css";
|
|
import { SpaceTreeNode } from "@/features/page/tree/types.ts";
|
|
import { buildPageUrl } from "@/features/page/page.utils.ts";
|
|
import { usePageQuery } from "@/features/page/queries/page-query.ts";
|
|
import { extractPageSlugId } from "@/lib";
|
|
import { useMediaQuery } from "@mantine/hooks";
|
|
|
|
function getTitle(name: string, icon: string) {
|
|
if (icon) {
|
|
return `${icon} ${name}`;
|
|
}
|
|
return name;
|
|
}
|
|
|
|
export default function Breadcrumb() {
|
|
const treeData = useAtomValue(treeDataAtom);
|
|
const [breadcrumbNodes, setBreadcrumbNodes] = useState<
|
|
SpaceTreeNode[] | null
|
|
>(null);
|
|
const { pageSlug, spaceSlug } = useParams();
|
|
const { data: currentPage } = usePageQuery({
|
|
pageId: extractPageSlugId(pageSlug),
|
|
});
|
|
const isMobile = useMediaQuery("(max-width: 48em)");
|
|
|
|
useEffect(() => {
|
|
if (treeData?.length > 0 && currentPage) {
|
|
const breadcrumb = findBreadcrumbPath(treeData, currentPage.id);
|
|
setBreadcrumbNodes(breadcrumb || null);
|
|
}
|
|
}, [currentPage?.id, treeData]);
|
|
|
|
const HiddenNodesTooltipContent = () =>
|
|
breadcrumbNodes?.slice(1, -1).map((node) => (
|
|
<Button.Group orientation="vertical" key={node.id}>
|
|
<Button
|
|
justify="start"
|
|
component={Link}
|
|
to={buildPageUrl(spaceSlug, node.slugId, node.name)}
|
|
variant="default"
|
|
style={{ border: "none" }}
|
|
>
|
|
<Text fz={"sm"} className={classes.truncatedText}>
|
|
{getTitle(node.name, node.icon)}
|
|
</Text>
|
|
</Button>
|
|
</Button.Group>
|
|
));
|
|
|
|
const MobileHiddenNodesTooltipContent = () =>
|
|
breadcrumbNodes?.map((node) => (
|
|
<Button.Group orientation="vertical" key={node.id}>
|
|
<Button
|
|
justify="start"
|
|
component={Link}
|
|
to={buildPageUrl(spaceSlug, node.slugId, node.name)}
|
|
variant="default"
|
|
style={{ border: "none" }}
|
|
>
|
|
<Text fz={"sm"} className={classes.truncatedText}>
|
|
{getTitle(node.name, node.icon)}
|
|
</Text>
|
|
</Button>
|
|
</Button.Group>
|
|
));
|
|
|
|
const renderAnchor = useCallback(
|
|
(node: SpaceTreeNode) => (
|
|
<Tooltip label={node.name} key={node.id}>
|
|
<Anchor
|
|
component={Link}
|
|
to={buildPageUrl(spaceSlug, node.slugId, node.name)}
|
|
underline="never"
|
|
fz="sm"
|
|
key={node.id}
|
|
className={classes.truncatedText}
|
|
>
|
|
{getTitle(node.name, node.icon)}
|
|
</Anchor>
|
|
</Tooltip>
|
|
),
|
|
[spaceSlug],
|
|
);
|
|
|
|
const getBreadcrumbItems = () => {
|
|
if (!breadcrumbNodes) return [];
|
|
|
|
if (breadcrumbNodes.length > 3) {
|
|
const firstNode = breadcrumbNodes[0];
|
|
//const secondLastNode = breadcrumbNodes[breadcrumbNodes.length - 2];
|
|
const lastNode = breadcrumbNodes[breadcrumbNodes.length - 1];
|
|
|
|
return [
|
|
renderAnchor(firstNode),
|
|
<Popover
|
|
width={250}
|
|
position="bottom"
|
|
withArrow
|
|
shadow="xl"
|
|
key="hidden-nodes"
|
|
>
|
|
<Popover.Target>
|
|
<ActionIcon color="gray" variant="transparent">
|
|
<IconDots size={20} stroke={2} />
|
|
</ActionIcon>
|
|
</Popover.Target>
|
|
<Popover.Dropdown>
|
|
<HiddenNodesTooltipContent />
|
|
</Popover.Dropdown>
|
|
</Popover>,
|
|
//renderAnchor(secondLastNode),
|
|
renderAnchor(lastNode),
|
|
];
|
|
}
|
|
|
|
return breadcrumbNodes.map(renderAnchor);
|
|
};
|
|
|
|
const getMobileBreadcrumbItems = () => {
|
|
if (!breadcrumbNodes) return [];
|
|
|
|
if (breadcrumbNodes.length > 0) {
|
|
return [
|
|
<Popover
|
|
width={250}
|
|
position="bottom"
|
|
withArrow
|
|
shadow="xl"
|
|
key="mobile-hidden-nodes"
|
|
>
|
|
<Popover.Target>
|
|
<Tooltip label="Breadcrumbs">
|
|
<ActionIcon color="gray" variant="transparent">
|
|
<IconCornerDownRightDouble size={20} stroke={2} />
|
|
</ActionIcon>
|
|
</Tooltip>
|
|
</Popover.Target>
|
|
<Popover.Dropdown>
|
|
<MobileHiddenNodesTooltipContent />
|
|
</Popover.Dropdown>
|
|
</Popover>,
|
|
];
|
|
}
|
|
|
|
return breadcrumbNodes.map(renderAnchor);
|
|
};
|
|
|
|
return (
|
|
<div className={classes.breadcrumbDiv}>
|
|
{breadcrumbNodes && (
|
|
<Breadcrumbs className={classes.breadcrumbs}>
|
|
{isMobile ? getMobileBreadcrumbItems() : getBreadcrumbItems()}
|
|
</Breadcrumbs>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|