-
-
Notifications
You must be signed in to change notification settings - Fork 977
feat(webapp): Add MiddleTruncate component for long task names #2946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0ski
wants to merge
9
commits into
main
Choose a base branch
from
claude/slack-fix-branch-name-truncation-9PMl9
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+171
−2
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6e11d5a
feat(webapp): show beginning and end of task names in filter dropdown
claude 3370c96
Widen task filter dropdown popup by 50%
claude e5d9927
fix(webapp): set min-width for task filter dropdown when text is trun…
claude 59c69b1
Revert "fix(webapp): set min-width for task filter dropdown when text…
claude 22db328
fix(webapp): set min-width on MiddleTruncate component when text is t…
claude f06d1a6
fix(webapp): address review feedback for MiddleTruncate component
claude 147f228
fix(webapp): address CodeRabbit review comments
claude 3df5767
fix(webapp): increase min width for run slug in filter dropdown
0ski b011415
feat(webapp): MiddleTruncate to take decent amount of space when poss…
0ski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168
apps/webapp/app/components/primitives/MiddleTruncate.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import { useRef, useState, useLayoutEffect, useCallback } from "react"; | ||
| import { cn } from "~/utils/cn"; | ||
| import { SimpleTooltip } from "./Tooltip"; | ||
|
|
||
| type MiddleTruncateProps = { | ||
| text: string; | ||
| className?: string; | ||
| }; | ||
|
|
||
| /** | ||
| * A component that truncates text in the middle, showing the beginning and end. | ||
| * Shows the full text in a tooltip on hover when truncated. | ||
| * | ||
| * Example: "namespace:category:subcategory:task-name" becomes "namespace:cat…task-name" | ||
| */ | ||
| export function MiddleTruncate({ text, className }: MiddleTruncateProps) { | ||
| const containerRef = useRef<HTMLSpanElement>(null); | ||
| const measureRef = useRef<HTMLSpanElement>(null); | ||
| const [displayText, setDisplayText] = useState(text); | ||
| const [isTruncated, setIsTruncated] = useState(false); | ||
|
|
||
| const calculateTruncation = useCallback(() => { | ||
| const container = containerRef.current; | ||
| const measure = measureRef.current; | ||
| if (!container || !measure) return; | ||
|
|
||
| const parent = container.parentElement; | ||
| if (!parent) return; | ||
|
|
||
| // Get the available width from the parent container | ||
| const parentStyle = getComputedStyle(parent); | ||
| const availableWidth = | ||
| parent.clientWidth - | ||
| parseFloat(parentStyle.paddingLeft) - | ||
| parseFloat(parentStyle.paddingRight); | ||
|
|
||
| // Measure full text width | ||
| measure.textContent = text; | ||
| const fullTextWidth = measure.offsetWidth; | ||
|
|
||
| // If text fits, no truncation needed | ||
| if (fullTextWidth <= availableWidth) { | ||
| setDisplayText(text); | ||
| setIsTruncated(false); | ||
| return; | ||
| } | ||
|
|
||
| // Text needs truncation - find optimal split | ||
| const ellipsis = "…"; | ||
| measure.textContent = ellipsis; | ||
| const ellipsisWidth = measure.offsetWidth; | ||
|
|
||
| const targetWidth = availableWidth - ellipsisWidth - 4; // small buffer | ||
|
|
||
| if (targetWidth <= 0) { | ||
| setDisplayText(ellipsis); | ||
| setIsTruncated(true); | ||
| return; | ||
| } | ||
|
|
||
| // Incrementally find the optimal character counts | ||
| let startChars = 0; | ||
| let endChars = 0; | ||
|
|
||
| // Alternate adding characters from start and end | ||
| while (startChars + endChars < text.length) { | ||
| // Try adding to start | ||
| const testStart = text.slice(0, startChars + 1); | ||
| const testEnd = endChars > 0 ? text.slice(-endChars) : ""; | ||
| measure.textContent = testStart + ellipsis + testEnd; | ||
|
|
||
| if (measure.offsetWidth > targetWidth) break; | ||
| startChars++; | ||
|
|
||
| if (startChars + endChars >= text.length) break; | ||
|
|
||
| // Try adding to end | ||
| const newTestEnd = text.slice(-(endChars + 1)); | ||
| measure.textContent = text.slice(0, startChars) + ellipsis + newTestEnd; | ||
|
|
||
| if (measure.offsetWidth > targetWidth) break; | ||
| endChars++; | ||
| } | ||
|
|
||
| // Ensure minimum characters on each side for readability | ||
| const minChars = 4; | ||
| const prevStartChars = startChars; | ||
| const prevEndChars = endChars; | ||
|
|
||
| if (startChars < minChars && text.length > minChars * 2 + 1) { | ||
| startChars = minChars; | ||
| } | ||
| if (endChars < minChars && text.length > minChars * 2 + 1) { | ||
| endChars = minChars; | ||
| } | ||
|
|
||
| // Re-measure after enforcing minChars to prevent overflow | ||
| if (startChars !== prevStartChars || endChars !== prevEndChars) { | ||
| measure.textContent = text.slice(0, startChars) + ellipsis + text.slice(-endChars); | ||
| if (measure.offsetWidth > targetWidth) { | ||
| // Revert to previous values if minChars enforcement causes overflow | ||
| startChars = prevStartChars; | ||
| endChars = prevEndChars; | ||
| } | ||
| } | ||
|
|
||
| // If combined chars would exceed text length, show full text | ||
| if (startChars + endChars >= text.length) { | ||
| setDisplayText(text); | ||
| setIsTruncated(false); | ||
| return; | ||
| } | ||
|
|
||
| const result = text.slice(0, startChars) + ellipsis + text.slice(-endChars); | ||
nicktrn marked this conversation as resolved.
Show resolved
Hide resolved
0ski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| setDisplayText(result); | ||
| setIsTruncated(true); | ||
| }, [text]); | ||
|
|
||
| useLayoutEffect(() => { | ||
| calculateTruncation(); | ||
|
|
||
| // Recalculate on resize (guard for jsdom/older browsers) | ||
| if (typeof ResizeObserver === "undefined") { | ||
| return; | ||
| } | ||
|
|
||
| const resizeObserver = new ResizeObserver(() => { | ||
| calculateTruncation(); | ||
| }); | ||
|
|
||
| const container = containerRef.current; | ||
| if (container?.parentElement) { | ||
| resizeObserver.observe(container.parentElement); | ||
| } | ||
|
|
||
| return () => { | ||
| resizeObserver.disconnect(); | ||
| }; | ||
| }, [calculateTruncation]); | ||
|
|
||
| const content = ( | ||
| <span | ||
| ref={containerRef} | ||
| className={cn("block", isTruncated && "min-w-[360px]", className)} | ||
| > | ||
| {/* Hidden span for measuring text width */} | ||
| <span | ||
| ref={measureRef} | ||
| className="invisible absolute whitespace-nowrap" | ||
| aria-hidden="true" | ||
| /> | ||
| {displayText} | ||
| </span> | ||
| ); | ||
|
|
||
| if (isTruncated) { | ||
| return ( | ||
| <SimpleTooltip | ||
| button={content} | ||
| content={<span className="max-w-xs break-all font-mono text-xs">{text}</span>} | ||
| side="top" | ||
| asChild | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| return content; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.