'use client'; import React, { useState, useEffect } from 'react'; import { Document, DocumentActivity, docApi } from '@/lib/api-docs'; import { ClockIcon, DocumentIcon, PlusIcon, MagnifyingGlassIcon, ChevronRightIcon, HomeIcon, ArrowUpIcon, FolderIcon, FolderOpenIcon } from "@heroicons/react/24/outline"; import { format, parseISO } from 'date-fns'; import { ptBR } from 'date-fns/locale'; import { toast } from 'react-hot-toast'; interface DocumentSidebarProps { documentId: string; onNavigate: (doc: Document) => void; } export default function DocumentSidebar({ documentId, onNavigate }: DocumentSidebarProps) { const [currentDoc, setCurrentDoc] = useState(null); const [children, setChildren] = useState([]); const [parentDoc, setParentDoc] = useState(null); const [rootPages, setRootPages] = useState([]); const [activities, setActivities] = useState([]); const [activeTab, setActiveTab] = useState<'subpages' | 'activities'>('subpages'); const [searchTerm, setSearchTerm] = useState(''); const [loading, setLoading] = useState(false); useEffect(() => { if (documentId) { fetchData(); } }, [documentId]); const fetchData = async () => { setLoading(true); console.log('🔍 Sidebar fetching data for:', documentId); const fetchSafely = async (promise: Promise) => { try { return await promise; } catch (e) { console.warn('Sidebar part failed:', e); return null; } }; try { const [doc, roots, logs, subpages] = await Promise.all([ docApi.getDocument(documentId), fetchSafely(docApi.getDocuments()), fetchSafely(docApi.getActivities(documentId)), fetchSafely(docApi.getSubpages(documentId)) ]); if (doc) { setCurrentDoc(doc); setChildren(subpages || []); if (doc.parent_id) { const parent = await fetchSafely(docApi.getDocument(doc.parent_id)); setParentDoc(parent); } else { setParentDoc(null); } } setRootPages(roots || []); setActivities(logs || []); } catch (e) { console.error('❌ Sidebar critical error:', e); toast.error('Erro ao carregar estrutura'); } finally { setLoading(false); } }; const handleCreateSubpage = async () => { try { const newDoc = await docApi.createDocument({ title: 'Nova Subpágina', parent_id: documentId as any, content: '{"type":"doc","content":[{"type":"paragraph"}]}', status: 'published' }); toast.success('Página criada!'); // Refresh local para aparecer imediatamente setChildren(prev => [...prev, newDoc]); onNavigate(newDoc); } catch (e) { toast.error('Erro ao criar subpágina'); } }; const filteredChildren = children.filter(p => p.title.toLowerCase().includes(searchTerm.toLowerCase())); const rootWikis = rootPages.filter(p => !p.parent_id); return (
{/* Tabs de Navegação */}
{activeTab === 'subpages' ? ( <> {/* Status do NĂ­vel Atual */}
{parentDoc && ( )}

Você está em

{currentDoc?.title || 'Sem tĂ­tulo'}

{/* Search */}
setSearchTerm(e.target.value)} className="w-full bg-white dark:bg-zinc-900 border border-zinc-200 dark:border-zinc-800 rounded-xl pl-10 pr-4 py-2 text-xs outline-none focus:ring-2 ring-brand-500/20" />
{/* Subpages (Children) */}

Subpáginas ({children.length})

{filteredChildren.length === 0 && (

Nenhuma subpágina

)} {filteredChildren.map(page => ( ))}
{/* Root wikis */} {!searchTerm && (

Todas as Wikis

{rootWikis.map(page => ( ))}
)} ) : (
{activities.map(log => (
{log.user_name?.[0]?.toUpperCase()}

{log.user_name} {log.description}

{format(parseISO(log.created_at), "dd MMM, HH:mm", { locale: ptBR })}
))}
)}
); }