Skip to content

🎥 Make videos programmatically with React

refactor(tp2mg): unify error displays with variant-based ErrorDisplay

ASchwadd78e66f57627
diff --git a/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer/RenderControls/Error.tsx b/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer/RenderControls/Error.tsx
index 7f5932d905..1eb512da59 100644
--- a/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer/RenderControls/Error.tsx
+++ b/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer/RenderControls/Error.tsx
@@ -1,6 +1,5 @@
import React from "react";
-import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert";
-import { AlertCircle } from "lucide-react";
+import { ErrorDisplay } from "@/components/ErrorDisplay";
const isLambdaNotConfiguredError = (message: string): boolean => {
return (
@@ -16,32 +15,30 @@ export const ErrorComp: React.FC<{
if (isLambdaError) {
return (
- <Alert variant="destructive" className="my-2">
- <AlertCircle className="h-4 w-4" />
- <AlertTitle>Lambda not configured</AlertTitle>
- <AlertDescription>
- To render videos, set up Remotion Lambda and add your AWS credentials
- to the <code className="font-mono text-xs">.env</code> file.{" "}
- <a
- href="https://www.remotion.dev/docs/lambda/setup"
- target="_blank"
- rel="noopener noreferrer"
- className="underline"
- >
- Setup guide
- </a>
- </AlertDescription>
- </Alert>
+ <ErrorDisplay
+ error={`To render videos, set up Remotion Lambda and add your AWS credentials to the .env file.`}
+ title="Lambda not configured"
+ variant="card"
+ size="md"
+ >
+ <a
+ href="https://www.remotion.dev/docs/lambda/setup"
+ target="_blank"
+ rel="noopener noreferrer"
+ className="underline text-sm"
+ >
+ Setup guide
+ </a>
+ </ErrorDisplay>
);
}
return (
- <Alert variant="destructive" className="my-2">
- <AlertCircle className="h-4 w-4" />
- <AlertTitle>Error</AlertTitle>
- <AlertDescription className="font-mono whitespace-pre-wrap break-words">
- {message}
- </AlertDescription>
- </Alert>
+ <ErrorDisplay
+ error={message}
+ title="Error"
+ variant="card"
+ size="md"
+ />
);
};
diff --git a/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer/index.tsx b/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer/index.tsx
index 9b0283d786..d4cb87c8fb 100644
--- a/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer/index.tsx
+++ b/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer/index.tsx
@@ -6,18 +6,20 @@ import { ErrorDisplay, type ErrorType } from "../ErrorDisplay";
import { RenderControls } from "./RenderControls";
import { SettingsModal } from "./SettingsModal";
+const errorTitles: Record<ErrorType, string> = {
+ validation: "Invalid Prompt",
+ api: "API Error",
+ compilation: "Compilation Error",
+};
+
const renderErrorFallback: ErrorFallback = ({ error }) => {
return (
- <div className="w-full h-full flex justify-center items-center bg-background-error p-10">
- <div className="text-center max-w-[80%]">
- <div className="text-destructive text-3xl font-bold mb-4 font-sans">
- Runtime Error
- </div>
- <div className="text-destructive-foreground text-xl font-mono whitespace-pre-wrap break-words">
- {error.message || "An error occurred while rendering"}
- </div>
- </div>
- </div>
+ <ErrorDisplay
+ error={error.message || "An error occurred while rendering"}
+ title="Runtime Error"
+ variant="fullscreen"
+ size="lg"
+ />
);
};
@@ -107,7 +109,14 @@ export const AnimationPlayer: React.FC<AnimationPlayerProps> = ({
}
if (error) {
- return <ErrorDisplay error={error} errorType={errorType} />;
+ return (
+ <ErrorDisplay
+ error={error}
+ title={errorTitles[errorType]}
+ variant="fullscreen"
+ size="lg"
+ />
+ );
}
if (!Component) {
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 d98a5ac440..12174e45e5 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
@@ -3,7 +3,7 @@
import { useState, useEffect, type ComponentType } from "react";
import { ArrowUp, Camera, X, Paperclip } from "lucide-react";
import { Button } from "@/components/ui/button";
-import { Alert, AlertDescription } from "@/components/ui/alert";
+import { ErrorDisplay } from "@/components/ErrorDisplay";
import {
Select,
SelectContent,
@@ -116,18 +116,13 @@ export function ChatInput({
>
{/* Error message */}
{error && (
- <Alert variant="destructive" className="mb-2 py-2 flex items-center justify-between text-xs">
- <AlertDescription className="text-xs">{error}</AlertDescription>
- <Button
- type="button"
- variant="ghost"
- size="icon-sm"
- onClick={clearError}
- className="h-5 w-5 text-destructive hover:text-destructive hover:bg-destructive/20"
- >
- <X className="w-3 h-3" />
- </Button>
- </Alert>
+ <ErrorDisplay
+ error={error}
+ variant="inline"
+ size="sm"
+ onDismiss={clearError}
+ className="mb-2 py-2"
+ />
)}
{/* Image previews */}
diff --git a/packages/template-prompt-to-motion-graphics/src/components/ErrorDisplay.tsx b/packages/template-prompt-to-motion-graphics/src/components/ErrorDisplay.tsx
index 523b13b7ab..b2eb0d07e0 100644
--- a/packages/template-prompt-to-motion-graphics/src/components/ErrorDisplay.tsx
+++ b/packages/template-prompt-to-motion-graphics/src/components/ErrorDisplay.tsx
@@ -2,36 +2,89 @@
import React from "react";
import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert";
-import { AlertCircle } from "lucide-react";
+import { Button } from "@/components/ui/button";
+import { AlertCircle, X } from "lucide-react";
+import { cn } from "@/lib/utils";
-export type ErrorType = "compilation" | "api" | "validation";
+export type ErrorVariant = "inline" | "card" | "fullscreen";
+export type ErrorSize = "sm" | "md" | "lg";
interface ErrorDisplayProps {
error: string;
- errorType: ErrorType;
+ title?: string;
+ variant?: ErrorVariant;
+ size?: ErrorSize;
+ onDismiss?: () => void;
+ children?: React.ReactNode;
+ className?: string;
}
-const errorTitles: Record<ErrorType, string> = {
- validation: "Invalid Prompt",
- api: "API Error",
- compilation: "Compilation Error",
+const sizeStyles: Record<ErrorSize, { icon: string; title: string; description: string }> = {
+ sm: { icon: "h-3 w-3", title: "text-xs", description: "text-xs" },
+ md: { icon: "h-4 w-4", title: "text-sm font-semibold", description: "text-sm" },
+ lg: { icon: "h-5 w-5", title: "text-base font-semibold", description: "text-base" },
};
export const ErrorDisplay: React.FC<ErrorDisplayProps> = ({
error,
- errorType,
+ title,
+ variant = "card",
+ size = "md",
+ onDismiss,
+ children,
+ className,
}) => {
- return (
- <div className="w-full aspect-video max-h-[calc(100%-80px)] flex justify-center items-center bg-background-error rounded-lg overflow-hidden shadow-[0_0_60px_rgba(0,0,0,0.5)] border border-destructive">
- <Alert variant="destructive" className="max-w-[80%] bg-transparent border-none text-center">
- <AlertCircle className="h-5 w-5 mx-auto" />
- <AlertTitle className="text-base font-semibold">
- {errorTitles[errorType]}
- </AlertTitle>
- <AlertDescription className="text-destructive-foreground font-mono whitespace-pre-wrap break-words">
- {error}
- </AlertDescription>
+ const styles = sizeStyles[size];
+
+ if (variant === "fullscreen") {
+ return (
+ <div className="w-full aspect-video max-h-[calc(100%-80px)] flex justify-center items-center bg-background-error rounded-lg overflow-hidden shadow-[0_0_60px_rgba(0,0,0,0.5)] border border-destructive">
+ <Alert variant="destructive" className="max-w-[80%] bg-transparent border-none text-center">
+ <AlertCircle className={cn(styles.icon, "mx-auto")} />
+ {title && <AlertTitle className={styles.title}>{title}</AlertTitle>}
+ <AlertDescription className={cn(styles.description, "text-destructive-foreground font-mono whitespace-pre-wrap break-words")}>
+ {error}
+ </AlertDescription>
+ {children}
+ </Alert>
+ </div>
+ );
+ }
+
+ if (variant === "inline") {
+ return (
+ <Alert variant="destructive" className={cn("flex items-center justify-between", className)}>
+ <AlertDescription className={styles.description}>{error}</AlertDescription>
+ {onDismiss && (
+ <Button
+ type="button"
+ variant="ghost"
+ size="icon-sm"
+ onClick={onDismiss}
+ className={cn(
+ "text-destructive hover:text-destructive hover:bg-destructive/20",
+ size === "sm" ? "h-5 w-5" : "h-6 w-6"
+ )}
+ >
+ <X className={styles.icon} />
+ </Button>
+ )}
</Alert>
- </div>
+ );
+ }
+
+ // card variant (default)
+ return (
+ <Alert variant="destructive" className={cn("my-2", className)}>
+ <AlertCircle className={styles.icon} />
+ {title && <AlertTitle className={styles.title}>{title}</AlertTitle>}
+ <AlertDescription className={cn(styles.description, "font-mono whitespace-pre-wrap break-words")}>
+ {error}
+ </AlertDescription>
+ {children}
+ </Alert>
);
};
+
+// ErrorType is still used by AnimationPlayer for its interface
+export type ErrorType = "compilation" | "api" | "validation";
diff --git a/packages/template-prompt-to-motion-graphics/src/components/LandingPageInput.tsx b/packages/template-prompt-to-motion-graphics/src/components/LandingPageInput.tsx
index 70b846dae2..0eea122bb4 100644
--- a/packages/template-prompt-to-motion-graphics/src/components/LandingPageInput.tsx
+++ b/packages/template-prompt-to-motion-graphics/src/components/LandingPageInput.tsx
@@ -22,7 +22,7 @@ import {
SelectValue,
} from "@/components/ui/select";
import { Button } from "@/components/ui/button";
-import { Alert, AlertDescription } from "@/components/ui/alert";
+import { ErrorDisplay } from "@/components/ErrorDisplay";
import { examplePrompts } from "@/examples/prompts";
import { type ModelId, MODELS } from "@/types/generation";
import { useImageAttachments } from "@/hooks/useImageAttachments";
@@ -102,18 +102,13 @@ export function LandingPageInput({
>
{/* Error message */}
{error && (
- <Alert variant="destructive" className="mb-3 flex items-center justify-between">
- <AlertDescription>{error}</AlertDescription>
- <Button
- type="button"
- variant="ghost"
- size="icon-sm"
- onClick={clearError}
- className="h-6 w-6 text-destructive hover:text-destructive hover:bg-destructive/20"
- >
- <X className="w-4 h-4" />
- </Button>
- </Alert>
+ <ErrorDisplay
+ error={error}
+ variant="inline"
+ size="md"
+ onDismiss={clearError}
+ className="mb-3"
+ />
)}
{/* Image previews */}