-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathuseDesktopNotifications.ts
154 lines (127 loc) · 4.27 KB
/
useDesktopNotifications.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { createDevLogger } from '@tloncorp/shared';
import * as api from '@tloncorp/shared/api';
import * as db from '@tloncorp/shared/db';
import { getTextContent } from '@tloncorp/shared/urbit';
import { useCallback, useEffect, useRef } from 'react';
import { useIsElectron } from './useIsElectron';
const logger = createDevLogger('useDesktopNotifications', false);
interface NotificationData {
type: string;
channelId?: string;
groupId?: string;
}
export default function useDesktopNotifications(isClientReady: boolean) {
const processedNotifications = useRef<Set<string>>(new Set());
const isElectron = useIsElectron();
const processActivityEvent = useCallback(
async (activityEvent: db.ActivityEvent) => {
if (!isElectron || !window.electronAPI) {
return;
}
const notificationKey = `${activityEvent.channelId}-${activityEvent.timestamp}`;
if (processedNotifications.current.has(notificationKey)) {
return;
}
processedNotifications.current.add(notificationKey);
// Limit the size of the set to avoid memory leaks
if (processedNotifications.current.size > 100) {
// Keep only the most recent 50 notification keys
processedNotifications.current = new Set(
Array.from(processedNotifications.current).slice(-50)
);
}
let body = '';
if (!activityEvent.channelId) {
logger.error('No channel ID in activity event:', activityEvent);
return;
}
try {
const channel = await db.getChannelWithRelations({
id: activityEvent.channelId,
});
if (activityEvent.content) {
body =
getTextContent(activityEvent.content as api.PostContent) ||
'New message';
} else {
body = 'New message';
}
const contactId = activityEvent.authorId;
const contact = contactId
? await db.getContact({ id: contactId })
: null;
if (!channel) return;
let title = channel.title
? channel.title
: contact?.nickname
? contact.nickname
: contactId || 'New message';
const contactName = contact?.peerNickname
? contact.peerNickname
: contact?.customNickname
? contact.customNickname
: contactId;
if (activityEvent.groupId) {
const group = await db.getGroup({ id: activityEvent.groupId });
if (group) {
if (activityEvent.content) {
body = `${contactName}: ${getTextContent(activityEvent.content as api.PostContent)}`;
title = title + ` in ${group.title}`;
} else {
body = `New message in ${group.title}`;
}
}
}
logger.log('Showing desktop notification:', title);
window.electronAPI.showNotification({
title,
body,
data: {
type: 'channel',
channelId: activityEvent.channelId,
groupId: channel.groupId,
},
});
} catch (error) {
logger.error(
'Error processing channel activity for notifications',
error
);
}
},
[]
);
const handleNotificationClick = useCallback(
async (data: NotificationData) => {
if (!data || !data.channelId) return;
try {
logger.log('Notification clicked:', data);
await api.markChatRead(data.channelId);
// In the future, we could add navigation logic here
} catch (error) {
logger.error('Error handling notification click', error);
}
},
[]
);
useEffect(() => {
if (!isElectron || !window.electronAPI || !isClientReady) return;
const handleActivityEvent = (event: api.ActivityEvent) => {
logger.log('Activity event:', event);
if (event.type === 'addActivityEvent' && event.events[0].shouldNotify) {
processActivityEvent(event.events[0]);
}
};
api.subscribeToActivity(handleActivityEvent);
const unsubscribeNotificationClick =
window.electronAPI.onNotificationClicked(handleNotificationClick);
return () => {
unsubscribeNotificationClick();
};
}, [
processActivityEvent,
handleNotificationClick,
isClientReady,
isElectron,
]);
}