-
Notifications
You must be signed in to change notification settings - Fork 13
feat: handle onesignal push notification initialization #1247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kushagrasarathe
wants to merge
31
commits into
peanut-wallet-dev
Choose a base branch
from
feat/notifications-fixed
base: peanut-wallet-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
0797396
feat: update setup component for notification modal
kushagrasarathe dd31bc9
feat: notifications pop-up, ui and hook
kushagrasarathe c2bbb0d
feat: use react sdk methods from onesignal
kushagrasarathe 4577606
Merge branch 'peanut-wallet-dev' into feat/notifications
kushagrasarathe 5ec9b93
Merge branch 'peanut-wallet-dev' into feat/notifications
kushagrasarathe 9623812
Merge branch 'peanut-wallet-dev' into feat/notifications
kushagrasarathe 358d7d7
Merge branch 'peanut-wallet-dev' into feat/notifications
kushagrasarathe bc7a06a
feat: resolve cr comments
kushagrasarathe 0b2f69b
Merge branch 'peanut-wallet-dev' into feat/notifications
kushagrasarathe 8e01811
feat: notifications routes sevice
kushagrasarathe 835f7b0
feat: homepage notification bell for navigation
kushagrasarathe c1baee2
feat: notificaitons page
kushagrasarathe b295e82
fix: resolve more cr comments
kushagrasarathe 88af85c
fix: notif modal ux copy
kushagrasarathe 940e0bb
Merge branch 'feat/notifications-fixed' into feat/notifications-center
kushagrasarathe 345febd
chore: format
kushagrasarathe 3eff7ad
fix: pr review comments #1
kushagrasarathe e89783c
fix: more pr review comments #2
kushagrasarathe 351e9fe
Merge branch 'feat/notifications-fixed' into feat/notifications-center
kushagrasarathe c9774fb
feat: add error state in notis page
kushagrasarathe 2ffda52
chore: style
kushagrasarathe c49c5d7
fix: resolve cr comment
kushagrasarathe e352b5f
Merge pull request #1248 from peanutprotocol/feat/notifications-center
kushagrasarathe 9c8dad4
feat: auto mark notif on read when user visits notifs page
kushagrasarathe a5a9397
Merge branch 'peanut-wallet-dev' into feat/notifications-fixed
kushagrasarathe 1b8f4ae
Merge branch 'peanut-wallet-dev' into feat/notifications-fixed
kushagrasarathe 99358f3
fix: update notifications setup modal ui
kushagrasarathe bc8e290
feat: use banners corousel for notifs banner
kushagrasarathe 276f416
fix: notif hub responsiveness
kushagrasarathe 7281c38
feat: mark notif read on click
kushagrasarathe 8ae0b8d
Merge branch 'peanut-wallet-dev' into feat/notifications-fixed
kushagrasarathe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,3 +54,8 @@ export PROMO_LIST={} | |
export NEXT_PUBLIC_VAPID_PUBLIC_KEY= | ||
export VAPID_PRIVATE_KEY= | ||
export NEXT_PUBLIC_VAPID_SUBJECT="mailto:[email protected]" | ||
|
||
# one signal | ||
export NEXT_PUBLIC_ONESIGNAL_APP_ID= | ||
export NEXT_PUBLIC_SAFARI_WEB_ID= | ||
export NEXT_PUBLIC_ONESIGNAL_WEBHOOK= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// use the stable v16 service worker path per onesignal docs | ||
// note: onesignal does not publish minor-pinned sw paths; use v16 channel | ||
importScripts('https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.sw.js') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
'use client' | ||
|
||
import PageContainer from '@/components/0_Bruddle/PageContainer' | ||
import Card, { CardPosition } from '@/components/Global/Card' | ||
import NavHeader from '@/components/Global/NavHeader' | ||
import PeanutLoading from '@/components/Global/PeanutLoading' | ||
import { notificationsApi, type InAppItem } from '@/services/notifications' | ||
import { formatGroupHeaderDate, getDateGroup, getDateGroupKey } from '@/utils/dateGrouping.utils' | ||
import React, { useEffect, useMemo, useRef, useState } from 'react' | ||
import Image from 'next/image' | ||
import { PEANUTMAN_LOGO } from '@/assets' | ||
import Link from 'next/link' | ||
import EmptyState from '@/components/Global/EmptyStates/EmptyState' | ||
import { Button } from '@/components/0_Bruddle' | ||
|
||
export default function NotificationsPage() { | ||
const loadingRef = useRef<HTMLDivElement>(null) | ||
const [notifications, setNotifications] = useState<InAppItem[]>([]) | ||
const [nextPageCursor, setNextPageCursor] = useState<string | null>(null) | ||
const [isInitialLoading, setIsInitialLoading] = useState(true) | ||
const [isLoadingMore, setIsLoadingMore] = useState(false) | ||
const [errorMessage, setErrorMessage] = useState<string | null>(null) | ||
const [loadMoreError, setLoadMoreError] = useState<string | null>(null) | ||
const [markedIds, setMarkedIds] = useState<Set<string>>(new Set()) | ||
|
||
const loadInitialPage = async () => { | ||
// load the first page of notifications | ||
setIsInitialLoading(true) | ||
setErrorMessage(null) | ||
try { | ||
const res = await notificationsApi.list({ limit: 20 }) | ||
setNotifications(res.items) | ||
setNextPageCursor(res.nextCursor) | ||
} catch (_e) { | ||
// set an error state if api fails | ||
setErrorMessage('Failed to load notifications. Please try again.') | ||
setNotifications([]) | ||
setNextPageCursor(null) | ||
} finally { | ||
setIsInitialLoading(false) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
void loadInitialPage() | ||
}, []) | ||
|
||
useEffect(() => { | ||
const observer = new IntersectionObserver( | ||
(entries) => { | ||
const target = entries[0] | ||
if (target.isIntersecting && nextPageCursor && !isLoadingMore) { | ||
void loadNextPage() | ||
} | ||
}, | ||
{ threshold: 0.1 } | ||
) | ||
const element = loadingRef.current | ||
if (element) observer.observe(element) | ||
return () => { | ||
if (element) observer.unobserve(element) | ||
} | ||
}, [nextPageCursor, isLoadingMore]) | ||
|
||
const loadNextPage = async () => { | ||
// load the next page when the sentinel enters the viewport | ||
if (!nextPageCursor) return | ||
setIsLoadingMore(true) | ||
setLoadMoreError(null) | ||
try { | ||
const res = await notificationsApi.list({ limit: 20, cursor: nextPageCursor }) | ||
setNotifications((prev) => [...prev, ...res.items]) | ||
setNextPageCursor(res.nextCursor) | ||
} catch (_e) { | ||
// show error below the list and allow retry | ||
setLoadMoreError('Failed to load more notifications. Tap to retry.') | ||
} finally { | ||
setIsLoadingMore(false) | ||
} | ||
} | ||
|
||
const grouped = useMemo(() => { | ||
const today = new Date() | ||
const groups: Array<{ header: string; items: InAppItem[] }> = [] | ||
let lastKey: string | null = null | ||
for (const notif of notifications) { | ||
const d = new Date(notif.createdAt) | ||
const grp = getDateGroup(d, today) | ||
const key = getDateGroupKey(d, grp) | ||
const header = formatGroupHeaderDate(d, grp, today) | ||
if (key !== lastKey) { | ||
groups.push({ header, items: [notif] }) | ||
lastKey = key | ||
} else { | ||
groups[groups.length - 1].items.push(notif) | ||
} | ||
} | ||
return groups | ||
}, [notifications]) | ||
|
||
// mark notification as read when clicked | ||
const handleNotificationClick = (notifId: string) => { | ||
const notif = notifications.find((n) => n.id === notifId) | ||
if (!notif || notif.state.readAt || markedIds.has(notifId)) return | ||
|
||
// optimistically update ui | ||
setNotifications((prev) => | ||
prev.map((it) => | ||
it.id === notifId ? { ...it, state: { ...it.state, readAt: new Date().toISOString() } } : it | ||
) | ||
) | ||
setMarkedIds((prev) => new Set([...prev, notifId])) | ||
|
||
// fire-and-forget api call to mark as read | ||
void notificationsApi | ||
.markRead([notifId]) | ||
.then(() => { | ||
// broadcast update so other ui (e.g. bell icon) can refresh unread count | ||
if (typeof window !== 'undefined') { | ||
window.dispatchEvent(new CustomEvent('notifications:updated')) | ||
} | ||
}) | ||
.catch(() => {}) | ||
} | ||
|
||
if (isInitialLoading && notifications.length === 0) { | ||
return <PeanutLoading /> | ||
} | ||
|
||
return ( | ||
<PageContainer> | ||
<div className="h-full w-full space-y-6"> | ||
<NavHeader title="Notifications" /> | ||
<div className="h-full w-full"> | ||
{/* error banner for partial failures */} | ||
{!isInitialLoading && notifications.length > 0 && errorMessage && ( | ||
<div className="px-2"> | ||
<EmptyState title="Something went wrong" description={errorMessage ?? ''} icon="bell" /> | ||
<div className="mt-4 flex justify-center"> | ||
<Button shadowSize="4" onClick={() => void loadInitialPage()}> | ||
Retry | ||
</Button> | ||
</div> | ||
</div> | ||
)} | ||
|
||
{!!grouped.length ? ( | ||
grouped.map((group, groupIdx) => { | ||
return ( | ||
<React.Fragment key={groupIdx}> | ||
<div className="mb-2 mt-4 px-1 text-sm font-semibold capitalize"> | ||
{group.header} | ||
</div> | ||
{group.items.map((notif, idx) => { | ||
let position: CardPosition = 'middle' | ||
if (group.items.length === 1) position = 'single' | ||
else if (idx === 0) position = 'first' | ||
else if (idx === group.items.length - 1) position = 'last' | ||
const href = notif.ctaDeeplink | ||
return ( | ||
<Card | ||
key={notif.id} | ||
position={position} | ||
className="relative flex min-h-16 w-full items-center justify-center px-5 py-2" | ||
data-notification-id={notif.id} | ||
> | ||
<Link | ||
href={href ?? ''} | ||
className="flex w-full items-center gap-3" | ||
data-notification-id={notif.id} | ||
onClick={() => handleNotificationClick(notif.id)} | ||
> | ||
<Image | ||
src={notif.iconUrl ?? PEANUTMAN_LOGO} | ||
alt="icon" | ||
width={32} | ||
height={32} | ||
className="size-8 min-w-8 self-center" | ||
/> | ||
|
||
<div className="flex min-w-0 flex-col"> | ||
<div className="flex items-center gap-2"> | ||
<div className="line-clamp-2 font-semibold"> | ||
{notif.title} | ||
</div> | ||
</div> | ||
{notif.body ? ( | ||
<div className="line-clamp-2 text-sm text-gray-600"> | ||
{notif.body} | ||
</div> | ||
) : null} | ||
</div> | ||
</Link> | ||
{!notif.state.readAt ? ( | ||
<span className="absolute right-2 top-2 size-2 rounded-full bg-orange-2" /> | ||
) : null} | ||
</Card> | ||
) | ||
})} | ||
</React.Fragment> | ||
) | ||
}) | ||
) : ( | ||
<div> | ||
{errorMessage ? ( | ||
<div className="px-2"> | ||
<EmptyState title="Something went wrong" description={errorMessage} icon="bell" /> | ||
<div className="mt-4 flex justify-center"> | ||
<Button shadowSize="4" onClick={() => void loadInitialPage()}> | ||
Retry | ||
</Button> | ||
</div> | ||
</div> | ||
) : ( | ||
<EmptyState | ||
title="No notifications yet!" | ||
description="You will see your notifications here." | ||
icon="bell" | ||
/> | ||
)} | ||
</div> | ||
)} | ||
<div ref={loadingRef} className="w-full py-4"> | ||
{isLoadingMore && <div className="w-full text-center">Loading more...</div>} | ||
{loadMoreError && ( | ||
<div className="w-full text-center text-sm text-red"> | ||
<button onClick={() => void loadNextPage()} className="underline"> | ||
{loadMoreError} | ||
</button> | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
</PageContainer> | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { FC, SVGProps } from 'react' | ||
|
||
export const BellIcon: FC<SVGProps<SVGSVGElement>> = (props) => ( | ||
<svg width="18" height="17" viewBox="0 0 18 17" fill="none" xmlns="http://www.w3.org/2000/svg" {...props}> | ||
<path | ||
d="M9.00003 16.5488C9.9026 16.5488 10.6411 15.8104 10.6411 14.9078H7.35901C7.35901 15.8104 8.09747 16.5488 9.00003 16.5488ZM13.9231 11.6258V7.52319C13.9231 5.00421 12.5857 2.89549 10.2308 2.33755V1.7796C10.2308 1.09857 9.68106 0.548828 9.00003 0.548828C8.31901 0.548828 7.76926 1.09857 7.76926 1.7796V2.33755C5.4226 2.89549 4.07696 4.99601 4.07696 7.52319V11.6258L2.43593 13.2668V14.0873H15.5641V13.2668L13.9231 11.6258ZM12.2821 12.4463H5.71798V7.52319C5.71798 5.48832 6.95696 3.83088 9.00003 3.83088C11.0431 3.83088 12.2821 5.48832 12.2821 7.52319V12.4463ZM5.37337 1.84524L4.20003 0.671905C2.2308 2.17344 0.934391 4.48729 0.819519 7.11293H2.46054C2.58362 4.93857 3.69952 3.03498 5.37337 1.84524ZM15.5395 7.11293H17.1805C17.0575 4.48729 15.7611 2.17344 13.8 0.671905L12.6349 1.84524C14.2923 3.03498 15.4164 4.93857 15.5395 7.11293Z" | ||
fill="currentColor" | ||
/> | ||
</svg> | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid double-instantiating
useNotifications
; modal never clearsHome
storesshowPermissionModal
from its own hook instance, butSetupNotificationsModal
spins up another copy ofuseNotifications
. When the user clicks “Not now” / allow, the modal updates only its local state, leaving the parent’sshowPermissionModal
stuck attrue
. The banner/modal remains mounted (just hidden) and the add-money prompt is now permanently suppressed by the guard at Line 206.Lift the hook instance into
Home
and pass the state/handlers down so both sites share the same state container, e.g.:Then update
SetupNotificationsModal
to consume the injected props instead of callinguseNotifications
again. Without this change the notification permission flow can’t recover.Also applies to: 264-265
🤖 Prompt for AI Agents