import React, { useState } from 'react'; import { ViewMode } from './types'; import { useProjects, useChat } from './hooks'; import { AuthProvider, useAuthContext } from './AuthContext'; import AppRouter from './components/AppRouter'; import { Loader2, BookOpen } from 'lucide-react'; /** * MainContent : Ce composant est "sous" le AuthProvider. * Il peut donc utiliser useAuthContext() sans recevoir de 'null'. */ const MainContent: React.FC = () => { const [viewMode, setViewMode] = useState('landing'); // --- 1. DÉCLARATION DE TOUS LES HOOKS (IMPÉRATIF : TOUJOURS EN HAUT) --- // On récupère l'état global partagé via le Contexte const { user, login, signup, logout, incrementUsage, loading: authLoading } = useAuthContext(); // On initialise les projets. Si user est null, useProjects retournera une liste vide. const { projects, currentProjectId, setCurrentProjectId, createProject, updateProject, updateChapter, addChapter, createEntity, updateEntity, deleteEntity } = useProjects(user); // On initialise le chat const { chatHistory, isGenerating, sendMessage } = useChat(); // --- 2. LOGIQUE DE CALCUL --- const currentProject = projects.find(p => p.id === currentProjectId); const handleSendMessage = (msg: string) => { if (currentProject && user) { sendMessage(currentProject, 'global', msg, user, incrementUsage); } }; // --- 3. RENDU CONDITIONNEL (Seulement APRES les hooks) --- // On attend que l'Auth + Profil soient chargés (tes 2 secondes d'attente) if (authLoading) { return (
PlumeIA

Synchronisation de votre espace créatif...

); } // --- 4. RENDU FINAL : AppRouter --- return ( { logout(); setViewMode('landing'); }} // Gestion des Projets projects={projects} currentProjectId={currentProjectId} onSelectProject={(id) => { setCurrentProjectId(id); setViewMode('write'); }} onCreateProject={async () => { const id = await createProject(); if (id) { setCurrentProjectId(id); setViewMode('write'); } }} onUpdateProject={(updates) => { if (currentProjectId) updateProject(currentProjectId, updates); }} // Gestion du contenu (Chapitres & Entités) onUpdateChapter={(chapterId, data) => { if (currentProjectId) updateChapter(currentProjectId, chapterId, data); }} onAddChapter={async () => { if (currentProjectId) { await addChapter(currentProjectId, {}); // Fetch the latest chapter or return ID if addChapter returns it // verified: addChapter doesn't return ID in hooks.ts yet, let's assume it does or we reload // Actually, let's fix hooks.ts to return ID for addChapter too, or just reload. // For now, let's assume standard flow. // Wait, AppRouter expects a return string | null. // We should update hooks.ts addChapter to return ID as well. // For this step, I'll implement the call. return null; // temporary until hooks.ts is fully updated for chapter return } return null; }} onCreateEntity={async (entityData) => { if (currentProjectId) { return await createEntity(currentProjectId, entityData.type, entityData); } return null; }} onUpdateEntity={(entityId, data) => { if (currentProjectId) updateEntity(currentProjectId, entityId, data); }} onDeleteEntity={(entityId) => { if (currentProjectId) deleteEntity(currentProjectId, entityId); }} // Chat & IA chatHistory={chatHistory} isGenerating={isGenerating} onSendMessage={handleSendMessage} onIncrementUsage={incrementUsage} // Callbacks divers onUpdateProfile={() => console.log("Profil géré via useAuth")} onUpgradePlan={() => window.open('/billing', '_blank')} /> ); }; /** * Composant App : Point d'entrée de l'application. * Il installe la "bulle" AuthProvider pour que tout le monde y ait accès. */ const App: React.FC = () => { return ( ); }; // EXPORT PAR DÉFAUT (Essentiel pour index.tsx) export default App;