Skip to content

Commit dd5592e

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
andauthored
refactor(desktop): consolidate notification helpers and add channel names to toasts (#1286)
Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
1 parent 25396c0 commit dd5592e

5 files changed

Lines changed: 111 additions & 45 deletions

File tree

‎desktop/src/app/AppShell.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,12 @@ export function AppShell() {
154154
const { feedProfilesQuery, homeFeedQuery, notificationSettings } =
155155
useHomeFeedNotifications(identityQuery.data?.pubkey);
156156
const feedItemState = useFeedItemState(identityQuery.data?.pubkey);
157+
const channelsQuery = useChannelsQuery();
158+
const channels = channelsQuery.data ?? [];
157159
useReminderNotifications(
158160
identityQuery.data?.pubkey,
159161
notificationSettings.settings,
162+
channels,
160163
);
161164
const refetchHomeFeedFromLiveSignal = React.useEffectEvent(() => {
162165
void homeFeedQuery.refetch();
@@ -165,9 +168,7 @@ export function AppShell() {
165168
identityQuery.data?.pubkey,
166169
refetchHomeFeedFromLiveSignal,
167170
);
168-
const channelsQuery = useChannelsQuery();
169171
const { refetch: refetchChannels } = channelsQuery;
170-
const channels = channelsQuery.data ?? [];
171172
const channelsErrorMessage =
172173
channelsQuery.error instanceof Error
173174
? channelsQuery.error.message

‎desktop/src/app/useAppShellDesktopNotifications.ts‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import {
1313
revealDesktopAppWindow,
1414
sendDesktopNotification,
1515
} from "@/features/notifications/lib/desktop";
16+
import {
17+
formatNotificationTitle,
18+
truncateNotificationBody,
19+
} from "@/features/notifications/lib/notificationFormat";
1620
import {
1721
playNotificationSound,
1822
resolveSlotSound,
@@ -54,13 +58,7 @@ export function useAppShellDesktopNotifications({
5458
}
5559

5660
const channelName = channel.name?.trim() || "Direct message";
57-
const content = event.content.trim();
58-
const body =
59-
content.length > 0
60-
? content.length > 140
61-
? `${content.slice(0, 137).trimEnd()}...`
62-
: content
63-
: "New message";
61+
const body = truncateNotificationBody(event.content, "New message");
6462
const threadRootId = getThreadReference(event.tags).rootId ?? null;
6563

6664
void sendDesktopNotification({
@@ -100,19 +98,16 @@ export function useAppShellDesktopNotifications({
10098
return;
10199
}
102100

103-
const channel = channels.find((entry) => entry.id === channelId);
104-
const channelName = channel?.name?.trim() || "Thread";
105-
const content = event.content.trim();
106-
const body =
107-
content.length > 0
108-
? content.length > 140
109-
? `${content.slice(0, 137).trimEnd()}...`
110-
: content
111-
: "New reply";
101+
const resolvedChannel = channels.find((c) => c.id === channelId);
102+
const channelName = resolvedChannel?.name?.trim() ?? null;
103+
// channelLabel is "#name" for the toast title; channelName is the raw
104+
// name stored in the navigation target for click-through routing.
105+
const channelLabel = channelName ? `#${channelName}` : null;
106+
const body = truncateNotificationBody(event.content, "New reply");
112107
const threadRootId = getThreadReference(event.tags).rootId ?? null;
113108

114109
void sendDesktopNotification({
115-
title: `Reply in ${channelName}`,
110+
title: formatNotificationTitle({ prefix: "Reply", channelLabel }),
116111
body,
117112
target: {
118113
channelId,

‎desktop/src/features/notifications/lib/feed.ts‎

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,47 @@
11
import type { FeedItem, HomeFeedResponse } from "@/shared/api/types";
2-
3-
const FEED_NOTIFICATION_BODY_MAX_LENGTH = 140;
2+
import {
3+
formatNotificationTitle,
4+
truncateNotificationBody,
5+
} from "@/features/notifications/lib/notificationFormat";
46

57
export function notificationTitle(item: FeedItem, senderName?: string) {
6-
const channelLabel = item.channelName.trim()
7-
? ` in #${item.channelName.trim()}`
8-
: "";
8+
const channelLabel =
9+
item.channelType !== "dm" && item.channelName.trim()
10+
? `#${item.channelName.trim()}`
11+
: null;
912

1013
if (item.channelType === "dm") {
1114
return senderName || "Direct message";
1215
}
1316

1417
if (item.category === "mention") {
15-
return senderName
16-
? `${senderName} mentioned you${channelLabel}`
17-
: `@Mention${channelLabel}`;
18+
return formatNotificationTitle({
19+
prefix: senderName ? `${senderName} mentioned you` : "@Mention",
20+
channelLabel,
21+
});
1822
}
1923

2024
if (item.kind === 46010) {
21-
return senderName
22-
? `${senderName} requested approval${channelLabel}`
23-
: `Approval Requested${channelLabel}`;
25+
return formatNotificationTitle({
26+
prefix: senderName
27+
? `${senderName} requested approval`
28+
: "Approval Requested",
29+
channelLabel,
30+
});
2431
}
2532

26-
return senderName
27-
? `${senderName}${channelLabel}`
28-
: `Needs Action${channelLabel}`;
33+
return formatNotificationTitle({
34+
prefix: senderName ? senderName : "Needs Action",
35+
channelLabel,
36+
});
2937
}
3038

3139
export function notificationBody(item: FeedItem) {
32-
const content = item.content.trim();
3340
const fallback =
3441
item.kind === 46010
3542
? "A workflow is waiting for your approval."
3643
: "Something in Buzz needs your attention.";
37-
const body = content.length > 0 ? content : fallback;
38-
39-
if (body.length <= FEED_NOTIFICATION_BODY_MAX_LENGTH) {
40-
return body;
41-
}
42-
43-
return `${body.slice(0, FEED_NOTIFICATION_BODY_MAX_LENGTH - 3).trimEnd()}...`;
44+
return truncateNotificationBody(item.content, fallback);
4445
}
4546

4647
export function collectHomeAlertItems(feed: HomeFeedResponse) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const NOTIFICATION_BODY_MAX_LENGTH = 140;
2+
3+
/**
4+
* Resolve a channel's display label for use in notification titles.
5+
*
6+
* Returns `"#channelName"` when the channel is found and has a non-empty name,
7+
* or `null` when the channelId is absent or the channel is not yet in the list
8+
* (e.g. channels query hasn't resolved — the caller should fall back gracefully
9+
* rather than blocking the toast).
10+
*/
11+
export function resolveNotificationChannelLabel(
12+
channelId: string | null | undefined,
13+
channels: ReadonlyArray<{ id: string; name?: string | null }>,
14+
): string | null {
15+
if (!channelId) return null;
16+
const channel = channels.find((c) => c.id === channelId);
17+
const name = channel?.name?.trim();
18+
return name ? `#${name}` : null;
19+
}
20+
21+
/**
22+
* Truncate notification body text to {@link NOTIFICATION_BODY_MAX_LENGTH}
23+
* characters, appending "..." when truncated. Returns `fallback` when
24+
* `content` is blank after trimming.
25+
*/
26+
export function truncateNotificationBody(
27+
content: string,
28+
fallback: string,
29+
): string {
30+
const trimmed = content.trim();
31+
if (trimmed.length === 0) return fallback;
32+
if (trimmed.length <= NOTIFICATION_BODY_MAX_LENGTH) return trimmed;
33+
return `${trimmed.slice(0, NOTIFICATION_BODY_MAX_LENGTH - 3).trimEnd()}...`;
34+
}
35+
36+
/**
37+
* Format a notification title with optional channel context.
38+
*
39+
* - With a channel label: `"prefix in #channel"`
40+
* - Without: `"prefix"`
41+
*/
42+
export function formatNotificationTitle(opts: {
43+
prefix: string;
44+
channelLabel: string | null;
45+
}): string {
46+
return opts.channelLabel
47+
? `${opts.prefix} in ${opts.channelLabel}`
48+
: opts.prefix;
49+
}

‎desktop/src/features/reminders/useReminderNotifications.ts‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import {
1212
sendDesktopNotification,
1313
} from "@/features/notifications/lib/desktop";
1414
import type { NotificationSettings } from "@/features/notifications/hooks";
15+
import {
16+
formatNotificationTitle,
17+
resolveNotificationChannelLabel,
18+
truncateNotificationBody,
19+
} from "@/features/notifications/lib/notificationFormat";
1520
import {
1621
playNotificationSound,
1722
resolveSlotSound,
@@ -53,13 +58,16 @@ function readWatermark(pubkey: string): number {
5358
export function useReminderNotifications(
5459
pubkey: string | undefined,
5560
settings: NotificationSettings,
61+
channels: ReadonlyArray<{ id: string; name?: string | null }>,
5662
): void {
5763
const reminders = useRemindersQuery(pubkey).data;
5864
const queryClient = useQueryClient();
5965
const remindersRef = React.useRef<Reminder[]>([]);
6066
remindersRef.current = reminders ?? [];
6167
const settingsRef = React.useRef(settings);
6268
settingsRef.current = settings;
69+
const channelsRef = React.useRef(channels);
70+
channelsRef.current = channels;
6371

6472
// Track whether the query has resolved at least once. On mount,
6573
// useRemindersQuery is still loading (data === undefined), so
@@ -78,15 +86,27 @@ export function useReminderNotifications(
7886
return;
7987
}
8088

89+
// For a single reminder, try to resolve the channel name from its target.
90+
// Multiple reminders may span different channels, so we omit channel context
91+
// in that case and let the body count speak for itself.
92+
const channelLabel =
93+
due.length === 1
94+
? resolveNotificationChannelLabel(
95+
due[0].content.target?.channelId ?? null,
96+
channelsRef.current,
97+
)
98+
: null;
99+
81100
const body =
82101
due.length === 1
83-
? (due[0].content.target?.preview ??
84-
due[0].content.note ??
85-
"A reminder is waiting")
102+
? truncateNotificationBody(
103+
due[0].content.target?.preview ?? due[0].content.note ?? "",
104+
"A reminder is waiting",
105+
)
86106
: `${due.length} reminders are due`;
87107

88108
void sendDesktopNotification({
89-
title: "Reminder due",
109+
title: formatNotificationTitle({ prefix: "Reminder due", channelLabel }),
90110
body,
91111
}).then((didSend) => {
92112
if (!didSend) return;

0 commit comments

Comments
 (0)