import { useState, useEffect } from "react"; import { motion, AnimatePresence } from "framer-motion"; import { Plus, Loader2, Bot, FolderCode } from "lucide-react"; import { api, type Project, type Session, type ClaudeMdFile } from "@/lib/api"; import { OutputCacheProvider } from "@/lib/outputCache"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { ProjectList } from "@/components/ProjectList"; import { SessionList } from "@/components/SessionList"; import { RunningClaudeSessions } from "@/components/RunningClaudeSessions"; import { Topbar } from "@/components/Topbar"; import { MarkdownEditor } from "@/components/MarkdownEditor"; import { ClaudeFileEditor } from "@/components/ClaudeFileEditor"; import { Settings } from "@/components/Settings"; import { CCAgents } from "@/components/CCAgents"; import { ClaudeCodeSession } from "@/components/ClaudeCodeSession"; import { UsageDashboard } from "@/components/UsageDashboard"; import { MCPManager } from "@/components/MCPManager"; import { NFOCredits } from "@/components/NFOCredits"; import { ClaudeBinaryDialog } from "@/components/ClaudeBinaryDialog"; import { Toast, ToastContainer } from "@/components/ui/toast"; type View = "welcome" | "projects" | "agents" | "editor" | "settings" | "claude-file-editor" | "claude-code-session" | "usage-dashboard" | "mcp"; /** * Main App component - Manages the Claude directory browser UI */ function App() { const [view, setView] = useState("welcome"); const [projects, setProjects] = useState([]); const [selectedProject, setSelectedProject] = useState(null); const [sessions, setSessions] = useState([]); const [editingClaudeFile, setEditingClaudeFile] = useState(null); const [selectedSession, setSelectedSession] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [showNFO, setShowNFO] = useState(false); const [showClaudeBinaryDialog, setShowClaudeBinaryDialog] = useState(false); const [toast, setToast] = useState<{ message: string; type: "success" | "error" | "info" } | null>(null); const [activeClaudeSessionId, setActiveClaudeSessionId] = useState(null); const [isClaudeStreaming, setIsClaudeStreaming] = useState(false); // Load projects on mount when in projects view useEffect(() => { if (view === "projects") { loadProjects(); } else if (view === "welcome") { // Reset loading state for welcome view setLoading(false); } }, [view]); // Listen for Claude session selection events useEffect(() => { const handleSessionSelected = (event: CustomEvent) => { const { session } = event.detail; setSelectedSession(session); handleViewChange("claude-code-session"); }; const handleClaudeNotFound = () => { setShowClaudeBinaryDialog(true); }; window.addEventListener('claude-session-selected', handleSessionSelected as EventListener); window.addEventListener('claude-not-found', handleClaudeNotFound as EventListener); return () => { window.removeEventListener('claude-session-selected', handleSessionSelected as EventListener); window.removeEventListener('claude-not-found', handleClaudeNotFound as EventListener); }; }, []); /** * Loads all projects from the ~/.claude/projects directory */ const loadProjects = async () => { try { setLoading(true); setError(null); const projectList = await api.listProjects(); setProjects(projectList); } catch (err) { console.error("Failed to load projects:", err); setError("Failed to load projects. Please ensure ~/.claude directory exists."); } finally { setLoading(false); } }; /** * Handles project selection and loads its sessions */ const handleProjectClick = async (project: Project) => { try { setLoading(true); setError(null); const sessionList = await api.getProjectSessions(project.id); setSessions(sessionList); setSelectedProject(project); } catch (err) { console.error("Failed to load sessions:", err); setError("Failed to load sessions for this project."); } finally { setLoading(false); } }; /** * Opens a new Claude Code session in the interactive UI */ const handleNewSession = async () => { handleViewChange("claude-code-session"); setSelectedSession(null); }; /** * Returns to project list view */ const handleBack = () => { setSelectedProject(null); setSessions([]); }; /** * Handles editing a CLAUDE.md file from a project */ const handleEditClaudeFile = (file: ClaudeMdFile) => { setEditingClaudeFile(file); handleViewChange("claude-file-editor"); }; /** * Returns from CLAUDE.md file editor to projects view */ const handleBackFromClaudeFileEditor = () => { setEditingClaudeFile(null); handleViewChange("projects"); }; /** * Handles view changes with navigation protection */ const handleViewChange = (newView: View) => { // Check if we're navigating away from an active Claude session if (view === "claude-code-session" && isClaudeStreaming && activeClaudeSessionId) { const shouldLeave = window.confirm( "Claude is still responding. If you navigate away, Claude will continue running in the background.\n\n" + "You can return to this session from the Projects view.\n\n" + "Do you want to continue?" ); if (!shouldLeave) { return; } } setView(newView); }; const renderContent = () => { switch (view) { case "welcome": return (
{/* Welcome Header */}

Welcome to Claudia

{/* Navigation Cards */}
{/* CC Agents Card */} handleViewChange("agents")} >

CC Agents

{/* CC Projects Card */} handleViewChange("projects")} >

CC Projects

); case "agents": return (
handleViewChange("welcome")} />
); case "editor": return (
handleViewChange("welcome")} />
); case "settings": return (
handleViewChange("welcome")} />
); case "projects": return (
{/* Header with back button */}

CC Projects

Browse your Claude Code sessions

{/* Error display */} {error && ( {error} )} {/* Loading state */} {loading && (
)} {/* Content */} {!loading && ( {selectedProject ? ( ) : ( {/* New session button at the top */} {/* Running Claude Sessions */} {/* Project list */} {projects.length > 0 ? ( ) : (

No projects found in ~/.claude/projects

)}
)}
)}
); case "claude-file-editor": return editingClaudeFile ? ( ) : null; case "claude-code-session": return ( { setSelectedSession(null); handleViewChange("projects"); }} onStreamingChange={(isStreaming, sessionId) => { setIsClaudeStreaming(isStreaming); setActiveClaudeSessionId(sessionId); }} /> ); case "usage-dashboard": return ( handleViewChange("welcome")} /> ); case "mcp": return ( handleViewChange("welcome")} /> ); default: return null; } }; return (
{/* Topbar */} handleViewChange("editor")} onSettingsClick={() => handleViewChange("settings")} onUsageClick={() => handleViewChange("usage-dashboard")} onMCPClick={() => handleViewChange("mcp")} onInfoClick={() => setShowNFO(true)} /> {/* Main Content */}
{renderContent()}
{/* NFO Credits Modal */} {showNFO && setShowNFO(false)} />} {/* Claude Binary Dialog */} { setToast({ message: "Claude binary path saved successfully", type: "success" }); // Trigger a refresh of the Claude version check window.location.reload(); }} onError={(message) => setToast({ message, type: "error" })} /> {/* Toast Container */} {toast && ( setToast(null)} /> )}
); } export default App;