English
The chat list
Step 1. The list pane
Why. The list pane is the home of a messenger. Each row shows the chat's avatar, its title with an icon for groups and channels, the time of the last activity, a preview of the last message ("You: see you tomorrow", "📎 Attachment", "Message deleted") and the number of unread messages. A search field filters the loaded chats by title, and more pages load as the user scrolls down.
Presence dots and "typing…" in the rows come with the realtime client in topic 20; one line of the template is indented differently until then (yellow).
Do. Create the pane:
angular-ts
import {
ChangeDetectionStrategy,
Component,
ElementRef,
computed,
effect,
inject,
signal,
viewChild,
} from '@angular/core';
import { RouterLink, RouterLinkActive } from '@angular/router';
import { chatTitle } from '../../core/api/models';
import { AuthService } from '../../core/auth/auth.service';
import { ChatListItem, ChatsStore } from '../../state/chats.store';
import { Avatar } from '../../shared/ui/avatar';
import { Dialogs } from '../../shared/ui/dialogs';
import { ICONS } from '../../shared/ui/icons';
import { ShortWhenPipe } from '../../shared/ui/pipes';
import { NewChatDialog } from '../new-chat/new-chat-dialog';
@Component({
selector: 'app-chat-list-pane',
imports: [RouterLink, RouterLinkActive, Avatar, ShortWhenPipe, ICONS],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<header class="pt-safe flex items-center gap-2 px-4">
<h1 class="flex-1 py-4 text-xl font-bold tracking-tight">Chats</h1>
<button
type="button"
class="icon-btn"
aria-label="New chat"
title="New chat"
(click)="newChat()"
>
<svg lucideSquarePen class="size-5"></svg>
</button>
</header>
<div class="px-4 pb-3">
<label class="relative block">
<span class="sr-only">Filter chats</span>
<svg
lucideSearch
class="pointer-events-none absolute top-1/2 left-3 size-4 -translate-y-1/2 text-muted"
></svg>
<input
class="input border-transparent bg-subtle pl-9"
type="search"
placeholder="Search chats"
[value]="filter()"
(input)="filter.set($any($event.target).value)"
/>
</label>
</div>
<div class="min-h-0 flex-1 overflow-y-auto pb-20 md:pb-2">
@if (!store.loaded() && store.loading()) {
<ul aria-hidden="true">
@for (i of skeleton; track i) {
<li class="flex animate-pulse items-center gap-3 px-4 py-3">
<span class="size-12 rounded-full bg-subtle"></span>
<span class="flex-1 space-y-2">
<span class="block h-3 w-1/2 rounded bg-subtle"></span>
<span class="block h-3 w-3/4 rounded bg-subtle"></span>
</span>
</li>
}
</ul>
} @else if (store.loaded() && !store.items().length) {
<div class="flex flex-col items-center gap-3 px-6 py-16 text-center">
<span
class="flex size-14 items-center justify-center rounded-full bg-brand-50 text-brand-600 dark:bg-brand-950 dark:text-brand-300"
>
<svg lucideMessagesSquare class="size-7"></svg>
</span>
<p class="font-medium">No chats yet</p>
<p class="text-sm text-muted">Start a conversation or find a public group.</p>
<div class="mt-2 flex gap-2">
<button type="button" class="btn-primary" (click)="newChat()">New chat</button>
<a routerLink="/discover" class="btn-secondary">Discover</a>
</div>
</div>
} @else if (!visible().length) {
<p class="px-4 py-10 text-center text-sm text-muted">No chats match “{{ filter() }}”.</p>
}
<ul>
@for (chat of visible(); track chat.id) {
<li>
<a
[routerLink]="['/chats', chat.id]"
routerLinkActive="!bg-brand-50 dark:!bg-brand-950/60"
class="flex items-center gap-3 px-4 py-2.5 transition-colors hover:bg-subtle"
>
<app-avatar
[kind]="chat.type === 'direct' ? 'user' : 'chat'"
[id]="chat.type === 'direct' ? chat.peer?.id : chat.id"
[name]="title(chat)"
[hasAvatar]="chat.type === 'direct' ? !!chat.peer?.hasAvatar : chat.hasAvatar"
[size]="48"
/>
<span class="min-w-0 flex-1">
<span class="flex items-center gap-1.5">
@if (chat.type === 'channel') {
<svg lucideMegaphone class="size-3.5 shrink-0 text-muted"></svg>
} @else if (chat.type === 'group') {
<svg lucideUsers class="size-3.5 shrink-0 text-muted"></svg>
}
<span class="truncate font-medium">{{ title(chat) }}</span>
@if (chat.isMuted) {
<svg
lucideBellOff
class="size-3.5 shrink-0 text-muted"
aria-label="Muted"
></svg>
}
<span class="ml-auto shrink-0 pl-2 text-xs text-muted">{{
chat.lastMessage?.createdAt ?? chat.lastActivityAt | shortWhen
}}</span>
</span>
<span class="mt-0.5 flex items-center gap-2">
<span class="min-w-0 flex-1 truncate text-sm text-muted">
{{ preview(chat) }}
</span>
@if (chat.unreadCount) {
<span
class="badge"
[class]="
chat.isMuted
? 'bg-zinc-400 text-white dark:bg-zinc-600'
: 'bg-brand-600 text-white'
"
>
{{ chat.unreadCount > 99 ? '99+' : chat.unreadCount }}
</span>
}
</span>
</span>
</a>
</li>
}
</ul>
@if (store.hasMore() && !filter()) {
<div #sentinel class="flex justify-center py-4 text-muted">
@if (store.loadingMore()) {
<svg lucideLoaderCircle class="size-5 animate-spin"></svg>
}
</div>
}
</div>
`,
})
export class ChatListPane {
protected readonly store = inject(ChatsStore);
private readonly me = inject(AuthService).user;
private readonly dialogs = inject(Dialogs);
protected readonly skeleton = [1, 2, 3, 4, 5, 6];
protected readonly filter = signal('');
protected readonly visible = computed(() => {
const q = this.filter().trim().toLowerCase();
const items = this.store.items();
return q ? items.filter((c) => this.title(c).toLowerCase().includes(q)) : items;
});
private readonly sentinel = viewChild<ElementRef<HTMLElement>>('sentinel');
constructor() {
// Infinite scroll: load the next page when the bottom sentinel comes into view.
effect((onCleanup) => {
const element = this.sentinel()?.nativeElement;
if (!element) {
return;
}
const observer = new IntersectionObserver((entries) => {
if (entries.some((e) => e.isIntersecting)) {
void this.store.loadMore();
}
});
observer.observe(element);
onCleanup(() => observer.disconnect());
});
}
protected title(chat: ChatListItem): string {
return chatTitle(chat);
}
protected preview(chat: ChatListItem): string {
const last = chat.lastMessage;
if (!last) {
return chat.type === 'channel' ? 'Channel created' : 'No messages yet';
}
const prefix = last.senderId === this.me()?.id && chat.type !== 'channel' ? 'You: ' : '';
if (last.deleted) {
return `${prefix}Message deleted`;
}
return prefix + (last.body || (last.hasAttachments !== false ? '📎 Attachment' : ''));
}
protected newChat(): void {
this.dialogs.open(NewChatDialog);
}
}- Three states before the list: a pulsing skeleton while the first page loads (better than a spinner: the layout does not jump when the data arrives), an empty state with "New chat" and "Discover" for a new user, and "No chats match …" when the filter finds nothing.
@for (chat of visible(); track chat.id)renders the rows;visibleiscomputedfrom the store's items and the localfiltersignal, so typing in the search field filters without any request.[routerLink]="['/chats', chat.id]"builds the link from segments;routerLinkActivehighlights the open chat.- Direct chats show the peer's avatar and name, groups and channels their own (
[kind],[id],[hasAvatar]choose between them).chatTitlefrommodels.tsprovides the title. - The time uses the
shortWhenpipe on the last message's time or, for a chat without messages, its last activity. preview(chat)writes "You: " before the user's own messages (not in channels, where the author is the channel), "Message deleted" for a deleted one, and "📎 Attachment" for a message with only files.- The unread badge is grey for muted chats and caps at "99+".
- Infinite scrolling. An empty
<div #sentinel>sits after the last row while there are more pages.viewChild('sentinel')is a signal that holds the element when it is rendered. Aneffectcreates anIntersectionObserver— a browser API that reports when an element enters the viewport — and callsstore.loadMore()when the sentinel becomes visible.onCleanupdisconnects the observer when the effect runs again or the component is destroyed. This is cheaper and more robust than listening to every scroll event. pb-20 md:pb-2leaves room for the bottom tab bar on phones.
Step 2. The shell with the list
Do. Add the list pane, the unread count and the user's avatar to the shell (new lines in green; the temporary lines of topic 17 are gone):
angular-ts
import { ChangeDetectionStrategy, Component, computed, effect, inject } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { NavigationEnd, Router, RouterLink, RouterLinkActive, RouterOutlet } from '@angular/router';
import { filter, map } from 'rxjs';
import { AuthService } from '../../core/auth/auth.service';
import { ThemeService } from '../../core/ui/theme.service';
import { ChatsStore } from '../../state/chats.store';
import { Avatar } from '../../shared/ui/avatar';
import { ICONS } from '../../shared/ui/icons';
import { ChatListPane } from '../chat-list/chat-list-pane';
/**
* The signed-in layout.
* - Phone (< md): one pane at a time. /chats shows the list, a chat fills the screen, and a bottom
* tab bar switches between Chats, Discover and Settings (hidden inside a conversation).
* - Tablet (md–xl): nav rail + chat list + main pane.
* - Desktop (≥ xl): the same with a wider list; chat info docks on the right of the conversation.
*/
@Component({
selector: 'app-shell',
imports: [RouterOutlet, RouterLink, RouterLinkActive, ChatListPane, Avatar, ICONS],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<div class="flex h-dvh overflow-hidden">
<!-- Nav rail (tablet and desktop) -->
<nav
class="hidden w-18 shrink-0 flex-col items-center gap-2 border-r border-line bg-surface py-4 md:flex"
aria-label="Main"
>
<a
routerLink="/chats"
class="mb-4 flex size-11 items-center justify-center rounded-xl bg-brand-600 text-white"
aria-label="Chatex home"
>
<svg lucideMessagesSquare class="size-6"></svg>
</a>
@for (item of nav; track item.path) {
<a
[routerLink]="item.path"
routerLinkActive="!bg-brand-50 !text-brand-700 dark:!bg-brand-950 dark:!text-brand-300"
class="relative flex size-11 items-center justify-center rounded-xl text-muted transition-colors hover:bg-subtle hover:text-fg"
[attr.aria-label]="item.label"
[title]="item.label"
>
@switch (item.icon) {
@case ('chats') {
<svg lucideMessagesSquare class="size-5"></svg>
}
@case ('discover') {
<svg lucideCompass class="size-5"></svg>
}
@case ('settings') {
<svg lucideSettings class="size-5"></svg>
}
}
@if (item.icon === 'chats' && unread()) {
<span class="absolute -top-1 -right-1 badge bg-brand-600 text-white">{{
unreadLabel()
}}</span>
}
</a>
}
<div class="mt-auto flex flex-col items-center gap-2">
<button
type="button"
class="icon-btn"
(click)="toggleTheme()"
[attr.aria-label]="theme.isDark() ? 'Switch to light theme' : 'Switch to dark theme'"
[title]="theme.isDark() ? 'Light theme' : 'Dark theme'"
>
@if (theme.isDark()) {
<svg lucideSun class="size-5"></svg>
} @else {
<svg lucideMoon class="size-5"></svg>
}
</button>
@if (auth.user(); as me) {
<a
routerLink="/settings"
class="rounded-full"
[title]="me.displayName"
aria-label="Your profile"
>
<app-avatar
[id]="me.id"
[name]="me.displayName"
[hasAvatar]="me.hasAvatar"
[size]="36"
/>
</a>
}
</div>
</nav>
<!-- Chat list: always visible from md up; on phones only on /chats -->
<app-chat-list-pane
class="w-full shrink-0 flex-col border-r border-line bg-surface md:flex md:w-80 xl:w-96"
[class.hidden]="!onChatsRoot()"
[class.flex]="onChatsRoot()"
/>
<!-- Main pane -->
<main
class="min-w-0 flex-1 flex-col md:flex"
[class.hidden]="onChatsRoot()"
[class.flex]="!onChatsRoot()"
>
<div class="flex min-h-0 flex-1 flex-col">
<router-outlet />
</div>
</main>
</div>
<!-- Bottom tab bar (phones), hidden inside a conversation -->
@if (!inConversation()) {
<nav
class="pb-safe fixed inset-x-0 bottom-0 z-30 flex border-t border-line bg-surface/95 backdrop-blur md:hidden"
aria-label="Main"
>
@for (item of nav; track item.path) {
<a
[routerLink]="item.path"
routerLinkActive="!text-brand-600 dark:!text-brand-400"
[routerLinkActiveOptions]="{
exact: item.path !== '/settings' && item.path !== '/discover',
}"
class="relative flex min-h-14 flex-1 flex-col items-center justify-center gap-0.5 text-[11px] text-muted"
>
@switch (item.icon) {
@case ('chats') {
<svg lucideMessagesSquare class="size-5"></svg>
}
@case ('discover') {
<svg lucideCompass class="size-5"></svg>
}
@case ('settings') {
<svg lucideSettings class="size-5"></svg>
}
}
{{ item.label }}
@if (item.icon === 'chats' && unread()) {
<span class="absolute top-1.5 left-1/2 ml-1.5 badge bg-brand-600 text-white">{{
unreadLabel()
}}</span>
}
</a>
}
</nav>
}
`,
})
export class Shell {
protected readonly auth = inject(AuthService);
protected readonly theme = inject(ThemeService);
private readonly chats = inject(ChatsStore);
private readonly router = inject(Router);
protected readonly nav = [
{ path: '/chats', label: 'Chats', icon: 'chats' },
{ path: '/discover', label: 'Discover', icon: 'discover' },
{ path: '/settings', label: 'Settings', icon: 'settings' },
] as const;
private readonly url = toSignal(
this.router.events.pipe(
filter((e) => e instanceof NavigationEnd),
map(() => this.router.url),
),
{ initialValue: this.router.url },
);
private readonly path = computed(() => this.url().split(/[?#]/)[0]);
protected readonly onChatsRoot = computed(() => this.path() === '/chats');
protected readonly inConversation = computed(() => /^\/chats\/[^/]+/.test(this.path()));
protected readonly unread = this.chats.totalUnread;
protected readonly unreadLabel = computed(() =>
this.unread() > 99 ? '99+' : String(this.unread()),
);
constructor() {
void this.chats.loadFirst().catch(() => undefined);
effect(() => {
const count = this.unread();
document.title = count ? `(${count}) Chatex` : 'Chatex';
});
}
protected toggleTheme(): void {
this.theme.set(this.theme.isDark() ? 'light' : 'dark');
}
}<app-chat-list-pane>is always visible frommdup; on phones it is shown only on/chats([class.hidden]="!onChatsRoot()"), where the main pane is hidden instead.unreadis the store'stotalUnreadsignal itself, andunreadLabelcaps it at "99+" for the badges on the rail and the tab bar.- The constructor loads the first page of chats when the shell appears, that is, right after signing in.
.catch(() => undefined)keeps a failed load from becoming an unhandled rejection; the list then simply stays in its loading state until the user reloads. - The tab title shows the unread count, "(3) Chatex", through an
effect: a side effect on the document, outside Angular's templates. - The avatar at the bottom of the rail links to the settings (topic 21).
The no-chat page gets its "New chat" button:
angular-ts
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { Dialogs } from '../../shared/ui/dialogs';
import { ICONS } from '../../shared/ui/icons';
import { NewChatDialog } from '../new-chat/new-chat-dialog';
/** The main pane on tablets and desktops when no chat is open (phones show the list instead). */
@Component({
selector: 'app-no-chat-page',
imports: [ICONS],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
class: 'flex flex-1 flex-col items-center justify-center gap-4 bg-canvas p-8 text-center',
},
template: `
<span
class="flex size-20 items-center justify-center rounded-3xl bg-brand-50 text-brand-600 dark:bg-brand-950 dark:text-brand-300"
>
<svg lucideMessagesSquare class="size-10"></svg>
</span>
<div>
<h2 class="text-lg font-semibold">Select a chat</h2>
<p class="mt-1 text-sm text-muted">Pick a conversation from the list or start a new one.</p>
</div>
<button type="button" class="btn-primary" (click)="newChat()">
<svg lucidePlus class="size-4"></svg>New chat
</button>
`,
})
export class NoChatPage {
private readonly dialogs = inject(Dialogs);
protected newChat(): void {
this.dialogs.open(NewChatDialog);
}
} Step 3. The discover route
Do. Add the discover page to the routes:
ts
import { Routes } from '@angular/router';
import { authGuard, guestGuard } from './core/auth/guards';
export const routes: Routes = [
{
path: 'login',
canActivate: [guestGuard],
title: 'Sign in · Chatex',
loadComponent: () => import('./features/auth/login.page').then((m) => m.LoginPage),
},
{
path: 'register',
canActivate: [guestGuard],
title: 'Create account · Chatex',
loadComponent: () => import('./features/auth/register.page').then((m) => m.RegisterPage),
},
{
path: '',
canActivate: [authGuard],
loadComponent: () => import('./features/shell/shell').then((m) => m.Shell),
children: [
{ path: '', pathMatch: 'full', redirectTo: 'chats' },
{
path: 'chats',
loadComponent: () => import('./features/shell/no-chat.page').then((m) => m.NoChatPage),
},
{
path: 'discover',
loadComponent: () =>
import('./features/discover/discover.page').then((m) => m.DiscoverPage),
},
],
},
{ path: '**', redirectTo: 'chats' },
];Check
Start the API and the client and sign in.
- A new user sees "No chats yet" with the buttons New chat and Discover.
- Register a second user (for example
bob) in a private window. Back in the first window, open New chat → Group, enter "Course team", the handlecourse_team, search "bo" and select Bob: the chip "Bob" appears. Create group closes the dialog, and the list shows "Course team" with the orange initials "CT" and "No messages yet". - New chat → Direct → search "bo" → click Bob: a direct chat with Bob's name appears in the list. Opening a chat does not work yet — its route comes in topic 19, so the URL falls back to
/chats. - In Bob's window open Discover, type "course": the group appears; select it and click Open (Bob is already a member). Create another public group as Bob and join it from Alice's window with Join group: the toast "Joined …" appears.
- Type in the list's search field: the rows are filtered as you type, without requests in the Network tab.
- In a narrow window, the list fills the screen on
/chatswith the tab bar below it.