Skip to content

Commit

Permalink
cut common paths in tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
andreymikhadyuk committed Dec 19, 2023
1 parent b8a0e13 commit a055b53
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import "../../../../../../constants";
@import "../../../../../../styles/sizes";

.linkIcon {
width: 1rem;
Expand All @@ -11,9 +12,13 @@

.tooltipContent {
max-width: 30rem;
z-index: 3;
z-index: 1;
display: flex;
flex-direction: column;

@include tablet {
max-width: 100vw;
}
}

.contentTitle {
Expand All @@ -29,7 +34,6 @@

.link {
width: 100%;
margin-bottom: 0.625rem;
display: flex;
align-items: center;
font-size: 0.875rem;
Expand All @@ -38,10 +42,9 @@
&:hover {
color: $c-pink-primary;
}

&:last-child {
margin-bottom: 0;
}
}
.linkWithBottomMargin {
margin-bottom: 0.625rem;
}

.linkLeft,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import React, {
FC,
MouseEventHandler,
ReactElement,
useEffect,
useState,
} from "react";
import React, { FC, MouseEventHandler, useEffect, useState } from "react";
import { NavLink } from "react-router-dom";
import classNames from "classnames";
import { ButtonIcon } from "@/shared/components";
import { useRoutesContext } from "@/shared/contexts";
import { RightArrowThinIcon, Link4Icon } from "@/shared/icons";
import { Common } from "@/shared/models";
import { useIsTabletView } from "@/shared/hooks/viewport";
import { Link4Icon } from "@/shared/icons";
import {
Loader,
Tooltip,
TooltipContent,
TooltipTrigger,
} from "@/shared/ui-kit";
import { LinkedItemTooltipContent } from "./components";
import { useCommonPaths } from "./hooks";
import styles from "./LinkedItemMark.module.scss";

Expand All @@ -31,6 +27,7 @@ const LinkedItemMark: FC<LinkedItemMarkProps> = (props) => {
originalCommonId = "",
linkedCommonIds = [],
} = props;
const isTabletView = useIsTabletView();
const [isOpen, setIsOpen] = useState(false);
const {
data: commonPaths,
Expand All @@ -57,45 +54,12 @@ const LinkedItemMark: FC<LinkedItemMarkProps> = (props) => {
fetchCommonPaths(commonIds);
}, [isOpen]);

const renderFullPath = (commons: Common[]): ReactElement => {
return (
<div className={styles.linkLeft}>
{commons.map((common, commonIndex) => (
<React.Fragment key={common.id}>
{common.name}
{commonIndex !== commons.length - 1 && (
<RightArrowThinIcon className={styles.arrowIcon} />
)}
</React.Fragment>
))}
</div>
);
};

const renderCutPath = (commons: Common[]): ReactElement => {
const lastCommon = commons[commons.length - 1];
const parentCommons = commons.slice(0, -1);

return (
<>
<div className={styles.linkLeft}>
{parentCommons.map((common, commonIndex) => (
<React.Fragment key={common.id}>
{common.name}
{commonIndex !== commons.length - 1 && (
<RightArrowThinIcon className={styles.arrowIcon} />
)}
</React.Fragment>
))}
</div>
<div className={styles.ellipsis}></div>
<div className={styles.linkRight}>{lastCommon?.name}</div>
</>
);
};

return (
<Tooltip open={isOpen} onOpenChange={setIsOpen} placement="right">
<Tooltip
open={isOpen}
onOpenChange={setIsOpen}
placement={isTabletView ? "bottom-start" : "right"}
>
<TooltipTrigger onClick={toggleTooltip} asChild>
<ButtonIcon>
<Link4Icon className={styles.linkIcon} />
Expand All @@ -112,11 +76,14 @@ const LinkedItemMark: FC<LinkedItemMarkProps> = (props) => {
return (
<NavLink
key={key}
className={styles.link}
className={classNames(styles.link, {
[styles.linkWithBottomMargin]:
index !== commonPaths.length - 1,
})}
to={getCommonPagePath(lastCommon?.id || "")}
title={lastCommon?.name}
>
{renderCutPath(commons)}
<LinkedItemTooltipContent commons={commons} />
</NavLink>
);
})}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.linkLeft,
.linkRight {
white-space: nowrap;
overflow: hidden;
}

.linkLeft {
display: flex;
align-items: center;
text-overflow: ellipsis;
}

.linkRight {
flex-shrink: 0;
max-width: 25rem;
}

.ellipsis {
flex-shrink: 0;
}

.arrowIcon {
flex-shrink: 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React, { FC, ReactElement, useEffect, useState } from "react";
import { RightArrowThinIcon } from "@/shared/icons";
import { Common } from "@/shared/models";
import styles from "./LinkedItemTooltipContent.module.scss";

interface LinkedItemTooltipContentProps {
commons: Common[];
}

const renderCutPath = (commons: Common[]): ReactElement => {
const lastCommon = commons[commons.length - 1];
const parentCommons = commons.slice(0, -1);

return (
<>
<div className={styles.linkLeft}>
{parentCommons.map((common, commonIndex) => (
<React.Fragment key={common.id}>
{common.name}
{commonIndex !== commons.length - 1 && (
<RightArrowThinIcon className={styles.arrowIcon} />
)}
</React.Fragment>
))}
</div>
<div className={styles.ellipsis}></div>
<div className={styles.linkRight}>{lastCommon?.name}</div>
</>
);
};

const LinkedItemTooltipContent: FC<LinkedItemTooltipContentProps> = (props) => {
const { commons } = props;
const [containerRef, setContainerRef] = useState<HTMLDivElement | null>(null);
const [shouldCut, setShouldCut] = useState(false);

useEffect(() => {
if (containerRef && containerRef.scrollWidth > containerRef.clientWidth) {
setShouldCut(true);
}
}, [containerRef]);

if (shouldCut) {
return renderCutPath(commons);
}

return (
<div ref={setContainerRef} className={styles.linkLeft}>
{commons.map((common, commonIndex) => (
<React.Fragment key={common.id}>
{common.name}
{commonIndex !== commons.length - 1 && (
<RightArrowThinIcon className={styles.arrowIcon} />
)}
</React.Fragment>
))}
</div>
);
};

export default LinkedItemTooltipContent;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as LinkedItemTooltipContent } from "./LinkedItemTooltipContent";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./LinkedItemTooltipContent";

0 comments on commit a055b53

Please sign in to comment.