@@ -9,15 +9,18 @@ import {
99 MicOff,
1010 Moon,
1111 PhoneOff,
12+ Plus,
1213 ScreenShare,
1314 ScreenShareOff,
1415 Settings,
1516 Sun,
17+ Trash2,
1618 UserPlus,
1719 Volume2,
1820 X,
1921 } from "lucide-react";
2022 import { Button } from "@/components/ui/button";
23+import { Input } from "@/components/ui/input";
2124 import { api } from "../lib/api";
2225 import type { Channel, UserSummary } from "../lib/types";
2326 import type { Mode } from "../lib/theme";
@@ -34,6 +37,8 @@ interface Invite {
3437 // button still renders, disabled, so its absence is never a silent mystery.
3538 const canShare = typeof navigator.mediaDevices?.getDisplayMedia === "function";
3639
40+const byName = (a: Channel, b: Channel) => a.name.localeCompare(b.name);
41+
3742 export default function Sidebar({
3843 channels,
3944 selected,
@@ -73,6 +78,29 @@ export default function Sidebar({
7378 }) {
7479 const [label, setLabel] = useState("create invite");
7580 const [showUsers, setShowUsers] = useState(true);
81+ const [creating, setCreating] = useState<"text" | "voice" | null>(null);
82+ const [draft, setDraft] = useState("");
83+
84+ const texts = channels.filter((c) => c.type === "text").sort(byName);
85+ const voices = channels.filter((c) => c.type === "voice").sort(byName);
86+ const dms = channels.filter((c) => c.type === "dm").sort(byName);
87+
88+ async function createChannel(type: "text" | "voice") {
89+ const name = draft.trim();
90+ if (!name) return;
91+ try {
92+ await api("/channels", { method: "POST", body: JSON.stringify({ type, name }) });
93+ setCreating(null);
94+ setDraft("");
95+ } catch {
96+ return;
97+ }
98+ }
99+
100+ async function deleteChannel(c: Channel) {
101+ if (c.type === "text" && !window.confirm(`Delete #${c.name}? This is irreversible.`)) return;
102+ await api(`/channels/${c.id}`, { method: "DELETE" }).catch(() => {});
103+ }
76104
77105 async function invite() {
78106 try {
@@ -91,31 +119,64 @@ export default function Sidebar({
91119 <nav className="flex-1 space-y-4 overflow-y-auto px-2 pb-3">
92120 <Group
93121 label="Text"
94- channels={channels.filter((c) => c.type === "text")}
122+ channels={texts}
95123 selected={selected}
96124 onSelect={onSelect}
125+ onDelete={deleteChannel}
126+ onCreate={() => {
127+ setCreating("text");
128+ setDraft("");
129+ }}
97130 icon={<Hash size={14} />}
98131 />
132+ {creating === "text" && (
133+ <ChannelForm
134+ value={draft}
135+ onChange={setDraft}
136+ onSubmit={() => createChannel("text")}
137+ onCancel={() => setCreating(null)}
138+ />
139+ )}
99140 <div>
100- <div className="px-2 py-1 text-[10px] uppercase tracking-widest text-sepia/40">Voice</div>
101- {channels
102- .filter((c) => c.type === "voice")
103- .map((c) => (
104- <VoiceChannel
105- key={c.id}
106- channel={c}
107- voice={voice}
108- occupants={voiceRooms[String(c.id)] ?? []}
109- openShare={openShare}
110- onOpenShare={onOpenShare}
111- username={username}
112- />
113- ))}
141+ <div className="flex items-center justify-between px-2 py-1">
142+ <span className="text-[10px] uppercase tracking-widest text-sepia/40">Voice</span>
143+ <Button
144+ variant="ghost"
145+ size="icon-xs"
146+ onClick={() => {
147+ setCreating("voice");
148+ setDraft("");
149+ }}
150+ title="new voice channel"
151+ >
152+ <Plus />
153+ </Button>
154+ </div>
155+ {voices.map((c) => (
156+ <VoiceChannel
157+ key={c.id}
158+ channel={c}
159+ voice={voice}
160+ occupants={voiceRooms[String(c.id)] ?? []}
161+ openShare={openShare}
162+ onOpenShare={onOpenShare}
163+ username={username}
164+ onDelete={deleteChannel}
165+ />
166+ ))}
114167 </div>
115- {channels.some((c) => c.type === "dm") && (
168+ {creating === "voice" && (
169+ <ChannelForm
170+ value={draft}
171+ onChange={setDraft}
172+ onSubmit={() => createChannel("voice")}
173+ onCancel={() => setCreating(null)}
174+ />
175+ )}
176+ {dms.length > 0 && (
116177 <Group
117178 label="DMs"
118- channels={channels.filter((c) => c.type === "dm")}
179+ channels={dms}
119180 selected={selected}
120181 onSelect={onSelect}
121182 onClose={onCloseDM}
@@ -176,6 +237,8 @@ function Group({
176237 selected,
177238 onSelect,
178239 onClose,
240+ onDelete,
241+ onCreate,
179242 icon,
180243 }: {
181244 label: string;
@@ -183,11 +246,25 @@ function Group({
183246 selected: Channel | null;
184247 onSelect: (c: Channel) => void;
185248 onClose?: (c: Channel) => void;
249+ onDelete?: (c: Channel) => void;
250+ onCreate?: () => void;
186251 icon: ReactNode;
187252 }) {
188253 return (
189254 <div>
190- <div className="px-2 py-1 text-[10px] uppercase tracking-widest text-sepia/40">{label}</div>
255+ <div className="flex items-center justify-between px-2 py-1">
256+ <span className="text-[10px] uppercase tracking-widest text-sepia/40">{label}</span>
257+ {onCreate && (
258+ <Button
259+ variant="ghost"
260+ size="icon-xs"
261+ onClick={onCreate}
262+ title={`new ${label.toLowerCase()} channel`}
263+ >
264+ <Plus />
265+ </Button>
266+ )}
267+ </div>
191268 {channels.map((c) => (
192269 <div key={c.id} className="group/dm relative">
193270 <Button
@@ -209,12 +286,55 @@ function Group({
209286 <X />
210287 </Button>
211288 )}
289+ {onDelete && !c.permanent && (
290+ <Button
291+ variant="ghost"
292+ size="icon-xs"
293+ onClick={() => onDelete(c)}
294+ title="delete channel"
295+ className="absolute top-1/2 right-1 -translate-y-1/2 opacity-0 group-hover/dm:opacity-100"
296+ >
297+ <Trash2 />
298+ </Button>
299+ )}
212300 </div>
213301 ))}
214302 </div>
215303 );
216304 }
217305
306+function ChannelForm({
307+ value,
308+ onChange,
309+ onSubmit,
310+ onCancel,
311+}: {
312+ value: string;
313+ onChange: (v: string) => void;
314+ onSubmit: () => void;
315+ onCancel: () => void;
316+}) {
317+ return (
318+ <form
319+ className="px-2 py-1"
320+ onSubmit={(e) => {
321+ e.preventDefault();
322+ onSubmit();
323+ }}
324+ >
325+ <Input
326+ autoFocus
327+ value={value}
328+ placeholder="channel name"
329+ onChange={(e) => onChange(e.target.value)}
330+ onKeyDown={(e) => {
331+ if (e.key === "Escape") onCancel();
332+ }}
333+ />
334+ </form>
335+ );
336+}
337+
218338 function VoiceChannel({
219339 channel,
220340 voice,
@@ -222,6 +342,7 @@ function VoiceChannel({
222342 openShare,
223343 onOpenShare,
224344 username,
345+ onDelete,
225346 }: {
226347 channel: Channel;
227348 voice: Voice;
@@ -229,6 +350,7 @@ function VoiceChannel({
229350 openShare: string | null;
230351 onOpenShare: (id: string) => void;
231352 username: string;
353+ onDelete: (c: Channel) => void;
232354 }) {
233355 const active = voice.channel?.id === channel.id;
234356 return (
@@ -277,6 +399,16 @@ function VoiceChannel({
277399 join
278400 </Button>
279401 )}
402+ {!channel.permanent && occupants.length === 0 && (
403+ <Button
404+ size="icon-xs"
405+ variant="ghost"
406+ onClick={() => onDelete(channel)}
407+ title="delete channel"
408+ >
409+ <Trash2 />
410+ </Button>
411+ )}
280412 </div>
281413 {active && (
282414 <div className="flex flex-col gap-0.5 px-2 pb-0.5">