forked from janus-idp/backstage-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make the notifications icon active in dynamic plugin (janus-idp#…
…1111) FLPATH-905: make the notifications icon active in dynamic plugin
- Loading branch information
1 parent
24d477c
commit bc98491
Showing
10 changed files
with
165 additions
and
145 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
99 changes: 99 additions & 0 deletions
99
plugins/notifications/src/components/NotificationsActiveIcon/NotificationsActiveIcon.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,99 @@ | ||
import React from 'react'; | ||
|
||
import { configApiRef, useApi } from '@backstage/core-plugin-api'; | ||
|
||
import { Badge, Tooltip } from '@material-ui/core'; | ||
import NotificationsIcon from '@material-ui/icons/Notifications'; | ||
import NotificationsOffIcon from '@material-ui/icons/NotificationsOff'; | ||
|
||
import { notificationsApiRef } from '../../api'; | ||
import { DefaultPollingIntervalMs } from '../../constants'; | ||
import { Notification } from '../../openapi'; | ||
import { usePollingEffect } from '../usePollingEffect'; | ||
import { SystemNotificationAlert } from './SystemNotificationAlert'; | ||
|
||
const NotificationsErrorIcon = () => ( | ||
<Tooltip title="Failed to load notifications"> | ||
<NotificationsOffIcon /> | ||
</Tooltip> | ||
); | ||
|
||
/** | ||
* Dynamic plugins recently do not support passing configuration | ||
* to icons or making the left-side menu item texts active (so far strings only). | ||
* | ||
* This Icon component tries to workaround these limitations but will be subject of | ||
* change as the extension points by dynamic plugins will evolve. | ||
*/ | ||
export const NotificationsActiveIcon = () => { | ||
const notificationsApi = useApi(notificationsApiRef); | ||
const configApi = useApi(configApiRef); | ||
|
||
let pollingInterval = configApi.getOptionalNumber( | ||
'notifications.pollingIntervalMs', | ||
); | ||
if (pollingInterval === undefined) { | ||
pollingInterval = DefaultPollingIntervalMs; | ||
} | ||
|
||
const [error, setError] = React.useState<Error | undefined>(undefined); | ||
const [unreadCount, setUnreadCount] = React.useState(0); | ||
const [pageLoadingTime] = React.useState(new Date(Date.now())); | ||
const [lastSystemWideNotification, setLastSystemWideNotification] = | ||
React.useState<Notification>(); | ||
const [closedNotificationId, setClosedNotificationId] = | ||
React.useState<string>(); | ||
|
||
const pollCallback = React.useCallback(async () => { | ||
try { | ||
setUnreadCount( | ||
await notificationsApi.getNotificationsCount({ | ||
read: false, | ||
messageScope: 'user', | ||
}), | ||
); | ||
|
||
const data = await notificationsApi.getNotifications({ | ||
pageSize: 1, | ||
pageNumber: 1, | ||
createdAfter: pageLoadingTime, | ||
orderBy: 'created', | ||
orderByDirec: 'desc', | ||
messageScope: 'system', | ||
}); | ||
|
||
setLastSystemWideNotification(data?.[0]); | ||
} catch (e: unknown) { | ||
setError(e as Error); | ||
} | ||
}, [notificationsApi, pageLoadingTime]); | ||
|
||
usePollingEffect(pollCallback, [], pollingInterval); | ||
|
||
if (!!error) { | ||
return <NotificationsErrorIcon />; | ||
} | ||
|
||
if (unreadCount) { | ||
return ( | ||
<> | ||
<Badge color="secondary" variant="dot" overlap="circular"> | ||
<NotificationsIcon /> | ||
</Badge> | ||
|
||
{lastSystemWideNotification && | ||
!lastSystemWideNotification.readByUser && | ||
closedNotificationId !== lastSystemWideNotification.id && ( | ||
<SystemNotificationAlert | ||
message={lastSystemWideNotification.title} | ||
onCloseNotification={() => | ||
setClosedNotificationId(lastSystemWideNotification.id) | ||
} | ||
/> | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
return <NotificationsIcon />; | ||
}; |
53 changes: 53 additions & 0 deletions
53
plugins/notifications/src/components/NotificationsActiveIcon/SystemNotificationAlert.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,53 @@ | ||
import React from 'react'; | ||
|
||
import { useRouteRef } from '@backstage/core-plugin-api'; | ||
|
||
import { IconButton, Link, makeStyles, Snackbar } from '@material-ui/core'; | ||
import CloseIcon from '@material-ui/icons/Close'; | ||
|
||
import { notificationsRootRouteRef } from '../../routes'; | ||
|
||
const useStyles = makeStyles(_theme => ({ | ||
systemAlertAction: { | ||
marginRight: '1rem', | ||
}, | ||
})); | ||
|
||
export type SystemNotificationAlertProps = { | ||
message: string; | ||
onCloseNotification: () => void; | ||
}; | ||
|
||
export const SystemNotificationAlert = ({ | ||
message, | ||
onCloseNotification, | ||
}: SystemNotificationAlertProps) => { | ||
const styles = useStyles(); | ||
const notificationsRoute = useRouteRef(notificationsRootRouteRef); | ||
|
||
return ( | ||
<Snackbar | ||
anchorOrigin={{ vertical: 'top', horizontal: 'center' }} | ||
open | ||
message={message} | ||
action={ | ||
<> | ||
<Link | ||
href={`${notificationsRoute()}/updates`} | ||
className={styles.systemAlertAction} | ||
> | ||
Show | ||
</Link> | ||
<IconButton | ||
size="small" | ||
aria-label="close" | ||
color="inherit" | ||
onClick={onCloseNotification} | ||
> | ||
<CloseIcon fontSize="small" /> | ||
</IconButton> | ||
</> | ||
} | ||
/> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
plugins/notifications/src/components/NotificationsActiveIcon/index.ts
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 @@ | ||
export * from './NotificationsActiveIcon'; |
128 changes: 0 additions & 128 deletions
128
plugins/notifications/src/components/NotificationsSidebarItem.tsx
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export const NOTIFICATIONS_ROUTE = 'notifications'; | ||
|
||
export const DebounceDelayMs = 1000; | ||
export const DefaultPollingIntervalMs = 5 * 1000; |
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 |
---|---|---|
@@ -1,7 +1,5 @@ | ||
import { createRouteRef } from '@backstage/core-plugin-api'; | ||
|
||
import { NOTIFICATIONS_ROUTE } from './constants'; | ||
|
||
export const rootRouteRef = createRouteRef({ | ||
id: NOTIFICATIONS_ROUTE, | ||
export const notificationsRootRouteRef = createRouteRef({ | ||
id: 'notifications', | ||
}); |