| 1 | import { ArrowLeft, BellOff, Check, ChevronDown, Compass, Globe, Hash, Lock, Pin, PinOff, Search, X } from "lucide-react"; |
| 2 | import { ContextMenu } from "radix-ui"; |
| 3 | import { type ReactNode, useEffect, useState } from "react"; |
| 4 | import { NavLink, useFetcher, useLocation, useNavigate } from "react-router"; |
| 5 | |
| 6 | import type { Channel, ChannelInvitation, ChatSidebarEntry, GuestChannel } from "@g1t/contracts"; |
| 7 | |
| 8 | import { conversationCache } from "./conversation-cache"; |
| 9 | import { CreateChannelButton, NewMessageButton, useChatData, useChatSend, useChatSidebar } from "./actions"; |
| 10 | import { DndMenu } from "./dnd-menu"; |
| 11 | import { MemberAvatar } from "./marks"; |
| 12 | import { PersonStatusEmoji } from "../presence"; |
| 13 | import { Avatar } from "../ui/avatar"; |
| 14 | import { Badge } from "../ui/badge"; |
| 15 | import { Button } from "../ui/button"; |
| 16 | import { Hint } from "../ui/hint"; |
| 17 | import { Skeleton } from "../ui/skeleton"; |
| 18 | import { type ChatFilter, agentsOnlyDm, channelPath, filterEntries, g1tEntry, sections } from "../../lib/chat"; |
| 19 | import { feedConnected } from "../../lib/notify-client"; |
| 20 | import { FALLBACK_REFRESH_MS } from "../../lib/notify-store"; |
| 21 | import { setMutedConversations } from "../../lib/sound-events"; |
| 22 | |
| 23 | /** How often the sidebar asks again while the tab is shown and the feed socket is down. */ |
| 24 | const REFRESH_MS = FALLBACK_REFRESH_MS; |
| 25 | |
| 26 | const ROW = |
| 27 | "group/row flex h-9 items-center gap-2 rounded-md pr-1.5 pl-2 text-[0.8125rem] transition-colors max-md:h-11 max-md:text-[0.9375rem] max-md:active:bg-raised"; |
| 28 | |
| 29 | /** Unread on a row: lavender for a mention, quiet for the rest. */ |
| 30 | function Count({ entry }: { entry: ChatSidebarEntry | undefined }) { |
| 31 | if (!entry || entry.muted) return null; |
| 32 | const count = entry.mentions > 0 ? entry.mentions : entry.unread; |
| 33 | if (count <= 0) return null; |
| 34 | return ( |
| 35 | <span |
| 36 | className={`min-w-5 shrink-0 rounded-full group-hover/row:invisible px-1.5 text-center text-[0.6875rem] leading-4.5 font-semibold tabular-nums ${ |
| 37 | entry.mentions > 0 ? "bg-accent text-bg" : "bg-line-strong text-fg" |
| 38 | }`} |
| 39 | > |
| 40 | {count} |
| 41 | </span> |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Chat mode's sidebar: what you pinned, the workspace's own channels (and a |
| 47 | * way to browse the rest), its public channels, then direct messages, the |
| 48 | * latest first. Sections fold, and |
| 49 | * stay folded on this device. All, Unread and Mentions filter every section. It refreshes |
| 50 | * itself while the tab is shown, so unread counts move without a reload. |
| 51 | */ |
| 52 | export function ChatSidebar({ slug: home, heading = true }: { slug: string; heading?: boolean }) { |
| 53 | const data = useChatData(); |
| 54 | // The workspace whose chat is open: another one's, for a guest there. |
| 55 | const slug = data?.slug ?? home; |
| 56 | const { sidebar, refresh } = useChatSidebar(); |
| 57 | const send = useChatSend(slug); |
| 58 | const [filter, setFilter] = useState<ChatFilter>("all"); |
| 59 | const [query, setQuery] = useState(""); |
| 60 | const navigate = useNavigate(); |
| 61 | const { pathname } = useLocation(); |
| 62 | |
| 63 | useEffect(() => { |
| 64 | if (!data) return; |
| 65 | // Notify: counts arrive live over the feed socket; this asks again only while it is down. |
| 66 | const tick = () => { |
| 67 | if (document.visibilityState === "visible" && !feedConnected()) refresh(); |
| 68 | }; |
| 69 | const timer = setInterval(tick, REFRESH_MS); |
| 70 | document.addEventListener("visibilitychange", tick); |
| 71 | return () => { |
| 72 | clearInterval(timer); |
| 73 | document.removeEventListener("visibilitychange", tick); |
| 74 | }; |
| 75 | }, [data, refresh]); |
| 76 | |
| 77 | // Pins show at once and go back if the service refuses. |
| 78 | const [pins, setPins] = useState<Map<string, boolean>>(new Map()); |
| 79 | useEffect(() => setPins(new Map()), [sidebar]); |
| 80 | // Sounds leave muted conversations alone (lib/sound-events.ts). |
| 81 | useEffect(() => { |
| 82 | if (sidebar) setMutedConversations(sidebar.entries.filter((e) => e.muted).map((e) => e.channel.id)); |
| 83 | }, [sidebar]); |
| 84 | const entries = (sidebar?.entries ?? []).map((e) => (pins.has(e.channel.id) ? { ...e, starred: pins.get(e.channel.id)! } : e)); |
| 85 | // The open conversation stays listed even when it is an old one with an agent alone, which Jump to otherwise finds. |
| 86 | const currentId = entries.find((e) => pathname === channelPath(slug, e.channel) || pathname.startsWith(`${channelPath(slug, e.channel)}/`))?.channel.id ?? null; |
| 87 | const shown = filterEntries(entries, filter, query, currentId); |
| 88 | const { pinned, channels, publicChannels, dms } = sections(shown); |
| 89 | const loading = !data; |
| 90 | |
| 91 | // Folded sections, remembered per workspace on this device. A folded |
| 92 | // section still shows what is unread and the conversation open now. |
| 93 | const [folded, setFolded] = useState<ReadonlySet<string>>(new Set()); |
| 94 | useEffect(() => setFolded(readFolded(slug)), [slug]); |
| 95 | const fold = (id: string) => |
| 96 | setFolded((now) => { |
| 97 | const next = new Set(now); |
| 98 | if (!next.delete(id)) next.add(id); |
| 99 | writeFolded(slug, next); |
| 100 | return next; |
| 101 | }); |
| 102 | const keepShown = (entry: ChatSidebarEntry) => { |
| 103 | const to = channelPath(slug, entry.channel); |
| 104 | return pathname === to || pathname.startsWith(`${to}/`) || (!entry.muted && (entry.unread > 0 || entry.mentions > 0)); |
| 105 | }; |
| 106 | |
| 107 | const togglePin = async (entry: ChatSidebarEntry) => { |
| 108 | const next = !entry.starred; |
| 109 | setPins((now) => new Map(now).set(entry.channel.id, next)); |
| 110 | const done = await send({ intent: "preferences", channel_id: entry.channel.id, starred: next }); |
| 111 | if (!done.ok) |
| 112 | setPins((now) => { |
| 113 | const back = new Map(now); |
| 114 | back.delete(entry.channel.id); |
| 115 | return back; |
| 116 | }); |
| 117 | }; |
| 118 | |
| 119 | const conversation = (entry: ChatSidebarEntry) => ( |
| 120 | <ConversationRow key={entry.channel.id} entry={entry} slug={slug} pathname={pathname} onPin={() => void togglePin(entry)} /> |
| 121 | ); |
| 122 | |
| 123 | return ( |
| 124 | // The colour behind it, for the dots cut out of people's avatars. |
| 125 | <div className="flex h-full flex-col [--chat-sidebar-bg:var(--color-sidebar)]"> |
| 126 | {heading && ( |
| 127 | <div className="flex h-9 shrink-0 items-center justify-between pr-1 pl-3"> |
| 128 | <h2 className="text-[0.6875rem] font-medium tracking-wide text-faint uppercase">Chat</h2> |
| 129 | <div className="flex items-center gap-0.5"> |
| 130 | <DndMenu /> |
| 131 | <NewMessageButton slug={slug} /> |
| 132 | </div> |
| 133 | </div> |
| 134 | )} |
| 135 | {sidebar?.guest && sidebar.workspace && <GuestBanner workspace={sidebar.workspace} home={home} />} |
| 136 | <div className="space-y-2.5 px-2.5 pt-3"> |
| 137 | <div className="flex items-center gap-2"> |
| 138 | <label className="flex h-8 min-w-0 grow 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"> |
| 139 | <Search size={14} className="shrink-0 text-faint" /> |
| 140 | <input |
| 141 | value={query} |
| 142 | onChange={(event) => setQuery(event.target.value)} |
| 143 | onKeyDown={(event) => { |
| 144 | if (event.key === "Enter") { |
| 145 | const first = [...pinned, ...channels, ...publicChannels, ...dms][0]; |
| 146 | if (first) { |
| 147 | navigate(channelPath(slug, first.channel)); |
| 148 | setQuery(""); |
| 149 | } |
| 150 | } else if (event.key === "Escape") setQuery(""); |
| 151 | }} |
| 152 | placeholder="Jump to channel or person" |
| 153 | aria-label="Jump to channel or person" |
| 154 | autoComplete="off" |
| 155 | data-1p-ignore |
| 156 | className="min-w-0 grow bg-transparent text-fg outline-none placeholder:text-faint" |
| 157 | /> |
| 158 | </label> |
| 159 | {/* A phone has no heading row: Do not disturb sits by the search box instead. */} |
| 160 | {!heading && <DndMenu className="size-8 max-md:size-11" />} |
| 161 | </div> |
| 162 | <div role="radiogroup" aria-label="Show" className="grid grid-cols-3 rounded-md bg-surface p-0.5 ring-1 ring-line"> |
| 163 | {(["all", "unread", "mentions"] as const).map((key) => ( |
| 164 | <button |
| 165 | key={key} |
| 166 | type="button" |
| 167 | role="radio" |
| 168 | aria-checked={filter === key} |
| 169 | onClick={() => setFilter(key)} |
| 170 | className={`h-6 rounded-[5px] text-xs font-medium capitalize transition-colors ${ |
| 171 | filter === key ? "bg-raised text-fg shadow-sm ring-1 ring-line-strong" : "text-muted hover:text-fg" |
| 172 | }`} |
| 173 | > |
| 174 | {key} |
| 175 | </button> |
| 176 | ))} |
| 177 | </div> |
| 178 | </div> |
| 179 | {/* A phone's list (no heading): room at the end so the compose button never covers the last row. */} |
| 180 | <nav |
| 181 | aria-label="Conversations" |
| 182 | className={`min-h-0 grow overflow-y-auto px-2.5 pt-3 scrollbar-thin ${heading ? "pb-4" : "pb-[calc(5.5rem+env(safe-area-inset-bottom))]"}`} |
| 183 | > |
| 184 | {loading ? ( |
| 185 | <SidebarSkeleton /> |
| 186 | ) : !sidebar ? ( |
| 187 | <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> |
| 188 | ) : ( |
| 189 | <> |
| 190 | {pinned.length > 0 && ( |
| 191 | <Section id="pinned" title="Pinned" folded={folded} onFold={fold} keep={pinned.filter(keepShown).map(conversation)}> |
| 192 | {pinned.map(conversation)} |
| 193 | </Section> |
| 194 | )} |
| 195 | {/* A guest has only the channels they were asked into here; with none, the public ones are all there is. */} |
| 196 | {(!sidebar.guest || channels.length > 0) && ( |
| 197 | <Section |
| 198 | id="channels" |
| 199 | title="Channels" |
| 200 | folded={folded} |
| 201 | onFold={fold} |
| 202 | action={sidebar.guest ? undefined : <CreateChannelButton slug={slug} />} |
| 203 | keep={channels.filter(keepShown).map(conversation)} |
| 204 | > |
| 205 | {channels.map(conversation)} |
| 206 | {channels.length === 0 && filter === "all" && !query && <Empty>No channels yet.</Empty>} |
| 207 | {!sidebar.guest && filter === "all" && !query && <BrowseRow slug={slug} count={sidebar.browsable} />} |
| 208 | </Section> |
| 209 | )} |
| 210 | {/* Open to anyone on g1t, apart from the workspace's own: only when there are some, or for a guest, to find them. */} |
| 211 | {(publicChannels.length > 0 || (sidebar.guest && filter === "all" && !query)) && ( |
| 212 | <Section id="public" title="Public channels" folded={folded} onFold={fold} keep={publicChannels.filter(keepShown).map(conversation)}> |
| 213 | {publicChannels.map(conversation)} |
| 214 | {sidebar.guest && filter === "all" && !query && ( |
| 215 | <> |
| 216 | {publicChannels.length === 0 && <Empty>You're in none of its public channels yet.</Empty>} |
| 217 | <BrowseRow slug={slug} count={sidebar.browsable} guest /> |
| 218 | </> |
| 219 | )} |
| 220 | </Section> |
| 221 | )} |
| 222 | {(sidebar.invitations?.length ?? 0) > 0 && filter === "all" && !query && ( |
| 223 | <Section id="invitations" title="Invitations" folded={folded} onFold={fold}> |
| 224 | {sidebar.invitations!.map((invitation) => ( |
| 225 | <InvitationRow key={invitation.id} invitation={invitation} onAnswered={refresh} /> |
| 226 | ))} |
| 227 | </Section> |
| 228 | )} |
| 229 | {(sidebar.elsewhere?.length ?? 0) > 0 && ( |
| 230 | <Section id="elsewhere" title="Other workspaces" folded={folded} onFold={fold}> |
| 231 | {sidebar.elsewhere! |
| 232 | .filter((entry) => (filter === "all" || (filter === "unread" ? entry.unread > 0 : entry.mentions > 0)) && (!query || entry.channel.name?.includes(query.toLowerCase()))) |
| 233 | .map((entry) => ( |
| 234 | <ElsewhereRow key={entry.channel.id} entry={entry} pathname={pathname} /> |
| 235 | ))} |
| 236 | </Section> |
| 237 | )} |
| 238 | {/* A guest has no direct messages here: those are for the workspace's members. */} |
| 239 | {!sidebar.guest && (<Section |
| 240 | id="people" |
| 241 | title="Direct messages" |
| 242 | folded={folded} |
| 243 | onFold={fold} |
| 244 | action={<NewMessageButton slug={slug} />} |
| 245 | keep={dms.filter(keepShown).map(conversation)} |
| 246 | > |
| 247 | {/* @g1t, before the first word to it: its direct message opens on first use. */} |
| 248 | {filter === "all" && !query && !entries.some(g1tEntry) && ( |
| 249 | <ExtraRow to={`/${slug}/-/chat/dm/g1t`} icon={<MemberAvatar member={{ kind: "system", id: "g1t", name: "g1t", avatar: null }} size={18} ring="var(--chat-sidebar-bg)" />}> |
| 250 | g1t |
| 251 | </ExtraRow> |
| 252 | )} |
| 253 | {dms.map(conversation)} |
| 254 | {dms.length === 0 && filter === "all" && !query && <Empty>Message a teammate.</Empty>} |
| 255 | </Section>)} |
| 256 | {shown.length === 0 && (filter !== "all" || query) && ( |
| 257 | <p className="px-2 py-2 text-xs text-faint">{query ? "Nothing matches." : filter === "unread" ? "You're all caught up." : "No mentions."}</p> |
| 258 | )} |
| 259 | </> |
| 260 | )} |
| 261 | </nav> |
| 262 | </div> |
| 263 | ); |
| 264 | } |
| 265 | |
| 266 | const FOLDED_KEY = (slug: string) => `g1t:chat-folded:${slug}`; |
| 267 | |
| 268 | /** The sections folded in this workspace, as last left; none if storage is not there. */ |
| 269 | function readFolded(slug: string): ReadonlySet<string> { |
| 270 | try { |
| 271 | const saved = JSON.parse(localStorage.getItem(FOLDED_KEY(slug)) ?? "[]"); |
| 272 | return new Set(Array.isArray(saved) ? saved.filter((id): id is string => typeof id === "string") : []); |
| 273 | } catch { |
| 274 | return new Set(); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | function writeFolded(slug: string, folded: ReadonlySet<string>) { |
| 279 | try { |
| 280 | if (folded.size === 0) localStorage.removeItem(FOLDED_KEY(slug)); |
| 281 | else localStorage.setItem(FOLDED_KEY(slug), JSON.stringify([...folded])); |
| 282 | } catch { |
| 283 | // Private windows and blocked storage: folding still works until a reload. |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * A titled part of the sidebar that folds away. Folded, it keeps `keep` |
| 289 | * showing: the unread conversations and the open one. |
| 290 | */ |
| 291 | function Section({ |
| 292 | id, |
| 293 | title, |
| 294 | folded, |
| 295 | onFold, |
| 296 | action, |
| 297 | keep, |
| 298 | children, |
| 299 | }: { |
| 300 | id: string; |
| 301 | title: string; |
| 302 | folded: ReadonlySet<string>; |
| 303 | onFold: (id: string) => void; |
| 304 | action?: ReactNode; |
| 305 | keep?: ReactNode[]; |
| 306 | children: ReactNode; |
| 307 | }) { |
| 308 | const open = !folded.has(id); |
| 309 | const list = `chat-section-${id}`; |
| 310 | return ( |
| 311 | <section className="mb-3"> |
| 312 | <div className="group/head flex h-7 items-center justify-between pr-0.5 pl-1"> |
| 313 | <button |
| 314 | type="button" |
| 315 | onClick={() => onFold(id)} |
| 316 | aria-expanded={open} |
| 317 | aria-controls={list} |
| 318 | className="flex min-w-0 grow items-center gap-1 rounded px-1 py-1 text-left text-[0.6875rem] font-medium tracking-wide text-faint uppercase transition-colors hover:text-muted focus-visible:ring-2 focus-visible:ring-accent focus-visible:outline-none max-md:text-[0.8125rem]" |
| 319 | > |
| 320 | <ChevronDown size={12} className={`shrink-0 transition-transform ${open ? "" : "-rotate-90"}`} /> |
| 321 | {title} |
| 322 | </button> |
| 323 | <span className="opacity-70 transition-opacity group-hover/head:opacity-100 focus-within:opacity-100 [@media(hover:none)]:opacity-100">{action}</span> |
| 324 | </div> |
| 325 | {(open || (keep && keep.length > 0)) && ( |
| 326 | <ul id={list} className="space-y-px"> |
| 327 | {open ? children : keep} |
| 328 | </ul> |
| 329 | )} |
| 330 | </section> |
| 331 | ); |
| 332 | } |
| 333 | |
| 334 | /** A quieter row at the end of a section: somewhere to go rather than a conversation. */ |
| 335 | function ExtraRow({ to, icon, count, countLabel, children }: { to: string; icon: ReactNode; count?: number; countLabel?: string; children: ReactNode }) { |
| 336 | return ( |
| 337 | <li> |
| 338 | <NavLink |
| 339 | to={to} |
| 340 | end |
| 341 | className={({ isActive }) => `${ROW} ${isActive ? "bg-sidebar-accent text-fg" : "text-faint hover:bg-sidebar-accent/60 hover:text-muted"}`} |
| 342 | > |
| 343 | <span className="flex w-4.5 shrink-0 justify-center">{icon}</span> |
| 344 | <span className="min-w-0 grow truncate">{children}</span> |
| 345 | {count != null && ( |
| 346 | <span className="shrink-0 text-xs tabular-nums"> |
| 347 | {count} |
| 348 | {countLabel && <span className="sr-only"> {countLabel}</span>} |
| 349 | </span> |
| 350 | )} |
| 351 | </NavLink> |
| 352 | </li> |
| 353 | ); |
| 354 | } |
| 355 | |
| 356 | /** Browse channels, with how many more there are to join; a guest browses the public ones. */ |
| 357 | function BrowseRow({ slug, count, guest = false }: { slug: string; count: number; guest?: boolean }) { |
| 358 | return ( |
| 359 | <ExtraRow to={`/${slug}/-/chat/browse`} icon={<Compass size={15} />} count={count > 0 ? count : undefined} countLabel="to join"> |
| 360 | {guest ? "Browse public channels" : "Browse channels"} |
| 361 | </ExtraRow> |
| 362 | ); |
| 363 | } |
| 364 | |
| 365 | function Empty({ children }: { children: ReactNode }) { |
| 366 | return <li className="px-2 py-1 text-xs text-faint">{children}</li>; |
| 367 | } |
| 368 | |
| 369 | /** Pin or unpin, on hover or focus; the same as the context menu's first item. */ |
| 370 | function PinButton({ pinned, onPin, label }: { pinned: boolean; onPin: () => void; label: string }) { |
| 371 | return ( |
| 372 | <Hint label={pinned ? "Unpin" : "Pin to the top"}> |
| 373 | <Button |
| 374 | type="button" |
| 375 | aria-label={`${pinned ? "Unpin" : "Pin"} ${label}`} |
| 376 | onClick={(event) => { |
| 377 | event.preventDefault(); |
| 378 | event.stopPropagation(); |
| 379 | onPin(); |
| 380 | }} |
| 381 | variant="secondary" size="icon" |
| 382 | className="absolute top-1/2 right-1 hidden size-6 -translate-y-1/2 rounded text-faint group-hover/row:flex group-focus-within/row:flex hover:text-fg [@media(hover:none)]:hidden" |
| 383 | > |
| 384 | {pinned ? <PinOff size={13} /> : <Pin size={13} />} |
| 385 | </Button> |
| 386 | </Hint> |
| 387 | ); |
| 388 | } |
| 389 | |
| 390 | /** Right-click on a row: pin it, or open it. */ |
| 391 | function RowMenu({ pinned, onPin, to, children }: { pinned: boolean; onPin: () => void; to?: string; children: ReactNode }) { |
| 392 | const navigate = useNavigate(); |
| 393 | const item = |
| 394 | "flex cursor-default items-center gap-2 rounded-md px-2 py-1.5 text-[0.8125rem] text-fg/90 outline-none select-none data-highlighted:bg-line data-highlighted:text-fg [&_svg]:size-4 [&_svg]:text-muted"; |
| 395 | return ( |
| 396 | <ContextMenu.Root> |
| 397 | <ContextMenu.Trigger asChild>{children}</ContextMenu.Trigger> |
| 398 | <ContextMenu.Portal> |
| 399 | <ContextMenu.Content className="z-50 min-w-44 rounded-lg border border-line-strong bg-raised p-1 shadow-xl shadow-black/40 data-[state=open]:animate-pop-in"> |
| 400 | <ContextMenu.Item className={item} onSelect={onPin}> |
| 401 | {pinned ? <PinOff /> : <Pin />} |
| 402 | {pinned ? "Unpin" : "Pin to the top"} |
| 403 | </ContextMenu.Item> |
| 404 | {to && ( |
| 405 | <ContextMenu.Item className={item} onSelect={() => navigate(to)}> |
| 406 | <Hash /> |
| 407 | Open |
| 408 | </ContextMenu.Item> |
| 409 | )} |
| 410 | </ContextMenu.Content> |
| 411 | </ContextMenu.Portal> |
| 412 | </ContextMenu.Root> |
| 413 | ); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Reads a conversation ahead, on hover, focus or the start of a touch, so |
| 418 | * opening it draws at once (its route's `clientLoader` keeps it). |
| 419 | */ |
| 420 | function usePrefetchConversation(to: string) { |
| 421 | const fetcher = useFetcher(); |
| 422 | const load = () => { |
| 423 | if (fetcher.state === "idle" && fetcher.data === undefined && !conversationCache.has(to)) fetcher.load(to); |
| 424 | }; |
| 425 | return { onPointerEnter: load, onFocus: load, onTouchStart: load }; |
| 426 | } |
| 427 | |
| 428 | /** One channel or direct message: its mark, its name, its unread count, and a pin. */ |
| 429 | function ConversationRow({ |
| 430 | entry, |
| 431 | slug, |
| 432 | pathname, |
| 433 | onPin, |
| 434 | }: { |
| 435 | entry: ChatSidebarEntry; |
| 436 | slug: string; |
| 437 | pathname: string; |
| 438 | onPin: () => void; |
| 439 | }) { |
| 440 | const to = channelPath(slug, entry.channel); |
| 441 | const current = pathname === to || pathname.startsWith(`${to}/`); |
| 442 | const prefetch = usePrefetchConversation(to); |
| 443 | const unread = !entry.muted && (entry.unread > 0 || entry.mentions > 0); |
| 444 | // People first: an old agent (agents no longer exist) shows only when nobody else is there. |
| 445 | const other = entry.others.find((m) => m.kind === "user") ?? entry.others[0]; |
| 446 | // An old conversation with an agent alone (lib/chat.ts): still here to read, dimmed. |
| 447 | const former = agentsOnlyDm(entry); |
| 448 | const icon = |
| 449 | entry.channel.kind === "dm" && other ? ( |
| 450 | // One person: their dot, cut out of the sidebar behind it. |
| 451 | <MemberAvatar member={other} size={18} presence={entry.others.length === 1} ring="var(--chat-sidebar-bg)" /> |
| 452 | ) : ( |
| 453 | <ChannelMark channel={entry.channel} /> |
| 454 | ); |
| 455 | return ( |
| 456 | <li className="group/row relative"> |
| 457 | <RowMenu pinned={entry.starred} onPin={onPin} to={to}> |
| 458 | <NavLink |
| 459 | to={to} |
| 460 | {...prefetch} |
| 461 | aria-current={current ? "page" : undefined} |
| 462 | className={`${ROW} ${ |
| 463 | current ? "bg-sidebar-accent text-fg" : unread ? "text-fg hover:bg-sidebar-accent/60" : entry.muted || former ? "text-faint hover:bg-sidebar-accent/60 hover:text-muted" : "text-muted hover:bg-sidebar-accent/60 hover:text-fg" |
| 464 | }`} |
| 465 | > |
| 466 | <span className={`flex w-4.5 shrink-0 justify-center ${current || unread ? "text-muted" : "text-faint"} ${former ? "opacity-60" : ""}`}>{icon}</span> |
| 467 | <span className={`min-w-0 grow truncate ${unread ? "font-semibold" : current ? "font-medium" : ""}`}> |
| 468 | {entry.title} |
| 469 | {entry.channel.kind === "dm" && other?.kind === "user" && entry.others.length === 1 && ( |
| 470 | <PersonStatusEmoji person={{ id: other.id, username: other.name }} size={13} className="ml-1.5 align-[-2px]" inert /> |
| 471 | )} |
| 472 | </span> |
| 473 | {entry.muted && <BellOff size={12} className="shrink-0 text-faint" aria-label="Muted" />} |
| 474 | <Count entry={entry} /> |
| 475 | </NavLink> |
| 476 | </RowMenu> |
| 477 | <PinButton pinned={entry.starred} onPin={onPin} label={entry.title} /> |
| 478 | </li> |
| 479 | ); |
| 480 | } |
| 481 | |
| 482 | /** A channel's mark: a lock when private, a globe when open to anyone on g1t, else a hash. */ |
| 483 | export function ChannelMark({ channel, size = 15 }: { channel: Pick<Channel, "private" | "visibility">; size?: number }) { |
| 484 | if (channel.private || channel.visibility === "private") return <Lock size={size - 1} aria-label="Private channel" />; |
| 485 | if (channel.visibility === "public") return <Globe size={size - 1} aria-label="Public channel, open to anyone on g1t" />; |
| 486 | return <Hash size={size} aria-label="Workspace channel" />; |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * At the top of a guest's sidebar: whose chat this is, that they are not |
| 491 | * in the workspace, and the way back to their own. |
| 492 | */ |
| 493 | function GuestBanner({ workspace, home }: { workspace: { slug: string; name: string; avatar: string | null }; home: string }) { |
| 494 | const own = home.toLowerCase() !== workspace.slug; |
| 495 | return ( |
| 496 | <div className="mx-2.5 mt-1 rounded-lg bg-surface p-2.5 ring-1 ring-line"> |
| 497 | <div className="flex items-center gap-2"> |
| 498 | <Avatar name={workspace.slug} image={workspace.avatar} size={20} square /> |
| 499 | <span className="min-w-0 grow truncate text-[0.8125rem] font-medium">{workspace.name}</span> |
| 500 | <Badge>Guest</Badge> |
| 501 | </div> |
| 502 | <p className="mt-1.5 text-xs leading-relaxed text-muted"> |
| 503 | You're not a member of this workspace. You see only the channels you're in and its public ones. |
| 504 | </p> |
| 505 | {own && ( |
| 506 | <NavLink to={`/${home}/-/chat`} className="mt-2 inline-flex items-center gap-1 text-xs text-accent hover:underline"> |
| 507 | <ArrowLeft size={12} /> |
| 508 | Back to your chat |
| 509 | </NavLink> |
| 510 | )} |
| 511 | </div> |
| 512 | ); |
| 513 | } |
| 514 | |
| 515 | /** A channel of another workspace the viewer is a guest in: its workspace's icon, its name, its count. */ |
| 516 | function ElsewhereRow({ entry, pathname }: { entry: GuestChannel; pathname: string }) { |
| 517 | const to = `/${entry.workspace.slug}/-/chat/${entry.channel.name}`; |
| 518 | const current = pathname === to; |
| 519 | const count = entry.mentions > 0 ? entry.mentions : entry.unread; |
| 520 | return ( |
| 521 | <li> |
| 522 | <NavLink |
| 523 | to={to} |
| 524 | aria-current={current ? "page" : undefined} |
| 525 | className={`${ROW} ${current ? "bg-sidebar-accent text-fg" : count > 0 ? "text-fg hover:bg-sidebar-accent/60" : "text-muted hover:bg-sidebar-accent/60 hover:text-fg"}`} |
| 526 | > |
| 527 | <span className="flex w-4.5 shrink-0 justify-center"> |
| 528 | <Avatar name={entry.workspace.slug} image={entry.workspace.avatar} size={16} square /> |
| 529 | </span> |
| 530 | <span className={`min-w-0 grow truncate ${count > 0 ? "font-semibold" : ""}`}> |
| 531 | #{entry.channel.name} |
| 532 | <span className="ml-1.5 text-xs font-normal text-faint">{entry.workspace.name}</span> |
| 533 | </span> |
| 534 | {count > 0 && ( |
| 535 | <span className={`min-w-5 shrink-0 rounded-full px-1.5 text-center text-[0.6875rem] leading-4.5 font-semibold tabular-nums ${entry.mentions > 0 ? "bg-accent text-bg" : "bg-line-strong text-fg"}`}> |
| 536 | {count} |
| 537 | </span> |
| 538 | )} |
| 539 | </NavLink> |
| 540 | </li> |
| 541 | ); |
| 542 | } |
| 543 | |
| 544 | /** An invitation into another workspace's channel: who asked, and Join or Decline. */ |
| 545 | function InvitationRow({ invitation, onAnswered }: { invitation: ChannelInvitation; onAnswered: () => void }) { |
| 546 | const navigate = useNavigate(); |
| 547 | const [busy, setBusy] = useState(false); |
| 548 | const [error, setError] = useState<string | null>(null); |
| 549 | const answer = async (accept: boolean) => { |
| 550 | setBusy(true); |
| 551 | setError(null); |
| 552 | try { |
| 553 | const response = await fetch(`/${invitation.workspace.slug}/-/chat/api`, { |
| 554 | method: "POST", |
| 555 | headers: { "content-type": "application/json" }, |
| 556 | body: JSON.stringify({ intent: "answer_invitation", id: invitation.id, accept }), |
| 557 | }); |
| 558 | const result = (await response.json()) as { ok: boolean; value?: { to: string | null }; error?: { message: string } }; |
| 559 | if (!result.ok) { |
| 560 | setError(result.error?.message ?? "That didn't work."); |
| 561 | return; |
| 562 | } |
| 563 | onAnswered(); |
| 564 | if (accept && result.value?.to) navigate(result.value.to); |
| 565 | } catch { |
| 566 | setError("Couldn't reach g1t. Try again."); |
| 567 | } finally { |
| 568 | setBusy(false); |
| 569 | } |
| 570 | }; |
| 571 | return ( |
| 572 | <li className="rounded-md px-2 py-1.5 text-[0.8125rem]"> |
| 573 | <div className="flex items-center gap-2"> |
| 574 | <Avatar name={invitation.workspace.slug} image={invitation.workspace.avatar} size={16} square /> |
| 575 | <span className="min-w-0 grow truncate"> |
| 576 | #{invitation.channel.name} <span className="text-xs text-faint">{invitation.workspace.name}</span> |
| 577 | </span> |
| 578 | </div> |
| 579 | <p className="mt-0.5 pl-6 text-xs text-faint">Asked in as a guest by @{invitation.invited_by.display_username ?? invitation.invited_by.name}</p> |
| 580 | <div className="mt-1.5 flex gap-1.5 pl-6"> |
| 581 | <Button type="button" size="xs" disabled={busy} onClick={() => void answer(true)}> |
| 582 | <Check size={12} /> |
| 583 | Join |
| 584 | </Button> |
| 585 | <Button type="button" size="xs" variant="ghost" disabled={busy} onClick={() => void answer(false)}> |
| 586 | <X size={12} /> |
| 587 | Decline |
| 588 | </Button> |
| 589 | </div> |
| 590 | {error && <p className="mt-1 pl-6 text-xs text-danger">{error}</p>} |
| 591 | </li> |
| 592 | ); |
| 593 | } |
| 594 | |
| 595 | function SidebarSkeleton() { |
| 596 | return ( |
| 597 | <div className="space-y-2 px-2 pt-1" aria-busy="true"> |
| 598 | <Skeleton className="h-3 w-16" /> |
| 599 | {[70, 52, 84, 60].map((w) => ( |
| 600 | <div key={w} className="flex h-6 items-center gap-2"> |
| 601 | <Skeleton className="size-4 rounded" /> |
| 602 | <Skeleton className="h-3" style={{ width: `${w}%` }} /> |
| 603 | </div> |
| 604 | ))} |
| 605 | <Skeleton className="mt-4 h-3 w-24" /> |
| 606 | {[64, 48, 72].map((w) => ( |
| 607 | <div key={w} className="flex h-6 items-center gap-2"> |
| 608 | <Skeleton className="size-4 rounded-full" /> |
| 609 | <Skeleton className="h-3" style={{ width: `${w}%` }} /> |
| 610 | </div> |
| 611 | ))} |
| 612 | </div> |
| 613 | ); |
| 614 | } |