forked from mozilla/pontoon
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Award badges on file uploads (mozilla#3522)
- Loading branch information
1 parent
402bf74
commit 89f390a
Showing
6 changed files
with
92 additions
and
29 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,49 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { NotificationMessage } from '~/context/Notification'; | ||
import { BadgeTooltipMessage } from '~/context/BadgeTooltip'; | ||
|
||
export function useNotifications() { | ||
const [message, setMessage] = useState<NotificationMessage | null>(null); | ||
const [badgeMessage, setBadgeMessage] = useState<BadgeTooltipMessage | null>( | ||
null, | ||
); | ||
|
||
// If there's a notification in the DOM set by Django, show it. | ||
// Note that we only show it once, and only when the UI has already | ||
// been rendered, to make sure users do see it. | ||
useEffect(() => { | ||
const rootElt = document.getElementById('root'); | ||
if (rootElt?.dataset.notifications) { | ||
const notifications = JSON.parse(rootElt.dataset.notifications); | ||
if (notifications.length > 0) { | ||
// Extra tags from the Django messages framework are combined | ||
// with the level into a single string as notification.type | ||
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 }; | ||
} |