import { useEffect, useState } from 'react'; import { api } from '../lib/api.ts'; interface ListItem { id: string; channel_id: string; creator_id: string; title: string; status: string; position: number; created_at: string; updated_at: string; } interface ListViewProps { channelId: string; channelName: string; } export function ListView({ channelId, channelName }: ListViewProps) { const [items, setItems] = useState([]); const [newTitle, setNewTitle] = useState(''); useEffect(() => { api.get(`/channels/${channelId}/items`) .then((data) => setItems(Array.isArray(data) ? data : [])); }, [channelId]); const addItem = async () => { if (!newTitle.trim()) return; const it = await api.post(`/channels/${channelId}/items`, { title: newTitle }); setItems((prev) => [...prev, it]); setNewTitle(''); }; const toggleStatus = async (it: ListItem) => { const next = it.status === 'done' ? 'todo' : it.status === 'in_progress' ? 'done' : 'in_progress'; const updated = await api.patch(`/channels/items/${it.id}`, { status: next }); setItems((prev) => prev.map((x) => x.id === updated.id ? updated : x)); }; const deleteItem = async (id: string) => { await api.delete(`/channels/items/${id}`); setItems((prev) => prev.filter((x) => x.id !== id)); }; const todo = items.filter((x) => x.status === 'todo'); const inProgress = items.filter((x) => x.status === 'in_progress'); const done = items.filter((x) => x.status === 'done'); const col = (list: ListItem[], label: string, color: string) => (
{label} ({list.length})
{list.map((it) => (
{it.title}
))}
); return (
☑ {channelName}
setNewTitle(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && addItem()} placeholder="add item..." className="terminal-input flex-1" />
{col(todo, 'TODO', 'text-gb-fg')} {col(inProgress, 'DOING', 'text-gb-blue')} {col(done, 'DONE', 'text-gb-fg-f')}
); }