From 638e4d06d2fe069d103e0696003f005d646eb335 Mon Sep 17 00:00:00 2001 From: hazre Date: Sun, 22 Mar 2026 22:17:29 +0100 Subject: [PATCH] fix: sync sidebar unread badge mode and positioning --- src/app/components/sidebar/Sidebar.css.ts | 41 ++------------- src/app/components/sidebar/SidebarItem.tsx | 4 +- .../sidebar/SidebarUnreadBadge.test.tsx | 46 +++++++++++++++++ .../components/sidebar/SidebarUnreadBadge.tsx | 38 ++++++++++++++ src/app/components/sidebar/index.ts | 1 + .../unread-badge/UnreadBadge.test.ts | 50 ++++++++++++++++++ .../components/unread-badge/UnreadBadge.tsx | 51 ++++++++++++++----- .../client/sidebar/AccountSwitcherTab.tsx | 12 ++--- .../pages/client/sidebar/DirectDMsList.tsx | 23 +++------ src/app/pages/client/sidebar/DirectTab.tsx | 21 +++----- src/app/pages/client/sidebar/HomeTab.tsx | 19 ++----- src/app/pages/client/sidebar/InboxTab.tsx | 11 ++-- src/app/pages/client/sidebar/SpaceTabs.tsx | 35 ++++--------- .../pages/client/sidebar/UnverifiedTab.tsx | 4 +- 14 files changed, 216 insertions(+), 140 deletions(-) create mode 100644 src/app/components/sidebar/SidebarUnreadBadge.test.tsx create mode 100644 src/app/components/sidebar/SidebarUnreadBadge.tsx create mode 100644 src/app/components/unread-badge/UnreadBadge.test.ts diff --git a/src/app/components/sidebar/Sidebar.css.ts b/src/app/components/sidebar/Sidebar.css.ts index 457ceecbe..2952a0f7d 100644 --- a/src/app/components/sidebar/Sidebar.css.ts +++ b/src/app/components/sidebar/Sidebar.css.ts @@ -128,53 +128,20 @@ export const SidebarItemBadge = recipe({ }, ], variants: { - hasCount: { - true: { + mode: { + count: { top: toRem(-6), left: toRem(-6), }, - false: { + dot: { top: toRem(-4), left: toRem(-4), }, }, }, defaultVariants: { - hasCount: false, + mode: 'dot', }, - // Adjust positioning when badge is on a folder (closed folder button) - compoundVariants: [ - { - variants: { - hasCount: true, - }, - style: { - selectors: { - // When the badge is within an item containing a closed folder, adjust position - // to align with the folder's square border - 'div:has(> button[data-id]) &': { - top: toRem(0), - left: 'auto', - right: toRem(0), - }, - }, - }, - }, - { - variants: { - hasCount: false, - }, - style: { - selectors: { - 'div:has(> button[data-id]) &': { - top: toRem(0), - left: 'auto', - right: toRem(0), - }, - }, - }, - }, - ], }); export type SidebarItemBadgeVariants = RecipeVariants; diff --git a/src/app/components/sidebar/SidebarItem.tsx b/src/app/components/sidebar/SidebarItem.tsx index 6e6e0146b..b624bf0c9 100644 --- a/src/app/components/sidebar/SidebarItem.tsx +++ b/src/app/components/sidebar/SidebarItem.tsx @@ -14,9 +14,9 @@ export const SidebarItem = as<'div', css.SidebarItemVariants>( ); export const SidebarItemBadge = as<'div', css.SidebarItemBadgeVariants>( - ({ as: AsSidebarBadgeBox = 'div', className, hasCount, ...props }, ref) => ( + ({ as: AsSidebarBadgeBox = 'div', className, mode, ...props }, ref) => ( diff --git a/src/app/components/sidebar/SidebarUnreadBadge.test.tsx b/src/app/components/sidebar/SidebarUnreadBadge.test.tsx new file mode 100644 index 000000000..026c1a714 --- /dev/null +++ b/src/app/components/sidebar/SidebarUnreadBadge.test.tsx @@ -0,0 +1,46 @@ +import { describe, expect, it, vi } from 'vitest'; +import { render, screen } from '@testing-library/react'; +import type { ReactNode } from 'react'; +import { SidebarUnreadBadge } from './SidebarUnreadBadge'; + +const settings = { + showUnreadCounts: true, + badgeCountDMsOnly: false, + showPingCounts: false, +}; + +vi.mock('$state/hooks/settings', () => ({ + useSetting: (_atom: unknown, key: keyof typeof settings) => [settings[key]], +})); + +vi.mock('$state/settings', () => ({ + settingsAtom: {}, +})); + +vi.mock('./SidebarItem', () => ({ + SidebarItemBadge: ({ mode, children }: { mode: 'dot' | 'count'; children: ReactNode }) => ( +
+ {children} +
+ ), +})); + +describe('SidebarUnreadBadge', () => { + it('uses count mode when room unread counts are enabled', () => { + settings.showUnreadCounts = true; + + render(); + + expect(screen.getByTestId('sidebar-item-badge')).toHaveAttribute('data-mode', 'count'); + expect(screen.getByText('4')).toBeInTheDocument(); + }); + + it('uses dot mode when room unread counts are disabled', () => { + settings.showUnreadCounts = false; + + render(); + + expect(screen.getByTestId('sidebar-item-badge')).toHaveAttribute('data-mode', 'dot'); + expect(screen.queryByText('4')).not.toBeInTheDocument(); + }); +}); diff --git a/src/app/components/sidebar/SidebarUnreadBadge.tsx b/src/app/components/sidebar/SidebarUnreadBadge.tsx new file mode 100644 index 000000000..df1f5b7ee --- /dev/null +++ b/src/app/components/sidebar/SidebarUnreadBadge.tsx @@ -0,0 +1,38 @@ +import { UnreadBadge, UnreadBadgeMode, resolveUnreadBadgeMode } from '$components/unread-badge'; +import { useSetting } from '$state/hooks/settings'; +import { settingsAtom } from '$state/settings'; +import { SidebarItemBadge } from './SidebarItem'; + +type SidebarUnreadBadgeProps = { + highlight?: boolean; + count: number; + dm?: boolean; + mode?: UnreadBadgeMode; +}; + +export function SidebarUnreadBadge({ + highlight, + count, + dm, + mode, +}: Readonly) { + const [showUnreadCounts] = useSetting(settingsAtom, 'showUnreadCounts'); + const [badgeCountDMsOnly] = useSetting(settingsAtom, 'badgeCountDMsOnly'); + const [showPingCounts] = useSetting(settingsAtom, 'showPingCounts'); + const resolvedMode = + mode ?? + resolveUnreadBadgeMode({ + highlight, + count, + dm, + showUnreadCounts, + badgeCountDMsOnly, + showPingCounts, + }); + + return ( + + + + ); +} diff --git a/src/app/components/sidebar/index.ts b/src/app/components/sidebar/index.ts index 49e15b3e1..2d8e58672 100644 --- a/src/app/components/sidebar/index.ts +++ b/src/app/components/sidebar/index.ts @@ -1,5 +1,6 @@ export * from './Sidebar'; export * from './SidebarItem'; +export * from './SidebarUnreadBadge'; export * from './SidebarContent'; export * from './SidebarStack'; export * from './SidebarStackSeparator'; diff --git a/src/app/components/unread-badge/UnreadBadge.test.ts b/src/app/components/unread-badge/UnreadBadge.test.ts new file mode 100644 index 000000000..ad19bf079 --- /dev/null +++ b/src/app/components/unread-badge/UnreadBadge.test.ts @@ -0,0 +1,50 @@ +import { describe, expect, it } from 'vitest'; +import { resolveUnreadBadgeMode } from './UnreadBadge'; + +describe('resolveUnreadBadgeMode', () => { + it('returns count for a room unread when unread counts are enabled', () => { + expect( + resolveUnreadBadgeMode({ + count: 4, + showUnreadCounts: true, + badgeCountDMsOnly: false, + showPingCounts: false, + }) + ).toBe('count'); + }); + + it('returns dot for a room unread when unread counts are disabled', () => { + expect( + resolveUnreadBadgeMode({ + count: 4, + showUnreadCounts: false, + badgeCountDMsOnly: false, + showPingCounts: false, + }) + ).toBe('dot'); + }); + + it('returns count for a DM unread when DM counts are enabled', () => { + expect( + resolveUnreadBadgeMode({ + count: 4, + dm: true, + showUnreadCounts: false, + badgeCountDMsOnly: true, + showPingCounts: false, + }) + ).toBe('count'); + }); + + it('returns count for a highlight when ping counts are enabled', () => { + expect( + resolveUnreadBadgeMode({ + count: 2, + highlight: true, + showUnreadCounts: false, + badgeCountDMsOnly: false, + showPingCounts: true, + }) + ).toBe('count'); + }); +}); diff --git a/src/app/components/unread-badge/UnreadBadge.tsx b/src/app/components/unread-badge/UnreadBadge.tsx index 1ad2605ab..cef15accd 100644 --- a/src/app/components/unread-badge/UnreadBadge.tsx +++ b/src/app/components/unread-badge/UnreadBadge.tsx @@ -9,7 +9,32 @@ type UnreadBadgeProps = { count: number; /** Whether this badge belongs to a DM room. Used with the badgeCountDMsOnly setting. */ dm?: boolean; + mode?: UnreadBadgeMode; }; + +type ResolveUnreadBadgeModeOptions = Omit & { + showUnreadCounts: boolean; + badgeCountDMsOnly: boolean; + showPingCounts: boolean; +}; + +export type UnreadBadgeMode = 'dot' | 'count'; + +export function resolveUnreadBadgeMode({ + highlight, + count, + dm, + showUnreadCounts, + badgeCountDMsOnly, + showPingCounts, +}: ResolveUnreadBadgeModeOptions): UnreadBadgeMode { + const showNumber = + count > 0 && + ((dm && badgeCountDMsOnly) || (!dm && showUnreadCounts) || (highlight && showPingCounts)); + + return showNumber ? 'count' : 'dot'; +} + const styles: CSSProperties = { minWidth: toRem(16), }; @@ -21,30 +46,30 @@ export function UnreadBadgeCenter({ children }: { children: ReactNode }) { ); } -export function UnreadBadge({ highlight, count, dm }: UnreadBadgeProps) { +export function UnreadBadge({ highlight, count, dm, mode }: UnreadBadgeProps) { const [showUnreadCounts] = useSetting(settingsAtom, 'showUnreadCounts'); const [badgeCountDMsOnly] = useSetting(settingsAtom, 'badgeCountDMsOnly'); const [showPingCounts] = useSetting(settingsAtom, 'showPingCounts'); - - /** - * Show a number if there is a count and: - * - Its a dm and dm counts are enabled - * - Its a normal room and unread counts are enabled - * - Its a ping and ping counts are enabled - */ - const showNumber = - count > 0 && - ((dm && badgeCountDMsOnly) || (!dm && showUnreadCounts) || (highlight && showPingCounts)); + const resolvedMode = + mode ?? + resolveUnreadBadgeMode({ + highlight, + count, + dm, + showUnreadCounts, + badgeCountDMsOnly, + showPingCounts, + }); return ( - {showNumber && ( + {resolvedMode === 'count' && ( {millify(count)} diff --git a/src/app/pages/client/sidebar/AccountSwitcherTab.tsx b/src/app/pages/client/sidebar/AccountSwitcherTab.tsx index 75a7d099b..089c7b84b 100644 --- a/src/app/pages/client/sidebar/AccountSwitcherTab.tsx +++ b/src/app/pages/client/sidebar/AccountSwitcherTab.tsx @@ -30,7 +30,7 @@ import { SidebarItem, SidebarItemTooltip, SidebarAvatar, - SidebarItemBadge, + SidebarUnreadBadge, } from '$components/sidebar'; import { UserAvatar } from '$components/user-avatar'; import { nameInitials } from '$utils/common'; @@ -287,12 +287,10 @@ export function AccountSwitcherTab() { )} {(totalBackgroundUnread > 0 || anyBackgroundHighlight) && ( - - - + )} {unread && (unread.total > 0 || unread.highlight > 0) && ( - 0} - style={{ - left: unread.total > 0 ? toRem(-6) : toRem(-4), - right: 'auto', - }} - > - 0} - count={unread.highlight > 0 ? unread.highlight : unread.total} - dm - /> - + 0} + count={unread.highlight > 0 ? unread.highlight : unread.total} + dm + /> )} ); diff --git a/src/app/pages/client/sidebar/DirectTab.tsx b/src/app/pages/client/sidebar/DirectTab.tsx index a1ff6ff58..17c88a042 100644 --- a/src/app/pages/client/sidebar/DirectTab.tsx +++ b/src/app/pages/client/sidebar/DirectTab.tsx @@ -13,11 +13,10 @@ import { useRoomsUnread } from '$state/hooks/unread'; import { SidebarAvatar, SidebarItem, - SidebarItemBadge, + SidebarUnreadBadge, SidebarItemTooltip, } from '$components/sidebar'; import { useDirectSelected } from '$hooks/router/useDirectSelected'; -import { UnreadBadge } from '$components/unread-badge'; import { ScreenSize, useScreenSizeContext } from '$hooks/useScreenSize'; import { useNavToActivePathAtom } from '$state/hooks/navToActivePath'; import { markAsRead } from '$utils/notifications'; @@ -115,19 +114,11 @@ export function DirectTab() { )} {directUnread && ( - 0} - style={{ - left: directUnread.total > 0 ? toRem(-6) : toRem(-4), - right: 'auto', - }} - > - 0} - count={directUnread.highlight > 0 ? directUnread.highlight : directUnread.total} - dm - /> - + 0} + count={directUnread.highlight > 0 ? directUnread.highlight : directUnread.total} + dm + /> )} {menuAnchor && ( {homeUnread && ( - 0} - style={{ - left: homeUnread.total > 0 ? toRem(-6) : toRem(-4), - right: 'auto', - }} - > - 0} - count={homeUnread.highlight > 0 ? homeUnread.highlight : homeUnread.total} - /> - + 0} + count={homeUnread.highlight > 0 ? homeUnread.highlight : homeUnread.total} + /> )} {menuAnchor && ( )} - {inviteCount > 0 && ( - - - - )} + {inviteCount > 0 && } ); } diff --git a/src/app/pages/client/sidebar/SpaceTabs.tsx b/src/app/pages/client/sidebar/SpaceTabs.tsx index c751f00cb..5370aff2e 100644 --- a/src/app/pages/client/sidebar/SpaceTabs.tsx +++ b/src/app/pages/client/sidebar/SpaceTabs.tsx @@ -51,7 +51,7 @@ import { getSpaceLobbyPath, getSpacePath, joinPathComponent } from '$pages/pathU import { SidebarAvatar, SidebarItem, - SidebarItemBadge, + SidebarUnreadBadge, SidebarItemTooltip, SidebarStack, SidebarStackSeparator, @@ -60,7 +60,6 @@ import { } from '$components/sidebar'; import { RoomUnreadProvider, RoomsUnreadProvider } from '$components/RoomUnreadProvider'; import { useSelectedSpace } from '$hooks/router/useSelectedSpace'; -import { UnreadBadge } from '$components/unread-badge'; import { getCanonicalAliasOrRoomId, isRoomAlias } from '$utils/matrix'; import { RoomAvatar } from '$components/room-avatar'; import { nameInitials, randomStr } from '$utils/common'; @@ -465,18 +464,10 @@ function SpaceTab({ )} {unread && ( - 0} - style={{ - left: unread.total > 0 ? toRem(-6) : toRem(-4), - right: 'auto', - }} - > - 0} - count={unread.highlight > 0 ? unread.highlight : unread.total} - /> - + 0} + count={unread.highlight > 0 ? unread.highlight : unread.total} + /> )} {menuAnchor && ( {unread && ( - 0} - style={{ - left: unread.total > 0 ? toRem(-6) : toRem(-4), - right: 'auto', - }} - > - 0} - count={unread.highlight > 0 ? unread.highlight : unread.total} - /> - + 0} + count={unread.highlight > 0 ? unread.highlight : unread.total} + /> )} )} diff --git a/src/app/pages/client/sidebar/UnverifiedTab.tsx b/src/app/pages/client/sidebar/UnverifiedTab.tsx index a6c446b44..206b55db9 100644 --- a/src/app/pages/client/sidebar/UnverifiedTab.tsx +++ b/src/app/pages/client/sidebar/UnverifiedTab.tsx @@ -1,5 +1,5 @@ import { useState } from 'react'; -import { Badge, color, Icon, Icons, Text, toRem } from 'folds'; +import { Badge, color, Icon, Icons, Text } from 'folds'; import { SidebarAvatar, SidebarItem, @@ -66,7 +66,7 @@ function UnverifiedIndicator() { )} {!unverified && unverifiedDeviceCount && unverifiedDeviceCount > 0 && ( - + {unverifiedDeviceCount}