-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factored out notification useEffect hook, added extra tag for badge messages
- Loading branch information
Harmit Goswami
authored and
Harmit Goswami
committed
Jan 15, 2025
1 parent
a0b431c
commit ecb761a
Showing
10 changed files
with
103 additions
and
91 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { | ||
NotificationMessage, | ||
BadgeTooltipMessage, | ||
} from '../context/Notification'; | ||
|
||
export function useNotifications() { | ||
const [message, setMessage] = useState<NotificationMessage | null>(null); | ||
const [badgeMessage, setBadgeMessage] = useState<BadgeTooltipMessage | null>( | ||
null, | ||
); | ||
|
||
useEffect(() => { | ||
const rootElt = document.getElementById('root'); | ||
if (rootElt?.dataset.notifications) { | ||
const notifications = JSON.parse(rootElt.dataset.notifications); | ||
if (notifications.length > 0) { | ||
const generalNotification = notifications.find( | ||
(notification: { type: string }) => | ||
notification.type !== 'badge info', | ||
); | ||
const badgeNotification = notifications.find( | ||
(notification: { type: string }) => | ||
notification.type === 'badge info', | ||
); | ||
|
||
if (generalNotification) { | ||
setMessage({ | ||
type: generalNotification.type, | ||
content: generalNotification.content, | ||
}); | ||
} | ||
|
||
if (badgeNotification) { | ||
const badgeData = JSON.parse(badgeNotification.content); | ||
setBadgeMessage({ | ||
badgeName: badgeData.name || null, | ||
badgeLevel: badgeData.level || null, | ||
}); | ||
} | ||
} | ||
} | ||
}, []); | ||
|
||
return { message, setMessage, badgeMessage, setBadgeMessage }; | ||
} |
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