|
| 1 | +import { useRef, useState, useLayoutEffect, useCallback } from "react"; |
| 2 | +import { cn } from "~/utils/cn"; |
| 3 | +import { SimpleTooltip } from "./Tooltip"; |
| 4 | + |
| 5 | +interface MiddleTruncateProps { |
| 6 | + text: string; |
| 7 | + className?: string; |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * A component that truncates text in the middle, showing the beginning and end. |
| 12 | + * Shows the full text in a tooltip on hover when truncated. |
| 13 | + * |
| 14 | + * Example: "namespace:category:subcategory:task-name" becomes "namespace:cat…task-name" |
| 15 | + */ |
| 16 | +export function MiddleTruncate({ text, className }: MiddleTruncateProps) { |
| 17 | + const containerRef = useRef<HTMLSpanElement>(null); |
| 18 | + const measureRef = useRef<HTMLSpanElement>(null); |
| 19 | + const [displayText, setDisplayText] = useState(text); |
| 20 | + const [isTruncated, setIsTruncated] = useState(false); |
| 21 | + |
| 22 | + const calculateTruncation = useCallback(() => { |
| 23 | + const container = containerRef.current; |
| 24 | + const measure = measureRef.current; |
| 25 | + if (!container || !measure) return; |
| 26 | + |
| 27 | + const parent = container.parentElement; |
| 28 | + if (!parent) return; |
| 29 | + |
| 30 | + // Get the available width from the parent container |
| 31 | + const parentStyle = getComputedStyle(parent); |
| 32 | + const availableWidth = |
| 33 | + parent.clientWidth - |
| 34 | + parseFloat(parentStyle.paddingLeft) - |
| 35 | + parseFloat(parentStyle.paddingRight); |
| 36 | + |
| 37 | + // Measure full text width |
| 38 | + measure.textContent = text; |
| 39 | + const fullTextWidth = measure.offsetWidth; |
| 40 | + |
| 41 | + // If text fits, no truncation needed |
| 42 | + if (fullTextWidth <= availableWidth) { |
| 43 | + setDisplayText(text); |
| 44 | + setIsTruncated(false); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + // Text needs truncation - find optimal split |
| 49 | + const ellipsis = "…"; |
| 50 | + measure.textContent = ellipsis; |
| 51 | + const ellipsisWidth = measure.offsetWidth; |
| 52 | + |
| 53 | + const targetWidth = availableWidth - ellipsisWidth - 4; // small buffer |
| 54 | + |
| 55 | + if (targetWidth <= 0) { |
| 56 | + setDisplayText(ellipsis); |
| 57 | + setIsTruncated(true); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // Binary search for optimal character counts |
| 62 | + let startChars = 0; |
| 63 | + let endChars = 0; |
| 64 | + |
| 65 | + // Alternate adding characters from start and end |
| 66 | + while (startChars + endChars < text.length) { |
| 67 | + // Try adding to start |
| 68 | + const testStart = text.slice(0, startChars + 1); |
| 69 | + const testEnd = endChars > 0 ? text.slice(-endChars) : ""; |
| 70 | + measure.textContent = testStart + ellipsis + testEnd; |
| 71 | + |
| 72 | + if (measure.offsetWidth > targetWidth) break; |
| 73 | + startChars++; |
| 74 | + |
| 75 | + if (startChars + endChars >= text.length) break; |
| 76 | + |
| 77 | + // Try adding to end |
| 78 | + const newTestEnd = text.slice(-(endChars + 1)); |
| 79 | + measure.textContent = text.slice(0, startChars) + ellipsis + newTestEnd; |
| 80 | + |
| 81 | + if (measure.offsetWidth > targetWidth) break; |
| 82 | + endChars++; |
| 83 | + } |
| 84 | + |
| 85 | + // Ensure minimum characters on each side for readability |
| 86 | + const minChars = 4; |
| 87 | + if (startChars < minChars && text.length > minChars * 2 + 1) { |
| 88 | + startChars = minChars; |
| 89 | + } |
| 90 | + if (endChars < minChars && text.length > minChars * 2 + 1) { |
| 91 | + endChars = minChars; |
| 92 | + } |
| 93 | + |
| 94 | + // If combined chars would exceed text length, show full text |
| 95 | + if (startChars + endChars >= text.length) { |
| 96 | + setDisplayText(text); |
| 97 | + setIsTruncated(false); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + const result = text.slice(0, startChars) + ellipsis + text.slice(-endChars); |
| 102 | + setDisplayText(result); |
| 103 | + setIsTruncated(true); |
| 104 | + }, [text]); |
| 105 | + |
| 106 | + useLayoutEffect(() => { |
| 107 | + calculateTruncation(); |
| 108 | + |
| 109 | + // Recalculate on resize |
| 110 | + const resizeObserver = new ResizeObserver(() => { |
| 111 | + calculateTruncation(); |
| 112 | + }); |
| 113 | + |
| 114 | + const container = containerRef.current; |
| 115 | + if (container?.parentElement) { |
| 116 | + resizeObserver.observe(container.parentElement); |
| 117 | + } |
| 118 | + |
| 119 | + return () => { |
| 120 | + resizeObserver.disconnect(); |
| 121 | + }; |
| 122 | + }, [calculateTruncation]); |
| 123 | + |
| 124 | + const content = ( |
| 125 | + <span ref={containerRef} className={cn("block", className)}> |
| 126 | + {/* Hidden span for measuring text width */} |
| 127 | + <span |
| 128 | + ref={measureRef} |
| 129 | + className="invisible absolute whitespace-nowrap" |
| 130 | + aria-hidden="true" |
| 131 | + /> |
| 132 | + {displayText} |
| 133 | + </span> |
| 134 | + ); |
| 135 | + |
| 136 | + if (isTruncated) { |
| 137 | + return ( |
| 138 | + <SimpleTooltip |
| 139 | + button={content} |
| 140 | + content={<span className="max-w-xs break-all font-mono text-xs">{text}</span>} |
| 141 | + side="top" |
| 142 | + asChild |
| 143 | + /> |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + return content; |
| 148 | +} |
0 commit comments