"use client";
import { useState } from "react";
import { ChevronDown, ChevronRight, Copy, Check, Lightbulb, AlertTriangle, Info, Zap, ArrowRight, ArrowLeft } from "lucide-react";
import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";
import { RunPromptButton } from "@/components/prompts/run-prompt-button";
import Link from "next/link";
import { useTranslations } from "next-intl";
// Collapsible Component
interface CollapsibleProps {
title: string;
children: React.ReactNode;
defaultOpen?: boolean;
}
export function Collapsible({ title, children, defaultOpen = false }: CollapsibleProps) {
const [isOpen, setIsOpen] = useState(defaultOpen);
return (
{isOpen && (
{children}
)}
);
}
// Callout Component
interface CalloutProps {
type?: "info" | "warning" | "tip" | "example";
title?: string;
children: React.ReactNode;
}
export function Callout({ type = "info", title, children }: CalloutProps) {
const styles = {
info: {
bg: "bg-blue-50 dark:bg-blue-950/30",
border: "border-blue-200 dark:border-blue-800",
icon: ,
},
warning: {
bg: "bg-amber-50 dark:bg-amber-950/30",
border: "border-amber-200 dark:border-amber-800",
icon: ,
},
tip: {
bg: "bg-green-50 dark:bg-green-950/30",
border: "border-green-200 dark:border-green-800",
icon: ,
},
example: {
bg: "bg-purple-50 dark:bg-purple-950/30",
border: "border-purple-200 dark:border-purple-800",
icon: ,
},
};
const style = styles[type];
return (
{style.icon}
{title &&
{title}}
{children}
);
}
// CopyableCode Component
interface CopyableCodeProps {
code: string;
language?: string;
}
export function CopyableCode({ code, language }: CopyableCodeProps) {
const [copied, setCopied] = useState(false);
const handleCopy = async () => {
await navigator.clipboard.writeText(code);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
};
return (
);
}
// Quiz Component
interface QuizProps {
question: string;
options: string[];
correctIndex: number;
explanation: string;
}
export function Quiz({ question, options, correctIndex, explanation }: QuizProps) {
const [selected, setSelected] = useState(null);
const [showExplanation, setShowExplanation] = useState(false);
const t = useTranslations("book.interactive");
const handleSelect = (index: number) => {
setSelected(index);
setShowExplanation(true);
};
const isCorrect = selected === correctIndex;
return (
{question}
{options.map((option, index) => (
))}
{showExplanation && (
{isCorrect ? t("correct") : t("notQuite")}
{explanation}
)}
);
}
// TryIt Component
interface TryItProps {
prompt: string;
description?: string;
title?: string;
compact?: boolean;
}
function parsePromptVariables(content: string): { name: string; defaultValue: string }[] {
const regex = /\$\{([^:}]+)(?::([^}]*))?\}/g;
const seen = new Map();
let match;
while ((match = regex.exec(content)) !== null) {
const name = match[1];
const defaultValue = match[2] || "";
if (!seen.has(name)) {
seen.set(name, defaultValue);
}
}
return Array.from(seen.entries()).map(([name, defaultValue]) => ({ name, defaultValue }));
}
export function TryIt({ prompt, description, title, compact = false }: TryItProps) {
const [copied, setCopied] = useState(false);
const t = useTranslations("book.interactive");
const displayTitle = title || t("tryIt");
const unfilledVariables = parsePromptVariables(prompt);
const getContentWithVariables = (values: Record) => {
let result = prompt;
for (const [name, value] of Object.entries(values)) {
const regex = new RegExp(`\\$\\{${name}(?::[^}]*)?\\}`, 'g');
result = result.replace(regex, value);
}
return result;
};
const handleCopy = async () => {
await navigator.clipboard.writeText(prompt);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
};
if (compact) {
return (
);
}
return (
{displayTitle}
{description &&
{description}
}
{prompt}
);
}
// Navigation Button Component
interface NavButtonProps {
href: string;
label: string;
direction?: "next" | "prev";
}
export function NavButton({ href, label, direction = "next" }: NavButtonProps) {
return (
);
}
// Navigation Footer Component
interface NavFooterProps {
prev?: { href: string; label: string };
next?: { href: string; label: string };
}
export function NavFooter({ prev, next }: NavFooterProps) {
return (
{prev && }
{next && }
);
}