diff --git a/.changeset/cap_unread_badges.md b/.changeset/cap_unread_badges.md new file mode 100644 index 000000000..affe5ee1e --- /dev/null +++ b/.changeset/cap_unread_badges.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +Cap unread badge numbers at `1k+`, and something extra :) diff --git a/src/app/components/unread-badge/UnreadBadge.test.ts b/src/app/components/unread-badge/UnreadBadge.test.ts deleted file mode 100644 index ad19bf079..000000000 --- a/src/app/components/unread-badge/UnreadBadge.test.ts +++ /dev/null @@ -1,50 +0,0 @@ -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.test.tsx b/src/app/components/unread-badge/UnreadBadge.test.tsx new file mode 100644 index 000000000..8c4216439 --- /dev/null +++ b/src/app/components/unread-badge/UnreadBadge.test.tsx @@ -0,0 +1,126 @@ +import { render, screen } from '@testing-library/react'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { formatUnreadBadgeCount, resolveUnreadBadgeMode, UnreadBadge } from './UnreadBadge'; + +const settings = { + showUnreadCounts: true, + badgeCountDMsOnly: true, + showPingCounts: true, + showEasterEggs: true, +}; + +vi.mock('$state/hooks/settings', () => ({ + useSetting: (_atom: unknown, key: keyof typeof settings) => [settings[key]], +})); + +vi.mock('$state/settings', () => ({ + settingsAtom: {}, +})); + +beforeEach(() => { + settings.showUnreadCounts = true; + settings.badgeCountDMsOnly = true; + settings.showPingCounts = true; + settings.showEasterEggs = true; +}); + +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'); + }); +}); + +describe('formatUnreadBadgeCount', () => { + it.each([ + [1, '1'], + [9, '9'], + [99, '99'], + [999, '999'], + [1000, '1k'], + [1001, ':3'], + [2000, ':3'], + [2001, ':3'], + [9000, ':3'], + [9001, ':3'], + [10000, ':3'], + ])('formats %i as %s when easter eggs are enabled', (count, expected) => { + expect(formatUnreadBadgeCount(count, true)).toBe(expected); + }); + + it.each([ + [1, '1'], + [99, '99'], + [999, '999'], + [1000, '1k'], + [1001, '1k+'], + [2000, '1k+'], + [2001, '1k+'], + [9000, '1k+'], + [9001, '1k+'], + [10000, '1k+'], + ])('formats %i as %s when easter eggs are disabled', (count, expected) => { + expect(formatUnreadBadgeCount(count, false)).toBe(expected); + }); +}); + +describe('UnreadBadge', () => { + it('renders :3 instead of k+ when easter eggs are enabled', () => { + settings.showEasterEggs = true; + + render(); + + expect(screen.getByText(':3')).toBeInTheDocument(); + expect(screen.queryByText('1k+')).not.toBeInTheDocument(); + }); + + it('renders k+ when easter eggs are disabled', () => { + settings.showEasterEggs = false; + + render(); + + expect(screen.getByText('1k+')).toBeInTheDocument(); + expect(screen.queryByText(':3')).not.toBeInTheDocument(); + }); +}); diff --git a/src/app/components/unread-badge/UnreadBadge.tsx b/src/app/components/unread-badge/UnreadBadge.tsx index cef15accd..70a7dc114 100644 --- a/src/app/components/unread-badge/UnreadBadge.tsx +++ b/src/app/components/unread-badge/UnreadBadge.tsx @@ -1,6 +1,5 @@ import { CSSProperties, ReactNode } from 'react'; import { Box, Badge, toRem, Text } from 'folds'; -import { millify } from '$plugins/millify'; import { useSetting } from '$state/hooks/settings'; import { settingsAtom } from '$state/settings'; @@ -35,6 +34,18 @@ export function resolveUnreadBadgeMode({ return showNumber ? 'count' : 'dot'; } +export function formatUnreadBadgeCount(count: number, showEasterEggs: boolean): string { + if (count <= 999) { + return count.toString(); + } + + if (count === 1000) { + return '1k'; + } + + return showEasterEggs ? ':3' : '1k+'; +} + const styles: CSSProperties = { minWidth: toRem(16), }; @@ -50,6 +61,7 @@ export function UnreadBadge({ highlight, count, dm, mode }: UnreadBadgeProps) { const [showUnreadCounts] = useSetting(settingsAtom, 'showUnreadCounts'); const [badgeCountDMsOnly] = useSetting(settingsAtom, 'badgeCountDMsOnly'); const [showPingCounts] = useSetting(settingsAtom, 'showPingCounts'); + const [showEasterEggs] = useSetting(settingsAtom, 'showEasterEggs'); const resolvedMode = mode ?? resolveUnreadBadgeMode({ @@ -71,7 +83,7 @@ export function UnreadBadge({ highlight, count, dm, mode }: UnreadBadgeProps) { > {resolvedMode === 'count' && ( - {millify(count)} + {formatUnreadBadgeCount(count, showEasterEggs)} )} diff --git a/src/app/features/settings/cosmetics/Themes.tsx b/src/app/features/settings/cosmetics/Themes.tsx index a9d8e020c..0ec660aa0 100644 --- a/src/app/features/settings/cosmetics/Themes.tsx +++ b/src/app/features/settings/cosmetics/Themes.tsx @@ -434,6 +434,7 @@ function PageZoomInput() { } export function Appearance() { const [twitterEmoji, setTwitterEmoji] = useSetting(settingsAtom, 'twitterEmoji'); + const [showEasterEggs, setShowEasterEggs] = useSetting(settingsAtom, 'showEasterEggs'); return ( @@ -450,6 +451,14 @@ export function Appearance() { /> + + } + /> + + } /> diff --git a/src/app/state/settings.ts b/src/app/state/settings.ts index bf805f346..779c99db7 100644 --- a/src/app/state/settings.ts +++ b/src/app/state/settings.ts @@ -88,6 +88,7 @@ export interface Settings { showUnreadCounts: boolean; badgeCountDMsOnly: boolean; showPingCounts: boolean; + showEasterEggs: boolean; hideReads: boolean; emojiSuggestThreshold: number; underlineLinks: boolean; @@ -174,6 +175,7 @@ const defaultSettings: Settings = { showUnreadCounts: false, badgeCountDMsOnly: true, showPingCounts: true, + showEasterEggs: true, hideReads: false, emojiSuggestThreshold: 2, underlineLinks: false,