Skip to content

Commit dc54ed4

Browse files
committed
Add notif for New Vote Page
1 parent 0415d86 commit dc54ed4

File tree

5 files changed

+101
-7
lines changed

5 files changed

+101
-7
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {getPrivateUser} from 'shared/utils'
2+
import {createSupabaseDirectClient, SupabaseDirectClient} from 'shared/supabase/init'
3+
import {Notification} from 'common/notifications'
4+
import {insertNotificationToSupabase} from 'shared/supabase/notifications'
5+
import {getProfile} from 'shared/profiles/supabase'
6+
import {tryCatch} from "common/util/try-catch";
7+
import {Row} from "common/supabase/utils";
8+
9+
export const createVoteNotificationAll = async () => {
10+
const pg = createSupabaseDirectClient()
11+
const {data: users, error} = await tryCatch(
12+
pg.many<Row<'users'>>('select * from users')
13+
)
14+
15+
if (error) {
16+
console.error('Error fetching users', error)
17+
return
18+
}
19+
20+
if (!users) {
21+
console.error('No users found')
22+
return
23+
}
24+
25+
for (const user of users) {
26+
try {
27+
await createVoteNotification(user, pg)
28+
} catch (e) {
29+
console.error('Failed to create vote notification', e, user)
30+
}
31+
}
32+
33+
return {
34+
success: true,
35+
}
36+
}
37+
38+
export const createVoteNotification = async (user: Row<'users'>, pg: SupabaseDirectClient) => {
39+
const targetPrivateUser = await getPrivateUser(user.id)
40+
const profile = await getProfile(user.id)
41+
42+
if (!targetPrivateUser || !profile) return
43+
44+
const id = `vote-${Date.now()}`
45+
const notification: Notification = {
46+
id,
47+
userId: user.id,
48+
reason: 'vote',
49+
createdTime: Date.now(),
50+
isSeen: false,
51+
sourceId: '',
52+
sourceType: 'vote',
53+
sourceUpdateType: 'created',
54+
sourceUserName: '',
55+
sourceUserUsername: 'vote',
56+
sourceSlug: '/vote',
57+
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',
58+
title: 'New Proposals & Votes Page',
59+
sourceText: 'Create proposals and vote on other people\'s suggestions!',
60+
}
61+
console.log('notification', user.username)
62+
return await insertNotificationToSupabase(notification, pg)
63+
}

common/src/notifications.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Row, SupabaseClient } from 'common/supabase/utils'
33
export type Notification = {
44
id: string
55
userId: string
6+
title?: string
67
reasonText?: string
78
reason: string
89
createdTime: number

web/components/notification-items.tsx

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function NotificationItem(props: { notification: Notification }) {
3535
} else if (reason === 'new_profile_ship') {
3636
return <ProfileShipNotification {...params} />
3737
} else {
38-
return <>unknown notification: {sourceType}</>
38+
return <BaseNotification {...params}/>
3939
}
4040
}
4141

@@ -76,6 +76,34 @@ export function CommentOnProfileNotification(props: {
7676
)
7777
}
7878

79+
export function BaseNotification(props: {
80+
notification: Notification
81+
highlighted: boolean
82+
setHighlighted: (highlighted: boolean) => void
83+
}) {
84+
const { notification, highlighted, setHighlighted } = props
85+
return (
86+
<NotificationFrame
87+
notification={notification}
88+
highlighted={highlighted}
89+
setHighlighted={setHighlighted}
90+
icon={
91+
<AvatarNotificationIcon notification={notification} />
92+
}
93+
subtitle={
94+
<div className="line-clamp-2">
95+
<Linkify text={notification.sourceText} />
96+
</div>
97+
}
98+
link={notification.sourceSlug}
99+
>
100+
<div className="line-clamp-3">
101+
<span>{notification.title}</span>
102+
</div>
103+
</NotificationFrame>
104+
)
105+
}
106+
79107
export function NewMatchNotification(props: {
80108
notification: Notification
81109
highlighted: boolean
@@ -250,7 +278,7 @@ export function NotificationUserLink(props: {
250278

251279
export function AvatarNotificationIcon(props: {
252280
notification: Notification
253-
symbol: string | ReactNode
281+
symbol?: string | ReactNode
254282
}) {
255283
const { notification, symbol } = props
256284
const { sourceUserName, sourceUserAvatarUrl, sourceUserUsername } =

web/hooks/use-notifications.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function useGroupedUnseenNotifications(
2020

2121
export function useGroupedNotifications(user: User, selectTypes?: string[]) {
2222
const notifications = useNotifications(user.id)?.filter((n) =>
23-
selectTypes?.includes(n.sourceType)
23+
selectTypes ? selectTypes.includes(n.sourceType) : true
2424
)
2525
const sortedNotifications = notifications
2626
? sortBy(notifications, (n) => -n.createdTime)

web/pages/notifications.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {type Notification, NOTIFICATION_TYPES_TO_SELECT, NOTIFICATIONS_PER_PAGE,} from 'common/notifications'
1+
import {type Notification, NOTIFICATIONS_PER_PAGE,} from 'common/notifications'
22
import {PrivateUser, type User} from 'common/src/user'
33
import {
44
notification_destination_types,
@@ -25,7 +25,7 @@ import {useRedirectIfSignedOut} from "web/hooks/use-redirect-if-signed-out";
2525
export default function NotificationsPage() {
2626
useRedirectIfSignedOut()
2727
return (
28-
<PageBase trackPageView={'notifications page'}>
28+
<PageBase trackPageView={'notifications page'} className={'mx-4'}>
2929
<NoSEO/>
3030
<Title>Updates</Title>
3131
<UncontrolledTabs
@@ -49,8 +49,10 @@ function LoadedNotificationsContent(props: { user: User }) {
4949
const {user} = props
5050
const privateUser = usePrivateUser()
5151

52-
const {groupedNotifications, mostRecentNotification} =
53-
useGroupedNotifications(user, NOTIFICATION_TYPES_TO_SELECT)
52+
const {groupedNotifications, mostRecentNotification} = useGroupedNotifications(
53+
user,
54+
// NOTIFICATION_TYPES_TO_SELECT
55+
)
5456

5557
const [page, setPage] = useState(0)
5658

0 commit comments

Comments
 (0)