Skip to content

Commit 18cb4e7

Browse files
committed
Refactor notification process
1 parent e07cb7f commit 18cb4e7

5 files changed

Lines changed: 52 additions & 51 deletions

File tree

backend/api/src/create-vote-notification.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,25 @@ import {insertNotificationToSupabase} from 'shared/supabase/notifications'
44
import {tryCatch} from "common/util/try-catch";
55
import {Row} from "common/supabase/utils";
66

7-
export const createVoteNotificationAll = async () => {
7+
export const createVoteNotifications = async () => {
8+
const createdTime = Date.now();
9+
const id = `vote-${createdTime}`
10+
const notification: Notification = {
11+
id,
12+
userId: 'todo',
13+
createdTime: createdTime,
14+
isSeen: false,
15+
sourceType: 'info',
16+
sourceUpdateType: 'created',
17+
sourceSlug: '/vote',
18+
sourceUserAvatarUrl: 'https://firebasestorage.googleapis.com/v0/b/compass-130ba.firebasestorage.app/o/misc%2Fvote-icon-design-free-vector.jpg?alt=media&token=f70b6d14-0511-49b2-830d-e7cabf7bb751',
19+
title: 'New Proposals & Votes Page',
20+
sourceText: 'Create proposals and vote on other people\'s suggestions!',
21+
}
22+
return await createNotifications(notification)
23+
}
24+
25+
export const createNotifications = async (notification: Notification) => {
826
const pg = createSupabaseDirectClient()
927
const {data: users, error} = await tryCatch(
1028
pg.many<Row<'users'>>('select * from users')
@@ -22,9 +40,9 @@ export const createVoteNotificationAll = async () => {
2240

2341
for (const user of users) {
2442
try {
25-
await createVoteNotification(user, pg)
43+
await createNotification(user, notification, pg)
2644
} catch (e) {
27-
console.error('Failed to create vote notification', e, user)
45+
console.error('Failed to create notification', e, user)
2846
}
2947
}
3048

@@ -33,24 +51,8 @@ export const createVoteNotificationAll = async () => {
3351
}
3452
}
3553

36-
export const createVoteNotification = async (user: Row<'users'>, pg: SupabaseDirectClient) => {
37-
const id = `vote-${Date.now()}`
38-
const notification: Notification = {
39-
id,
40-
userId: user.id,
41-
reason: 'vote',
42-
createdTime: Date.now(),
43-
isSeen: false,
44-
sourceId: '',
45-
sourceType: 'vote',
46-
sourceUpdateType: 'created',
47-
sourceUserName: '',
48-
sourceUserUsername: 'vote',
49-
sourceSlug: '/vote',
50-
sourceUserAvatarUrl: 'https://firebasestorage.googleapis.com/v0/b/compass-130ba.firebasestorage.app/o/misc%2Fvote-icon-design-free-vector.jpg?alt=media&token=f70b6d14-0511-49b2-830d-e7cabf7bb751',
51-
title: 'New Proposals & Votes Page',
52-
sourceText: 'Create proposals and vote on other people\'s suggestions!',
53-
}
54+
export const createNotification = async (user: Row<'users'>, notification: Notification, pg: SupabaseDirectClient) => {
55+
notification.userId = user.id
5456
console.log('notification', user.username)
5557
return await insertNotificationToSupabase(notification, pg)
5658
}

common/src/notifications.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ export type Notification = {
55
userId: string
66
title?: string
77
reasonText?: string
8-
reason: string
8+
reason?: string
99
createdTime: number
1010
viewTime?: number
1111
isSeen: boolean
1212

13-
sourceId: string
13+
sourceId?: string
1414
sourceType: string
1515
sourceUpdateType?: 'created' | 'updated' | 'deleted'
1616

17-
sourceUserName: string
18-
sourceUserUsername: string
19-
sourceUserAvatarUrl: string
17+
sourceUserName?: string
18+
sourceUserUsername?: string
19+
sourceUserAvatarUrl?: string
2020
sourceText: string
2121
data?: { [key: string]: any }
2222

web/components/multi-user-reaction-link.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export function MultiUserReactionModal(props: {
2020
className="w-full"
2121
user={{
2222
id: notif.userId,
23-
name: notif.sourceUserName,
24-
username: notif.sourceUserUsername,
23+
name: notif.sourceUserName ?? 'Name',
24+
username: notif.sourceUserUsername ?? 'Username',
2525
}}
2626
/>
2727
))}

web/components/notification-items.tsx

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,66 +39,66 @@ export function NotificationItem(props: { notification: Notification }) {
3939
}
4040
}
4141

42-
export function CommentOnProfileNotification(props: {
42+
export function BaseNotification(props: {
4343
notification: Notification
4444
highlighted: boolean
4545
setHighlighted: (highlighted: boolean) => void
46-
isChildOfGroup?: boolean
4746
}) {
48-
const { notification, isChildOfGroup, highlighted, setHighlighted } = props
49-
const { sourceUserName, sourceUserUsername, sourceText } = notification
50-
const reasonText = `commented `
47+
const { notification, highlighted, setHighlighted } = props
5148
return (
5249
<NotificationFrame
5350
notification={notification}
54-
isChildOfGroup={isChildOfGroup}
5551
highlighted={highlighted}
5652
setHighlighted={setHighlighted}
5753
icon={
58-
<AvatarNotificationIcon notification={notification} symbol={'💬'} />
54+
<AvatarNotificationIcon notification={notification} />
5955
}
6056
subtitle={
6157
<div className="line-clamp-2">
62-
<Linkify text={sourceText} />
58+
<Linkify text={notification.sourceText} />
6359
</div>
6460
}
6561
link={notification.sourceSlug}
6662
>
6763
<div className="line-clamp-3">
68-
<NotificationUserLink
69-
name={sourceUserName}
70-
username={sourceUserUsername}
71-
/>{' '}
72-
{reasonText}
73-
{!isChildOfGroup && <span>on your profile</span>}
64+
<span>{notification.title}</span>
7465
</div>
7566
</NotificationFrame>
7667
)
7768
}
7869

79-
export function BaseNotification(props: {
70+
export function CommentOnProfileNotification(props: {
8071
notification: Notification
8172
highlighted: boolean
8273
setHighlighted: (highlighted: boolean) => void
74+
isChildOfGroup?: boolean
8375
}) {
84-
const { notification, highlighted, setHighlighted } = props
76+
const { notification, isChildOfGroup, highlighted, setHighlighted } = props
77+
const { sourceUserName, sourceUserUsername, sourceText } = notification
78+
const reasonText = `commented `
8579
return (
8680
<NotificationFrame
8781
notification={notification}
82+
isChildOfGroup={isChildOfGroup}
8883
highlighted={highlighted}
8984
setHighlighted={setHighlighted}
9085
icon={
91-
<AvatarNotificationIcon notification={notification} />
86+
<AvatarNotificationIcon notification={notification} symbol={'💬'} />
9287
}
9388
subtitle={
9489
<div className="line-clamp-2">
95-
<Linkify text={notification.sourceText} />
90+
<Linkify text={sourceText} />
9691
</div>
9792
}
9893
link={notification.sourceSlug}
9994
>
10095
<div className="line-clamp-3">
101-
<span>{notification.title}</span>
96+
<NotificationUserLink
97+
name={sourceUserName}
98+
username={sourceUserUsername}
99+
/>{' '}
100+
{reasonText}
101+
{!isChildOfGroup && <span>on your profile</span>}
102102
</div>
103103
</NotificationFrame>
104104
)
@@ -281,9 +281,8 @@ export function AvatarNotificationIcon(props: {
281281
symbol?: string | ReactNode
282282
}) {
283283
const { notification, symbol } = props
284-
const { sourceUserName, sourceUserAvatarUrl, sourceUserUsername } =
285-
notification
286-
const href = `/${sourceUserUsername}`
284+
const { sourceUserName, sourceUserAvatarUrl, sourceUserUsername, sourceSlug } = notification
285+
const href = !!sourceUserUsername ? `/${sourceUserUsername}` : sourceSlug ?? '/'
287286
return (
288287
<div className="relative">
289288
<Link

web/hooks/use-notifications.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function groupGeneralNotifications(
4848
if (!sortedNotifications) return []
4949

5050
const groupedNotificationsByDayAndContract = groupBy(
51-
sortedNotifications.filter((n) => !except.includes(n.reason)),
51+
sortedNotifications.filter((n) => !except.includes(n.reason ?? '')),
5252
(n) =>
5353
new Date(n.createdTime).toDateString() +
5454
(n.sourceType === 'betting_streak_bonus' || n.reason === 'quest_payout'

0 commit comments

Comments
 (0)