Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | 1x 1x 1x 1x 1x 74x 42x 35x 7x 28x 1x 70x 658x 70x 74x 1x 15x 15x 8x 7x 28x 21x 210x 70x | import { useQuery } from '@tanstack/react-query'
import { Link } from '@tanstack/react-router'
import {
Bookmark,
BellOff,
Hash,
Lock,
MessageSquareText,
Plus,
Search,
Send,
SquarePen,
type LucideIcon,
} from 'lucide-react'
import type { ReactElement } from 'react'
import { Avatar, AvatarBadge, AvatarFallback } from '@/components/Avatar'
import { CountBadge } from '@/components/CountBadge'
import { IconButton } from '@/components/IconButton'
import { Skeleton } from '@/components/Skeleton'
import { useShellDrawer } from '@/features/shell'
import { presenceClass, presenceLabel } from '@/helpers/presence'
import { toneClass } from '@/helpers/tone'
import { initials } from '@chattra/utils/string'
import { fetchSidebar, sidebarKey } from '../../api'
import type { ConversationSummary, Person, SidebarSection } from '../../types'
const SECTIONS: { section: SidebarSection; label: string }[] = [
{ section: 'starred', label: 'Starred' },
{ section: 'channels', label: 'Channels' },
{ section: 'dms', label: 'Direct messages' },
]
// shortcut: these four open nothing yet; Threads is M3, Drafts & sent and Saved M4, Browse channels M2.
const QUICK_LINKS: { label: string; icon: LucideIcon; hasUnread: boolean }[] = [
{ label: 'Threads', icon: MessageSquareText, hasUnread: true },
{ label: 'Drafts & sent', icon: Send, hasUnread: false },
{ label: 'Saved', icon: Bookmark, hasUnread: false },
{ label: 'Browse channels', icon: Hash, hasUnread: false },
]
const ROW =
'flex h-8 shrink-0 items-center gap-2 rounded-lg px-2.5 text-sm text-sidebar-foreground outline-none hover:bg-hover focus-visible:ring-3 focus-visible:ring-ring'
// The icon turns dark with the text on the selected row, where the muted grey would fade out.
const ICON = 'size-4 text-muted-foreground group-aria-[current=page]/row:text-current'
const ConversationIcon = ({
conversation,
peer,
}: {
conversation: ConversationSummary
peer: Person | undefined
}): ReactElement => {
if (conversation.kind === 'channel') return <Hash aria-hidden="true" className={ICON} />
if (conversation.kind === 'private') return <Lock aria-hidden="true" className={ICON} />
if (conversation.kind === 'group') {
return (
<span
aria-hidden="true"
className="grid size-5.5 place-items-center rounded-md bg-muted text-2xs font-bold text-muted-foreground"
>
{conversation.memberCount - 1}
</span>
)
}
return (
<Avatar size="sm" aria-hidden="true">
<AvatarFallback className={toneClass(conversation.name)}>
{initials(conversation.name)}
</AvatarFallback>
{peer ? <AvatarBadge className={presenceClass(peer.presence)} /> : null}
</Avatar>
)
}
const ConversationLink = ({
conversation,
people,
}: {
conversation: ConversationSummary
people: Person[]
}): ReactElement => {
const drawer = useShellDrawer()
const peer = people.find((person) => person.id === conversation.peerId)
return (
<Link
to="/c/$conversationId"
params={{ conversationId: conversation.id }}
onClick={drawer.close}
className={`${ROW} group/row aria-[current=page]:bg-sidebar-primary aria-[current=page]:font-semibold aria-[current=page]:text-sidebar-primary-foreground`}
>
{({ isActive }) => (
<>
<ConversationIcon conversation={conversation} peer={peer} />
<span
className={`min-w-0 flex-1 truncate ${conversation.hasUnread ? 'font-bold' : ''} ${conversation.isMuted && !isActive ? 'text-muted-foreground' : ''}`}
>
{conversation.name}
</span>
{peer ? (
<span className="sr-only">{`, ${presenceLabel(peer.presence).toLowerCase()}`}</span>
) : null}
{conversation.isMuted ? (
<BellOff
aria-label="Muted"
className="size-3.5 text-muted-foreground group-aria-[current=page]/row:text-current"
/>
) : null}
{/* The open conversation is being read, so it carries no count. */}
{conversation.mentionCount > 0 && !isActive ? (
<CountBadge
aria-label={`${conversation.mentionCount} ${conversation.mentionCount === 1 ? 'mention' : 'mentions'} of you`}
>
{conversation.mentionCount}
</CountBadge>
) : null}
</>
)}
</Link>
)
}
// The channel list of the prototype: workspace name, search box, quick links, three sections.
export const ConversationSidebar = (): ReactElement => {
const sidebar = useQuery({ queryKey: sidebarKey, queryFn: fetchSidebar })
if (!sidebar.data) {
return (
<div role="status" aria-label="Loading…" className="flex flex-col gap-3 p-4">
<Skeleton className="h-6 w-28" />
<Skeleton className="h-8 w-full" />
<Skeleton className="h-4 w-36" />
<Skeleton className="h-4 w-40" />
</div>
)
}
return (
<div className="flex min-h-0 flex-1 flex-col">
<div className="flex items-center gap-1 px-4 pt-3.5 pb-2">
<h2 className="flex-1 truncate text-lg font-bold">{sidebar.data.workspaceName}</h2>
{/* shortcut: new message and the search box below open nothing yet; new DM is M2, search (Ctrl K) M4. */}
<IconButton label="New message" size="icon">
<SquarePen />
</IconButton>
</div>
<button
type="button"
className="mx-2.5 mb-2 flex h-9 items-center gap-2 rounded-xl border border-border-strong bg-glass-strong px-2.5 text-sm text-muted-foreground outline-none focus-visible:ring-3 focus-visible:ring-ring"
>
<Search aria-hidden="true" className="size-4" />
<span className="flex-1 text-left">Search</span>{' '}
<kbd className="rounded border px-1 font-mono text-2xs">Ctrl K</kbd>
</button>
<div className="flex min-h-0 flex-1 flex-col gap-0.5 overflow-y-auto px-2.5 pb-3">
{QUICK_LINKS.map((link) => (
<button key={link.label} type="button" className={ROW}>
<link.icon aria-hidden="true" className="size-4 text-muted-foreground" />
<span className={link.hasUnread ? 'font-bold' : ''}>{link.label}</span>
</button>
))}
{SECTIONS.map(({ section, label }) => (
<section
key={section}
aria-labelledby={`sidebar-${section}`}
className="mt-3 flex flex-col gap-0.5"
>
<div className="flex h-7 items-center px-2.5">
<h3
id={`sidebar-${section}`}
className="flex-1 font-sans text-2xs font-bold tracking-wider text-muted-foreground uppercase"
>
{label}
</h3>
{section === 'starred' ? null : (
<Plus aria-hidden="true" className="size-4 text-muted-foreground" />
)}
</div>
{sidebar.data.conversations
.filter((conversation) => conversation.section === section)
.map((conversation) => (
<ConversationLink
key={conversation.id}
conversation={conversation}
people={sidebar.data.people}
/>
))}
</section>
))}
</div>
</div>
)
}
|