Add aligned assistant timestamps and Copy/Fork actions
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { useEffect, useRef, useState } from "preact/hooks";
|
||||
import { Check, Copy, GitFork } from "lucide-preact";
|
||||
import type { Message } from "@/lib/api";
|
||||
|
||||
const receivedFormat = new Intl.DateTimeFormat(undefined, {
|
||||
year: "numeric", month: "short", day: "numeric", hour: "numeric", minute: "2-digit",
|
||||
});
|
||||
const actionClass = "inline-flex h-10 items-center gap-1.5 rounded-md px-2 text-xs transition-colors hover:bg-violet-400/12 hover:text-violet-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-ring disabled:cursor-wait disabled:opacity-50";
|
||||
|
||||
export function AssistantMessageFooter({ message, onFork, isForking = false }: {
|
||||
message: Message;
|
||||
onFork?: (messageId: string) => void;
|
||||
isForking?: boolean;
|
||||
}) {
|
||||
const [copyState, setCopyState] = useState<"idle" | "copied" | "failed">("idle");
|
||||
const resetTimer = useRef<ReturnType<typeof setTimeout>>();
|
||||
useEffect(() => () => clearTimeout(resetTimer.current), []);
|
||||
|
||||
const copy = async () => {
|
||||
clearTimeout(resetTimer.current);
|
||||
try {
|
||||
await navigator.clipboard.writeText(message.content);
|
||||
setCopyState("copied");
|
||||
resetTimer.current = setTimeout(() => setCopyState("idle"), 2000);
|
||||
} catch {
|
||||
setCopyState("failed");
|
||||
}
|
||||
};
|
||||
const received = new Date(message.createdAt);
|
||||
const hasDate = Number.isFinite(received.getTime());
|
||||
|
||||
return (
|
||||
<div className="text-muted-foreground" data-assistant-footer={message.id} data-mobile-swipe-ignore>
|
||||
{hasDate ? <time className="block text-xs leading-5" dateTime={message.createdAt} title={`Received ${received.toLocaleString()}`}>
|
||||
{receivedFormat.format(received)}
|
||||
</time> : null}
|
||||
<div className="-ml-2 flex flex-wrap items-center gap-1" role="group" aria-label="Message actions">
|
||||
{message.content.trim() ? <button type="button" className={actionClass} onClick={() => void copy()} title="Copy message" aria-label="Copy message">
|
||||
{copyState === "copied" ? <Check className="h-3.5 w-3.5" aria-hidden="true" /> : <Copy className="h-3.5 w-3.5" aria-hidden="true" />}
|
||||
<span>{copyState === "copied" ? "Copied" : "Copy"}</span>
|
||||
</button> : null}
|
||||
{onFork ? <button type="button" className={actionClass} onClick={() => onFork(message.id)} disabled={isForking} title="Fork chat from this message" aria-label="Fork chat from this message">
|
||||
<GitFork className="h-3.5 w-3.5" aria-hidden="true" />
|
||||
<span>Fork</span>
|
||||
</button> : null}
|
||||
</div>
|
||||
<span className={copyState === "failed" ? "block text-xs leading-5" : "sr-only"} role="status" aria-live="polite">
|
||||
{copyState === "copied" ? "Message copied." : copyState === "failed" ? "Could not copy. Select the message text and use your browser’s Copy command." : ""}
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -2,17 +2,19 @@ import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "preact/ho
|
||||
import type { ComponentChildren, JSX } from "preact";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { ChatAttachmentList } from "@/components/chat/chat-attachment-list";
|
||||
import { AssistantMessageFooter } from "@/components/chat/assistant-message-footer";
|
||||
import { getMessageAttachments, type Message } from "@/lib/api";
|
||||
import { asStreamingAssistantMetadata, isInlineAgentReplyPending } from "@/lib/chat-stream-presentation";
|
||||
import { AgentBubble } from "@/components/agent/agent-conversation";
|
||||
import { MarkdownContent } from "@/components/markdown/markdown-content";
|
||||
import { ChevronDown, ChevronUp, Globe2, Link2, MoreHorizontal, Wrench } from "lucide-preact";
|
||||
import { ChevronDown, ChevronUp, Globe2, Link2, Wrench } from "lucide-preact";
|
||||
|
||||
type Props = {
|
||||
messages: Message[];
|
||||
isLoading: boolean;
|
||||
isSending: boolean;
|
||||
onMessageActions?: (event: MouseEvent, messageId: string) => void;
|
||||
onForkMessage?: (messageId: string) => void;
|
||||
isForking?: boolean;
|
||||
};
|
||||
|
||||
type ToolLogMetadata = {
|
||||
@@ -538,7 +540,7 @@ export function StreamingAssistantPresentation({ message, isSending }: { message
|
||||
);
|
||||
}
|
||||
|
||||
export function ChatMessagesPanel({ messages, isLoading, isSending, onMessageActions }: Props) {
|
||||
export function ChatMessagesPanel({ messages, isLoading, isSending, onForkMessage, isForking }: Props) {
|
||||
const hasPendingAssistant = messages.some((message) => message.id.startsWith("temp-assistant-"));
|
||||
const renderItems = useMemo(() => buildMessageRenderItems(messages), [messages]);
|
||||
const toolCallMessageIDs = useMemo(() => getToolCallMessageIDs(messages), [messages]);
|
||||
@@ -637,12 +639,8 @@ export function ChatMessagesPanel({ messages, isLoading, isSending, onMessageAct
|
||||
className={cn("[&_a]:text-inherit [&_a]:underline", !isUser && "leading-[1.82] text-violet-50")}
|
||||
/>
|
||||
) : null}
|
||||
{message.role === "assistant" && !message.id.startsWith("temp-") && onMessageActions ? (
|
||||
<button type="button" aria-label="Message actions" title="Message actions"
|
||||
className="flex h-9 w-9 items-center justify-center rounded text-muted-foreground hover:bg-violet-400/12 hover:text-violet-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-ring"
|
||||
onClick={event => onMessageActions(event, message.id)}>
|
||||
<MoreHorizontal className="h-4 w-4" />
|
||||
</button>
|
||||
{message.role === "assistant" && !message.id.startsWith("temp-") ? (
|
||||
<AssistantMessageFooter message={message} onFork={onForkMessage} isForking={isForking} />
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user