mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 09:01:17 -08:00
feat: cap unread badges and something extra :)
This commit is contained in:
6 files changed
+156
-52
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
default: patch
|
||||
---
|
||||
|
||||
Cap unread badge numbers at `1k+`, and something extra :)
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
@@ -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(<UnreadBadge count={1001} mode="count" />);
|
||||
|
||||
expect(screen.getByText(':3')).toBeInTheDocument();
|
||||
expect(screen.queryByText('1k+')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders k+ when easter eggs are disabled', () => {
|
||||
settings.showEasterEggs = false;
|
||||
|
||||
render(<UnreadBadge count={1001} mode="count" />);
|
||||
|
||||
expect(screen.getByText('1k+')).toBeInTheDocument();
|
||||
expect(screen.queryByText(':3')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -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' && (
|
||||
<Text as="span" size="L400">
|
||||
{millify(count)}
|
||||
{formatUnreadBadgeCount(count, showEasterEggs)}
|
||||
</Text>
|
||||
)}
|
||||
</Badge>
|
||||
|
||||
@@ -434,6 +434,7 @@ function PageZoomInput() {
|
||||
}
|
||||
export function Appearance() {
|
||||
const [twitterEmoji, setTwitterEmoji] = useSetting(settingsAtom, 'twitterEmoji');
|
||||
const [showEasterEggs, setShowEasterEggs] = useSetting(settingsAtom, 'showEasterEggs');
|
||||
|
||||
return (
|
||||
<Box direction="Column" gap="700">
|
||||
@@ -450,6 +451,14 @@ export function Appearance() {
|
||||
/>
|
||||
</SequenceCard>
|
||||
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile
|
||||
title="Show Easter Eggs"
|
||||
description="Lets the interface keep a little mischief turned on."
|
||||
after={<Switch variant="Primary" value={showEasterEggs} onChange={setShowEasterEggs} />}
|
||||
/>
|
||||
</SequenceCard>
|
||||
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile title="Page Zoom" after={<PageZoomInput />} />
|
||||
</SequenceCard>
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user