mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-21 23:43:35 +00:00
🪢 feat: Collapse Activity Folds From Their Rails (#16637)
* 🪢 feat: Collapse Activity Folds From Their Rails
Hovering an open fold's rail lights the line and swaps the header's glyph for a collapse knob; clicking the rail collapses that fold and brings its header back into view.
* fix: Stabilize Fold Rail Hover and Header Reveal
* fix: Reserve the Chat Toolbar When Revealing Folds
---------
Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
parent
aab44c5e35
commit
e689243d9d
9 changed files with 551 additions and 67 deletions
|
|
@ -5,6 +5,7 @@ import { ContentTypes } from 'librechat-data-provider';
|
|||
import { Check, Lightbulb, ChevronDown, TriangleAlert } from 'lucide-react';
|
||||
import type { TAttachment, TMessageContentParts } from 'librechat-data-provider';
|
||||
import type { CSSProperties, ReactNode } from 'react';
|
||||
import type { RailHover } from './rail';
|
||||
import {
|
||||
useLocalize,
|
||||
useExpandCollapse,
|
||||
|
|
@ -19,6 +20,7 @@ import {
|
|||
LIVE_ACTIVITY_THROTTLE_MS,
|
||||
LIVE_REASONING_HOLD_MS,
|
||||
} from './live';
|
||||
import { FoldRail, RailGlyph, FoldHeaderContext, revealFoldHeader, useRailHover } from './rail';
|
||||
import { FailedRevealContext, FailedRevealPill, useFailedRevealTrigger } from './reveal';
|
||||
import { FOLD_RAIL_CLASSES, ROW_GLYPH_SLOT, TOOL_ROW_CLASSES } from './rows';
|
||||
import useSmoothStreaming from '~/hooks/Messages/useSmoothStreaming';
|
||||
|
|
@ -273,6 +275,7 @@ function LivePhaseHeader({
|
|||
detailId,
|
||||
attachments,
|
||||
onAnnounce,
|
||||
railHover,
|
||||
}: {
|
||||
parts: ReadonlyArray<TMessageContentParts | undefined>;
|
||||
animate: boolean;
|
||||
|
|
@ -284,6 +287,7 @@ function LivePhaseHeader({
|
|||
detailId: string;
|
||||
attachments?: TAttachment[];
|
||||
onAnnounce: (text: string) => void;
|
||||
railHover: RailHover;
|
||||
}) {
|
||||
const localize = useLocalize();
|
||||
const mcpIconMap = useMCPIconMap();
|
||||
|
|
@ -359,28 +363,30 @@ function LivePhaseHeader({
|
|||
|
||||
return (
|
||||
<>
|
||||
{iconNames.length === 0 ? (
|
||||
/** A span that is only reasoning so far has no tool to show; it takes
|
||||
* the glyph the reasoning row itself uses. */
|
||||
<span
|
||||
className={cn(ROW_GLYPH_SLOT, 'text-text-primary animate-pulse')}
|
||||
aria-hidden="true"
|
||||
data-testid="live-phase-thinking"
|
||||
>
|
||||
<Lightbulb size={14} />
|
||||
</span>
|
||||
) : (
|
||||
<span className={ROW_GLYPH_SLOT} aria-hidden="true">
|
||||
<StackedToolIcons
|
||||
toolNames={iconNames}
|
||||
mcpIconMap={mcpIconMap}
|
||||
maxIcons={SPAN_ICONS}
|
||||
sourceDomains={sourceDomains}
|
||||
status={getOutcomeStatus(activity.outcome)}
|
||||
isAnimating
|
||||
/>
|
||||
</span>
|
||||
)}
|
||||
<RailGlyph hover={railHover}>
|
||||
{iconNames.length === 0 ? (
|
||||
/** A span that is only reasoning so far has no tool to show; it takes
|
||||
* the glyph the reasoning row itself uses. */
|
||||
<span
|
||||
className={cn(ROW_GLYPH_SLOT, 'text-text-primary animate-pulse')}
|
||||
aria-hidden="true"
|
||||
data-testid="live-phase-thinking"
|
||||
>
|
||||
<Lightbulb size={14} />
|
||||
</span>
|
||||
) : (
|
||||
<span className={ROW_GLYPH_SLOT} aria-hidden="true">
|
||||
<StackedToolIcons
|
||||
toolNames={iconNames}
|
||||
mcpIconMap={mcpIconMap}
|
||||
maxIcons={SPAN_ICONS}
|
||||
sourceDomains={sourceDomains}
|
||||
status={getOutcomeStatus(activity.outcome)}
|
||||
isAnimating
|
||||
/>
|
||||
</span>
|
||||
)}
|
||||
</RailGlyph>
|
||||
{/** The multiplier counts the line it is printed next to, so it travels
|
||||
* with that line instead of sitting out at the row's right edge beside
|
||||
* the chevron, where it read as a property of the row. `Create File ×2`
|
||||
|
|
@ -586,6 +592,9 @@ export default function ActivityPhaseGroup({
|
|||
const [isExpanded, setIsExpanded] = useState(foldsIn);
|
||||
const [isSettled, setIsSettled] = useState(!foldsIn);
|
||||
const rootRef = useRef<HTMLDivElement | null>(null);
|
||||
const headerRef = useRef<HTMLDivElement | null>(null);
|
||||
const railHover = useRailHover();
|
||||
const railScope = useMemo(() => ({ header: headerRef, expanded: isExpanded }), [isExpanded]);
|
||||
const panelId = useId();
|
||||
const lineId = useId();
|
||||
const comboId = useId();
|
||||
|
|
@ -664,6 +673,16 @@ export default function ActivityPhaseGroup({
|
|||
setIsExpanded(!isExpanded);
|
||||
}, [mountBody, isExpanded, onExpansionChange]);
|
||||
|
||||
/** The rail stays drawn while the panel animates shut, so a second click on
|
||||
* it must not reopen what the first one closed. */
|
||||
const handleRailCollapse = useCallback(() => {
|
||||
if (!isExpanded) {
|
||||
return;
|
||||
}
|
||||
revealFoldHeader(rootRef.current, headerRef.current);
|
||||
handleToggle();
|
||||
}, [isExpanded, handleToggle]);
|
||||
|
||||
/** One click to the error from a closed card: open the card the way a
|
||||
* toggle would, then ask every failed row below to open its own panel. */
|
||||
const handleRevealFailed = useCallback(() => {
|
||||
|
|
@ -797,6 +816,7 @@ export default function ActivityPhaseGroup({
|
|||
{announcement}
|
||||
</span>
|
||||
<div
|
||||
ref={headerRef}
|
||||
style={headerStyle}
|
||||
/** Pinned while open, so a run long enough to scroll keeps its name
|
||||
* at the top of the viewport. The containing block is this card, so
|
||||
|
|
@ -841,14 +861,17 @@ export default function ActivityPhaseGroup({
|
|||
detailId={detailId}
|
||||
attachments={attachments}
|
||||
onAnnounce={setAnnouncement}
|
||||
railHover={railHover}
|
||||
/>
|
||||
) : (
|
||||
<>
|
||||
{outcomeParts != null && !hasFailure ? (
|
||||
<SpanGlyph parts={outcomeParts} attachments={attachments} />
|
||||
) : (
|
||||
<PhaseGlyph failed={hasFailure} />
|
||||
)}
|
||||
<RailGlyph hover={railHover}>
|
||||
{outcomeParts != null && !hasFailure ? (
|
||||
<SpanGlyph parts={outcomeParts} attachments={attachments} />
|
||||
) : (
|
||||
<PhaseGlyph failed={hasFailure} />
|
||||
)}
|
||||
</RailGlyph>
|
||||
<PhaseLabel text={label} failed={hasFailure} animate={smoothStreaming} />
|
||||
</>
|
||||
)}
|
||||
|
|
@ -873,9 +896,12 @@ export default function ActivityPhaseGroup({
|
|||
>
|
||||
{shouldRenderBody && (
|
||||
<div className={cn('overflow-hidden', FOLD_RAIL_CLASSES)} ref={expandRef}>
|
||||
<FailedRevealContext.Provider value={revealValue}>
|
||||
{children}
|
||||
</FailedRevealContext.Provider>
|
||||
<FoldRail hover={railHover} expanded={isExpanded} onCollapse={handleRailCollapse} />
|
||||
<FoldHeaderContext.Provider value={railScope}>
|
||||
<FailedRevealContext.Provider value={revealValue}>
|
||||
{children}
|
||||
</FailedRevealContext.Provider>
|
||||
</FoldHeaderContext.Provider>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -26,9 +26,10 @@ const contentClass =
|
|||
* sits under a header and in the gutter when it stands alone, so a failure
|
||||
* is findable by shape before its text is read. A pseudo-element rather than
|
||||
* a border: the row's content is absolutely positioned against the padding
|
||||
* box, so a border would push it and change the row's geometry. */
|
||||
* box, so a border would push it and change the row's geometry. It lies over
|
||||
* the rail's hit area, so it lets the pointer through to the rail. */
|
||||
const failedStripeClass =
|
||||
"before:absolute before:-left-3 before:top-0 before:h-full before:w-0.5 before:rounded-full before:bg-status-error before:content-['']";
|
||||
"before:pointer-events-none before:absolute before:-left-3 before:top-0 before:h-full before:w-0.5 before:rounded-full before:bg-status-error before:content-['']";
|
||||
|
||||
const Wrapper = ({
|
||||
popover,
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import {
|
|||
useFailedRevealTrigger,
|
||||
} from './reveal';
|
||||
import { useLocalize, useExpandCollapse, scheduleMessageContentLayoutReconcile } from '~/hooks';
|
||||
import { FoldRail, RailGlyph, FoldHeaderContext, revealFoldHeader, useRailHover } from './rail';
|
||||
import { ASK_USER_QUESTION, getSubmittedAskAnswer } from '~/utils/approval';
|
||||
import { ToolAuthWarning, ToolAuthWarningContext } from './auth';
|
||||
import { useMCPIconMap, useMCPServerNames } from '~/hooks/MCP';
|
||||
|
|
@ -92,6 +93,9 @@ export default function ToolCallGroup({
|
|||
const mcpIconMap = useMCPIconMap();
|
||||
const mcpServerNames = useMCPServerNames();
|
||||
const rootRef = useRef<HTMLDivElement | null>(null);
|
||||
const headerRef = useRef<HTMLDivElement | null>(null);
|
||||
const railHover = useRailHover();
|
||||
const phaseHeader = useContext(FoldHeaderContext);
|
||||
const cancelLayoutReconcileRef = useRef<(() => void) | null>(null);
|
||||
const retainedForPendingApprovalRef = useRef(false);
|
||||
|
||||
|
|
@ -346,6 +350,16 @@ export default function ToolCallGroup({
|
|||
onExpansionChange?.({ isExpanded: nextExpanded, userOverride: true });
|
||||
}, [isExpanded, onExpansionChange]);
|
||||
|
||||
/** The rail stays drawn while the panel animates shut, so a second click on
|
||||
* it must not reopen what the first one closed. */
|
||||
const handleRailCollapse = useCallback(() => {
|
||||
if (!isExpanded) {
|
||||
return;
|
||||
}
|
||||
revealFoldHeader(rootRef.current, headerRef.current, phaseHeader?.header.current);
|
||||
handleToggle();
|
||||
}, [isExpanded, handleToggle, phaseHeader]);
|
||||
|
||||
const handleToolExpand = useCallback(() => {
|
||||
setUserOverride(true);
|
||||
setShouldRenderBody(true);
|
||||
|
|
@ -541,7 +555,7 @@ export default function ToolCallGroup({
|
|||
|
||||
return (
|
||||
<div className="mt-1 mb-2" ref={rootRef}>
|
||||
<div className="flex w-full items-center gap-2">
|
||||
<div className="flex w-full items-center gap-2" ref={headerRef}>
|
||||
<button
|
||||
type="button"
|
||||
className={cn(
|
||||
|
|
@ -554,31 +568,33 @@ export default function ToolCallGroup({
|
|||
aria-expanded={isExpanded}
|
||||
aria-label={groupAriaLabel}
|
||||
>
|
||||
{iconStatus == null && (allSubagents || allAskQuestions || allTaskChecks) ? (
|
||||
/** Homogeneous categories keep the same glyph as their individual
|
||||
* cards instead of stacking identical tool icons. */
|
||||
<div
|
||||
className={cn(
|
||||
ROW_GLYPH_SLOT,
|
||||
'text-text-secondary',
|
||||
isGroupLive && 'text-text-primary animate-pulse',
|
||||
)}
|
||||
aria-hidden="true"
|
||||
>
|
||||
<CategoryIcon size={14} />
|
||||
</div>
|
||||
) : (
|
||||
<div className={ROW_GLYPH_SLOT} aria-hidden="true">
|
||||
<StackedToolIcons
|
||||
toolNames={iconToolNames}
|
||||
mcpIconMap={mcpIconMap}
|
||||
maxIcons={4}
|
||||
sourceDomains={sourceDomains}
|
||||
status={iconStatus}
|
||||
isAnimating={isGroupLive}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<RailGlyph hover={railHover}>
|
||||
{iconStatus == null && (allSubagents || allAskQuestions || allTaskChecks) ? (
|
||||
/** Homogeneous categories keep the same glyph as their individual
|
||||
* cards instead of stacking identical tool icons. */
|
||||
<div
|
||||
className={cn(
|
||||
ROW_GLYPH_SLOT,
|
||||
'text-text-secondary',
|
||||
isGroupLive && 'text-text-primary animate-pulse',
|
||||
)}
|
||||
aria-hidden="true"
|
||||
>
|
||||
<CategoryIcon size={14} />
|
||||
</div>
|
||||
) : (
|
||||
<div className={ROW_GLYPH_SLOT} aria-hidden="true">
|
||||
<StackedToolIcons
|
||||
toolNames={iconToolNames}
|
||||
mcpIconMap={mcpIconMap}
|
||||
maxIcons={4}
|
||||
sourceDomains={sourceDomains}
|
||||
status={iconStatus}
|
||||
isAnimating={isGroupLive}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</RailGlyph>
|
||||
<span
|
||||
className={cn(
|
||||
'tool-status-text min-w-0 truncate font-medium',
|
||||
|
|
@ -621,6 +637,11 @@ export default function ToolCallGroup({
|
|||
>
|
||||
{shouldRenderBody && (
|
||||
<div className={cn('overflow-hidden', FOLD_RAIL_CLASSES)} ref={expandRef}>
|
||||
<FoldRail
|
||||
hover={railHover}
|
||||
expanded={isExpanded && (phaseHeader?.expanded ?? true)}
|
||||
onCollapse={handleRailCollapse}
|
||||
/>
|
||||
<ToolAuthWarningContext.Provider value>
|
||||
<FailedRevealContext.Provider value={revealValue}>
|
||||
<SoleToolContext.Provider value={phaseSole ?? count === 1}>
|
||||
|
|
|
|||
|
|
@ -422,7 +422,14 @@ describe('ActivityPhaseGroup', () => {
|
|||
|
||||
const trigger = screen.getByRole('button', { name: LABEL });
|
||||
fireEvent.click(trigger);
|
||||
const rail = screen.getByTestId('fold-rail');
|
||||
fireEvent.mouseEnter(rail);
|
||||
expect(screen.getByTestId('fold-rail-knob')).toBeInTheDocument();
|
||||
fireEvent.click(trigger);
|
||||
expect(screen.queryByTestId('fold-rail-knob')).toBeNull();
|
||||
expect(rail).toBeDisabled();
|
||||
fireEvent.mouseEnter(rail);
|
||||
expect(screen.queryByTestId('fold-rail-knob')).toBeNull();
|
||||
fireEvent.transitionEnd(screen.getByTestId('activity-phase-panel'));
|
||||
expect(screen.getByTestId('phase-content')).toBeInTheDocument();
|
||||
|
||||
|
|
@ -694,6 +701,26 @@ describe('ActivityPhaseGroup open header', () => {
|
|||
expect(pinned).not.toHaveClass('bg-presentation');
|
||||
expect(screen.getByTestId('activity-phase-panel').firstElementChild).toHaveClass('pl-6');
|
||||
});
|
||||
|
||||
test('collapses from its rail, showing the knob on its header while the rail is hovered', () => {
|
||||
render(
|
||||
<ActivityPhaseGroup labelPart={labelPart} hasContent>
|
||||
<div data-testid="phase-content" />
|
||||
</ActivityPhaseGroup>,
|
||||
);
|
||||
const header = screen.getByRole('button', { name: LABEL });
|
||||
fireEvent.click(header);
|
||||
const rail = screen.getByTestId('fold-rail');
|
||||
expect(rail).toHaveAttribute('aria-hidden', 'true');
|
||||
fireEvent.mouseEnter(rail);
|
||||
expect(header).toContainElement(screen.getByTestId('fold-rail-knob'));
|
||||
fireEvent.mouseLeave(rail);
|
||||
expect(screen.queryByTestId('fold-rail-knob')).toBeNull();
|
||||
fireEvent.click(rail);
|
||||
expect(header).toHaveAttribute('aria-expanded', 'false');
|
||||
fireEvent.click(rail);
|
||||
expect(header).toHaveAttribute('aria-expanded', 'false');
|
||||
});
|
||||
});
|
||||
|
||||
describe('ActivityPhaseGroup open live header', () => {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import { SoleToolContext, useToolAutoExpand } from '../disclosure';
|
|||
import { FailedRevealContext, useFailedReveal } from '../reveal';
|
||||
import { scheduleMessageContentLayoutReconcile } from '~/hooks';
|
||||
import ToolCallGroup from '../ToolCallGroup';
|
||||
import { FoldHeaderContext } from '../rail';
|
||||
import { ToolAuthWarning } from '../auth';
|
||||
|
||||
const mockMCPServerNames: string[] = [];
|
||||
|
|
@ -135,6 +136,7 @@ jest.mock('lucide-react', () => ({
|
|||
MessageCircleQuestion: () => <span data-testid="question-icon">{'question'}</span>,
|
||||
ListChecks: () => <span data-testid="task-check-icon">{'checks'}</span>,
|
||||
TriangleAlert: () => <span>{'warning'}</span>,
|
||||
CircleMinus: () => <span>{'collapse'}</span>,
|
||||
}));
|
||||
|
||||
const mockSubmittedAskAnswers = new Map<string, string>();
|
||||
|
|
@ -621,8 +623,15 @@ describe('ToolCallGroup image hoisting', () => {
|
|||
const button = screen.getByRole('button', { name: /^Ran 2 actions/ });
|
||||
const collapsible = screen.getByTestId('tool-call-group-panel');
|
||||
expect(screen.getByTestId('approval-0')).toBeInTheDocument();
|
||||
const rail = screen.getByTestId('fold-rail');
|
||||
fireEvent.mouseEnter(rail);
|
||||
expect(screen.getByTestId('fold-rail-knob')).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(button);
|
||||
expect(screen.queryByTestId('fold-rail-knob')).toBeNull();
|
||||
expect(rail).toBeDisabled();
|
||||
fireEvent.mouseEnter(rail);
|
||||
expect(screen.queryByTestId('fold-rail-knob')).toBeNull();
|
||||
fireEvent.transitionEnd(collapsible);
|
||||
|
||||
expect(button).toHaveAttribute('aria-expanded', 'false');
|
||||
|
|
@ -630,6 +639,31 @@ describe('ToolCallGroup image hoisting', () => {
|
|||
expect(screen.getByTestId('approval-1')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('clears a retained group rail when its containing phase collapses', () => {
|
||||
const phaseHeader = { current: document.createElement('div') };
|
||||
const groupProps = {
|
||||
...baseProps,
|
||||
parts: [{ part: makeApprovalPart('t1'), idx: 0 }],
|
||||
};
|
||||
const group = (expanded: boolean) => (
|
||||
<RecoilRoot>
|
||||
<FoldHeaderContext.Provider value={{ header: phaseHeader, expanded }}>
|
||||
<ToolCallGroup {...groupProps} />
|
||||
</FoldHeaderContext.Provider>
|
||||
</RecoilRoot>
|
||||
);
|
||||
const { rerender } = render(group(true));
|
||||
const rail = screen.getByTestId('fold-rail');
|
||||
fireEvent.mouseEnter(rail);
|
||||
expect(screen.getByTestId('fold-rail-knob')).toBeInTheDocument();
|
||||
rerender(group(false));
|
||||
expect(screen.queryByTestId('fold-rail-knob')).toBeNull();
|
||||
expect(rail).toBeDisabled();
|
||||
rerender(group(true));
|
||||
expect(screen.queryByTestId('fold-rail-knob')).toBeNull();
|
||||
expect(rail).not.toBeDisabled();
|
||||
});
|
||||
|
||||
it('keeps deeply nested unresolved approval bodies mounted while the group is collapsed', () => {
|
||||
const nestedApprovalParts = [
|
||||
{
|
||||
|
|
@ -1419,6 +1453,24 @@ describe('ToolCallGroup failure fast path', () => {
|
|||
expect(header).toHaveClass('text-text-primary');
|
||||
expect(screen.getByTestId('tool-call-group-panel').firstElementChild).toHaveClass('pl-6');
|
||||
});
|
||||
|
||||
it('collapses from its rail, showing the knob on its header while the rail is hovered', () => {
|
||||
renderGroup(props(jest.fn()));
|
||||
const header = screen.getByRole('button', { name: /· 1\/2 failed$/ });
|
||||
fireEvent.click(header);
|
||||
const rail = screen.getByTestId('fold-rail');
|
||||
expect(rail).toHaveAttribute('tabindex', '-1');
|
||||
expect(rail).toHaveAttribute('aria-hidden', 'true');
|
||||
expect(screen.queryByTestId('fold-rail-knob')).toBeNull();
|
||||
fireEvent.mouseEnter(rail);
|
||||
expect(header).toContainElement(screen.getByTestId('fold-rail-knob'));
|
||||
fireEvent.click(rail);
|
||||
expect(header).toHaveAttribute('aria-expanded', 'false');
|
||||
expect(screen.queryByTestId('fold-rail-knob')).toBeNull();
|
||||
/** Still drawn while the panel animates shut: a second click is a no-op. */
|
||||
fireEvent.click(rail);
|
||||
expect(header).toHaveAttribute('aria-expanded', 'false');
|
||||
});
|
||||
});
|
||||
|
||||
describe('grouped tool preparation', () => {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,130 @@
|
|||
import React from 'react';
|
||||
import { render, renderHook, screen, fireEvent } from '@testing-library/react';
|
||||
import { FoldRail, RailGlyph, revealFoldHeader, useRailHover } from '../rail';
|
||||
|
||||
function box(top: number, height = 28) {
|
||||
const el = document.createElement('div');
|
||||
el.getBoundingClientRect = () => ({ top, height }) as DOMRect;
|
||||
el.scrollIntoView = jest.fn();
|
||||
return el;
|
||||
}
|
||||
|
||||
describe('revealFoldHeader', () => {
|
||||
it('scrolls the card to its start when the header is pinned below the card top', () => {
|
||||
const root = box(-400);
|
||||
const header = box(0);
|
||||
revealFoldHeader(root, header);
|
||||
expect(root.scrollIntoView).toHaveBeenCalledWith({ block: 'start', behavior: 'instant' });
|
||||
expect(header.scrollIntoView).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('brings an unpinned header just into view', () => {
|
||||
const root = box(-400);
|
||||
const header = box(-400);
|
||||
revealFoldHeader(root, header);
|
||||
expect(header.scrollIntoView).toHaveBeenCalledWith({ block: 'nearest', behavior: 'instant' });
|
||||
expect(root.scrollIntoView).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('reserves the sticky phase header height and restores the previous margin', () => {
|
||||
const root = box(-400);
|
||||
const header = box(-400);
|
||||
const phaseHeader = box(0, 36);
|
||||
header.style.scrollMarginTop = '8px';
|
||||
header.scrollIntoView = jest.fn(() => {
|
||||
expect(header.style.scrollMarginTop).toBe('36px');
|
||||
});
|
||||
revealFoldHeader(root, header, phaseHeader);
|
||||
expect(header.scrollIntoView).toHaveBeenCalledWith({ block: 'nearest', behavior: 'instant' });
|
||||
expect(header.style.scrollMarginTop).toBe('8px');
|
||||
});
|
||||
|
||||
it.each([false, true])('reserves the host toolbar for a pinned=%s header', (pinned) => {
|
||||
const message = box(0);
|
||||
message.className = 'message-render';
|
||||
message.style.scrollMarginTop = '64px';
|
||||
const root = box(-400);
|
||||
const header = box(pinned ? 0 : -400);
|
||||
message.append(root);
|
||||
root.append(header);
|
||||
const target = pinned ? root : header;
|
||||
const phaseHeader = pinned ? null : box(0, 36);
|
||||
target.scrollIntoView = jest.fn(() => {
|
||||
expect(target.style.scrollMarginTop).toBe(pinned ? '64px' : '100px');
|
||||
});
|
||||
revealFoldHeader(root, header, phaseHeader);
|
||||
expect(target.scrollIntoView).toHaveBeenCalledWith({
|
||||
block: pinned ? 'start' : 'nearest',
|
||||
behavior: 'instant',
|
||||
});
|
||||
expect(target.style.scrollMarginTop).toBe('');
|
||||
});
|
||||
|
||||
it('does nothing after the fold has unmounted', () => {
|
||||
expect(() => revealFoldHeader(null, null)).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('FoldRail', () => {
|
||||
it('drops the knob when it unmounts under the pointer', () => {
|
||||
const { result } = renderHook(() => useRailHover());
|
||||
const hover = result.current;
|
||||
const { unmount } = render(<FoldRail hover={hover} expanded onCollapse={jest.fn()} />);
|
||||
render(
|
||||
<RailGlyph hover={hover}>
|
||||
<span data-testid="glyph" />
|
||||
</RailGlyph>,
|
||||
);
|
||||
fireEvent.mouseEnter(screen.getByTestId('fold-rail'));
|
||||
expect(screen.getByTestId('fold-rail-knob')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('glyph').parentElement).toHaveClass('invisible');
|
||||
unmount();
|
||||
expect(screen.queryByTestId('fold-rail-knob')).toBeNull();
|
||||
expect(screen.getByTestId('glyph').parentElement).not.toHaveClass('invisible');
|
||||
});
|
||||
|
||||
it('keeps the same glyph mounted in layout across hover transitions', () => {
|
||||
const { result } = renderHook(() => useRailHover());
|
||||
const hover = result.current;
|
||||
const mounted = jest.fn();
|
||||
const unmounted = jest.fn();
|
||||
function Glyph() {
|
||||
React.useEffect(() => {
|
||||
mounted();
|
||||
return unmounted;
|
||||
}, []);
|
||||
return <span data-testid="glyph" />;
|
||||
}
|
||||
render(
|
||||
<>
|
||||
<FoldRail hover={hover} expanded onCollapse={jest.fn()} />
|
||||
<RailGlyph hover={hover}>
|
||||
<Glyph />
|
||||
</RailGlyph>
|
||||
</>,
|
||||
);
|
||||
const glyph = screen.getByTestId('glyph');
|
||||
fireEvent.mouseEnter(screen.getByTestId('fold-rail'));
|
||||
expect(screen.getByTestId('glyph')).toBe(glyph);
|
||||
fireEvent.mouseLeave(screen.getByTestId('fold-rail'));
|
||||
expect(screen.getByTestId('glyph')).toBe(glyph);
|
||||
expect(mounted).toHaveBeenCalledTimes(1);
|
||||
expect(unmounted).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('clears hover and disables the rail when a retained body collapses', () => {
|
||||
const { result } = renderHook(() => useRailHover());
|
||||
const hover = result.current;
|
||||
const collapse = jest.fn();
|
||||
const { rerender } = render(<FoldRail hover={hover} expanded onCollapse={collapse} />);
|
||||
fireEvent.mouseEnter(screen.getByTestId('fold-rail'));
|
||||
expect(hover.get()).toBe(true);
|
||||
rerender(<FoldRail hover={hover} expanded={false} onCollapse={collapse} />);
|
||||
expect(hover.get()).toBe(false);
|
||||
fireEvent.mouseEnter(screen.getByTestId('fold-rail'));
|
||||
fireEvent.click(screen.getByTestId('fold-rail'));
|
||||
expect(hover.get()).toBe(false);
|
||||
expect(collapse).not.toHaveBeenCalled();
|
||||
expect(screen.getByTestId('fold-rail')).toBeDisabled();
|
||||
});
|
||||
});
|
||||
143
client/src/components/Chat/Messages/Content/rail.tsx
Normal file
143
client/src/components/Chat/Messages/Content/rail.tsx
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
import { createContext, useEffect, useState, useSyncExternalStore } from 'react';
|
||||
import { CircleMinus } from 'lucide-react';
|
||||
import type { ReactNode, RefObject } from 'react';
|
||||
import { ROW_GLYPH_SLOT } from './rows';
|
||||
import { cn } from '~/utils';
|
||||
|
||||
/**
|
||||
* Whether a fold's rail is under the pointer. The rail lives in the panel and
|
||||
* the knob in the header, so the two share this tiny store instead of state on
|
||||
* the fold: hovering repaints the header's glyph, not every row under it.
|
||||
*/
|
||||
export type RailHover = {
|
||||
get: () => boolean;
|
||||
set: (hovered: boolean) => void;
|
||||
subscribe: (listener: () => void) => () => void;
|
||||
};
|
||||
|
||||
export const FoldHeaderContext = createContext<{
|
||||
header: RefObject<HTMLDivElement>;
|
||||
expanded: boolean;
|
||||
} | null>(null);
|
||||
|
||||
function createRailHover(): RailHover {
|
||||
let hovered = false;
|
||||
const listeners = new Set<() => void>();
|
||||
return {
|
||||
get: () => hovered,
|
||||
set: (next) => {
|
||||
if (next === hovered) {
|
||||
return;
|
||||
}
|
||||
hovered = next;
|
||||
listeners.forEach((listener) => listener());
|
||||
},
|
||||
subscribe: (listener) => {
|
||||
listeners.add(listener);
|
||||
return () => {
|
||||
listeners.delete(listener);
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function useRailHover(): RailHover {
|
||||
const [hover] = useState(createRailHover);
|
||||
return hover;
|
||||
}
|
||||
|
||||
/** The header's glyph, swapped for a collapse knob while the fold's rail is
|
||||
* hovered, so the reader sees which fold the rail closes before clicking. */
|
||||
export function RailGlyph({ hover, children }: { hover: RailHover; children: ReactNode }) {
|
||||
const hovered = useSyncExternalStore(hover.subscribe, hover.get, hover.get);
|
||||
return (
|
||||
<span className={cn(ROW_GLYPH_SLOT, 'relative')} aria-hidden="true">
|
||||
<span className={cn('flex', hovered && 'invisible')}>{children}</span>
|
||||
{hovered && (
|
||||
<span
|
||||
className={cn(ROW_GLYPH_SLOT, 'text-text-primary absolute inset-y-0 left-0')}
|
||||
data-testid="fold-rail-knob"
|
||||
>
|
||||
<CircleMinus size={16} />
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The hairline that hangs from an open header's glyph, drawn inside a hit area
|
||||
* the width of the panel's inset so the pointer only has to come near it. A
|
||||
* pointer shortcut only: the header button stays the one control keyboard and
|
||||
* screen-reader users reach, so the rail is hidden from both and never takes
|
||||
* focus. Must sit in a `FOLD_RAIL_CLASSES` wrapper, whose inset it fills.
|
||||
*/
|
||||
export function FoldRail({
|
||||
hover,
|
||||
expanded,
|
||||
onCollapse,
|
||||
}: {
|
||||
hover: RailHover;
|
||||
expanded: boolean;
|
||||
onCollapse: () => void;
|
||||
}) {
|
||||
/** Collapsed approval bodies stay mounted without a pointer leave. */
|
||||
useEffect(() => {
|
||||
if (!expanded) {
|
||||
hover.set(false);
|
||||
}
|
||||
return () => hover.set(false);
|
||||
}, [hover, expanded]);
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
tabIndex={-1}
|
||||
aria-hidden="true"
|
||||
disabled={!expanded}
|
||||
className="group/rail absolute inset-y-0 left-0 w-6 cursor-pointer disabled:pointer-events-none"
|
||||
onMouseDown={(event) => event.preventDefault()}
|
||||
onMouseEnter={() => expanded && hover.set(true)}
|
||||
onMouseLeave={() => hover.set(false)}
|
||||
onClick={() => {
|
||||
hover.set(false);
|
||||
onCollapse();
|
||||
}}
|
||||
data-testid="fold-rail"
|
||||
>
|
||||
<span className="bg-border-medium group-hover/rail:bg-text-secondary absolute top-0.5 bottom-1.5 left-[11px] w-px transition-colors duration-150 motion-reduce:transition-none" />
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collapsing from a rail far down a long fold would leave the reader below the
|
||||
* point where the fold now ends, so its header comes back into view first. A
|
||||
* pinned (sticky) header sits below its card's top; scrolling the card to the
|
||||
* start puts the header where it will rest once the rows are gone.
|
||||
*/
|
||||
export function revealFoldHeader(
|
||||
root: HTMLElement | null,
|
||||
header: HTMLElement | null,
|
||||
stickyHeader?: HTMLElement | null,
|
||||
) {
|
||||
if (root == null || header == null || typeof root.scrollIntoView !== 'function') {
|
||||
return;
|
||||
}
|
||||
const pinned = root.getBoundingClientRect().top < header.getBoundingClientRect().top;
|
||||
const target = pinned ? root : header;
|
||||
const previousMargin = target.style.scrollMarginTop;
|
||||
/** The host message's scroll margin reserves its overlaid chat toolbar. */
|
||||
const message = root.closest('.message-render');
|
||||
const hostMargin =
|
||||
message == null ? 0 : parseFloat(getComputedStyle(message).scrollMarginTop) || 0;
|
||||
const targetMargin = parseFloat(getComputedStyle(target).scrollMarginTop) || 0;
|
||||
target.style.scrollMarginTop = `${Math.max(
|
||||
targetMargin,
|
||||
hostMargin + (stickyHeader?.getBoundingClientRect().height ?? 0),
|
||||
)}px`;
|
||||
try {
|
||||
target.scrollIntoView({ block: pinned ? 'start' : 'nearest', behavior: 'instant' });
|
||||
} finally {
|
||||
target.style.scrollMarginTop = previousMargin;
|
||||
}
|
||||
}
|
||||
|
|
@ -21,13 +21,11 @@ export const ROW_GLYPH_SLOT = 'flex h-5 min-w-6 shrink-0 items-center justify-ce
|
|||
|
||||
/**
|
||||
* The panel under an open header: its rows step in by one glyph slot and a
|
||||
* hairline hangs from the header's glyph down their left, so what is under the
|
||||
* header reads as under it. Carried by the panel's clipping wrapper, the one
|
||||
* `overflow-hidden` element the fold animation needs: the inset then lies
|
||||
* INSIDE the clip, which is what lets a failed row's stripe, drawn 12px left
|
||||
* of the row, show at all. The line is a pseudo-element, out of flow, so the
|
||||
* height animation is untouched; it starts at the wrapper's top and stops
|
||||
* short of the last row's bottom margin.
|
||||
* hairline (`FoldRail`) hangs from the header's glyph down their left, so what
|
||||
* is under the header reads as under it. Carried by the panel's clipping
|
||||
* wrapper, the one `overflow-hidden` element the fold animation needs: the
|
||||
* inset then lies INSIDE the clip, which is what lets a failed row's stripe,
|
||||
* drawn 12px left of the row, show at all. The rail is absolutely positioned
|
||||
* in that inset, out of flow, so the height animation is untouched.
|
||||
*/
|
||||
export const FOLD_RAIL_CLASSES =
|
||||
"relative pl-6 before:absolute before:bottom-1.5 before:left-[11px] before:top-0.5 before:w-px before:bg-border-medium before:content-['']";
|
||||
export const FOLD_RAIL_CLASSES = 'relative pl-6';
|
||||
|
|
|
|||
|
|
@ -139,6 +139,38 @@ test.describe('activity fold', () => {
|
|||
await expect(panel).toContainText(/error/i);
|
||||
await shot(page, 'revealed-by-pill');
|
||||
|
||||
/** Each open fold's rail lights up under the pointer, swaps ITS header's
|
||||
* glyph for a collapse knob, and collapses only that fold. */
|
||||
const knobs = messagesView(page).getByTestId('fold-rail-knob');
|
||||
const phaseRail = messagesView(page)
|
||||
.getByTestId('activity-phase-panel')
|
||||
.locator('> div > [data-testid="fold-rail"]');
|
||||
const groupPanel = messagesView(page)
|
||||
.getByTestId('tool-call-group-panel')
|
||||
.filter({ has: page.getByTestId('tool-call').filter({ hasText: /^Failed:/ }) });
|
||||
const groupRail = groupPanel.locator('> div > [data-testid="fold-rail"]');
|
||||
const groupHeader = groupPanel.locator('xpath=preceding-sibling::div[1]').getByRole('button');
|
||||
const lineColor = (railLocator: typeof groupRail) =>
|
||||
railLocator.locator('span').evaluate((line) => getComputedStyle(line).backgroundColor);
|
||||
const restingLine = await lineColor(groupRail);
|
||||
await expect(knobs).toHaveCount(0);
|
||||
await groupRail.hover();
|
||||
await expect(knobs).toHaveCount(1);
|
||||
await expect(groupHeader.getByTestId('fold-rail-knob')).toBeVisible();
|
||||
await expect.poll(() => lineColor(groupRail)).not.toBe(restingLine);
|
||||
await shot(page, 'group-rail-hover');
|
||||
await phaseRail.hover();
|
||||
await expect(knobs).toHaveCount(1);
|
||||
await expect(header.getByTestId('fold-rail-knob')).toBeVisible();
|
||||
await shot(page, 'phase-rail-hover');
|
||||
await groupRail.click();
|
||||
await expect(groupHeader).toHaveAttribute('aria-expanded', 'false');
|
||||
await expect(header).toHaveAttribute('aria-expanded', 'true');
|
||||
await phaseRail.click();
|
||||
await expect(header).toHaveAttribute('aria-expanded', 'false');
|
||||
await expect(knobs).toHaveCount(0);
|
||||
await shot(page, 'rail-collapsed');
|
||||
|
||||
/** Survives a reload from the persisted message. */
|
||||
await page.reload();
|
||||
const reloaded = messagesView(page).getByRole('button', { name: PHASE_LABEL, exact: true });
|
||||
|
|
@ -185,6 +217,60 @@ test.describe('activity fold', () => {
|
|||
await groupHeader.click();
|
||||
await expect(messagesView(page).locator('[data-testid="tool-call"]').first()).toBeVisible();
|
||||
await shot(page, 'clean-open-group');
|
||||
|
||||
const phaseRail = messagesView(page)
|
||||
.getByTestId('activity-phase-panel')
|
||||
.locator('> div > [data-testid="fold-rail"]');
|
||||
const groupRail = group.locator('> div > [data-testid="fold-rail"]');
|
||||
const phaseLabel = header.getByText(PHASE_LABEL, { exact: true });
|
||||
const beforeHover = await phaseLabel.boundingBox();
|
||||
await phaseRail.hover();
|
||||
await expect(header.getByTestId('fold-rail-knob')).toBeVisible();
|
||||
expect(await phaseLabel.boundingBox()).toEqual(beforeHover);
|
||||
|
||||
const groupLabel = groupHeader.locator('[role="status"]');
|
||||
const beforeGroupHover = await groupLabel.boundingBox();
|
||||
await groupRail.hover();
|
||||
await expect(groupHeader.getByTestId('fold-rail-knob')).toBeVisible();
|
||||
expect(await groupLabel.boundingBox()).toEqual(beforeGroupHover);
|
||||
|
||||
/** Keep room below the historical fold so collapse cannot clamp away an overlap. */
|
||||
const phaseCard = messagesView(page).getByTestId('activity-phase-card');
|
||||
await phaseCard.evaluate((card) => {
|
||||
const spacer = document.createElement('div');
|
||||
spacer.style.height = '1200px';
|
||||
card.after(spacer);
|
||||
});
|
||||
await group.locator('> div > div').evaluate((rows) => {
|
||||
(rows as HTMLElement).style.paddingBottom = '1200px';
|
||||
});
|
||||
await groupRail.click({ position: { x: 12, y: 900 } });
|
||||
await expect(groupHeader).toHaveAttribute('aria-expanded', 'false');
|
||||
await expect(header).toHaveAttribute('aria-expanded', 'true');
|
||||
await expect
|
||||
.poll(async () => {
|
||||
const phaseBox = await header.boundingBox();
|
||||
const groupBox = await groupHeader.boundingBox();
|
||||
return phaseBox != null && groupBox != null && groupBox.y >= phaseBox.y + phaseBox.height;
|
||||
})
|
||||
.toBe(true);
|
||||
const isPointerAccessible = (button: typeof header) =>
|
||||
button.evaluate((element) => {
|
||||
const box = element.getBoundingClientRect();
|
||||
return element.contains(document.elementFromPoint(box.left + 12, box.top + box.height / 2));
|
||||
});
|
||||
await expect(groupHeader).toBeInViewport();
|
||||
await expect.poll(() => isPointerAccessible(groupHeader)).toBe(true);
|
||||
await shot(page, 'nested-rail-revealed');
|
||||
|
||||
await phaseRail.locator('..').evaluate((rows) => {
|
||||
(rows as HTMLElement).style.paddingBottom = '1200px';
|
||||
});
|
||||
await phaseRail.click({ position: { x: 12, y: 900 } });
|
||||
await expect(header).toHaveAttribute('aria-expanded', 'false');
|
||||
await expect(header).toBeInViewport();
|
||||
await expect.poll(() => isPointerAccessible(header)).toBe(true);
|
||||
await shot(page, 'phase-rail-revealed');
|
||||
expect(problems.filter((line) => !line.includes('favicon'))).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue