Skip to content

🎥 Make videos programmatically with React

refactor(tp2mg): extract ConversationHistory component from ChatSidebar

ASchwadd46767f7aa98
diff --git a/packages/template-prompt-to-motion-graphics/src/components/ChatSidebar/ChatInput.tsx b/packages/template-prompt-to-motion-graphics/src/components/ChatSidebar/ChatInput.tsx
index b7eaf8dfb9..dba0ddbf50 100644
--- a/packages/template-prompt-to-motion-graphics/src/components/ChatSidebar/ChatInput.tsx
+++ b/packages/template-prompt-to-motion-graphics/src/components/ChatSidebar/ChatInput.tsx
@@ -104,7 +104,7 @@ export function ChatInput({
const canCapture = Component && !isLoading && !isCapturing && canAddMore;
return (
- <div className="px-12 pt-4 pb-8">
+ <div className="px-4 pt-4 pb-4">
<form onSubmit={handleSubmit}>
<div
className={`bg-background-elevated rounded-xl border p-3 transition-colors ${
diff --git a/packages/template-prompt-to-motion-graphics/src/components/ChatSidebar/ChatSidebar.tsx b/packages/template-prompt-to-motion-graphics/src/components/ChatSidebar/ChatSidebar.tsx
index 67c69ddf08..f2b1a676fb 100644
--- a/packages/template-prompt-to-motion-graphics/src/components/ChatSidebar/ChatSidebar.tsx
+++ b/packages/template-prompt-to-motion-graphics/src/components/ChatSidebar/ChatSidebar.tsx
@@ -8,21 +8,8 @@ import {
useEffect,
type ComponentType,
} from "react";
-import {
- PanelLeftClose,
- PanelLeftOpen,
- RotateCcw,
- FileCode,
- AlertTriangle,
- PenLine,
- BookOpen,
-} from "lucide-react";
+import { PanelLeftClose, PanelLeftOpen, RotateCcw } from "lucide-react";
import { Button } from "@/components/ui/button";
-import {
- Tooltip,
- TooltipContent,
- TooltipTrigger,
-} from "@/components/ui/tooltip";
import { cn } from "@/lib/utils";
import type {
ConversationMessage,
@@ -38,10 +25,14 @@ import {
MODELS,
} from "@/types/generation";
import { ChatInput } from "./ChatInput";
+import { ConversationHistory } from "./ConversationHistory";
import { useGenerationApi } from "@/hooks/useGenerationApi";
export interface ChatSidebarRef {
- triggerGeneration: (options?: { silent?: boolean; attachedImages?: string[] }) => void;
+ triggerGeneration: (options?: {
+ silent?: boolean;
+ attachedImages?: string[];
+ }) => void;
}
interface ChatSidebarProps {
@@ -57,7 +48,11 @@ interface ChatSidebarProps {
onCodeGenerated?: (code: string) => void;
onStreamingChange?: (isStreaming: boolean) => void;
onStreamPhaseChange?: (phase: StreamPhase) => void;
- onError?: (error: string, type: GenerationErrorType, failedEdit?: EditOperation) => void;
+ onError?: (
+ error: string,
+ type: GenerationErrorType,
+ failedEdit?: EditOperation,
+ ) => void;
prompt: string;
onPromptChange: (prompt: string) => void;
currentCode?: string;
@@ -116,7 +111,6 @@ export const ChatSidebar = forwardRef<ChatSidebarRef, ChatSidebarProps>(
ref,
) {
const [model, setModel] = useState<ModelId>(MODELS[1].id);
- const messagesEndRef = useRef<HTMLDivElement>(null);
const promptRef = useRef<string>("");
const { isLoading, runGeneration } = useGenerationApi();
@@ -126,11 +120,6 @@ export const ChatSidebar = forwardRef<ChatSidebarRef, ChatSidebarProps>(
promptRef.current = prompt;
}, [prompt]);
- // Auto-scroll to bottom when messages change
- useEffect(() => {
- messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
- }, [messages]);
-
const handleGeneration = async (options?: {
silent?: boolean;
attachedImages?: string[];
@@ -175,7 +164,7 @@ export const ChatSidebar = forwardRef<ChatSidebarRef, ChatSidebarProps>(
return (
<div
className={cn(
- "flex flex-col bg-background/80 backdrop-blur-sm border-r border-border/50 transition-all duration-300",
+ "flex flex-col bg-background transition-all duration-300",
isCollapsed
? "w-12 shrink-0"
: "w-full h-[40vh] min-[1000px]:h-auto min-[1000px]:w-[40%] min-[1000px]:min-w-[320px] min-[1000px]:max-w-[520px] shrink",
@@ -193,9 +182,10 @@ export const ChatSidebar = forwardRef<ChatSidebarRef, ChatSidebarProps>(
</Button>
</div>
) : (
- <>
+ /* Chat area with subtle backdrop */
+ <div className="flex-1 flex flex-col min-h-0 ml-12 mr-8 mb-8 rounded-xl bg-muted/20 border border-border/30 shadow-sm">
{/* Header */}
- <div className="flex items-start justify-between px-12 mb-3">
+ <div className="flex items-start justify-between px-4 pt-4 pb-2">
<h2 className="text-sm font-medium text-muted-foreground">
Assistant Chat
</h2>
@@ -230,15 +220,10 @@ export const ChatSidebar = forwardRef<ChatSidebarRef, ChatSidebarProps>(
</div>
{/* Messages */}
- <div className="flex-1 overflow-y-auto px-12 pb-4 space-y-6">
- {messages.map((message) => (
- <ChatMessage key={message.id} message={message} />
- ))}
- {pendingMessage && (
- <PendingMessage skills={pendingMessage.skills} />
- )}
- <div ref={messagesEndRef} />
- </div>
+ <ConversationHistory
+ messages={messages}
+ pendingMessage={pendingMessage}
+ />
{/* Input */}
<ChatInput
@@ -255,149 +240,9 @@ export const ChatSidebar = forwardRef<ChatSidebarRef, ChatSidebarProps>(
durationInFrames={durationInFrames}
currentFrame={currentFrame}
/>
- </>
+ </div>
)}
</div>
);
},
);
-
-function ChatMessage({ message }: { message: ConversationMessage }) {
- const isUser = message.role === "user";
- const isError = message.role === "error";
- const metadata = message.metadata;
-
- const time = new Date(message.timestamp).toLocaleTimeString([], {
- hour: "numeric",
- minute: "2-digit",
- });
-
- if (isUser) {
- return (
- <div className="space-y-1">
- <div className="flex items-center gap-2">
- <span className="text-xs font-medium text-muted-foreground">You</span>
- <span className="text-xs text-muted-foreground-dim">{time}</span>
- </div>
- <div className="text-sm text-foreground leading-relaxed bg-secondary/50 rounded-lg px-3 py-2">
- {message.content}
- {message.attachedImages && message.attachedImages.length > 0 && (
- <div className="flex gap-2 mt-2 overflow-x-auto">
- {message.attachedImages.map((img, index) => (
- // eslint-disable-next-line @next/next/no-img-element
- <img
- key={index}
- src={img}
- alt={`Attached ${index + 1}`}
- className="h-20 w-auto rounded border border-border object-cover flex-shrink-0"
- />
- ))}
- </div>
- )}
- </div>
- </div>
- );
- }
-
- if (isError) {
- const errorLabel =
- message.errorType === "api"
- ? "API Error"
- : message.errorType === "validation"
- ? "Validation Error"
- : "Edit Failed";
-
- return (
- <div className="space-y-2">
- <div className="flex items-center gap-2 text-red-400">
- <AlertTriangle className="w-4 h-4" />
- <span className="text-xs font-medium">{errorLabel}</span>
- </div>
- <div className="text-xs text-red-300/80 leading-relaxed pl-6">
- {message.content}
- </div>
- </div>
- );
- }
-
- // Assistant message
- return (
- <div className="space-y-1">
- <div className="flex items-center gap-2">
- <span className="text-xs font-medium text-blue-400">Assistant</span>
- <span className="text-xs text-muted-foreground-dim">{time}</span>
- </div>
-
- <div className="text-sm text-foreground leading-relaxed">
- {message.content}
- {metadata?.skills && metadata.skills.length > 0 && (
- <Tooltip>
- <TooltipTrigger asChild>
- <span className="inline-flex ml-1.5 align-middle cursor-help">
- <BookOpen className="w-3.5 h-3.5 text-muted-foreground" />
- </span>
- </TooltipTrigger>
- <TooltipContent side="right" sideOffset={4}>
- Skills used: {metadata.skills.join(", ")}
- </TooltipContent>
- </Tooltip>
- )}
- </div>
-
- {metadata?.editType === "tool_edit" && metadata.edits && (
- <div className="space-y-1.5 pl-1 mt-2">
- {metadata.edits.map((edit, i) => (
- <div key={i} className="flex items-center gap-2">
- <PenLine className="w-3.5 h-3.5 text-muted-foreground shrink-0" />
- <div className="flex-1 min-w-0 leading-none">
- <span className="text-xs text-muted-foreground">
- {edit.description}
- </span>
- {edit.lineNumber && (
- <span className="text-[10px] text-muted-foreground-dim font-mono ml-2">
- L{edit.lineNumber}
- </span>
- )}
- </div>
- </div>
- ))}
- </div>
- )}
-
- {metadata?.editType === "full_replacement" && (
- <div className="flex items-center gap-2 text-muted-foreground">
- <FileCode className="w-3.5 h-3.5" />
- <span className="text-xs">Full code rewrite</span>
- </div>
- )}
- </div>
- );
-}
-
-function PendingMessage({ skills }: { skills?: string[] }) {
- return (
- <div className="space-y-1">
- <div className="flex items-center gap-2">
- <span className="text-xs font-medium text-blue-400">Assistant</span>
- <span className="text-xs text-muted-foreground-dim">now</span>
- </div>
-
- <div className="flex items-center gap-2 text-sm text-muted-foreground">
- <div className="w-3 h-3 border-2 border-muted-foreground border-t-blue-400 rounded-full animate-spin" />
- <span>Generating...</span>
- {skills && skills.length > 0 && (
- <Tooltip>
- <TooltipTrigger asChild>
- <span className="inline-flex align-middle cursor-help">
- <BookOpen className="w-3.5 h-3.5 text-muted-foreground" />
- </span>
- </TooltipTrigger>
- <TooltipContent side="right" sideOffset={4}>
- Skills used: {skills.join(", ")}
- </TooltipContent>
- </Tooltip>
- )}
- </div>
- </div>
- );
-}
diff --git a/packages/template-prompt-to-motion-graphics/src/components/ChatSidebar/ConversationHistory.tsx b/packages/template-prompt-to-motion-graphics/src/components/ChatSidebar/ConversationHistory.tsx
new file mode 100644
index 0000000000..ed4c872b3f
--- /dev/null
+++ b/packages/template-prompt-to-motion-graphics/src/components/ChatSidebar/ConversationHistory.tsx
@@ -0,0 +1,180 @@
+"use client";
+
+import { useRef, useEffect } from "react";
+import { AlertTriangle, FileCode, PenLine, BookOpen } from "lucide-react";
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipTrigger,
+} from "@/components/ui/tooltip";
+import type { ConversationMessage } from "@/types/conversation";
+
+interface ConversationHistoryProps {
+ messages: ConversationMessage[];
+ pendingMessage?: {
+ skills?: string[];
+ startedAt: number;
+ };
+}
+
+export function ConversationHistory({
+ messages,
+ pendingMessage,
+}: ConversationHistoryProps) {
+ const messagesEndRef = useRef<HTMLDivElement>(null);
+
+ // Auto-scroll to bottom when messages change
+ useEffect(() => {
+ messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
+ }, [messages]);
+
+ return (
+ <div className="flex-1 overflow-y-auto px-4 pb-4 space-y-6">
+ {messages.map((message) => (
+ <ChatMessage key={message.id} message={message} />
+ ))}
+ {pendingMessage && <PendingMessage skills={pendingMessage.skills} />}
+ <div ref={messagesEndRef} />
+ </div>
+ );
+}
+
+function ChatMessage({ message }: { message: ConversationMessage }) {
+ const isUser = message.role === "user";
+ const isError = message.role === "error";
+ const metadata = message.metadata;
+
+ const time = new Date(message.timestamp).toLocaleTimeString([], {
+ hour: "numeric",
+ minute: "2-digit",
+ });
+
+ if (isUser) {
+ return (
+ <div className="space-y-1">
+ <div className="flex items-center gap-2">
+ <span className="text-xs font-medium text-muted-foreground">You</span>
+ <span className="text-xs text-muted-foreground-dim">{time}</span>
+ </div>
+ <div className="text-sm text-foreground leading-relaxed bg-secondary/50 rounded-lg px-3 py-2">
+ {message.content}
+ {message.attachedImages && message.attachedImages.length > 0 && (
+ <div className="flex gap-2 mt-2 overflow-x-auto">
+ {message.attachedImages.map((img, index) => (
+ // eslint-disable-next-line @next/next/no-img-element
+ <img
+ key={index}
+ src={img}
+ alt={`Attached ${index + 1}`}
+ className="h-20 w-auto rounded border border-border object-cover flex-shrink-0"
+ />
+ ))}
+ </div>
+ )}
+ </div>
+ </div>
+ );
+ }
+
+ if (isError) {
+ const errorLabel =
+ message.errorType === "api"
+ ? "API Error"
+ : message.errorType === "validation"
+ ? "Validation Error"
+ : "Edit Failed";
+
+ return (
+ <div className="space-y-2">
+ <div className="flex items-center gap-2 text-red-400">
+ <AlertTriangle className="w-4 h-4" />
+ <span className="text-xs font-medium">{errorLabel}</span>
+ </div>
+ <div className="text-xs text-red-300/80 leading-relaxed pl-6">
+ {message.content}
+ </div>
+ </div>
+ );
+ }
+
+ // Assistant message
+ return (
+ <div className="space-y-1">
+ <div className="flex items-center gap-2">
+ <span className="text-xs font-medium text-blue-400">Assistant</span>
+ <span className="text-xs text-muted-foreground-dim">{time}</span>
+ </div>
+
+ <div className="text-sm text-foreground leading-relaxed">
+ {message.content}
+ {metadata?.skills && metadata.skills.length > 0 && (
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <span className="inline-flex ml-1.5 align-middle cursor-help">
+ <BookOpen className="w-3.5 h-3.5 text-muted-foreground" />
+ </span>
+ </TooltipTrigger>
+ <TooltipContent side="right" sideOffset={4}>
+ Skills used: {metadata.skills.join(", ")}
+ </TooltipContent>
+ </Tooltip>
+ )}
+ </div>
+
+ {metadata?.editType === "tool_edit" && metadata.edits && (
+ <div className="space-y-1.5 pl-1 mt-2">
+ {metadata.edits.map((edit, i) => (
+ <div key={i} className="flex items-center gap-2">
+ <PenLine className="w-3.5 h-3.5 text-muted-foreground shrink-0" />
+ <div className="flex-1 min-w-0 leading-none">
+ <span className="text-xs text-muted-foreground">
+ {edit.description}
+ </span>
+ {edit.lineNumber && (
+ <span className="text-[10px] text-muted-foreground-dim font-mono ml-2">
+ L{edit.lineNumber}
+ </span>
+ )}
+ </div>
+ </div>
+ ))}
+ </div>
+ )}
+
+ {metadata?.editType === "full_replacement" && (
+ <div className="flex items-center gap-2 text-muted-foreground">
+ <FileCode className="w-3.5 h-3.5" />
+ <span className="text-xs">Full code rewrite</span>
+ </div>
+ )}
+ </div>
+ );
+}
+
+function PendingMessage({ skills }: { skills?: string[] }) {
+ return (
+ <div className="space-y-1">
+ <div className="flex items-center gap-2">
+ <span className="text-xs font-medium text-blue-400">Assistant</span>
+ <span className="text-xs text-muted-foreground-dim">now</span>
+ </div>
+
+ <div className="flex items-center gap-2 text-sm text-muted-foreground">
+ <div className="w-3 h-3 border-2 border-muted-foreground border-t-blue-400 rounded-full animate-spin" />
+ <span>Generating...</span>
+ {skills && skills.length > 0 && (
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <span className="inline-flex align-middle cursor-help">
+ <BookOpen className="w-3.5 h-3.5 text-muted-foreground" />
+ </span>
+ </TooltipTrigger>
+ <TooltipContent side="right" sideOffset={4}>
+ Skills used: {skills.join(", ")}
+ </TooltipContent>
+ </Tooltip>
+ )}
+ </div>
+ </div>
+ );
+}