feat(webui): inspect file context in a live graph sidebar (#6562)
* feat(webui): inspect file context in a live graph sidebar * refactor(webui): place preview controls in the shared sidebar header * fix(webui): synchronize npm lockfile for graph dependencies * fix(webui): preserve compacted context and refine graph inspection * fix(context): harden provenance, request usage, and drawer lifecycle * fix(webui): align source skeleton and browser fixtures * perf(context): reuse source analysis and capture request graphs once * perf(webui): compact graph history and render graph motion on canvas * fix(webui): remove redundant context sidebar header * style(webui): clarify context replay icon * perf(webui): pace context replay on an adaptive timeline * feat(webui): briefly reveal labels for new context nodes * fix(webui): reveal new labels only for loaded context files * fix(webui): restart graph animation clock after idle * fix(webui): reveal labels when replay reverses node exit * refactor(context): project file-use facts without retention tracking * refactor(context): observe file reads outside agent execution * fix(webui): animate context sidebar closing * refactor(context): remove unused graph and usage panel code * refactor(context): isolate prompt observation from execution metadata * fix(webui): clarify cache rate and show context token usage * fix(webui): abbreviate context token counts in thousands * fix(webui): omit token unit from context usage details * fix(webui): restore large source preview highlighting and panel elevation * fix(webui): distinguish modifier scrolling from keyboard focus * fix(webui): align preview tab and panel corner radii * fix(webui): attach file tabs to the header divider * fix(webui): use one action to toggle file preview mode * fix(webui): use hover hint for context replay * fix(webui): share composer layout between graph and file sidebars * fix(webui): align sidebar bottom with composer surface * fix(webui): restore mobile graph drawing and touch file details * test(webui): align legacy cache assertions with current label * perf(context): retain source analysis and bound graph animation work * style(webui): add light elevation to user messages * test(webui): await lazy graph scene before replay assertions * fix(webui): align preview skeleton and browser contracts
This commit is contained in:
1 parent
2b44088413
commit
519ed81132
132 files changed
+4673
-1159
No files matched your search
@@ -1,82 +1,72 @@
|
||||
import { Fragment, useCallback, useMemo, useRef, useState, type PointerEvent, type ReactNode } from "react";
|
||||
import { useEffect, useId, useRef, useState, type KeyboardEvent } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import {
|
||||
Popover,
|
||||
PopoverContent,
|
||||
PopoverTrigger,
|
||||
} from "@/components/ui/popover";
|
||||
import { Sheet, SheetContent, SheetTitle, SheetTrigger } from "@/components/ui/sheet";
|
||||
import { FloatingPortalContext } from "@/components/ui/floating-portal";
|
||||
import {
|
||||
Tooltip,
|
||||
TooltipContent,
|
||||
TooltipProvider,
|
||||
TooltipTrigger,
|
||||
} from "@/components/ui/tooltip";
|
||||
import { formatCompactTokenCount } from "@/lib/format";
|
||||
import { formatTurnLatency } from "@/lib/format-time";
|
||||
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
|
||||
import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog";
|
||||
import { SidebarPanel } from "@/components/ui/sidebar-panel";
|
||||
import { ContextInspector } from "./ContextInspector";
|
||||
import type { ContextSnapshot } from "@/lib/types";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
export interface ComposerContextUsage {
|
||||
estimated?: boolean;
|
||||
contextTokens: number;
|
||||
contextWindowTokens?: number;
|
||||
}
|
||||
|
||||
export interface ComposerRoundUsage {
|
||||
id: string;
|
||||
timestamp: number;
|
||||
inputTokens: number;
|
||||
outputTokens?: number;
|
||||
cachedTokens?: number;
|
||||
estimatedTokens?: number;
|
||||
generationMs?: number;
|
||||
}
|
||||
|
||||
interface NormalizedRoundUsage extends ComposerRoundUsage {
|
||||
cachedTokens?: number;
|
||||
outputTokens: number;
|
||||
}
|
||||
|
||||
function normalizeRounds(
|
||||
rounds: readonly ComposerRoundUsage[],
|
||||
): NormalizedRoundUsage[] {
|
||||
return rounds
|
||||
.filter((round) => Number.isFinite(round.inputTokens) && round.inputTokens > 0)
|
||||
.map((round) => ({
|
||||
...round,
|
||||
inputTokens: Math.max(0, round.inputTokens),
|
||||
outputTokens: Number.isFinite(round.outputTokens)
|
||||
? Math.max(0, round.outputTokens ?? 0)
|
||||
: 0,
|
||||
...(Number.isFinite(round.cachedTokens)
|
||||
? { cachedTokens: Math.min(round.inputTokens, Math.max(0, round.cachedTokens ?? 0)) }
|
||||
: {}),
|
||||
}));
|
||||
}
|
||||
|
||||
export function ComposerUsagePopover({
|
||||
context,
|
||||
rounds,
|
||||
snapshots = [],
|
||||
onOpenFile,
|
||||
showLabel = false,
|
||||
compactLabel = false,
|
||||
bottomSheet = false,
|
||||
sidebarHost,
|
||||
triggerHost,
|
||||
open: controlledOpen,
|
||||
onOpenChange,
|
||||
}: {
|
||||
context: ComposerContextUsage | null;
|
||||
rounds: readonly ComposerRoundUsage[];
|
||||
snapshots?: readonly ContextSnapshot[];
|
||||
onOpenFile?: (path: string) => void;
|
||||
showLabel?: boolean;
|
||||
compactLabel?: boolean;
|
||||
bottomSheet?: boolean;
|
||||
sidebarHost?: HTMLElement | null;
|
||||
triggerHost?: HTMLElement | null;
|
||||
open?: boolean;
|
||||
onOpenChange?: (open: boolean) => void;
|
||||
}) {
|
||||
const { t, i18n } = useTranslation();
|
||||
const [open, setOpen] = useState(false);
|
||||
const normalizedRounds = useMemo(() => normalizeRounds(rounds), [rounds]);
|
||||
const { t } = useTranslation();
|
||||
const [localOpen, setLocalOpen] = useState(false);
|
||||
const open = controlledOpen ?? localOpen;
|
||||
const interactedOutside = useRef(false);
|
||||
const setOpen = (next: boolean) => { if (next) interactedOutside.current = false; setLocalOpen(next); onOpenChange?.(next); };
|
||||
const triggerRef = useRef<HTMLButtonElement>(null);
|
||||
const panelId = useId();
|
||||
const [narrow, setNarrow] = useState(false);
|
||||
useEffect(() => {
|
||||
const measure = () => setNarrow((sidebarHost?.clientWidth ?? window.innerWidth) < 780);
|
||||
measure();
|
||||
const observer = new ResizeObserver(measure);
|
||||
if (sidebarHost) observer.observe(sidebarHost);
|
||||
window.addEventListener("resize", measure);
|
||||
return () => { observer.disconnect(); window.removeEventListener("resize", measure); };
|
||||
}, [sidebarHost]);
|
||||
const close = () => { setOpen(false); requestAnimationFrame(() => triggerRef.current?.focus()); };
|
||||
const hasRounds = rounds.some((round) => Number.isFinite(round.inputTokens) && round.inputTokens > 0);
|
||||
const hasContext = !!context
|
||||
&& Number.isFinite(context.contextTokens)
|
||||
&& context.contextTokens >= 0
|
||||
&& Number.isFinite(context.contextWindowTokens)
|
||||
&& (context.contextWindowTokens ?? 0) > 0;
|
||||
if (!hasContext && normalizedRounds.length === 0) {
|
||||
if (!open && !hasContext && !hasRounds && !snapshots.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -99,37 +89,21 @@ export function ComposerUsagePopover({
|
||||
? detailsLabel
|
||||
: t("thread.composer.context.tooltip", {
|
||||
defaultValue: "Context {{percent}}%",
|
||||
percent: contextPercentage,
|
||||
percent: `${context?.estimated ? "≈" : ""}${contextPercentage}`,
|
||||
});
|
||||
const triggerLabel = contextPercentage === null
|
||||
? detailsLabel
|
||||
: `${contextDescription}. ${detailsLabel}`;
|
||||
const ringCircumference = 2 * Math.PI * 6;
|
||||
const ringLength = ringCircumference * meterPercentage / 100;
|
||||
const maxInputTokens = Math.max(0, ...normalizedRounds.map((round) => round.inputTokens));
|
||||
const numberFormatter = new Intl.NumberFormat(i18n.language);
|
||||
const percentageFormatter = new Intl.NumberFormat(i18n.language, {
|
||||
style: "percent",
|
||||
maximumFractionDigits: 0,
|
||||
});
|
||||
const roundDateFormatter = new Intl.DateTimeFormat(i18n.language, {
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
});
|
||||
|
||||
const Root = bottomSheet ? Sheet : Popover;
|
||||
const Trigger = bottomSheet ? SheetTrigger : PopoverTrigger;
|
||||
const RoundRoot = bottomSheet ? Popover : Tooltip;
|
||||
const RoundTrigger = bottomSheet ? PopoverTrigger : TooltipTrigger;
|
||||
const RoundContent = bottomSheet ? PopoverContent : TooltipContent;
|
||||
const RoundBar = bottomSheet ? "button" : "span";
|
||||
const showTextLabel = showLabel && (!compactLabel || contextPercentage !== null);
|
||||
const trigger = (
|
||||
<Trigger asChild>
|
||||
<button
|
||||
type="button"
|
||||
ref={triggerRef}
|
||||
onClick={() => setOpen(!open)}
|
||||
aria-expanded={open}
|
||||
aria-controls={open ? panelId : undefined}
|
||||
data-testid="composer-context-usage"
|
||||
aria-label={triggerLabel}
|
||||
className={cn(
|
||||
@@ -184,372 +158,43 @@ export function ComposerUsagePopover({
|
||||
)}
|
||||
{showTextLabel ? (
|
||||
<span aria-hidden className="whitespace-nowrap tabular-nums">
|
||||
{compactLabel && contextPercentage !== null ? `${contextPercentage}%` : contextDescription}
|
||||
{compactLabel && contextPercentage !== null ? `${context?.estimated ? "≈" : ""}${contextPercentage}%` : contextDescription}
|
||||
</span>
|
||||
) : null}
|
||||
</button>
|
||||
</Trigger>
|
||||
);
|
||||
|
||||
return (
|
||||
<Root open={open} onOpenChange={setOpen}>
|
||||
<TooltipProvider>
|
||||
{bottomSheet ? trigger : (
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>{trigger}</TooltipTrigger>
|
||||
<TooltipContent
|
||||
side="top"
|
||||
align="center"
|
||||
sideOffset={8}
|
||||
className="w-fit max-w-[calc(100vw-2rem)] rounded-full border-border/70 px-2.5 py-1 text-xs font-medium shadow-[0_8px_24px_rgba(15,23,42,0.13)]"
|
||||
>
|
||||
<span className="whitespace-nowrap tabular-nums">{contextDescription}</span>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
)}
|
||||
const triggerWithTooltip = <TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>{trigger}</TooltipTrigger>
|
||||
<TooltipContent side="top" className="text-xs">{contextDescription}</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>;
|
||||
|
||||
<UsagePanel bottomSheet={bottomSheet} onDismiss={() => setOpen(false)}>
|
||||
<div className="px-4 pb-4 pt-3.5">
|
||||
{contextPercentage !== null ? (
|
||||
<>
|
||||
<div className="flex items-baseline justify-between gap-3">
|
||||
<div className="flex min-w-0 items-baseline gap-2">
|
||||
<span className={cn("shrink-0 font-medium text-foreground", bottomSheet ? "text-sm" : "text-[12px]")}>
|
||||
{t("thread.composer.context.contextTitle", {
|
||||
defaultValue: "Context",
|
||||
})}
|
||||
</span>
|
||||
<span className={cn("truncate tabular-nums text-muted-foreground", bottomSheet ? "text-xs" : "text-[11px]")}>
|
||||
{formatCompactTokenCount(context!.contextTokens)} / {formatCompactTokenCount(
|
||||
context!.contextWindowTokens!,
|
||||
)}
|
||||
</span>
|
||||
</div>
|
||||
<span className={cn("shrink-0 tabular-nums text-muted-foreground", bottomSheet ? "text-xs" : "text-[11px]")}>
|
||||
{contextPercentage}%
|
||||
</span>
|
||||
</div>
|
||||
<div
|
||||
role="progressbar"
|
||||
aria-label={contextDescription}
|
||||
aria-valuemin={0}
|
||||
aria-valuemax={100}
|
||||
aria-valuenow={contextPercentage}
|
||||
className="mt-2 h-1 overflow-hidden rounded-full bg-muted"
|
||||
>
|
||||
<div
|
||||
className={cn(
|
||||
"h-full rounded-full",
|
||||
status === "critical" && "bg-destructive",
|
||||
status === "caution" && "bg-amber-500",
|
||||
status === "normal" && "bg-foreground/45",
|
||||
)}
|
||||
style={{ width: `${contextPercentage}%` }}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
) : null}
|
||||
|
||||
{normalizedRounds.length > 0 ? (
|
||||
<>
|
||||
<div className={cn(
|
||||
"flex items-baseline justify-between gap-3",
|
||||
contextPercentage === null ? "mt-0" : "mt-5",
|
||||
)}>
|
||||
<span className={cn("font-medium text-foreground", bottomSheet ? "text-sm" : "text-[12px]")}>
|
||||
{t("thread.composer.context.recentRounds", {
|
||||
defaultValue: "Recent rounds",
|
||||
})}
|
||||
</span>
|
||||
<span className={cn("text-muted-foreground", bottomSheet ? "text-xs" : "text-[11px]")}>
|
||||
{t("thread.composer.context.inputTrend", {
|
||||
defaultValue: "Input tokens",
|
||||
})}
|
||||
</span>
|
||||
</div>
|
||||
<div
|
||||
role="group"
|
||||
aria-label={t("thread.composer.context.inputTrend", {
|
||||
defaultValue: "Input tokens",
|
||||
})}
|
||||
className="mt-2 flex h-28 items-end gap-1.5 border-b border-border/60"
|
||||
>
|
||||
{normalizedRounds.map((round, index) => {
|
||||
const cachedKnown = typeof round.cachedTokens === "number";
|
||||
const cachedTokens = round.cachedTokens ?? 0;
|
||||
const notReusedTokens = Math.max(0, round.inputTokens - cachedTokens);
|
||||
const cacheHitRate = cachedKnown
|
||||
? percentageFormatter.format(cachedTokens / round.inputTokens)
|
||||
: null;
|
||||
const cachedHeight = cachedKnown
|
||||
? cachedTokens / round.inputTokens * 100
|
||||
: 0;
|
||||
const barHeight = round.inputTokens / maxInputTokens * 108;
|
||||
const timestampLabel = roundDateFormatter.format(round.timestamp);
|
||||
const detailRows = [
|
||||
{
|
||||
key: "input",
|
||||
label: t("thread.composer.context.input", {
|
||||
defaultValue: "Input tokens",
|
||||
}),
|
||||
value: numberFormatter.format(round.inputTokens),
|
||||
},
|
||||
cachedKnown
|
||||
? {
|
||||
key: "cache-hit-rate",
|
||||
label: t("thread.composer.context.cacheHitRate", {
|
||||
defaultValue: "KV cache hit rate",
|
||||
}),
|
||||
value: cacheHitRate!,
|
||||
}
|
||||
: null,
|
||||
{
|
||||
key: "output",
|
||||
label: t("thread.composer.context.output", {
|
||||
defaultValue: "Output tokens",
|
||||
}),
|
||||
value: numberFormatter.format(round.outputTokens),
|
||||
},
|
||||
typeof round.generationMs === "number"
|
||||
? {
|
||||
key: "duration",
|
||||
label: t("thread.composer.context.duration", {
|
||||
defaultValue: "Generation time",
|
||||
}),
|
||||
value: formatTurnLatency(round.generationMs, i18n.language),
|
||||
}
|
||||
: null,
|
||||
].filter((row): row is NonNullable<typeof row> => !!row);
|
||||
const detailNote = (round.estimatedTokens ?? 0) > 0
|
||||
? t("message.usage.estimated", {
|
||||
defaultValue: "Includes estimated usage",
|
||||
})
|
||||
: null;
|
||||
const detailLabel = [
|
||||
timestampLabel,
|
||||
...detailRows.map((row) => `${row.label} ${row.value}`),
|
||||
detailNote,
|
||||
].filter((part): part is string => !!part).join(". ");
|
||||
|
||||
return (
|
||||
<span
|
||||
key={round.id}
|
||||
className={cn(
|
||||
"flex h-full min-w-0 flex-1 items-end justify-center rounded-sm",
|
||||
bottomSheet && "relative",
|
||||
"opacity-70 transition-opacity hover:opacity-100",
|
||||
index === 0 && "justify-start",
|
||||
normalizedRounds.length > 1
|
||||
&& index === normalizedRounds.length - 1
|
||||
&& "justify-end",
|
||||
index === normalizedRounds.length - 1 && "opacity-100",
|
||||
)}
|
||||
>
|
||||
<RoundRoot>
|
||||
<RoundTrigger asChild>
|
||||
<RoundBar
|
||||
type={bottomSheet ? "button" : undefined}
|
||||
role={bottomSheet ? undefined : "img"}
|
||||
tabIndex={0}
|
||||
aria-label={detailLabel}
|
||||
data-testid="round-usage-bar"
|
||||
className={cn(
|
||||
"flex w-full max-w-7 flex-col overflow-hidden rounded-t-[3px] bg-muted",
|
||||
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
|
||||
// Let touch users hit the entire column, even when a
|
||||
// round's proportional bar is less than a pixel high.
|
||||
bottomSheet && "before:absolute before:inset-0 before:content-[''] data-[state=open]:ring-2 data-[state=open]:ring-foreground/20",
|
||||
)}
|
||||
style={{ height: `${barHeight}px` }}
|
||||
>
|
||||
{cachedKnown ? (
|
||||
<>
|
||||
{notReusedTokens > 0 ? (
|
||||
<span
|
||||
className="kv-cache-not-reused block w-full"
|
||||
style={{ height: `${100 - cachedHeight}%` }}
|
||||
/>
|
||||
) : null}
|
||||
{cachedTokens > 0 ? (
|
||||
<span
|
||||
className="kv-cache-reused block w-full"
|
||||
style={{ height: `${cachedHeight}%` }}
|
||||
/>
|
||||
) : null}
|
||||
</>
|
||||
) : (
|
||||
<span className="block h-full w-full bg-muted-foreground/25" />
|
||||
)}
|
||||
</RoundBar>
|
||||
</RoundTrigger>
|
||||
<RoundContent
|
||||
side="top"
|
||||
align="center"
|
||||
className="max-w-72 px-3 py-2 text-[11px]"
|
||||
{...(bottomSheet ? {
|
||||
"aria-label": timestampLabel,
|
||||
onOpenAutoFocus: (event: Event) => event.preventDefault(),
|
||||
} : {})}
|
||||
>
|
||||
<span
|
||||
className="block font-medium text-foreground"
|
||||
>
|
||||
{timestampLabel}
|
||||
</span>
|
||||
<span className="mt-1 grid grid-cols-[max-content_max-content] gap-x-3 gap-y-0.5">
|
||||
{detailRows.map((row) => (
|
||||
<Fragment key={row.key}>
|
||||
<span className="text-muted-foreground">
|
||||
{row.label}
|
||||
</span>
|
||||
<span className="text-end tabular-nums text-foreground">
|
||||
{row.value}
|
||||
</span>
|
||||
</Fragment>
|
||||
))}
|
||||
</span>
|
||||
{detailNote ? (
|
||||
<span
|
||||
className="mt-1 block text-muted-foreground"
|
||||
>
|
||||
{detailNote}
|
||||
</span>
|
||||
) : null}
|
||||
</RoundContent>
|
||||
</RoundRoot>
|
||||
</span>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</>
|
||||
) : null}
|
||||
|
||||
</div>
|
||||
</UsagePanel>
|
||||
</TooltipProvider>
|
||||
</Root>
|
||||
);
|
||||
}
|
||||
|
||||
function UsagePanel({ bottomSheet, onDismiss, children }: {
|
||||
bottomSheet: boolean;
|
||||
onDismiss: () => void;
|
||||
children: ReactNode;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const panelRef = useRef<HTMLDivElement | null>(null);
|
||||
const [panelNode, setPanelNode] = useState<HTMLDivElement | null>(null);
|
||||
const sheetRef = useCallback((node: HTMLDivElement | null) => {
|
||||
panelRef.current = node;
|
||||
setPanelNode(node);
|
||||
}, []);
|
||||
const gesture = useRef<{ pointerId: number; x: number; y: number; time: number } | null>(null);
|
||||
const suppressClick = useRef(false);
|
||||
const [offset, setOffset] = useState(0);
|
||||
const [dragging, setDragging] = useState(false);
|
||||
const title = t("thread.composer.context.panelTitle", { defaultValue: "Context usage" });
|
||||
const focusPanel = (event: Event) => {
|
||||
event.preventDefault();
|
||||
gesture.current = null;
|
||||
suppressClick.current = false;
|
||||
setOffset(0);
|
||||
setDragging(false);
|
||||
panelRef.current?.focus({ preventScroll: true });
|
||||
};
|
||||
const cancelDrag = (event: PointerEvent<HTMLButtonElement>) => {
|
||||
if (gesture.current?.pointerId !== event.pointerId) return;
|
||||
gesture.current = null;
|
||||
suppressClick.current = true;
|
||||
setDragging(false);
|
||||
setOffset(0);
|
||||
const inspector = <ContextInspector sidebar expandable={!narrow} context={context} rounds={rounds} snapshots={snapshots}
|
||||
onOpenFile={onOpenFile ? (path) => { setOpen(false); onOpenFile(path); } : undefined} />;
|
||||
const onPanelKeyDown = (event: KeyboardEvent<HTMLElement>) => {
|
||||
if (event.key === "Escape" && !event.defaultPrevented) { event.preventDefault(); close(); }
|
||||
};
|
||||
|
||||
if (bottomSheet) {
|
||||
return (
|
||||
<SheetContent
|
||||
ref={sheetRef}
|
||||
side="bottom"
|
||||
aria-describedby={undefined}
|
||||
showCloseButton={false}
|
||||
onOpenAutoFocus={focusPanel}
|
||||
style={{ translate: `0 ${offset}px`, ...(dragging ? { transition: "none" } : {}) }}
|
||||
className={cn(
|
||||
"mx-auto max-h-[60dvh] w-full max-w-md gap-0 rounded-t-3xl border-t border-border/60 p-0 shadow-xl outline-none",
|
||||
"transition-[translate]",
|
||||
"data-[state=open]:duration-300 data-[state=open]:ease-out data-[state=closed]:duration-200 data-[state=closed]:ease-in",
|
||||
)}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
aria-label={t("common.close")}
|
||||
className="mx-auto flex h-11 w-24 shrink-0 touch-none select-none items-center justify-center rounded-full cursor-grab active:cursor-grabbing focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
|
||||
onPointerDown={(event) => {
|
||||
if (event.button !== 0 || !event.isPrimary || gesture.current) return;
|
||||
event.currentTarget.setPointerCapture(event.pointerId);
|
||||
gesture.current = { pointerId: event.pointerId, x: event.clientX, y: event.clientY, time: event.timeStamp };
|
||||
suppressClick.current = false;
|
||||
setDragging(true);
|
||||
}}
|
||||
onPointerMove={(event) => {
|
||||
const start = gesture.current;
|
||||
if (!start || start.pointerId !== event.pointerId) return;
|
||||
const dy = event.clientY - start.y;
|
||||
const dx = event.clientX - start.x;
|
||||
if (Math.abs(dy) > 5 || Math.abs(dx) > 5) suppressClick.current = true;
|
||||
setOffset(Math.max(0, dy));
|
||||
}}
|
||||
onPointerUp={(event) => {
|
||||
const start = gesture.current;
|
||||
if (!start || start.pointerId !== event.pointerId) return;
|
||||
gesture.current = null;
|
||||
const dy = Math.max(0, event.clientY - start.y);
|
||||
const dx = Math.abs(event.clientX - start.x);
|
||||
const threshold = Math.max(48, Math.min(96, (panelRef.current?.offsetHeight ?? 320) * 0.3));
|
||||
const elapsed = Math.max(1, event.timeStamp - start.time);
|
||||
const dismiss = dy > dx && (dy >= threshold || (dy > 20 && dy / elapsed > 0.5));
|
||||
suppressClick.current ||= dy > 5 || dx > 5;
|
||||
event.currentTarget.releasePointerCapture(event.pointerId);
|
||||
setDragging(false);
|
||||
if (dismiss) onDismiss();
|
||||
else setOffset(0);
|
||||
}}
|
||||
onPointerCancel={cancelDrag}
|
||||
onLostPointerCapture={cancelDrag}
|
||||
onClick={(event) => {
|
||||
// A completed drag also emits a click; don't close after a snap-back.
|
||||
if (suppressClick.current && event.detail !== 0) {
|
||||
event.preventDefault();
|
||||
suppressClick.current = false;
|
||||
return;
|
||||
}
|
||||
onDismiss();
|
||||
}}
|
||||
>
|
||||
<span aria-hidden="true" className="h-1 w-8 rounded-full bg-foreground/15" />
|
||||
</button>
|
||||
<div className="shrink-0 px-5 pb-2">
|
||||
<SheetTitle className="text-base font-medium">{title}</SheetTitle>
|
||||
</div>
|
||||
<div className="min-h-0 overflow-y-auto overscroll-contain px-1 pb-[max(0.75rem,env(safe-area-inset-bottom))]">
|
||||
<FloatingPortalContext.Provider value={panelNode}>
|
||||
{children}
|
||||
</FloatingPortalContext.Provider>
|
||||
</div>
|
||||
</SheetContent>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<PopoverContent
|
||||
ref={panelRef}
|
||||
side="top"
|
||||
align="end"
|
||||
sideOffset={10}
|
||||
onOpenAutoFocus={focusPanel}
|
||||
aria-label={title}
|
||||
className="w-[min(22rem,calc(100vw-1.5rem))] p-0"
|
||||
>
|
||||
{children}
|
||||
</PopoverContent>
|
||||
);
|
||||
return <>
|
||||
{triggerHost === undefined ? triggerWithTooltip : triggerHost && createPortal(triggerWithTooltip, triggerHost)}
|
||||
{narrow ? <Dialog open={open} onOpenChange={setOpen} modal={!sidebarHost}>
|
||||
<DialogContent portalContainer={sidebarHost} placement="bottom" id={panelId} aria-describedby={undefined}
|
||||
data-testid="context-sidebar"
|
||||
overlayClassName={cn("context-drawer-backdrop", sidebarHost && "absolute bg-black/25 backdrop-blur-[8px] sm:bg-transparent sm:backdrop-blur-none")}
|
||||
className="context-drawer-panel flex h-[85%] min-h-0 max-w-none flex-col gap-2 p-4 pt-5 pb-[max(1rem,env(safe-area-inset-bottom))]"
|
||||
onCloseAutoFocus={(event) => { event.preventDefault(); if (!interactedOutside.current) triggerRef.current?.focus({ preventScroll: true }); }}
|
||||
onInteractOutside={(event) => {
|
||||
if (event.target instanceof Node && triggerRef.current?.contains(event.target)) event.preventDefault();
|
||||
else interactedOutside.current = true;
|
||||
}}>
|
||||
<DialogTitle className="sr-only">{t("thread.composer.context.panelTitle")}</DialogTitle>
|
||||
{inspector}
|
||||
</DialogContent>
|
||||
</Dialog> : createPortal(<SidebarPanel open={open} width={420} floating={!sidebarHost} id={panelId}
|
||||
aria-label={t("thread.composer.context.panelTitle")} data-testid="context-sidebar" onKeyDown={onPanelKeyDown}>
|
||||
{inspector}
|
||||
</SidebarPanel>, sidebarHost ?? document.body)}
|
||||
</>;
|
||||
}
|
||||
Reference in new issue
Block a user