|
| 1 | +import { Box, Button, Grid, Typography } from '@material-ui/core'; |
| 2 | +import { |
| 3 | + configApiRef, |
| 4 | + discoveryApiRef, |
| 5 | + errorApiRef, |
| 6 | + fetchApiRef, |
| 7 | + useApi, |
| 8 | +} from '@backstage/core-plugin-api'; |
| 9 | + |
| 10 | +import { InfoCard } from '@backstage/core-components'; |
| 11 | +import React from 'react'; |
| 12 | +import { UserNotificationSettingsCard } from '@backstage/plugin-notifications'; |
| 13 | + |
| 14 | +export const NotificationSettings = () => { |
| 15 | + const config = useApi(configApiRef); |
| 16 | + const fetchApi = useApi(fetchApiRef); |
| 17 | + const discovery = useApi(discoveryApiRef); |
| 18 | + const errorApi = useApi(errorApiRef); |
| 19 | + |
| 20 | + const isEnabled = |
| 21 | + config.getOptionalBoolean('notificationsTester.enabled') ?? true; |
| 22 | + |
| 23 | + const handleNotifyClick = async () => { |
| 24 | + const notificationTesterUrl = await discovery.getBaseUrl( |
| 25 | + 'notifications-tester', |
| 26 | + ); |
| 27 | + const response = await fetchApi.fetch(`${notificationTesterUrl}/test`, { |
| 28 | + method: 'POST', |
| 29 | + }); |
| 30 | + if (!response.ok) { |
| 31 | + errorApi.post( |
| 32 | + new Error(`Failed to send notification: ${response.status}`), |
| 33 | + ); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + return ( |
| 38 | + <Grid container justifyContent="center" spacing={2}> |
| 39 | + <Grid item xs={9}> |
| 40 | + <UserNotificationSettingsCard |
| 41 | + originNames={{ 'plugin:scaffolder': 'Scaffolder' }} |
| 42 | + /> |
| 43 | + </Grid> |
| 44 | + {isEnabled && ( |
| 45 | + <Grid item xs={3}> |
| 46 | + <InfoCard title="Send Test Notification"> |
| 47 | + <Button |
| 48 | + variant="contained" |
| 49 | + color="primary" |
| 50 | + onClick={async () => await handleNotifyClick()} |
| 51 | + > |
| 52 | + Notify |
| 53 | + </Button> |
| 54 | + <Box pt={2}> |
| 55 | + <Typography variant="subtitle2"> |
| 56 | + Note: this card is not part of the default Notifications Setting |
| 57 | + and was added to be able to try out the Notification system for |
| 58 | + this Demo site. |
| 59 | + </Typography> |
| 60 | + </Box> |
| 61 | + </InfoCard> |
| 62 | + </Grid> |
| 63 | + )} |
| 64 | + </Grid> |
| 65 | + ); |
| 66 | +}; |
0 commit comments