Add unread toast to room list sections (#33961)

* Forward the scroller element and scroll handle through VirtualizedList

The room list needs two things the generic list did not expose: the underlying
scroll container (to observe which items are genuinely visible) and the imperative
scroll handle (to scroll an item into view). Forward an optional scrollerRef from
useVirtualizedList and pass scrollHandleRef through FlatVirtualizedList, mirroring
what GroupedVirtualizedList already exposes. Both are additive and optional, so other
consumers are unaffected.

Signed-off-by: David Langley <langley.dave@gmail.com>

* Add an unread-activity toast to the room list

Show a clickable "You have unread activity" pill when there are unread rooms
scrolled below the visible fold, including unreads hidden inside collapsed sections
whose header is below the fold. Clicking it scrolls the next such unread into view.

The visible fold is tracked with an IntersectionObserver over the rendered item
elements, so the toast appears as soon as a room crosses the fold rather than only
once it leaves Virtuoso's overscan buffer. The toast click is wired through an
imperative scroll handle the view registers with the view model.

Signed-off-by: David Langley <langley.dave@gmail.com>

* Change toast to target notifications only, not unread activity.

* Fix formatting and refresh Toast snapshots for compound-web 9.7.0

- Apply oxfmt to the room list view model and virtualized view.
- Refresh the RoomListToast/RoomListView story snapshots: compound-web
  9.7.0 moves the typography classes onto the toast .content element and
  rebuilds the Toast CSS module hashes.
- Add the missing UnreadActivityToast render snapshot baseline.

* Add e2e tests for the room list unread activity toast

Cover the unread-activity toast end to end:
- it appears for a notifying room scrolled below the fold and clicking it
  scrolls that room into view (then the toast clears);
- a room with only an unread-activity dot (no notification count) does not
  raise it;
- a collapsed section hiding a notifying room raises it, and clicking
  scrolls the section header into view.

* Add visual snapshot baselines for the unread-activity toast stories

* Converge room-list toast codepaths into a single state-driven toast

Reviewer feedback: the unread-activity toast and the transient event toasts
(section_created / chat_moved) should share one mechanism, with toast
lifecycle and precedence owned by the view model rather than split across a
separate snapshot flag and a view-level ternary.

- RoomListViewModel now reconciles the transient event toast and the derived
  unread-activity state into a single snapshot.toast via recomputeToast(); the
  event toast still takes precedence and auto-dismisses, and the unread toast
  reappears once it clears. Drops hasUnreadActivityBelow from the snapshot.
- RoomListView renders a single RoomListToast driven by snapshot.toast; the
  ToastType union gains 'unread_activity'. The standalone UnreadActivityToast
  component is folded into RoomListToast (clickable arrow-down variant).
- Adds VM tests for the unread-activity toast and the event/unread precedence.

* Pre-bundle the room-list dnd-kit + virtuoso graph in vitest browser mode

The room-list suites (RoomListView, VirtualizedRoomListView,
RoomListItemMoreOptionsMenu) import a heavy @dnd-kit + react-virtuoso graph.
Only @dnd-kit/abstract was pinned in optimizeDeps.include; @dnd-kit/dom,
@dnd-kit/react, @dnd-kit/abstract/modifiers and react-virtuoso were left to
runtime discovery. Under CI load the browser-mode dep optimizer can discover
them late, re-bundle and reload the page, which fails the in-flight
setupTests.ts import for those suites. Pinning the whole graph forces it into
the initial optimize pass so no re-run happens while tests load.

---------

Signed-off-by: David Langley <langley.dave@gmail.com>
This commit is contained in:
David Langley
2026-06-29 08:55:26 +00:00
committed by GitHub
parent ed768f69e1
commit a0639bfc4b
17 changed files with 690 additions and 33 deletions
@@ -29,7 +29,10 @@ import RoomListStoreV3, {
type Section,
} from "../../stores/room-list-v3/RoomListStoreV3";
import { FilterEnum } from "../../stores/room-list-v3/skip-list/filters";
import { RoomNotificationStateStore } from "../../stores/notifications/RoomNotificationStateStore";
import {
RoomNotificationStateStore,
UPDATE_STATUS_INDICATOR,
} from "../../stores/notifications/RoomNotificationStateStore";
import { RoomListItemViewModel } from "./RoomListItemViewModel";
import { SdkContextClass } from "../../contexts/SDKContext";
import { hasCreateRoomRights } from "./utils";
@@ -101,10 +104,41 @@ export class RoomListViewModel
private readonly savedExpansionStates = new Map<string, boolean>();
/**
* Reference to the currently displayed toast, used to automatically close the toast after a timeout.
* Reference to the currently displayed event toast's auto-close timer, used to dismiss it
* after a timeout (see {@link showToast}).
*/
private toastRef?: number;
/**
* The currently active transient event toast ("section_created" / "chat_moved"), if any.
* Distinct from the derived "unread_activity" toast: this is set imperatively by an event
* and auto-dismisses, whereas unread activity is recomputed from list/notification state.
* {@link recomputeToast} reconciles the two into the single {@link RoomListViewSnapshot.toast}.
*/
private eventToast?: ToastType;
/**
* Whether there is currently unread activity (a notification count) in a room scrolled below
* the visible area of the list. Recomputed by {@link updateUnreadActivityBelow}; surfaced as
* the "unread_activity" toast by {@link recomputeToast} when no event toast takes precedence.
*/
private hasUnreadActivityBelow = false;
/**
* The last genuinely-visible index reported by the virtualized list (excluding the
* rendered overscan buffer), in the list's own entry space (room indices for a flat
* list; including a slot per section header for a grouped list). Initialised to -1 so
* that nothing is considered "below the fold" until the view reports the fold.
*/
private foldIndex = -1;
/**
* Imperative scroll handle registered by the view (see {@link setScrollToIndex}). The view
* owns the virtualized list's scroll handle, so it provides this; we call it to scroll a
* given item index into view in response to user actions.
*/
private scrollToIndex?: (index: number) => void;
public constructor(props: RoomListViewModelProps) {
const activeSpace = SpaceStore.instance.activeSpaceRoom;
@@ -172,6 +206,14 @@ export class RoomListViewModel
this.onRoomTagged,
);
// Recompute the "unread activity below" toast when room notification state
// changes (e.g. a room below the fold becomes unread, or is marked read).
this.disposables.trackListener(
RoomNotificationStateStore.instance,
UPDATE_STATUS_INDICATOR as any,
this.updateUnreadActivityBelow,
);
// Subscribe to active room changes to update selected room
const dispatcherRef = dispatcher.register(this.onDispatch);
this.disposables.track(() => {
@@ -311,8 +353,117 @@ export class RoomListViewModel
this.roomItemViewModels.delete(roomId);
}
}
// The rendered range changed, so re-evaluate whether unread activity is below the fold.
this.updateUnreadActivityBelow();
}
/**
* Update the last genuinely-visible item index (excluding the rendered overscan
* buffer), reported by the view from the scroller geometry. This is what the
* "unread activity" toast uses to decide what is below the fold, so that the toast
* appears as soon as an unread room scrolls just out of view rather than only once
* it leaves the overscan buffer.
*/
public updateVisibleFold = (visibleEndIndex: number): void => {
if (this.foldIndex === visibleEndIndex) return;
this.foldIndex = visibleEndIndex;
this.updateUnreadActivityBelow();
};
/**
* Find the first room with an unread-message notification (a count badge — the green or
* red decoration, not just the unread-activity dot) positioned below the visible area.
*
* "Below the fold" is determined from the last visible index reported by the
* virtualized list (see {@link updateVisibleRooms}). For a grouped list the
* virtualized list interleaves a section-header entry before each section's
* rooms, so we walk the rooms in the same entry order the view renders and
* compare against that index space.
*
* Collapsed sections render only their header (their rooms are removed from the
* displayed sections), so their notifying rooms are not directly reachable. When a
* collapsed section's header is itself below the fold and the section contains a
* notifying room, we surface the header as the target so clicking the toast scrolls
* it into view (revealing the header's aggregated notification badge).
*
* @returns The next notifying room below the fold, or undefined if there is none
* (or the view has not yet reported a visible range).
*/
private firstUnreadRoomBelowFold(): { room: Room; index: number } | undefined {
if (this.foldIndex < 0) return undefined;
// Only surface rooms showing a notification badge (a count/symbol — the green or red
// decoration), not rooms with just the unread-activity dot.
const hasNotification = (room: Room): boolean =>
RoomNotificationStateStore.instance.getRoomState(room).hasUnreadCount;
if (this.snapshot.current.isFlatList) {
// Flat list: virtualized indices map 1:1 to rooms.
const rooms = this.sections.flatMap((section) => section.rooms);
for (let i = this.foldIndex + 1; i < rooms.length; i++) {
if (hasNotification(rooms[i])) return { room: rooms[i], index: i };
}
return undefined;
}
// Full (pre-collapse) rooms per section tag, so we can detect unreads hidden inside
// collapsed sections whose displayed rooms have been emptied.
const fullRoomsByTag = new Map(this.roomsResult.sections.map((section) => [section.tag, section.rooms]));
// Grouped list: each section contributes a header entry followed by its rooms, so the
// index we return is in the virtualized list's entry space (matching scrollIntoView).
let entryIndex = -1;
for (const section of this.sections) {
entryIndex++; // section header entry
const isExpanded = this.roomSectionHeaderViewModels.get(section.tag)?.isExpanded ?? true;
if (!isExpanded) {
// Collapsed: rooms aren't rendered, so the header is the only entry. If it is
// below the fold and hides an unread room, target the header itself.
if (entryIndex > this.foldIndex) {
const notifyingRoom = (fullRoomsByTag.get(section.tag) ?? []).find(hasNotification);
if (notifyingRoom) return { room: notifyingRoom, index: entryIndex };
}
continue;
}
for (const room of section.rooms) {
entryIndex++; // this room's entry
if (entryIndex > this.foldIndex && hasNotification(room)) return { room, index: entryIndex };
}
}
return undefined;
}
/**
* Recompute whether there is unread activity below the visible area, reconciling the
* displayed toast if it changed.
*/
private updateUnreadActivityBelow = (): void => {
const hasUnreadActivityBelow = this.firstUnreadRoomBelowFold() !== undefined;
if (this.hasUnreadActivityBelow === hasUnreadActivityBelow) return;
this.hasUnreadActivityBelow = hasUnreadActivityBelow;
this.recomputeToast();
};
/**
* Register (or clear) the view's imperative scroll handler. Called by the view on mount
* since it owns the virtualized list's scroll handle.
*/
public setScrollToIndex = (scrollToIndex: ((index: number) => void) | undefined): void => {
this.scrollToIndex = scrollToIndex;
};
/**
* Scroll the next unread room below the visible area of the list into view (without opening
* it). Invoked when the user clicks the "unread activity" toast.
*/
public scrollToUnreadActivity = (): void => {
const target = this.firstUnreadRoomBelowFold();
if (target) this.scrollToIndex?.(target.index);
};
private onDispatch = (payload: any): void => {
if (payload.action === Action.ActiveRoomChanged) {
// When the active room changes, update the room list data to reflect the new selected room
@@ -595,6 +746,9 @@ export class RoomListViewModel
});
this.notifyCollapseState(isFlatList);
// Room list / sections changed: re-evaluate the unread-activity toast.
this.updateUnreadActivityBelow();
}
/**
@@ -654,20 +808,33 @@ export class RoomListViewModel
public closeToast: () => void = () => {
clearTimeout(this.toastRef);
this.snapshot.merge({
toast: undefined,
});
this.eventToast = undefined;
this.recomputeToast();
};
private showToast(toast: ToastType): void {
clearTimeout(this.toastRef);
this.snapshot.merge({ toast });
this.eventToast = toast;
this.recomputeToast();
// Automatically close the toast after 15 seconds
this.toastRef = setTimeout(() => {
this.closeToast();
}, 15 * 1000);
}
/**
* Reconcile the single toast shown by the view from the two independent sources: the
* transient event toast (which takes precedence and auto-dismisses) and the derived
* unread-activity state. The snapshot is only updated when the effective toast changes,
* to avoid unnecessary re-renders.
*/
private recomputeToast(): void {
const toast = this.eventToast ?? (this.hasUnreadActivityBelow ? "unread_activity" : undefined);
if (this.snapshot.current.toast !== toast) {
this.snapshot.merge({ toast });
}
}
public changeSectionOrder = async (sourceTag: string, targetTag: string): Promise<void> => {
await RoomListStoreV3.instance.reorderSection(sourceTag, targetTag);
// Scroll to the section after it moved