Skip to content

Commit f2c266b

Browse files
fix(desktop): route notification clicks to thread context (#790)
Signed-off-by: tlongwell-block <[email protected]>
1 parent 33e37de commit f2c266b

12 files changed

Lines changed: 87 additions & 29 deletions

‎desktop/src/app/AppShell.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
useOpenDmMutation,
2222
} from "@/features/channels/hooks";
2323
import { useUnreadChannels } from "@/features/channels/useUnreadChannels";
24+
import { getThreadReference } from "@/features/messages/lib/threading";
2425
import { useThreadFollows } from "@/features/messages/lib/useThreadFollows";
2526
import {
2627
useHomeFeedNotifications,
@@ -107,6 +108,7 @@ function toSearchHit(target: DesktopNotificationTarget): SearchHit | null {
107108
channelName: target.channelName ?? null,
108109
createdAt: target.createdAt ?? Math.floor(Date.now() / 1_000),
109110
score: 0,
111+
threadRootId: target.threadRootId ?? null,
110112
};
111113
}
112114

@@ -228,6 +230,8 @@ export function AppShell() {
228230
: content
229231
: "New message";
230232

233+
const threadRootId = getThreadReference(event.tags).rootId ?? null;
234+
231235
void sendDesktopNotification({
232236
title: channelName,
233237
body,
@@ -239,6 +243,7 @@ export function AppShell() {
239243
eventId: event.id,
240244
kind: event.kind,
241245
pubkey: event.pubkey,
246+
threadRootId,
242247
},
243248
}).then((didSend) => {
244249
if (!didSend) return;

‎desktop/src/app/navigation/resolveSearchHitDestination.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type SearchHitDestination =
88
kind: "channel";
99
channelId: string;
1010
messageId?: string;
11+
threadRootId?: string | null;
1112
}
1213
| {
1314
kind: "forum-post";
@@ -68,5 +69,6 @@ export async function resolveSearchHitDestination(
6869
kind: "channel",
6970
channelId: hit.channelId,
7071
messageId: hit.eventId,
72+
threadRootId: hit.threadRootId ?? null,
7173
};
7274
}

‎desktop/src/app/navigation/useAppNavigation.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export function useAppNavigation() {
135135
options?: {
136136
messageId?: string;
137137
replace?: boolean;
138+
threadRootId?: string | null;
138139
},
139140
) =>
140141
commitNavigation(
@@ -143,7 +144,12 @@ export function useAppNavigation() {
143144
params: {
144145
channelId,
145146
},
146-
search: options?.messageId ? { messageId: options.messageId } : {},
147+
search: options?.messageId
148+
? {
149+
messageId: options.messageId,
150+
threadRootId: options.threadRootId ?? undefined,
151+
}
152+
: {},
147153
},
148154
{
149155
replace: options?.replace,
@@ -217,6 +223,7 @@ export function useAppNavigation() {
217223

218224
return goChannel(destination.channelId, {
219225
messageId: destination.messageId,
226+
threadRootId: destination.threadRootId,
220227
});
221228
},
222229
[goChannel, goForumPost],

‎desktop/src/app/routes/ChannelRouteScreen.tsx‎

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ type ChannelRouteScreenProps = {
1515
selectedPostId: string | null;
1616
targetMessageId: string | null;
1717
targetReplyId: string | null;
18+
targetThreadRootId: string | null;
1819
};
1920

2021
export function ChannelRouteScreen({
2122
channelId,
2223
selectedPostId,
2324
targetMessageId,
2425
targetReplyId,
26+
targetThreadRootId,
2527
}: ChannelRouteScreenProps) {
2628
const { closeForumPost, goForumPost } = useAppNavigation();
2729
const channelsQuery = useChannelsQuery();
@@ -30,42 +32,61 @@ export function ChannelRouteScreen({
3032
const channels = channelsQuery.data ?? [];
3133
const activeChannel =
3234
channels.find((channel) => channel.id === channelId) ?? null;
33-
const [targetMessageEvent, setTargetMessageEvent] =
34-
React.useState<RelayEvent | null>(() =>
35-
getCachedSearchHitEvent(targetMessageId),
36-
);
35+
const [targetMessageEvents, setTargetMessageEvents] = React.useState<
36+
RelayEvent[]
37+
>(() => {
38+
const cachedTarget = getCachedSearchHitEvent(targetMessageId);
39+
return cachedTarget ? [cachedTarget] : [];
40+
});
3741

3842
React.useEffect(() => {
3943
let isCancelled = false;
4044

4145
if (!targetMessageId || selectedPostId) {
42-
setTargetMessageEvent(null);
46+
setTargetMessageEvents([]);
4347
return () => {
4448
isCancelled = true;
4549
};
4650
}
4751

48-
setTargetMessageEvent(getCachedSearchHitEvent(targetMessageId));
49-
void getEventById(targetMessageId)
50-
.then((event) => {
51-
if (!isCancelled) {
52-
setTargetMessageEvent(event);
52+
const cachedTarget = getCachedSearchHitEvent(targetMessageId);
53+
setTargetMessageEvents(cachedTarget ? [cachedTarget] : []);
54+
55+
const eventIds = [
56+
targetMessageId,
57+
targetThreadRootId && targetThreadRootId !== targetMessageId
58+
? targetThreadRootId
59+
: null,
60+
].filter((eventId): eventId is string => eventId !== null);
61+
62+
void Promise.all(
63+
eventIds.map(async (eventId) => {
64+
try {
65+
return await getEventById(eventId);
66+
} catch (error) {
67+
console.error("Failed to load route event", eventId, error);
68+
return null;
5369
}
54-
})
55-
.catch((error) => {
56-
if (!isCancelled) {
57-
console.error(
58-
"Failed to load route target event",
59-
targetMessageId,
60-
error,
70+
}),
71+
).then((events) => {
72+
if (!isCancelled) {
73+
setTargetMessageEvents((currentEvents) => {
74+
const fetchedEvents = events.filter(
75+
(event): event is RelayEvent => event !== null,
6176
);
62-
}
63-
});
77+
const eventsById = new Map<string, RelayEvent>();
78+
for (const event of [...currentEvents, ...fetchedEvents]) {
79+
eventsById.set(event.id, event);
80+
}
81+
return Array.from(eventsById.values());
82+
});
83+
}
84+
});
6485

6586
return () => {
6687
isCancelled = true;
6788
};
68-
}, [selectedPostId, targetMessageId]);
89+
}, [selectedPostId, targetMessageId, targetThreadRootId]);
6990

7091
if (channelsQuery.isPending && !activeChannel) {
7192
return (
@@ -89,7 +110,7 @@ export function ChannelRouteScreen({
89110
}}
90111
selectedForumPostId={selectedPostId}
91112
targetForumReplyId={targetReplyId}
92-
targetMessageEvent={targetMessageEvent}
113+
targetMessageEvents={targetMessageEvents}
93114
targetMessageId={targetMessageId}
94115
/>
95116
);

‎desktop/src/app/routes/channels.$channelId.posts.$postId.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ function ForumPostRouteComponent() {
4141
selectedPostId={postId}
4242
targetMessageId={null}
4343
targetReplyId={search.replyId ?? null}
44+
targetThreadRootId={null}
4445
/>
4546
</React.Suspense>
4647
);

‎desktop/src/app/routes/channels.$channelId.tsx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ViewLoadingFallback } from "@/shared/ui/ViewLoadingFallback";
55

66
type ChannelRouteSearch = {
77
messageId?: string;
8+
threadRootId?: string;
89
};
910

1011
function validateChannelSearch(
@@ -15,6 +16,10 @@ function validateChannelSearch(
1516
typeof search.messageId === "string" && search.messageId.length > 0
1617
? search.messageId
1718
: undefined,
19+
threadRootId:
20+
typeof search.threadRootId === "string" && search.threadRootId.length > 0
21+
? search.threadRootId
22+
: undefined,
1823
};
1924
}
2025

@@ -41,6 +46,7 @@ function ChannelRouteComponent() {
4146
selectedPostId={null}
4247
targetMessageId={search.messageId ?? null}
4348
targetReplyId={null}
49+
targetThreadRootId={search.threadRootId ?? null}
4450
/>
4551
</React.Suspense>
4652
);

‎desktop/src/features/channels/ui/ChannelScreen.tsx‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type ChannelScreenProps = {
6666
onSelectForumPost: (postId: string) => void;
6767
selectedForumPostId: string | null;
6868
targetForumReplyId: string | null;
69-
targetMessageEvent: RelayEvent | null;
69+
targetMessageEvents: RelayEvent[];
7070
targetMessageId: string | null;
7171
};
7272

@@ -78,7 +78,7 @@ export function ChannelScreen({
7878
onSelectForumPost,
7979
selectedForumPostId,
8080
targetForumReplyId,
81-
targetMessageEvent,
81+
targetMessageEvents,
8282
targetMessageId,
8383
}: ChannelScreenProps) {
8484
const {
@@ -144,9 +144,11 @@ export function ChannelScreen({
144144

145145
const resolvedMessages = React.useMemo(() => {
146146
const currentMessages = messagesQuery.data ?? [];
147-
if (!activeChannel || !targetMessageEvent) return currentMessages;
148-
return mergeMessages(currentMessages, targetMessageEvent);
149-
}, [activeChannel, messagesQuery.data, targetMessageEvent]);
147+
if (!activeChannel || targetMessageEvents.length === 0) {
148+
return currentMessages;
149+
}
150+
return targetMessageEvents.reduce(mergeMessages, currentMessages);
151+
}, [activeChannel, messagesQuery.data, targetMessageEvents]);
150152
const messageAuthorPubkeys = React.useMemo(
151153
() => collectMessageAuthorPubkeys(resolvedMessages),
152154
[resolvedMessages],

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type DesktopNotificationTarget = {
1818
eventId: string | null;
1919
kind: number | null;
2020
pubkey?: string;
21+
threadRootId?: string | null;
2122
};
2223

2324
type DesktopNotificationPayload = {
@@ -73,6 +74,8 @@ function parseNotificationTarget(
7374
const kind = typeof candidate.kind === "number" ? candidate.kind : null;
7475
const pubkey =
7576
typeof candidate.pubkey === "string" ? candidate.pubkey : undefined;
77+
const threadRootId =
78+
typeof candidate.threadRootId === "string" ? candidate.threadRootId : null;
7679

7780
if (!channelId && !eventId) {
7881
return null;
@@ -86,6 +89,7 @@ function parseNotificationTarget(
8689
eventId,
8790
kind,
8891
pubkey,
92+
threadRootId,
8993
};
9094
}
9195

‎desktop/src/features/notifications/use-feed-desktop-notifications.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
truncatePubkey,
66
type UserProfileLookup,
77
} from "@/features/profile/lib/identity";
8+
import { getThreadReference } from "@/features/messages/lib/threading";
89
import type { FeedItem, HomeFeedResponse } from "@/shared/api/types";
910
import {
1011
collectHomeAlertItems,
@@ -99,6 +100,7 @@ export function useFeedDesktopNotifications(
99100

100101
const deliverFeedNotification = React.useEffectEvent(
101102
async (item: FeedItem, senderName?: string) => {
103+
const threadRootId = getThreadReference(item.tags).rootId ?? null;
102104
const didSend = await sendDesktopNotification({
103105
body: notificationBody(item),
104106
target: {
@@ -109,6 +111,7 @@ export function useFeedDesktopNotifications(
109111
eventId: item.id,
110112
kind: item.kind,
111113
pubkey: item.pubkey,
114+
threadRootId,
112115
},
113116
title: notificationTitle(item, senderName),
114117
});

‎desktop/src/shared/api/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ export type SearchHit = {
231231
channelName: string | null;
232232
createdAt: number;
233233
score: number;
234+
threadRootId?: string | null;
234235
};
235236

236237
export type SearchMessagesResponse = {

0 commit comments

Comments
 (0)