-
Notifications
You must be signed in to change notification settings - Fork 61
fix(channels): render prompt expansion inline #3732
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -422,42 +422,80 @@ function ExpandablePrompt({ | |||||||||
| }) { | ||||||||||
| const observerRef = useRef<ResizeObserver | null>(null); | ||||||||||
| const [expanded, setExpanded] = useState(false); | ||||||||||
| const [truncated, setTruncated] = useState(false); | ||||||||||
| const [truncatedText, setTruncatedText] = useState<string | null>(null); | ||||||||||
| const measureTextRef = useRef<HTMLSpanElement>(null); | ||||||||||
| const measureMoreRef = useRef<HTMLSpanElement>(null); | ||||||||||
| const collapsedText = children.replace(/\s+/g, " ").trim(); | ||||||||||
|
|
||||||||||
| const measureRef = useCallback( | ||||||||||
| (body: HTMLDivElement | null) => { | ||||||||||
| observerRef.current?.disconnect(); | ||||||||||
| observerRef.current = null; | ||||||||||
| if (!body || expanded) return; | ||||||||||
| const measure = () => setTruncated(body.scrollHeight > body.clientHeight); | ||||||||||
| const measure = () => { | ||||||||||
| const text = measureTextRef.current; | ||||||||||
| const more = measureMoreRef.current; | ||||||||||
| if (!text || !more) return; | ||||||||||
|
|
||||||||||
| more.hidden = true; | ||||||||||
| text.textContent = collapsedText; | ||||||||||
| if (body.scrollHeight <= body.clientHeight) { | ||||||||||
| setTruncatedText(null); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| more.hidden = false; | ||||||||||
| let low = 0; | ||||||||||
| let high = collapsedText.length; | ||||||||||
| while (low < high) { | ||||||||||
| const middle = Math.ceil((low + high) / 2); | ||||||||||
| text.textContent = `${collapsedText.slice(0, middle).trimEnd()}... `; | ||||||||||
| if (body.scrollHeight <= body.clientHeight) { | ||||||||||
| low = middle; | ||||||||||
| } else { | ||||||||||
| high = middle - 1; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| setTruncatedText(collapsedText.slice(0, low).trimEnd()); | ||||||||||
| }; | ||||||||||
| measure(); | ||||||||||
| const observer = new ResizeObserver(measure); | ||||||||||
| observer.observe(body); | ||||||||||
| observer.observe(body.parentElement ?? body); | ||||||||||
| observerRef.current = observer; | ||||||||||
| }, | ||||||||||
| [expanded], | ||||||||||
| [collapsedText, expanded], | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| return ( | ||||||||||
| <div> | ||||||||||
| <ThreadItemBody | ||||||||||
| ref={measureRef} | ||||||||||
| className={cn( | ||||||||||
| "wrap-break-word whitespace-pre-wrap", | ||||||||||
| !expanded && (lines === 2 ? "line-clamp-2" : "line-clamp-4"), | ||||||||||
| <div className="relative"> | ||||||||||
| <ThreadItemBody className="wrap-break-word whitespace-pre-wrap"> | ||||||||||
| {expanded || truncatedText === null ? children : truncatedText} | ||||||||||
| {truncatedText !== null && !expanded && "... "} | ||||||||||
| {truncatedText !== null && ( | ||||||||||
|
Comment on lines
+473
to
+474
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The expanded branch renders the original prompt directly beside the
Suggested change
Rule Used: Maintain proper spacing in template strings. Ensur... (source) Learned From Prompt To Fix With AIThis is a comment left during a code review.
Path: packages/ui/src/features/canvas/components/ChannelFeedView.tsx
Line: 473-474
Comment:
**Expanded Action Joins Final Word**
The expanded branch renders the original prompt directly beside the `less` button. When the prompt does not end with whitespace, users see text such as `final wordless` instead of a separated inline action.
```suggestion
{truncatedText !== null && (expanded ? " " : "... ")}
{truncatedText !== null && (
```
**Rule Used:** Maintain proper spacing in template strings. Ensur... ([source](https://app.greptile.com/posthog-org-19734/-/custom-context?memory=d0a97985-7ddb-464d-9739-625899016a97))
**Learned From**
[PostHog/posthog#32603](https://github.com/PostHog/posthog/pull/32603)
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||
| <button | ||||||||||
| type="button" | ||||||||||
| aria-expanded={expanded} | ||||||||||
| className="text-muted-foreground text-xs underline underline-offset-2 hover:text-foreground" | ||||||||||
| onClick={() => setExpanded((value) => !value)} | ||||||||||
| > | ||||||||||
| {expanded ? "less" : "more"} | ||||||||||
| </button> | ||||||||||
| )} | ||||||||||
| > | ||||||||||
| {children} | ||||||||||
| </ThreadItemBody> | ||||||||||
| {(truncated || expanded) && ( | ||||||||||
| <button | ||||||||||
| type="button" | ||||||||||
| aria-expanded={expanded} | ||||||||||
| className="text-muted-foreground text-xs underline underline-offset-2 hover:text-foreground" | ||||||||||
| onClick={() => setExpanded((value) => !value)} | ||||||||||
| {!expanded && ( | ||||||||||
| <ThreadItemBody | ||||||||||
| ref={measureRef} | ||||||||||
| aria-hidden="true" | ||||||||||
| className={cn( | ||||||||||
| "wrap-break-word pointer-events-none invisible absolute inset-x-0 top-0 whitespace-normal", | ||||||||||
| lines === 2 ? "line-clamp-2" : "line-clamp-4", | ||||||||||
| )} | ||||||||||
| > | ||||||||||
| {expanded ? "less" : "more"} | ||||||||||
| </button> | ||||||||||
| <span ref={measureTextRef}>{collapsedText}</span> | ||||||||||
| <span ref={measureMoreRef} className="text-xs"> | ||||||||||
| more | ||||||||||
| </span> | ||||||||||
| </ThreadItemBody> | ||||||||||
| )} | ||||||||||
| </div> | ||||||||||
| ); | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The search slices by UTF-16 code units. When the fitting boundary lands inside a supplementary character such as an emoji, the collapsed prompt renders a replacement glyph before the ellipsis.
Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!