feat(webui): refine mobile message actions and hover timestamps

This commit is contained in:
Xubin Ren committed 2026-09-24 15:17:05 +08:00
1 parent 4693bba81f
commit 7aac9355ec
10 files changed
+334 -73

No files matched your search

+40 -48
View File
@@ -120,6 +120,34 @@ function useMessageCopy(content: string) {
return { copied, label, onCopy };
}
function messageCopyContent(message: UIMessage, t: (key: string) => string) {
return message.role === "assistant" && message.compactReply === "empty"
? t("thread.compaction.empty")
: message.role === "assistant" && message.compactReply === "failed"
? t("thread.compaction.failed")
: message.content;
}
/** The same copy behavior in the desktop menu and the mobile reply footer. */
export function MessageCopyButton({ message, className }: { message: UIMessage; className?: string }) {
const { t } = useTranslation();
const content = messageCopyContent(message, t);
const { copied, label, onCopy } = useMessageCopy(content);
if (!content.trim()) return null;
return <TooltipProvider><Tooltip><TooltipTrigger asChild><button type="button" data-message-block-copy-action
data-assistant-copy-action={message.role === "assistant" || undefined}
onClick={onCopy} aria-label={label}
className={cn(
"inline-flex h-[var(--message-block-control-size)] w-[var(--message-block-action-width)] items-center justify-center rounded-control",
"text-muted-foreground transition-[color,background-color,scale] hover:bg-muted/70 hover:text-foreground active:scale-[0.96]",
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring motion-reduce:transform-none",
className,
)}>
{copied ? <Check className="h-3.5 w-3.5" strokeWidth={1.75} aria-hidden />
: <Copy className="h-3.5 w-3.5" strokeWidth={1.75} aria-hidden />}
</button></TooltipTrigger><TooltipContent side="top" align="center">{label}</TooltipContent></Tooltip></TooltipProvider>;
}
interface MessageBlockMenuActivity {
label: string;
expanded: boolean;
@@ -133,6 +161,7 @@ interface MessageBlockMenuActionsProps {
onForkFromHere?: () => void;
activity?: MessageBlockMenuActivity;
onViewLinks?: () => void;
sheet?: boolean;
}
/** Actions and metadata shown after opening a visual message block's context panel. */
@@ -142,17 +171,11 @@ export function MessageBlockMenuActions({
onForkFromHere,
activity,
onViewLinks,
sheet = false,
}: MessageBlockMenuActionsProps) {
const { t } = useTranslation();
const content = message.role === "assistant"
? message.compactReply === "empty"
? t("thread.compaction.empty")
: message.compactReply === "failed"
? t("thread.compaction.failed")
: message.content
: message.content;
const content = messageCopyContent(message, t);
const hasText = content.trim().length > 0;
const { copied, label: copyLabel, onCopy } = useMessageCopy(content);
const showFork = message.role === "assistant"
&& !message.isStreaming
&& !isTurnStreaming
@@ -175,50 +198,16 @@ export function MessageBlockMenuActions({
<TooltipProvider>
<div
data-message-block-menu-actions
className="flex w-max max-w-full flex-col items-start gap-1"
className={cn("flex max-w-full flex-col items-start gap-1", sheet ? "w-full" : "w-max")}
>
{hasText || showFork || activity ? (
{(!sheet && hasText) || showFork || activity ? (
<div
data-message-block-toolbar
className="flex w-full flex-wrap items-center gap-x-1 gap-y-1"
className={cn("flex w-full flex-wrap gap-x-1 gap-y-1", sheet ? "flex-col items-stretch" : "items-center")}
>
{hasText || showFork ? (
{(!sheet && hasText) || showFork ? (
<div className="flex min-h-[var(--message-block-control-size)] items-center gap-0.5">
{hasText ? (
<Tooltip>
<TooltipTrigger asChild>
<button
type="button"
data-message-block-copy-action
data-assistant-copy-action={message.role === "assistant" || undefined}
onClick={onCopy}
aria-label={copyLabel}
className={cn(
"inline-flex h-[var(--message-block-control-size)] w-[var(--message-block-action-width)] items-center justify-center rounded-control",
"text-muted-foreground transition-[color,background-color,scale]",
"hover:bg-muted/70 hover:text-foreground active:scale-[0.96]",
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
"motion-reduce:transform-none",
)}
>
{copied ? (
<Check
className="h-3.5 w-3.5 -translate-x-px"
strokeWidth={1.75}
aria-hidden
/>
) : (
<Copy
className="h-3.5 w-3.5 -translate-x-px"
strokeWidth={1.75}
aria-hidden
/>
)}
</button>
</TooltipTrigger>
<TooltipContent side="top" align="center">{copyLabel}</TooltipContent>
</Tooltip>
) : null}
{!sheet && hasText ? <MessageCopyButton message={message} /> : null}
{showFork ? (
<Tooltip>
<TooltipTrigger asChild>
@@ -234,9 +223,11 @@ export function MessageBlockMenuActions({
"hover:bg-muted/70 hover:text-foreground active:scale-[0.96]",
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
"motion-reduce:transform-none",
sheet && "w-full justify-start gap-2 px-2 text-sm",
)}
>
<ForkArrowIcon className="h-3.5 w-3.5" />
{sheet ? <span>{t("message.forkFromHere")}</span> : null}
</button>
</TooltipTrigger>
<TooltipContent side="top" align="center">
@@ -258,6 +249,7 @@ export function MessageBlockMenuActions({
"text-start text-[11px] leading-4",
"text-muted-foreground transition-colors hover:text-foreground",
"focus-visible:outline-none",
sheet && "w-full rounded-control text-sm hover:bg-muted/70",
)}
>
<span
@@ -289,7 +281,7 @@ export function MessageBlockMenuActions({
{timestampLabel || automationSourceLabel ? (
<div
data-message-block-metadata
className="mt-0.5 w-full border-t border-border/45 px-1.5 pt-1 text-[10px] leading-4 text-muted-foreground/45"
className={cn("mt-0.5 w-full border-t border-border/45 px-1.5 pt-1 leading-4", sheet ? "text-xs text-muted-foreground" : "text-[10px] text-muted-foreground/45")}
>
{timestampLabel ? (
<time
+4 -3
View File
@@ -37,11 +37,12 @@ export function useMessageWebLinks(root: RefObject<HTMLElement>, open: boolean)
const itemClassName = `${floatingItemClassName} ${floatingItemFocusClassName} w-full min-h-11 text-start hover:bg-muted/70`;
/** A drill-in inside the existing message popover, not another floating layer. */
export function MessageLinksMenu({ links, onBack, onClose }: {
/** A drill-in inside the message's action surface, not another floating layer. */
export function MessageLinksMenu({ links, onBack, onClose, expanded = false }: {
links: MessageWebLink[];
onBack: () => void;
onClose: () => void;
expanded?: boolean;
}) {
const { t } = useTranslation();
const openPreview = useContext(WebPreviewContext);
@@ -49,7 +50,7 @@ export function MessageLinksMenu({ links, onBack, onClose }: {
const active = links.find((link) => link.href === selected);
const backRef = useRef<HTMLButtonElement>(null);
useLayoutEffect(() => { backRef.current?.focus({ preventScroll: true }); }, [selected]);
return <div className="w-60 max-w-full">
return <div className={expanded ? "w-full" : "w-60 max-w-full"}>
<button ref={backRef} type="button" className={itemClassName} aria-label={t("webPreview.back")}
onClick={() => active && links.length > 1 ? setSelected(null) : onBack()}>
<ArrowLeft aria-hidden /><span>{t("webPreview.viewLinks")}</span>
+69 -12
View File
@@ -1,7 +1,7 @@
import { memo, useCallback, useEffect, useId, useLayoutEffect, useMemo, useRef, useState, type RefObject } from "react";
import { MoreHorizontal } from "lucide-react";
import { useTranslation } from "react-i18next";
import { MessageBlockMenuActions, MessageBubble } from "@/components/MessageBubble";
import { MessageBlockMenuActions, MessageBubble, MessageCopyButton } from "@/components/MessageBubble";
import { MessageLinksMenu, useMessageWebLinks } from "@/components/MessageLinksMenu";
import { FallbackResponseSources } from "@/components/ResponseSourceBadge";
import {
@@ -11,6 +11,10 @@ import {
} from "@/components/thread/AgentActivityCluster";
import { AssistantSelectionAction } from "@/components/thread/AssistantSelectionAction";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { Dialog, DialogContent, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
import { useMediaQuery } from "@/hooks/useMediaQuery";
import { fmtDateTime, formatMessageHoverTime } from "@/lib/format";
import { projectActivityTimeline, type TurnUnit } from "@/lib/activity-timeline";
import { cn } from "@/lib/utils";
import type { CliAppInfo, McpPresetInfo, RetryStatus, SlashCommand, UIMessage } from "@/lib/types";
@@ -91,6 +95,7 @@ export function ThreadMessages({
}: ThreadMessagesProps) {
const { t } = useTranslation();
const messageListRef = useRef<HTMLDivElement>(null);
const mobileActions = useMediaQuery("(max-width: 767px)");
const units = useMemo(
() => buildDisplayUnits(messages),
[messages],
@@ -222,6 +227,7 @@ export function ThreadMessages({
key={unitKeys[index]}
unitKey={unitKeys[index]}
unit={unit}
mobileActions={mobileActions}
marginTop={marginTop}
userPromptId={userPromptId}
hasBodyBelow={hasBodyBelow}
@@ -323,6 +329,7 @@ function pendingTurnProjection(
}
interface ThreadDisplayUnitProps {
mobileActions: boolean;
unitKey: string;
unit: DisplayUnit;
marginTop: string;
@@ -356,6 +363,7 @@ interface ThreadDisplayUnitProps {
}
interface MessageBlockMenuProps {
mobileActions: boolean;
messageRoot: RefObject<HTMLElement>;
message: UIMessage;
isTurnStreaming: boolean;
@@ -374,6 +382,7 @@ interface MessageBlockMenuProps {
}
function MessageBlockMenu({
mobileActions,
messageRoot,
message,
isTurnStreaming,
@@ -405,6 +414,43 @@ function MessageBlockMenu({
const contextActiveRef = useRef(contextActive);
contextActiveRef.current = contextActive;
const label = t("message.actions");
const timestamp = message.role === "assistant" && Number.isFinite(message.completedAt)
? message.completedAt
: message.createdAt;
// Re-evaluated when this message becomes active, including after an overnight idle.
const shortTime = mobileActions ? "" : formatMessageHoverTime(timestamp);
const fullTime = shortTime ? fmtDateTime(timestamp) : "";
const actions = linksShown && links.length > 0 ? <MessageLinksMenu links={links}
expanded={mobileActions}
onBack={() => { setLinksShown(false); contentRef.current?.focus({ preventScroll: true }); }}
onClose={() => onOpenChange(false)} /> : <MessageBlockMenuActions
message={message}
isTurnStreaming={isTurnStreaming}
onForkFromHere={onForkFromHere ? () => { if (mobileActions) onOpenChange(false); onForkFromHere(); } : undefined}
activity={activity}
sheet={mobileActions}
onViewLinks={links.length > 0 ? () => setLinksShown(true) : undefined}
/>;
if (mobileActions) {
return <div data-message-mobile-actions className={cn("mt-1 flex items-center gap-0.5", message.role === "user" && "justify-end")}>
<MessageCopyButton message={message} className="h-11 w-11" />
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogTrigger asChild>
<button ref={triggerRef} type="button" data-message-block-menu-trigger aria-label={label}
className="inline-flex h-11 w-11 items-center justify-center rounded-control text-muted-foreground transition-colors hover:bg-muted/70 hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">
<MoreHorizontal className="h-4 w-4" strokeWidth={1.75} aria-hidden />
</button>
</DialogTrigger>
<DialogContent ref={contentRef} placement="bottom" aria-describedby={undefined}
data-message-block-menu data-message-mobile-sheet data-message-context-menu-block={contextBlockKey}
className="gap-2 p-4 pt-5 pb-[max(1rem,env(safe-area-inset-bottom))] outline-none"
onCloseAutoFocus={(event) => { if (activity?.expanded) event.preventDefault(); }}>
<DialogTitle className="px-2 pb-2 pe-10 text-sm font-medium text-muted-foreground">{label}</DialogTitle>
{actions}
</DialogContent>
</Dialog>
</div>;
}
return (
<Popover open={open} onOpenChange={onOpenChange}>
<PopoverTrigger asChild>
@@ -434,6 +480,21 @@ function MessageBlockMenu({
</span>
</button>
</PopoverTrigger>
{shortTime && fullTime && typeof timestamp === "number" ? (
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<time data-message-hover-time dateTime={new Date(timestamp).toISOString()}
tabIndex={0} aria-label={fullTime}
className="message-block-hover-time absolute start-[calc(-1*var(--message-block-trigger-offset)-2px)] top-[var(--message-block-control-size)] z-10 w-[calc(var(--message-block-control-size)+4px)] truncate rounded-mark text-center text-[10px] leading-4 tabular-nums text-muted-foreground outline-none transition-opacity focus-visible:ring-2 focus-visible:ring-ring motion-reduce:transition-none">
{shortTime}
</time>
</TooltipTrigger>
<TooltipContent side="right" align="start" collisionPadding={12}
data-message-context-menu-block={contextBlockKey}>{fullTime}</TooltipContent>
</Tooltip>
</TooltipProvider>
) : null}
<PopoverContent
ref={contentRef}
data-message-block-menu
@@ -472,21 +533,14 @@ function MessageBlockMenu({
"shadow-[0_2px_10px_rgba(0,0,0,0.08)] dark:shadow-[0_2px_10px_rgba(0,0,0,0.24)]",
)}
>
{linksShown && links.length > 0 ? <MessageLinksMenu links={links}
onBack={() => { setLinksShown(false); contentRef.current?.focus({ preventScroll: true }); }}
onClose={() => onOpenChange(false)} /> : <MessageBlockMenuActions
message={message}
isTurnStreaming={isTurnStreaming}
onForkFromHere={onForkFromHere}
activity={activity}
onViewLinks={links.length > 0 ? () => setLinksShown(true) : undefined}
/>}
{actions}
</PopoverContent>
</Popover>
);
}
const ThreadDisplayUnit = memo(function ThreadDisplayUnit({
mobileActions,
unitKey,
unit,
marginTop,
@@ -579,6 +633,7 @@ const ThreadDisplayUnit = memo(function ThreadDisplayUnit({
]);
const blockMenu = unit.type === "message" && contextBlockKey !== undefined ? (
<MessageBlockMenu
mobileActions={mobileActions}
messageRoot={elementRef}
message={unit.message}
isTurnStreaming={isTurnStreaming}
@@ -649,7 +704,7 @@ const ThreadDisplayUnit = memo(function ThreadDisplayUnit({
)
) : (
<div className={unit.message.role === "assistant" ? "relative" : undefined}>
{unit.message.role === "assistant" ? blockMenu : null}
{unit.message.role === "assistant" && !mobileActions ? blockMenu : null}
{showBlockContext && blockActivity ? (
<div className={blockActivityExpanded ? "mb-2" : undefined}>
<AgentActivityCluster
@@ -678,8 +733,9 @@ const ThreadDisplayUnit = memo(function ThreadDisplayUnit({
mcpPresets={mcpPresets}
slashCommands={slashCommands}
onOpenFilePreview={onOpenFilePreview}
contextMenu={unit.message.role === "user" ? blockMenu : undefined}
contextMenu={unit.message.role === "user" && !mobileActions ? blockMenu : undefined}
/>
{mobileActions ? blockMenu : null}
{unit.message.role === "assistant"
&& unit.message.kind !== "compaction"
&& contextBlockKey === undefined ? (
@@ -702,6 +758,7 @@ function threadDisplayUnitPropsEqual(
): boolean {
return (
displayUnitsEqual(previous.unit, next.unit)
&& previous.mobileActions === next.mobileActions
&& previous.marginTop === next.marginTop
&& previous.userPromptId === next.userPromptId
&& previous.hasBodyBelow === next.hasBodyBelow
+13 -7
View File
@@ -38,9 +38,10 @@ const DialogPositionedContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & {
positionerStyle?: React.CSSProperties;
placement?: "center" | "bottom";
}
>(({ positionerStyle, ...props }, ref) => (
<div className="fixed inset-0 z-50 flex items-center justify-center p-4" style={positionerStyle}>
>(({ positionerStyle, placement, ...props }, ref) => (
<div className={cn("fixed inset-0 z-50 flex justify-center", placement === "bottom" ? "items-end" : "items-center p-4")} style={positionerStyle}>
<DialogPrimitive.Content ref={ref} {...props} />
</div>
));
@@ -50,19 +51,20 @@ interface DialogContentProps
extends React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> {
showCloseButton?: boolean;
overlayClassName?: string;
placement?: "center" | "bottom";
}
const DialogContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
DialogContentProps
>(({ className, children, showCloseButton = true, overlayClassName, onOpenAutoFocus, ...props }, ref) => {
>(({ className, children, showCloseButton = true, overlayClassName, placement = "center", onOpenAutoFocus, ...props }, ref) => {
const { t } = useTranslation();
const [container, setContainer] = React.useState<HTMLDivElement | null>(null);
const contentNode = React.useRef<HTMLDivElement | null>(null);
const layoutAnchor = React.useContext(DialogLayoutContext);
const [layout, setLayout] = React.useState<React.CSSProperties>();
React.useLayoutEffect(() => {
if (!layoutAnchor) {
if (!layoutAnchor || placement === "bottom") {
setLayout(undefined);
return;
}
@@ -81,7 +83,7 @@ const DialogContent = React.forwardRef<
observer?.disconnect();
window.removeEventListener("resize", update);
};
}, [layoutAnchor]);
}, [layoutAnchor, placement]);
const contentRef = React.useCallback((node: HTMLDivElement | null) => {
contentNode.current = node;
setContainer(node);
@@ -93,6 +95,7 @@ const DialogContent = React.forwardRef<
<DialogOverlay className={overlayClassName} />
<DialogPositionedContent
positionerStyle={layout}
placement={placement}
ref={contentRef}
onOpenAutoFocus={(event) => {
if (onOpenAutoFocus) onOpenAutoFocus(event);
@@ -103,7 +106,10 @@ const DialogContent = React.forwardRef<
}}
className={cn(
modalSurfaceClassName,
"relative grid w-full max-w-lg origin-center gap-4 rounded-modal p-6 duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 motion-reduce:animate-none",
"relative grid w-full max-w-lg gap-4 p-6 duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 motion-reduce:animate-none",
placement === "bottom"
? "max-h-[85dvh] overflow-y-auto overscroll-contain rounded-t-modal pb-[max(1rem,env(safe-area-inset-bottom))] data-[state=open]:slide-in-from-bottom-4 data-[state=closed]:slide-out-to-bottom-4"
: "origin-center rounded-modal data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95",
className,
)}
{...props}
@@ -112,7 +118,7 @@ const DialogContent = React.forwardRef<
{children}
</FloatingPortalContext.Provider>
{showCloseButton ? (
<DialogPrimitive.Close className="absolute right-2.5 top-2.5 grid h-7 w-7 place-items-center rounded-full text-muted-foreground outline-none transition-colors hover:bg-muted hover:text-foreground active:bg-muted/80 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:pointer-events-none">
<DialogPrimitive.Close className={cn("absolute right-2.5 top-2.5 grid place-items-center rounded-full text-muted-foreground outline-none transition-colors hover:bg-muted hover:text-foreground active:bg-muted/80 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:pointer-events-none", placement === "bottom" ? "h-11 w-11" : "h-7 w-7")}>
<X className="h-4 w-4" />
<span className="sr-only">{t("common.close")}</span>
</DialogPrimitive.Close>