-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into deploy/amp
- Loading branch information
Showing
31 changed files
with
2,482 additions
and
807 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
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
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
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
20 changes: 19 additions & 1 deletion
20
src/components/auth/ProfileModal/contents/LogoutContent.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
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
118 changes: 118 additions & 0 deletions
118
src/components/auth/ProfileModal/contents/notifications/PushNotificationContent.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,118 @@ | ||
import Button from '@/components/Button' | ||
import { useLinkFcm } from '@/services/api/notifications/mutation' | ||
import { getMessageToken } from '@/services/firebase/messaging' | ||
import { LocalStorage } from '@/utils/storage' | ||
import { useEffect, useState } from 'react' | ||
import { ContentProps } from '../../types' | ||
|
||
const FCM_PUSH_NOTIFICATION_STORAGE_KEY = 'push-notification-fcm-token' | ||
export const fcmPushNotificationStorage = new LocalStorage( | ||
() => FCM_PUSH_NOTIFICATION_STORAGE_KEY | ||
) | ||
|
||
export default function PushNotificationContent(props: ContentProps) { | ||
const isNotificationNotSupported = typeof Notification === 'undefined' | ||
|
||
const [isRegistered, setIsRegistered] = useState(false) | ||
|
||
useEffect(() => { | ||
const storedFcmToken = fcmPushNotificationStorage.get() | ||
setIsRegistered(!!storedFcmToken) | ||
}, [isRegistered]) | ||
|
||
if (isNotificationNotSupported) { | ||
return ( | ||
<Button disabled size='lg'> | ||
Unsupported Browser Notifications | ||
</Button> | ||
) | ||
} | ||
|
||
const permission = Notification.permission | ||
if (permission === 'granted' && isRegistered) { | ||
return ( | ||
<DisableNotificationButton setIsRegistered={setIsRegistered} {...props} /> | ||
) | ||
} | ||
|
||
return ( | ||
<EnableNotificationButton setIsRegistered={setIsRegistered} {...props} /> | ||
) | ||
} | ||
|
||
type NotificationButtonProps = ContentProps & { | ||
setIsRegistered: (v: boolean) => void | ||
} | ||
|
||
function DisableNotificationButton({ | ||
address, | ||
setIsRegistered, | ||
}: NotificationButtonProps) { | ||
const [isGettingToken, setIsGettingToken] = useState(false) | ||
|
||
const { mutate: unlinkFcm, isLoading: isUnlinking } = useLinkFcm({ | ||
onSuccess: (data) => { | ||
if (!data) throw new Error('Error in disabling notification request') | ||
|
||
// FCM Token Disabled. | ||
fcmPushNotificationStorage.remove() | ||
setIsRegistered(false) | ||
}, | ||
}) | ||
|
||
const isLoading = isUnlinking || isGettingToken | ||
|
||
const handleClickDisable = async () => { | ||
if (!address) return | ||
setIsGettingToken(true) | ||
const fcmToken = await getMessageToken() | ||
setIsGettingToken(false) | ||
if (!fcmToken) return | ||
|
||
unlinkFcm({ address, fcmToken, action: 'unlink' }) | ||
} | ||
|
||
return ( | ||
<Button size='lg' onClick={handleClickDisable} isLoading={isLoading}> | ||
Disable Notifications | ||
</Button> | ||
) | ||
} | ||
|
||
function EnableNotificationButton({ | ||
address, | ||
setIsRegistered, | ||
}: NotificationButtonProps) { | ||
const [isGettingToken, setIsGettingToken] = useState(false) | ||
const [fcmToken, setFcmToken] = useState<string | undefined>() | ||
|
||
const { mutate: linkFcm, isLoading: isLinking } = useLinkFcm({ | ||
onSuccess: (data) => { | ||
// FCM Token Enabled. | ||
if (fcmToken) { | ||
fcmPushNotificationStorage.set(fcmToken) | ||
setIsRegistered(true) | ||
} | ||
}, | ||
}) | ||
|
||
const isLoading = isLinking || isGettingToken | ||
|
||
const handleClickEnable = async () => { | ||
if (!address) return | ||
setIsGettingToken(true) | ||
const fcmToken = await getMessageToken() | ||
setIsGettingToken(false) | ||
console.log('FCM Token', fcmToken) | ||
if (!fcmToken) return | ||
|
||
setFcmToken(fcmToken) | ||
linkFcm({ address, fcmToken, action: 'link' }) | ||
} | ||
|
||
return ( | ||
<Button size='lg' onClick={handleClickEnable} isLoading={isLoading}> | ||
Enable Notifications | ||
</Button> | ||
) | ||
} |
Oops, something went wrong.