Emily Xiong 1d10a19abc
feat(graph): show owners in pdv (#28211)
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->


<img width="428" alt="Screenshot 2024-09-30 at 8 54 38 PM"
src="https://github.com/user-attachments/assets/59b47117-4136-4a59-8e08-c223eafa94b4">
<img width="445" alt="Screenshot 2024-09-30 at 8 53 49 PM"
src="https://github.com/user-attachments/assets/9f9586fa-ae7a-44fe-bd67-305331b46bd4">
<img width="337" alt="Screenshot 2024-09-30 at 8 52 46 PM"
src="https://github.com/user-attachments/assets/762a6aaa-7260-47db-87b4-b394b4da4df1">

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
2024-10-01 18:00:01 -04:00

81 lines
2.3 KiB
TypeScript

import { useState, useRef, useEffect } from 'react';
import { Pill } from '../pill';
import { twMerge } from 'tailwind-merge';
interface OwnersListProps {
owners: string[];
className?: string;
}
export function OwnersList({ owners, className }: OwnersListProps) {
const [isExpanded, _setIsExpanded] = useState(false);
const [isOverflowing, setIsOverflowing] = useState(false);
const ownersContainerRef = useRef<HTMLSpanElement>(null);
const checkOverflow = () => {
requestAnimationFrame(() => {
if (ownersContainerRef.current) {
setIsOverflowing(
ownersContainerRef.current.scrollWidth >
ownersContainerRef.current.offsetWidth
);
}
});
};
const setExpanded = (value: boolean) => {
_setIsExpanded(value);
checkOverflow();
};
useEffect(() => {
checkOverflow();
window.addEventListener('resize', checkOverflow);
return () => window.removeEventListener('resize', checkOverflow);
}, [ownersContainerRef]);
return (
<div className={twMerge('relative max-w-full', className)}>
<p className="flex min-w-0 font-medium leading-loose">
<span className="inline-block">Owners:</span>
<span
className={`inline-block ${
isExpanded ? 'whitespace-normal' : 'whitespace-nowrap'
} w-full max-w-full overflow-hidden transition-all duration-300`}
style={{
maskImage:
isOverflowing && !isExpanded
? 'linear-gradient(to right, black 80%, transparent 100%)'
: 'none',
}}
ref={ownersContainerRef}
>
{owners.map((tag) => (
<span key={tag} className="ml-2 font-mono lowercase">
<Pill text={tag} />
</span>
))}
{isExpanded && (
<button
onClick={() => setExpanded(false)}
className="inline-block px-2 align-middle"
>
Show less
</button>
)}
</span>
{isOverflowing && !isExpanded && (
<button
onClick={() => setExpanded(true)}
className="ml-1 inline-block whitespace-nowrap"
>
Show more
</button>
)}
</p>
</div>
);
}