-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #515 from us3r-network/u3-dev
beta.20240201
- Loading branch information
Showing
28 changed files
with
1,119 additions
and
107 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -10,6 +10,9 @@ | |
], | ||
"u3-dev": [ | ||
"us3r-u3-dev" | ||
], | ||
"u3-pwa": [ | ||
"us3r-u3-pwa" | ||
] | ||
} | ||
} | ||
|
This file contains 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 |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
"unfollowed", | ||
"unpinup", | ||
"upsert", | ||
"Upvote", | ||
"viem", | ||
"Warpcast", | ||
"Whatsnew", | ||
|
This file contains 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 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 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,12 @@ | ||
/* eslint-disable */ | ||
|
||
// listen for push event | ||
self.addEventListener('push', (event) => { | ||
let { title, body, icon } = event.data.json(); | ||
if (!title || title === 'undefined') title = 'U3 - Your Web3 Gateway'; | ||
if (!body) return; | ||
self.registration.showNotification(title, { | ||
body, | ||
icon: icon || `logo192.png`, | ||
}); | ||
}); |
This file contains 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,12 @@ | ||
/* eslint-disable */ | ||
|
||
// listen for push event | ||
self.addEventListener('push', (event) => { | ||
let { title, body, icon } = event.data.json(); | ||
if (!title || title === 'undefined') title = 'U3 - Your Web3 Gateway'; | ||
if (!body) return; | ||
self.registration.showNotification(title, { | ||
body, | ||
icon: icon || `logo192.png`, | ||
}); | ||
}); |
This file contains 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,25 @@ | ||
export default function DegenTip({ className }: { className: string }) { | ||
return ( | ||
<svg | ||
width="789" | ||
height="668" | ||
viewBox="0 0 789 668" | ||
fill="none" | ||
xmlns="http://www.w3.org/2000/svg" | ||
className={className} | ||
> | ||
<path | ||
fillRule="evenodd" | ||
clipRule="evenodd" | ||
d="M115.185 411.477L118.367 469.444C118.575 473.392 120.055 477.107 122.552 480.15C140.007 501.282 218.264 575.014 394.616 575.014C570.968 575.014 648.278 502.234 666.38 480.544C669.108 477.27 670.68 473.206 670.934 468.933L674.089 411.517C695.084 399.997 716.758 386.98 716.758 386.98C750.835 368.518 788.866 395.038 788.935 433.936C789.051 496.87 739.877 561.545 673.548 602.301C598.758 648.258 487.117 667.324 394.664 667.324C302.211 667.324 190.57 648.258 115.78 602.301C49.4513 561.545 0.277187 496.893 0.392781 433.936C0.462138 395.038 38.4929 368.518 72.5702 386.98C72.5702 386.98 94.207 399.965 115.185 411.477Z" | ||
fill="#A36EFD" | ||
/> | ||
<path | ||
fillRule="evenodd" | ||
clipRule="evenodd" | ||
d="M394.641 0.113525C538.834 0.113525 577.929 3.48079 636.558 10.2154H636.535C663.561 13.3272 685.224 33.438 683.212 60.6782L661.616 354.872C654.858 356.83 647.488 359.303 639.223 362.077C595.905 376.615 527.997 399.404 394.64 399.404C261.283 399.404 193.376 376.615 150.057 362.077C141.784 359.3 134.407 356.825 127.643 354.866L106.047 60.6782C104.059 33.438 125.652 12.8395 152.724 10.2154C210.637 4.59548 270.932 0.113525 394.641 0.113525ZM137.991 495.835L138.067 496.869L139.557 497.212C139.024 496.748 138.502 496.289 137.991 495.835ZM649.85 497.178L651.193 496.869L651.262 495.928C650.8 496.341 650.329 496.757 649.85 497.178Z" | ||
fill="#A36EFD" | ||
/> | ||
</svg> | ||
); | ||
} |
This file contains 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
182 changes: 182 additions & 0 deletions
182
apps/u3/src/components/notification/PushNotificationsToogleBtn.tsx
This file contains 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,182 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { toast } from 'react-toastify'; | ||
import WebPushService from '@/utils/pwa/WebPushService'; | ||
import { sendNotification } from '@/utils/pwa/notification'; | ||
import { | ||
NotificationSetting, | ||
NotificationSettingType, | ||
} from '@/services/notification/types/notification-settings'; | ||
import useLogin from '@/hooks/shared/useLogin'; | ||
import { | ||
addNotificationSetting, | ||
fethNotificationSettings, | ||
updateNotificationSetting, | ||
} from '@/services/notification/api/notification-settings'; | ||
import { ApiRespCode } from '@/services/shared/types'; | ||
import { useFarcasterCtx } from '@/contexts/social/FarcasterCtx'; | ||
import Switch from '../common/switch/Switch'; | ||
import ColorButton from '../common/button/ColorButton'; | ||
|
||
export function NotificationSettingsGroup() { | ||
const { | ||
currFid, | ||
isConnected: isLoginFarcaster, | ||
currUserInfo: farcasterUserInfo, | ||
openFarcasterQR, | ||
} = useFarcasterCtx(); | ||
const { isLogin } = useLogin(); | ||
const [settings, setSettings] = useState<NotificationSetting[]>([]); | ||
const [loadingTypes, setLoadingTypes] = useState<NotificationSettingType[]>( | ||
[] | ||
); | ||
const [settingsLoading, setSettingsLoading] = useState(true); | ||
useEffect(() => { | ||
if (isLogin) { | ||
setSettingsLoading(true); | ||
fethNotificationSettings() | ||
.then((res) => { | ||
setSettings(res?.data?.data || []); | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
setSettings([]); | ||
}) | ||
.finally(() => { | ||
setSettingsLoading(false); | ||
}); | ||
} else { | ||
setSettings([]); | ||
setSettingsLoading(false); | ||
} | ||
}, [isLogin]); | ||
|
||
const upsertSetting = async (setting: Partial<NotificationSetting>) => { | ||
if (!isLogin) return; | ||
const index = settings.findIndex((s) => s.type === setting.type); | ||
|
||
try { | ||
setLoadingTypes((prev) => { | ||
if (prev.includes(setting.type)) return prev; | ||
return [...prev, setting.type]; | ||
}); | ||
if (index >= 0 && settings[index]?.id) { | ||
// update | ||
const res = await updateNotificationSetting(settings[index].id, { | ||
...settings[index], | ||
...setting, | ||
}); | ||
if (res.data.code === ApiRespCode.SUCCESS) { | ||
setSettings((prev) => { | ||
return [ | ||
...prev.slice(0, index), | ||
{ | ||
...prev[index], | ||
...setting, | ||
}, | ||
...prev.slice(index + 1), | ||
] as NotificationSetting[]; | ||
}); | ||
} | ||
} else { | ||
// add | ||
const res = await addNotificationSetting({ | ||
type: setting.type, | ||
...setting, | ||
}); | ||
if (res.data.code === ApiRespCode.SUCCESS) { | ||
setSettings((prev) => { | ||
return [...prev, res.data.data] as NotificationSetting[]; | ||
}); | ||
} | ||
} | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(error); | ||
throw error; | ||
} finally { | ||
setLoadingTypes((prev) => { | ||
if (prev.includes(setting.type)) { | ||
return prev.filter((t) => t !== setting.type); | ||
} | ||
return prev; | ||
}); | ||
} | ||
}; | ||
|
||
const webpushSubscribed = settings.some( | ||
(setting) => | ||
setting.type === NotificationSettingType.WEB_PUSH && | ||
setting?.enable === true && | ||
!!setting?.subscription | ||
); | ||
|
||
const webpushLoading = loadingTypes.includes( | ||
NotificationSettingType.WEB_PUSH | ||
); | ||
const webpushDisabled = settingsLoading || webpushLoading; | ||
|
||
const handlePushChange = async (checked: boolean) => { | ||
try { | ||
if (!checked) { | ||
const payload = await WebPushService.unsubscribe(); | ||
await upsertSetting({ | ||
type: NotificationSettingType.WEB_PUSH, | ||
fid: currFid ? String(currFid) : undefined, | ||
enable: false, | ||
subscription: payload ? JSON.stringify(payload) : undefined, | ||
}); | ||
} else { | ||
if (!WebPushService.hasPermission()) { | ||
await WebPushService.requestPermission(); | ||
} | ||
let subscription = await WebPushService.getSubscription(); | ||
if (!subscription) { | ||
subscription = await WebPushService.subscribe(); | ||
} | ||
|
||
await upsertSetting({ | ||
type: NotificationSettingType.WEB_PUSH, | ||
fid: currFid ? String(currFid) : undefined, | ||
enable: true, | ||
subscription: JSON.stringify(subscription), | ||
}); | ||
sendNotification(`Subscribed to notifications`); | ||
} | ||
} catch (error) { | ||
toast.error(error.message); | ||
console.error(error); | ||
} | ||
}; | ||
|
||
return ( | ||
// <div> | ||
// <p>Web Push</p> | ||
<> | ||
<Switch | ||
onColor="#5057AA" | ||
disabled={webpushDisabled} | ||
checked={webpushSubscribed} | ||
onChange={handlePushChange} | ||
/> | ||
{(() => { | ||
if (webpushLoading) { | ||
if (webpushSubscribed) { | ||
return 'Unsubscribing...'; | ||
} | ||
return 'Subscribing...'; | ||
} | ||
return 'Subscribe Notifications'; | ||
})()} | ||
|
||
{/* {!(isLoginFarcaster && farcasterUserInfo) && ( | ||
<ColorButton | ||
className="h-[24px] text-[12px] font-normal" | ||
onClick={() => openFarcasterQR()} | ||
> | ||
Login Farcaster | ||
</ColorButton> | ||
)} */} | ||
</> | ||
// </div> | ||
); | ||
} |
Oops, something went wrong.