Skip to content

Commit 4f554b6

Browse files
tellahoCarlRizz
authored
feat(sidebar): prioritize unread DMs in overflow navigation (block#6842)
**Category:** improvement **User Impact:** Users can see which unread direct messages are waiting offscreen and jump to the most relevant one first. **Problem:** The sidebar overflow control treated every offscreen unread conversation alike, so an unread DM could be hidden behind channel activity with no visible clue about who was waiting. **Solution:** Prioritize the first offscreen unread DM, preview up to three senders with avatars, and keep the arrow, avatars, separator, and message count intact while only the trailing phrase truncates in narrow sidebars. <details> <summary>File changes</summary> **desktop/src/features/sidebar/lib/useUnreadOverflow.ts** Adds ordered DM preview data to unread-overflow calculations while preserving the sidebar's existing visibility and direction rules. **desktop/src/features/sidebar/lib/useUnreadOverflow.test.mjs** Covers DM prioritization, ordering, offscreen filtering, and channel-only fallback behavior. **desktop/src/features/sidebar/ui/AppSidebar.tsx** Passes unread DM previews into the lower overflow control and navigates to the preferred DM before the next unread channel. **desktop/src/features/sidebar/ui/MoreUnreadButton.tsx** Renders the frontmost-first avatar stack, accessible DM target label, middot separator, and message-count treatment. **desktop/src/features/sidebar/ui/MoreUnreadButton.test.mjs** Locks the three-avatar cap, stacking order, preferred target, fallback, accessible copy, and truncation structure. **desktop/src/shared/ui/UnreadPill.tsx** Contains the primary pill and allows only its trailing label to ellipsize under width pressure. **desktop/src/shared/ui/UserAvatar.tsx** Allows the sidebar preview stack to identify each rendered avatar in regression coverage. </details> ## Reproduction steps 1. Open enough sidebar conversations that unread items fall below the visible area. 2. Leave one or more offscreen DMs unread, along with unread channels. 3. Confirm the bottom overflow pill shows up to three overlapping DM avatars followed by a middot and the total new-message count. 4. Select the pill and confirm it navigates to the first offscreen unread DM rather than the nearest unread channel. 5. Resize the sidebar from 420px through 300px to 220px and confirm the arrow, avatars, middot, and full numeric count remain visible; only `new messages` may ellipsize. 6. Repeat with channel-only unread overflow and confirm the control keeps its channel fallback behavior without avatars. ## Screenshots Built-UI matrix with 100px of visible space beyond the sidebar edge. All images are hosted through `pr-screenshots`. | State | 220px min | 300px default | 420px max | Result | | --- | --- | --- | --- | --- | | Channel-only · 13 | ![Channel-only · 13 at 220px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/01.png) | ![Channel-only · 13 at 300px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/02.png) | ![Channel-only · 13 at 420px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/03.png) | Full label throughout. | | Channel-only · 999 | ![Channel-only · 999 at 220px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/04.png) | ![Channel-only · 999 at 300px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/05.png) | ![Channel-only · 999 at 420px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/06.png) | Full label throughout. | | One DM only · 1 | ![One DM only · 1 at 220px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/07.png) | ![One DM only · 1 at 300px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/08.png) | ![One DM only · 1 at 420px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/09.png) | Full label throughout. | | One DM + channels · 99 | ![One DM + channels · 99 at 220px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/10.png) | ![One DM + channels · 99 at 300px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/11.png) | ![One DM + channels · 99 at 420px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/12.png) | Full label throughout. | | Two DMs only · 2 | ![Two DMs only · 2 at 220px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/13.png) | ![Two DMs only · 2 at 300px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/14.png) | ![Two DMs only · 2 at 420px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/15.png) | Full label throughout. | | Two DMs + channels · 47 | ![Two DMs + channels · 47 at 220px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/16.png) | ![Two DMs + channels · 47 at 300px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/17.png) | ![Two DMs + channels · 47 at 420px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/18.png) | Full label throughout. | | Three DMs + channels · 128 | ![Three DMs + channels · 128 at 220px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/19.png) | ![Three DMs + channels · 128 at 300px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/20.png) | ![Three DMs + channels · 128 at 420px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/21.png) | 220px: `128 new messa…`; full at 300/420. | | Three DMs + channels · 999 | ![Three DMs + channels · 999 at 220px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/22.png) | ![Three DMs + channels · 999 at 300px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/23.png) | ![Three DMs + channels · 999 at 420px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/24.png) | 220px: `999 new mess…`; full at 300/420. | | Three DMs + channels · 9,999 | ![Three DMs + channels · 9,999 at 220px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/25.png) | ![Three DMs + channels · 9,999 at 300px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/26.png) | ![Three DMs + channels · 9,999 at 420px](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6842/27.png) | 220px: `9,999 new mes…`; full at 300/420. | **Matrix result:** 27/27 states remain contained. Exactly 3/27 states ellipsize—all are three-avatar cases at 220px. The arrow, all avatars, middot, and complete numeric value remain visible; only the trailing `new messages` phrase yields. ## Validation - Desktop unit suite: 5,517 passed - TypeScript: passed - Biome on all seven changed files: passed - Pre-push desktop checks, typecheck, tests, file-size, and branch-skew gates: passed - 27-state built-UI matrix capture: 27/27 contained; only the three 220px three-avatar states truncate, and only the trailing phrase - `git diff --check`: passed --------- Signed-off-by: Taylor Ho <[email protected]> Co-authored-by: Carl <acda9e433d19dcd0e6b6840f7f4b98f3a56f1fab98049d444c087019e6d36560@users.noreply.github.com> Co-authored-by: Rizz <302abe414ca6e3134763d2539bfcf145aea2a63fe5f8455204ed602fd40cf381@buzz.block.builderlab.xyz>
1 parent de188eb commit 4f554b6

9 files changed

Lines changed: 473 additions & 26 deletions

‎desktop/src/features/sidebar/lib/useOffscreenActivityChannelIds.test.mjs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ test("keeps every unread channel navigable while adding working activity", () =>
2626
]);
2727
});
2828

29+
test("keeps working-only channels out of message overflow prioritization", () => {
30+
const activity = getOffscreenActivityChannelIds({
31+
activeWorkingByChannelId: new Map([["read-working-dm", {}]]),
32+
previewActivityChannelIds: new Set(),
33+
unreadChannelIds: new Set(["unread-channel"]),
34+
});
35+
36+
assert.deepEqual([...activity.messageChannelIds], ["unread-channel"]);
37+
assert.deepEqual(
38+
[...activity.channelIds],
39+
["unread-channel", "read-working-dm"],
40+
);
41+
});
42+
2943
test("uses an activity-neutral overflow label when work contributes", () => {
3044
assert.equal(
3145
getSidebarActivityOverflowLabel({ activityCount: 2, messageCount: 1 }),

‎desktop/src/features/sidebar/lib/useSidebarActivityOverflow.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export function useSidebarActivityOverflow({
3333

3434
return {
3535
...activityOverflow,
36+
unreadMessageAboveChannelIds: messageOverflow.unreadAboveChannelIds,
37+
unreadMessageBelowChannelIds: messageOverflow.unreadBelowChannelIds,
3638
unreadAboveLabel: getSidebarActivityOverflowLabel({
3739
activityCount: activityOverflow.unreadAboveCount,
3840
messageCount: messageOverflow.unreadAboveCount,
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import assert from "node:assert/strict";
2+
import { describe, it } from "node:test";
3+
4+
import { deriveUnreadOverflow } from "./useUnreadOverflow.ts";
5+
6+
const root = {
7+
getBoundingClientRect: () => ({ height: 100, top: 50 }),
8+
};
9+
10+
function entry(channelId, top, isIntersecting = false) {
11+
return {
12+
element: {
13+
getAttribute: (name) => (name === "data-channel-id" ? channelId : null),
14+
getBoundingClientRect: () => ({ top }),
15+
},
16+
isIntersecting,
17+
};
18+
}
19+
20+
describe("deriveUnreadOverflow", () => {
21+
it("orders offscreen channels by position", () => {
22+
const result = deriveUnreadOverflow(
23+
[
24+
entry("below-2", 180),
25+
entry("above-2", 0),
26+
entry("below-1", 160),
27+
entry("above-1", 20),
28+
],
29+
root,
30+
);
31+
32+
assert.deepEqual(result.unreadAboveChannelIds, ["above-1", "above-2"]);
33+
assert.deepEqual(result.unreadBelowChannelIds, ["below-1", "below-2"]);
34+
assert.equal(result.unreadAboveCount, 2);
35+
assert.equal(result.unreadBelowCount, 2);
36+
});
37+
38+
it("deduplicates repeated sidebar rows for the same channel", () => {
39+
const result = deriveUnreadOverflow(
40+
[entry("starred", 170), entry("starred", 240)],
41+
root,
42+
);
43+
44+
assert.deepEqual(result.unreadBelowChannelIds, ["starred"]);
45+
assert.equal(result.unreadBelowCount, 1);
46+
});
47+
48+
it("does not count a duplicate row when the same channel is visible", () => {
49+
const result = deriveUnreadOverflow(
50+
[entry("starred", 80, true), entry("starred", 240)],
51+
root,
52+
);
53+
54+
assert.deepEqual(result.unreadBelowChannelIds, []);
55+
assert.equal(result.unreadBelowCount, 0);
56+
});
57+
58+
it("ignores visible rows", () => {
59+
const result = deriveUnreadOverflow(
60+
[entry("visible", 80, true), entry("below", 180)],
61+
root,
62+
);
63+
64+
assert.deepEqual(result.unreadBelowChannelIds, ["below"]);
65+
});
66+
});

‎desktop/src/features/sidebar/lib/useUnreadOverflow.ts‎

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ type UnreadDirection = "above" | "below";
1010
type UnreadOverflowCounts = {
1111
unreadAboveCount: number;
1212
unreadBelowCount: number;
13+
unreadAboveChannelIds: string[];
14+
unreadBelowChannelIds: string[];
1315
};
1416

1517
const EMPTY_COUNTS: UnreadOverflowCounts = {
1618
unreadAboveCount: 0,
1719
unreadBelowCount: 0,
20+
unreadAboveChannelIds: [],
21+
unreadBelowChannelIds: [],
1822
};
1923

2024
function getChannelId(element: Element): string | null {
@@ -71,34 +75,65 @@ function findNextUnreadElement({
7175
return nextElement;
7276
}
7377

74-
function deriveCounts(
75-
visibilityById: Map<string, VisibilityEntry>,
78+
export function deriveUnreadOverflow(
79+
entries: Iterable<VisibilityEntry>,
7680
root: HTMLDivElement,
7781
): UnreadOverflowCounts {
78-
let unreadAboveCount = 0;
79-
let unreadBelowCount = 0;
80-
82+
const unreadAbove = new Map<string, number>();
83+
const unreadBelow = new Map<string, number>();
84+
const visibleChannelIds = new Set<string>();
8185
const rootHeight = root.getBoundingClientRect().height;
8286

83-
for (const entry of visibilityById.values()) {
84-
const top = getRelativeTop(entry.element, root);
87+
for (const entry of entries) {
88+
const channelId = getChannelId(entry.element);
89+
if (!channelId) continue;
90+
if (entry.isIntersecting) {
91+
visibleChannelIds.add(channelId);
92+
continue;
93+
}
8594

86-
if (entry.isIntersecting) continue;
95+
const top = getRelativeTop(entry.element, root);
8796

8897
if (top < 0) {
89-
unreadAboveCount += 1;
98+
unreadAbove.set(
99+
channelId,
100+
Math.max(unreadAbove.get(channelId) ?? -Infinity, top),
101+
);
90102
} else if (top > rootHeight) {
91-
unreadBelowCount += 1;
103+
unreadBelow.set(
104+
channelId,
105+
Math.min(unreadBelow.get(channelId) ?? Infinity, top),
106+
);
92107
}
93108
}
94109

95-
return { unreadAboveCount, unreadBelowCount };
110+
for (const channelId of visibleChannelIds) {
111+
unreadAbove.delete(channelId);
112+
unreadBelow.delete(channelId);
113+
}
114+
115+
const byPosition = (left: [string, number], right: [string, number]) =>
116+
left[1] - right[1];
117+
const unreadAboveChannelIds = [...unreadAbove.entries()]
118+
.sort((left, right) => byPosition(right, left))
119+
.map(([channelId]) => channelId);
120+
const unreadBelowChannelIds = [...unreadBelow.entries()]
121+
.sort(byPosition)
122+
.map(([channelId]) => channelId);
123+
124+
return {
125+
unreadAboveCount: unreadAboveChannelIds.length,
126+
unreadBelowCount: unreadBelowChannelIds.length,
127+
unreadAboveChannelIds,
128+
unreadBelowChannelIds,
129+
};
96130
}
97131

98132
export function useUnreadOverflow(args: {
99133
scrollRef: React.RefObject<HTMLDivElement | null>;
100134
unreadChannelIds: ReadonlySet<string>;
101135
}): UnreadOverflowCounts & {
136+
scrollToChannel: (channelId: string) => void;
102137
scrollToNextAbove: () => void;
103138
scrollToNextBelow: () => void;
104139
} {
@@ -117,15 +152,15 @@ export function useUnreadOverflow(args: {
117152
}
118153

119154
let intersectionObserver: IntersectionObserver | null = null;
120-
const visibilityById = new Map<string, VisibilityEntry>();
155+
const visibilityByElement = new Map<HTMLElement, VisibilityEntry>();
121156

122157
const updateCounts = () => {
123-
setCounts(deriveCounts(visibilityById, root));
158+
setCounts(deriveUnreadOverflow(visibilityByElement.values(), root));
124159
};
125160

126161
const bindUnreadRows = () => {
127162
intersectionObserver?.disconnect();
128-
visibilityById.clear();
163+
visibilityByElement.clear();
129164

130165
intersectionObserver = new IntersectionObserver(
131166
(entries) => {
@@ -135,8 +170,9 @@ export function useUnreadOverflow(args: {
135170
continue;
136171
}
137172

138-
visibilityById.set(channelId, {
139-
element: entry.target as HTMLElement,
173+
const element = entry.target as HTMLElement;
174+
visibilityByElement.set(element, {
175+
element,
140176
isIntersecting: entry.isIntersecting,
141177
});
142178
}
@@ -150,7 +186,7 @@ export function useUnreadOverflow(args: {
150186
const channelId = getChannelId(element);
151187
if (!channelId) continue;
152188

153-
visibilityById.set(channelId, {
189+
visibilityByElement.set(element, {
154190
element,
155191
isIntersecting: false,
156192
});
@@ -192,8 +228,20 @@ export function useUnreadOverflow(args: {
192228
})?.scrollIntoView({ behavior: "smooth", block: "center" });
193229
}, [scrollRef]);
194230

231+
const scrollToChannel = React.useCallback(
232+
(channelId: string) => {
233+
const root = scrollRef.current;
234+
if (!root) return;
235+
getUnreadElements(root, unreadChannelIdsRef.current)
236+
.find((element) => getChannelId(element) === channelId)
237+
?.scrollIntoView({ behavior: "smooth", block: "center" });
238+
},
239+
[scrollRef],
240+
);
241+
195242
return {
196243
...counts,
244+
scrollToChannel,
197245
scrollToNextAbove,
198246
scrollToNextBelow,
199247
};

‎desktop/src/features/sidebar/ui/AppSidebar.tsx‎

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ import {
3333
AppSidebarPinnedHeader,
3434
AppSidebarPrimaryMenu,
3535
} from "@/features/sidebar/ui/AppSidebarPinnedHeader";
36-
import { MoreUnreadButton } from "@/features/sidebar/ui/MoreUnreadButton";
36+
import {
37+
canPreviewUnreadDm,
38+
MoreUnreadButton,
39+
preferredUnreadTarget,
40+
} from "@/features/sidebar/ui/MoreUnreadButton";
41+
import { unreadCountLabel } from "@/shared/ui/UnreadPill";
3742
import { SidebarSection } from "@/features/sidebar/ui/SidebarSection";
3843
import {
3944
ChannelGroupSection,
@@ -148,7 +153,7 @@ export function AppSidebar({
148153
const scrollRef = React.useRef<HTMLDivElement>(null);
149154
useSidebarScrollLock(scrollRef);
150155
// biome-ignore format: keep compact to stay within file size limit
151-
const { scrollToNextAbove, scrollToNextBelow, unreadAboveCount, unreadBelowCount, unreadAboveLabel, unreadBelowLabel } = useSidebarActivityOverflow({ activeWorkingByChannelId, previewActivityChannelIds, scrollRef, unreadChannelIds });
156+
const { scrollToChannel, scrollToNextAbove, scrollToNextBelow, unreadAboveCount, unreadBelowCount, unreadMessageBelowChannelIds, unreadAboveLabel, unreadBelowLabel } = useSidebarActivityOverflow({ activeWorkingByChannelId, previewActivityChannelIds, scrollRef, unreadChannelIds });
152157

153158
React.useEffect(() => {
154159
const scrollElement = scrollRef.current;
@@ -390,6 +395,48 @@ export function AppSidebar({
390395
),
391396
[directMessages, dmChannelLabels, sortModeFor],
392397
);
398+
const unreadDmPreviewsBelow = React.useMemo(
399+
() =>
400+
unreadMessageBelowChannelIds.flatMap((channelId) => {
401+
const channel = directMessages.find(
402+
(candidate) => candidate.id === channelId,
403+
);
404+
const participants = dmParticipantsByChannelId[channelId];
405+
const participant = participants?.[0];
406+
if (
407+
!channel ||
408+
!participant ||
409+
!canPreviewUnreadDm(
410+
channel.participantPubkeys.length,
411+
participants?.length ?? 0,
412+
)
413+
) {
414+
return [];
415+
}
416+
return [
417+
{
418+
accessibleLabel: participant.label,
419+
avatarUrl: participant.avatarUrl,
420+
channelId,
421+
label: dmChannelLabels[channelId] ?? participant.label,
422+
},
423+
];
424+
}),
425+
[
426+
directMessages,
427+
dmChannelLabels,
428+
dmParticipantsByChannelId,
429+
unreadMessageBelowChannelIds,
430+
],
431+
);
432+
const unreadDmChannelIds = React.useMemo(
433+
() => new Set(directMessages.map(({ id }) => id)),
434+
[directMessages],
435+
);
436+
const nextUnreadDmBelowId = preferredUnreadTarget(
437+
unreadMessageBelowChannelIds,
438+
unreadDmChannelIds,
439+
);
393440
const sidebarLoadingShape = useSidebarLoadingShape({
394441
activeCommunityId: activeCommunity?.id,
395442
currentPubkey,
@@ -487,7 +534,7 @@ export function AppSidebar({
487534
{unreadAboveCount > 0 ? (
488535
<MoreUnreadButton
489536
count={unreadAboveCount}
490-
label={unreadAboveLabel}
537+
label={unreadAboveLabel ?? unreadCountLabel(unreadAboveCount)}
491538
onClick={scrollToNextAbove}
492539
position="top"
493540
testId="sidebar-more-unread-above"
@@ -760,9 +807,15 @@ export function AppSidebar({
760807
<MoreUnreadButton
761808
bottomClassName="bottom-full"
762809
count={unreadBelowCount}
763-
label={unreadBelowLabel}
764-
onClick={scrollToNextBelow}
810+
dmPreviews={unreadDmPreviewsBelow}
811+
label={unreadBelowLabel ?? unreadCountLabel(unreadBelowCount)}
812+
onClick={() =>
813+
nextUnreadDmBelowId
814+
? scrollToChannel(nextUnreadDmBelowId)
815+
: scrollToNextBelow()
816+
}
765817
position="bottom"
818+
targetChannelId={nextUnreadDmBelowId}
766819
testId="sidebar-more-unread-below"
767820
/>
768821
) : null}

0 commit comments

Comments
 (0)