import { useState, type FormEvent } from 'react'; import { useChannels } from '../hooks/use-channels'; import type { ChannelVisibility } from '../types'; /** Browse + join discoverable channels, and create a new one. Shown in the main pane. */ export function ChannelBrowser({ onJoined }: { onJoined?: (threadId: string) => void }) { const { browsable, loading, error, join, create } = useChannels(); const [name, setName] = useState(''); const [topic, setTopic] = useState(''); const [visibility, setVisibility] = useState('public'); const [busy, setBusy] = useState(false); async function submitCreate(e: FormEvent): Promise { e.preventDefault(); const n = name.trim(); if (!n || busy) return; setBusy(true); try { const threadId = await create({ name: n, ...(topic.trim() ? { topic: topic.trim() } : {}), visibility }); setName(''); setTopic(''); onJoined?.(threadId); } catch { // surfaced via the hook's error } finally { setBusy(false); } } async function doJoin(threadId: string): Promise { await join(threadId); onJoined?.(threadId); } return (
Channels
setName(e.target.value)} placeholder="New channel name" aria-label="Channel name" /> setTopic(e.target.value)} placeholder="Topic (optional)" aria-label="Channel topic" />
{loading && browsable.length === 0 ?
Loading…
: null} {error ?
{error}
: null} {!loading && browsable.length === 0 ?
No channels yet — create one above.
: null} {browsable.map((c) => (
{c.name} {c.topic ? {c.topic} : null} {c.memberCount} member{c.memberCount === 1 ? '' : 's'} {c.joined ? ( Joined ) : ( )}
))}
); }