import { useEffect, useState } from 'react'; import { api } from '../lib/api.ts'; import ReactMarkdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; interface DocSummary { id: string; channel_id: string; creator_id: string; title: string; created_at: string; updated_at: string; } interface DocDetail extends DocSummary { content: string; } interface DocsViewProps { channelId: string; channelName: string; } export function DocsView({ channelId, channelName }: DocsViewProps) { const [docs, setDocs] = useState([]); const [activeDoc, setActiveDoc] = useState(null); const [editing, setEditing] = useState(false); const [title, setTitle] = useState(''); const [content, setContent] = useState(''); const [loading, setLoading] = useState(false); const [showNew, setShowNew] = useState(false); const [newTitle, setNewTitle] = useState(''); useEffect(() => { setLoading(true); api.get(`/channels/${channelId}/docs`) .then((data) => setDocs(Array.isArray(data) ? data : [])) .finally(() => setLoading(false)); }, [channelId]); const openDoc = async (id: string) => { const d = await api.get(`/channels/docs/${id}`); setActiveDoc(d); setTitle(d.title); setContent(d.content); setEditing(false); }; const createDoc = async () => { if (!newTitle.trim()) return; const d = await api.post(`/channels/${channelId}/docs`, { title: newTitle, content: '' }); setDocs((prev) => [{ id: d.id, channel_id: d.channel_id, creator_id: d.creator_id, title: d.title, created_at: d.created_at, updated_at: d.updated_at }, ...prev]); setShowNew(false); setNewTitle(''); openDoc(d.id); }; const saveDoc = async () => { if (!activeDoc) return; const d = await api.patch(`/channels/docs/${activeDoc.id}`, { title, content }); setActiveDoc(d); setDocs((prev) => prev.map((s) => s.id === d.id ? { ...s, title: d.title, updated_at: d.updated_at } : s)); setEditing(false); }; const deleteDoc = async (id: string) => { if (!confirm('Delete this doc?')) return; await api.delete(`/channels/docs/${id}`); setDocs((prev) => prev.filter((d) => d.id !== id)); if (activeDoc?.id === id) setActiveDoc(null); }; return (
☰ {channelName}
{showNew && (
setNewTitle(e.target.value)} placeholder="doc title" className="terminal-input flex-1" />
)}
{loading &&
[loading...]
} {!loading && docs.length === 0 &&
[no docs]
} {docs.map((d) => (
openDoc(d.id)} className={`p-2 cursor-pointer truncate ${activeDoc?.id === d.id ? 'bg-gb-bg-t text-gb-fg' : 'text-gb-fg-s hover:bg-gb-bg-s'}`} > {d.title}
updated {new Date(d.updated_at).toLocaleDateString()}
))}
{!activeDoc && (
select or create a doc
)} {activeDoc && !editing && (

{activeDoc.title}

{activeDoc.content || '*empty*'}
)} {activeDoc && editing && (
setTitle(e.target.value)} className="terminal-input w-full text-sm font-bold" />