"use client"; import { useState, useEffect, useRef } from "react"; interface Message { role: "user" | "assistant"; content: string; timestamp: Date; } export default function ClaudeChatPage() { const [messages, setMessages] = useState([]); const [input, setInput] = useState(""); const [loading, setLoading] = useState(false); const [apiKey, setApiKey] = useState(""); const [showSettings, setShowSettings] = useState(false); const messagesEndRef = useRef(null); useEffect(() => { const savedKey = localStorage.getItem("anthropic_api_key") || ""; setApiKey(savedKey); if (!savedKey) { setShowSettings(true); } }, []); useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); }, [messages]); const sendMessage = async () => { if (!input.trim() || !apiKey) return; const userMessage: Message = { role: "user", content: input, timestamp: new Date(), }; setMessages(prev => [...prev, userMessage]); setInput(""); setLoading(true); try { const response = await fetch("https://api.anthropic.com/v1/messages", { method: "POST", headers: { "Content-Type": "application/json", "x-api-key": apiKey, "anthropic-version": "2023-06-01", "anthropic-dangerous-direct-browser-access": "true", }, body: JSON.stringify({ model: "claude-sonnet-4-20250514", max_tokens: 4096, messages: [ ...messages.map(m => ({ role: m.role, content: m.content })), { role: "user", content: input } ], }), }); if (!response.ok) { const error = await response.json(); throw new Error(error.error?.message || "API Error"); } const data = await response.json(); const assistantMessage: Message = { role: "assistant", content: data.content[0].text, timestamp: new Date(), }; setMessages(prev => [...prev, assistantMessage]); } catch (error: any) { const errorMessage: Message = { role: "assistant", content: `❌ Error: ${error.message}`, timestamp: new Date(), }; setMessages(prev => [...prev, errorMessage]); } setLoading(false); }; const clearChat = () => { setMessages([]); }; const activeTab = messages.length === 0; return (
{/* Header */}

Claude Code Chat

Direct chat with Claude AI

{/* Messages */}
{messages.length === 0 && !loading && (
💬

Chat with Claude

Send a message to start a conversation. Claude will respond directly.

{!apiKey && ( )}
)} {messages.map((msg, i) => (
{msg.content}
{msg.timestamp.toLocaleTimeString()}
))} {loading && (
Claude is thinking...
)}
{/* Input */}