|
6 | 6 | } from "~/assets/icons/EnvironmentIcons"; |
7 | 7 | import type { RuntimeEnvironment } from "~/models/runtimeEnvironment.server"; |
8 | 8 | import { cn } from "~/utils/cn"; |
| 9 | +import { SimpleTooltip } from "~/components/primitives/Tooltip"; |
| 10 | +import { useEffect, useRef, useState } from "react"; |
9 | 11 |
|
10 | 12 | type Environment = Pick<RuntimeEnvironment, "type"> & { branchName?: string | null }; |
11 | 13 |
|
@@ -72,11 +74,61 @@ export function EnvironmentLabel({ |
72 | 74 | environment: Environment; |
73 | 75 | className?: string; |
74 | 76 | }) { |
75 | | - return ( |
76 | | - <span className={cn("truncate text-left", environmentTextClassName(environment), className)}> |
77 | | - {environment.branchName ? environment.branchName : environmentFullTitle(environment)} |
| 77 | + const spanRef = useRef<HTMLSpanElement>(null); |
| 78 | + const [isTruncated, setIsTruncated] = useState(false); |
| 79 | + const text = environment.branchName ? environment.branchName : environmentFullTitle(environment); |
| 80 | + |
| 81 | + useEffect(() => { |
| 82 | + const checkTruncation = () => { |
| 83 | + if (spanRef.current) { |
| 84 | + const isTruncated = spanRef.current.scrollWidth > spanRef.current.clientWidth; |
| 85 | + console.log( |
| 86 | + "isTruncated", |
| 87 | + isTruncated, |
| 88 | + spanRef.current.scrollWidth, |
| 89 | + spanRef.current.clientWidth |
| 90 | + ); |
| 91 | + setIsTruncated(isTruncated); |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + checkTruncation(); |
| 96 | + // Add resize observer to recheck on window resize |
| 97 | + const resizeObserver = new ResizeObserver(checkTruncation); |
| 98 | + if (spanRef.current) { |
| 99 | + resizeObserver.observe(spanRef.current); |
| 100 | + } |
| 101 | + |
| 102 | + return () => resizeObserver.disconnect(); |
| 103 | + }, [text]); |
| 104 | + |
| 105 | + const content = ( |
| 106 | + <span |
| 107 | + ref={spanRef} |
| 108 | + className={cn("truncate text-left", environmentTextClassName(environment), className)} |
| 109 | + > |
| 110 | + {text} |
78 | 111 | </span> |
79 | 112 | ); |
| 113 | + |
| 114 | + if (isTruncated) { |
| 115 | + return ( |
| 116 | + <SimpleTooltip |
| 117 | + asChild |
| 118 | + button={content} |
| 119 | + content={ |
| 120 | + <span ref={spanRef} className={cn("text-left", environmentTextClassName(environment))}> |
| 121 | + {text} |
| 122 | + </span> |
| 123 | + } |
| 124 | + side="right" |
| 125 | + variant="dark" |
| 126 | + sideOffset={34} |
| 127 | + /> |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + return content; |
80 | 132 | } |
81 | 133 |
|
82 | 134 | export function environmentTitle(environment: Environment, username?: string) { |
|
0 commit comments