Skip to content

Commit fd1c430

Browse files
authored
fix(notificatoins): polling interval can be configured (#1134)
1 parent 1c4c54d commit fd1c430

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

plugins/notifications/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@ This Backstage front-end plugin provides:
1313

1414
Have `@janus-idp/plugin-notifications-backend` installed and running.
1515

16-
### Installing as a dynamic plugin?
16+
## Installing as a dynamic plugin
1717

1818
The sections below are relevant for static plugins. If the plugin is expected to be installed as a dynamic one:
1919

2020
- follow https://github.com/janus-idp/backstage-showcase/blob/main/showcase-docs/dynamic-plugins.md#installing-a-dynamic-plugin-package-in-the-showcase
2121
- add content of `app-config.janus-idp.yaml` into `app-config.local.yaml`.
22+
- customize configuration configure as needed
23+
24+
### Configuration
25+
26+
There is polling used to check for new notifications. It can be tuned via `pollingIntervalMs` config property (see `app-config.janus-idp.yaml`). When set to 0, the polling is turned off.
27+
28+
## Installing as a static plugin
2229

2330
### Add NPM dependency
2431

@@ -66,6 +73,8 @@ export const AppBase = () => {
6673

6774
## How to use the NotificationApi
6875

76+
Once installed as either static or dynamic plugin, a 3rd party frontend plugin can access the notifications via its frontend API:
77+
6978
```
7079
import { notificationsApiRef, Notification } from '@janus-idp/plugin-notifications';
7180

plugins/notifications/app-config.janus-idp.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ dynamicPlugins:
1212
menuItem:
1313
icon: notificationsIcon
1414
text: Notifications
15+
config:
16+
pollingIntervalMs: 5000

plugins/notifications/src/components/NotificationsActiveIcon/NotificationsActiveIcon.tsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@ const NotificationsErrorIcon = () => (
1818
</Tooltip>
1919
);
2020

21+
const usePollingIntervalConfig = (): number => {
22+
const configApi = useApi(configApiRef);
23+
24+
const dynamicRoutes =
25+
// @ts-ignore
26+
configApi.getOptionalConfig('dynamicPlugins')?.data?.frontend?.[
27+
'janus-idp.backstage-plugin-notifications'
28+
]?.dynamicRoutes;
29+
30+
const config = dynamicRoutes?.find(
31+
(r: { importName?: string }) => r.importName === 'NotificationsPage',
32+
)?.config;
33+
const pollingInterval = config?.pollingIntervalMs;
34+
35+
return pollingInterval === undefined
36+
? DefaultPollingIntervalMs
37+
: pollingInterval;
38+
};
39+
2140
/**
2241
* Dynamic plugins recently do not support passing configuration
2342
* to icons or making the left-side menu item texts active (so far strings only).
@@ -27,14 +46,6 @@ const NotificationsErrorIcon = () => (
2746
*/
2847
export const NotificationsActiveIcon = () => {
2948
const notificationsApi = useApi(notificationsApiRef);
30-
const configApi = useApi(configApiRef);
31-
32-
let pollingInterval = configApi.getOptionalNumber(
33-
'notifications.pollingIntervalMs',
34-
);
35-
if (pollingInterval === undefined) {
36-
pollingInterval = DefaultPollingIntervalMs;
37-
}
3849

3950
const [error, setError] = React.useState<Error | undefined>(undefined);
4051
const [unreadCount, setUnreadCount] = React.useState(0);
@@ -43,6 +54,7 @@ export const NotificationsActiveIcon = () => {
4354
React.useState<Notification>();
4455
const [closedNotificationId, setClosedNotificationId] =
4556
React.useState<string>();
57+
const pollingInterval = usePollingIntervalConfig();
4658

4759
const pollCallback = React.useCallback(async () => {
4860
try {

0 commit comments

Comments
 (0)