Skip to content
224 linesCodeBlameRaw
1import { BellOff, ChevronDown, Compass, Hash, Lock, Search } from "lucide-react";
2import { type ReactNode, useEffect, useState } from "react";
3import { NavLink, useLocation, useNavigate } from "react-router";
4
5import type { ChatSidebarEntry } from "@g1t/contracts";
6
7import { CreateChannelButton, NewMessageButton, useChatData, useChatSidebar } from "./actions";
8import { MemberAvatar } from "./marks";
9import { Skeleton } from "../ui/skeleton";
10import { type ChatFilter, channelPath, filterEntries, sections } from "../../lib/chat";
11
12/** How often the sidebar asks again while the tab is shown. */
13const REFRESH_MS = 30_000;
14
15/**
16 * Chat mode's sidebar: the conversations, starred first, then channels and
17 * direct messages, with what is unread in each. It refreshes itself while
18 * the tab is shown, so unread counts move without a reload.
19 */
20export function ChatSidebar({ slug }: { slug: string }) {
21 const data = useChatData();
22 const { sidebar, refresh } = useChatSidebar();
23 const [filter, setFilter] = useState<ChatFilter>("all");
24 const [query, setQuery] = useState("");
25 const navigate = useNavigate();
26 const { pathname } = useLocation();
27
28 useEffect(() => {
29 if (!data) return;
30 const tick = () => {
31 if (document.visibilityState === "visible") refresh();
32 };
33 const timer = setInterval(tick, REFRESH_MS);
34 document.addEventListener("visibilitychange", tick);
35 return () => {
36 clearInterval(timer);
37 document.removeEventListener("visibilitychange", tick);
38 };
39 }, [data, refresh]);
40 const entries = sidebar?.entries ?? [];
41 const shown = filterEntries(entries, filter, query);
42 const { starred, channels, dms } = sections(shown);
43 const loading = !data;
44
45 return (
46 <div className="flex h-full flex-col">
47 <div className="flex h-14 shrink-0 items-center justify-between border-b border-line pr-2.5 pl-4">
48 <h2 className="text-[0.9375rem] font-semibold">Chat</h2>
49 <NewMessageButton slug={slug} />
50 </div>
51 <div className="space-y-2.5 px-2.5 pt-3">
52 <label className="flex h-8 items-center gap-2 rounded-md bg-surface px-2.5 text-[0.8125rem] ring-1 ring-line transition-shadow focus-within:ring-accent-dim/70">
53 <Search size={14} className="shrink-0 text-faint" />
54 <input
55 value={query}
56 onChange={(event) => setQuery(event.target.value)}
57 onKeyDown={(event) => {
58 if (event.key === "Enter") {
59 const first = [...starred, ...channels, ...dms][0];
60 if (first) {
61 navigate(channelPath(slug, first.channel));
62 setQuery("");
63 }
64 } else if (event.key === "Escape") setQuery("");
65 }}
66 placeholder="Jump to channel or person"
67 aria-label="Jump to channel or person"
68 autoComplete="off"
69 data-1p-ignore
70 className="min-w-0 grow bg-transparent text-fg outline-none placeholder:text-faint"
71 />
72 </label>
73 <div role="radiogroup" aria-label="Show" className="grid grid-cols-3 rounded-md bg-surface p-0.5 ring-1 ring-line">
74 {(["all", "unread", "mentions"] as const).map((key) => (
75 <button
76 key={key}
77 type="button"
78 role="radio"
79 aria-checked={filter === key}
80 onClick={() => setFilter(key)}
81 className={`h-6 rounded-[5px] text-xs font-medium capitalize transition-colors ${
82 filter === key ? "bg-raised text-fg shadow-sm ring-1 ring-line-strong" : "text-muted hover:text-fg"
83 }`}
84 >
85 {key}
86 </button>
87 ))}
88 </div>
89 </div>
90 <nav aria-label="Conversations" className="min-h-0 grow overflow-y-auto px-2.5 pt-3 pb-4 [scrollbar-width:thin]">
91 {loading ? (
92 <SidebarSkeleton />
93 ) : !sidebar ? (
94 <p className="px-2 py-3 text-xs leading-relaxed text-faint">Chat didn't answer. Your conversations will show here once it does.</p>
95 ) : (
96 <>
97 {starred.length > 0 && (
98 <Section title="Starred">
99 {starred.map((entry) => (
100 <Row key={entry.channel.id} entry={entry} slug={slug} pathname={pathname} />
101 ))}
102 </Section>
103 )}
104 <Section title="Channels" action={<CreateChannelButton slug={slug} />}>
105 {channels.map((entry) => (
106 <Row key={entry.channel.id} entry={entry} slug={slug} pathname={pathname} />
107 ))}
108 {channels.length === 0 && filter === "all" && !query && <Empty>No channels yet.</Empty>}
109 </Section>
110 <Section title="Direct messages" action={<NewMessageButton slug={slug} />}>
111 {dms.map((entry) => (
112 <Row key={entry.channel.id} entry={entry} slug={slug} pathname={pathname} />
113 ))}
114 {dms.length === 0 && filter === "all" && !query && <Empty>Message a teammate or an agent.</Empty>}
115 </Section>
116 {shown.length === 0 && (filter !== "all" || query) && (
117 <p className="px-2 py-2 text-xs text-faint">{query ? "No conversation matches." : filter === "unread" ? "You're all caught up." : "No mentions."}</p>
118 )}
119 <NavLink
120 to={`/${slug}/-/chat/browse`}
121 className={({ isActive }) =>
122 `mt-3 flex h-8 items-center gap-2.5 rounded-md px-2 text-[0.8125rem] transition-colors ${isActive ? "bg-raised text-fg" : "text-muted hover:bg-raised/60 hover:text-fg"}`
123 }
124 >
125 <Compass size={15} className="text-faint" />
126 Browse all channels
127 <span className="ml-auto text-xs text-faint tabular-nums">{(sidebar.browsable ?? 0) + entries.filter((e) => e.channel.kind === "channel").length}</span>
128 </NavLink>
129 </>
130 )}
131 </nav>
132 </div>
133 );
134}
135
136function Section({ title, action, children }: { title: string; action?: ReactNode; children: ReactNode }) {
137 const [open, setOpen] = useState(true);
138 return (
139 <section className="mb-3">
140 <div className="group/head flex h-7 items-center justify-between pr-0.5 pl-1">
141 <button
142 type="button"
143 onClick={() => setOpen(!open)}
144 aria-expanded={open}
145 className="flex items-center gap-1 rounded px-1 text-xs font-medium text-faint transition-colors hover:text-muted"
146 >
147 <ChevronDown size={12} className={`transition-transform ${open ? "" : "-rotate-90"}`} />
148 {title}
149 </button>
150 <span className="opacity-70 transition-opacity group-hover/head:opacity-100">{action}</span>
151 </div>
152 {open && <ul className="space-y-px">{children}</ul>}
153 </section>
154 );
155}
156
157function Empty({ children }: { children: ReactNode }) {
158 return <li className="px-2 py-1 text-xs text-faint">{children}</li>;
159}
160
161/** One conversation: its mark, its name, and its unread count. */
162function Row({ entry, slug, pathname }: { entry: ChatSidebarEntry; slug: string; pathname: string }) {
163 const to = channelPath(slug, entry.channel);
164 const current = pathname === to || pathname.startsWith(`${to}/`);
165 const unread = !entry.muted && (entry.unread > 0 || entry.mentions > 0);
166 const count = entry.mentions > 0 ? entry.mentions : entry.unread;
167 const other = entry.others[0];
168 const icon =
169 entry.channel.kind === "dm" && other ? (
170 <MemberAvatar member={other} size={18} />
171 ) : entry.channel.private ? (
172 <Lock size={14} />
173 ) : (
174 <Hash size={15} />
175 );
176 return (
177 <li>
178 <NavLink
179 to={to}
180 prefetch="intent"
181 aria-current={current ? "page" : undefined}
182 className={`group flex h-8 items-center gap-2 rounded-md px-2 text-[0.8125rem] transition-colors ${
183 current ? "bg-raised text-fg" : unread ? "text-fg hover:bg-raised/60" : entry.muted ? "text-faint hover:bg-raised/60 hover:text-muted" : "text-muted hover:bg-raised/60 hover:text-fg"
184 }`}
185 >
186 <span className={`flex w-[18px] shrink-0 justify-center ${current || unread ? "text-muted" : "text-faint"}`}>{icon}</span>
187 <span className={`min-w-0 grow truncate ${unread ? "font-semibold" : current ? "font-medium" : ""}`}>
188 {entry.title}
189 </span>
190 {entry.muted && <BellOff size={12} className="shrink-0 text-faint" aria-label="Muted" />}
191 {unread && count > 0 && (
192 <span
193 className={`min-w-5 shrink-0 rounded-full px-1.5 text-center text-[0.6875rem] leading-[1.125rem] font-semibold tabular-nums ${
194 entry.mentions > 0 ? "bg-accent text-bg" : "bg-line-strong text-fg"
195 }`}
196 >
197 {count}
198 </span>
199 )}
200 </NavLink>
201 </li>
202 );
203}
204
205function SidebarSkeleton() {
206 return (
207 <div className="space-y-2 px-2 pt-1" aria-busy="true">
208 <Skeleton className="h-3 w-16" />
209 {[70, 52, 84, 60].map((w) => (
210 <div key={w} className="flex h-6 items-center gap-2">
211 <Skeleton className="size-4 rounded" />
212 <Skeleton className="h-3" style={{ width: `${w}%` }} />
213 </div>
214 ))}
215 <Skeleton className="mt-4 h-3 w-24" />
216 {[64, 48, 72].map((w) => (
217 <div key={w} className="flex h-6 items-center gap-2">
218 <Skeleton className="size-4 rounded-full" />
219 <Skeleton className="h-3" style={{ width: `${w}%` }} />
220 </div>
221 ))}
222 </div>
223 );
224}