Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rafpaf committed May 3, 2024
1 parent 96e1f02 commit da33509
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export const CollectionBreadcrumbsWithTooltip = ({
: collections;
const justOneShown = shownCollections.length === 1;

const { areAnyTruncated, ref } = useAreAnyTruncated<HTMLAnchorElement>();
const { areAnyTruncated, ref } = useAreAnyTruncated<HTMLAnchorElement>({
tolerance: 1,
});

const initialEllipsisRef = useRef<HTMLDivElement | null>(null);
const [
Expand Down
7 changes: 3 additions & 4 deletions frontend/src/metabase/hooks/use-is-truncated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export const useIsTruncated = <E extends Element>({
};

const getIsTruncated = (element: Element, tolerance: number): boolean => {
tolerance = 0;
return (
element.scrollHeight > element.clientHeight + tolerance ||
element.scrollWidth > element.clientWidth + tolerance
Expand All @@ -51,7 +50,7 @@ export const useAreAnyTruncated = <E extends Element>({
tolerance = 0,
}: UseIsTruncatedProps = {}) => {
const ref = useRef(new Map<string, E>());
const [truncatedStatuses, setTruncatedStatuses] = useState<
const [truncationStatusByKey, setTruncationStatusByKey] = useState<
Map<string, boolean>
>(new Map());

Expand All @@ -66,7 +65,7 @@ export const useAreAnyTruncated = <E extends Element>({
[...elementsMap.entries()].forEach(([elementKey, element]) => {
const handleResize = () => {
const isTruncated = getIsTruncated(element, tolerance);
setTruncatedStatuses(statuses => {
setTruncationStatusByKey(statuses => {
const newStatuses = new Map(statuses);
newStatuses.set(elementKey, isTruncated);
return newStatuses;
Expand All @@ -84,6 +83,6 @@ export const useAreAnyTruncated = <E extends Element>({
};
}, [disabled, tolerance]);

const areAnyTruncated = [...truncatedStatuses.values()].some(Boolean);
const areAnyTruncated = [...truncationStatusByKey.values()].some(Boolean);
return { areAnyTruncated, ref };
};

0 comments on commit da33509

Please sign in to comment.