Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Provide the browser notification for events #268

Merged
merged 2 commits into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add the browser notification for the event.
  • Loading branch information
noah committed Dec 18, 2021
commit 14e9af29f82e0f09aa14816f129b62cfb4a2f890
81 changes: 81 additions & 0 deletions ui/src/redux/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,87 @@ export const fetchLicense = createAsyncThunk<License, void, { state: { main: Mai
}
)

const notify = (title: string, options?: NotificationOptions) => {
if (!("Notification" in window)) {
console.log("This browser doesn't support the notification.")
return
}

if (Notification.permission === "default") {
Notification.requestPermission()
}

new Notification(title, options)
}

/**
* The browser notifies only the user who triggers the deployment.
*/
export const notifyDeploymentEvent = createAsyncThunk<void, Event, { state: { main: MainState } }>(
"main/notifyDeploymentEvent",
async (event, { getState }) => {
const { user } = getState().main

if (event.kind !== EventKindEnum.Deployment) {
return
}

if (event.deployment?.deployer?.id !== user?.id) {
return
}

if (event.type === EventTypeEnum.Created) {
notify(`New Deployment #${event.deployment?.number}`, {
icon: "/logo192.png",
body: `Start to deploy ${event.deployment?.ref.substring(0, 7)} to the ${event.deployment?.env} environment of ${event.deployment?.repo?.namespace}/${event.deployment?.repo?.name}.`,
tag: String(event.id),
})
return
}

notify(`Deployment Updated #${event.deployment?.number}`, {
icon: "/logo192.png",
body: `The deployment ${event.deployment?.number} of ${event.deployment?.repo?.namespace}/${event.deployment?.repo?.name} is updated ${event.deployment?.status}.`,
tag: String(event.id),
})
}
)

/**
* The browser notifies the requester when the review is responded to,
* but it should notify the reviewer when the review is requested.
*/
export const notifyReviewmentEvent = createAsyncThunk<void, Event, { state: { main: MainState } }>(
"main/notifyReviewmentEvent",
async (event, { getState }) => {
const { user } = getState().main
if (event.kind !== EventKindEnum.Review) {
return
}

if (event.type === EventTypeEnum.Created
&& event.review?.user?.id === user?.id) {
notify(`Review Requested`, {
icon: "/logo192.png",
body: `${event.review?.deployment?.deployer?.login} requested the review for the deployment ${event.review?.deployment?.number} of ${event.review?.deployment?.repo?.namespace}/${event.review?.deployment?.repo?.name}`,
tag: String(event.id),
})
return
}

if (event.type === EventTypeEnum.Updated
&& event.review?.deployment?.deployer?.id === user?.id) {
notify(`Review Responded`, {
icon: "/logo192.png",
body: `${event.review?.user?.login} ${event.review?.status} the deployment ${event.review?.deployment?.number} of ${event.review?.deployment?.repo?.namespace}/${event.review?.deployment?.repo?.name}`,
tag: String(event.id),
})
return
}
}
)


export const mainSlice = createSlice({
name: "main",
initialState,
Expand Down
4 changes: 3 additions & 1 deletion ui/src/views/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SettingFilled } from "@ant-design/icons"
import { Helmet } from "react-helmet"

import { useAppSelector, useAppDispatch } from "../redux/hooks"
import { init, searchDeployments, searchReviews, fetchLicense, mainSlice as slice } from "../redux/main"
import { init, searchDeployments, searchReviews, fetchLicense, notifyDeploymentEvent, notifyReviewmentEvent, mainSlice as slice } from "../redux/main"
import { subscribeEvents } from "../apis"

import RecentActivities from "../components/RecentActivities"
Expand Down Expand Up @@ -37,6 +37,8 @@ export default function Main(props: any) {
const sub = subscribeEvents((event) => {
dispatch(slice.actions.handleDeploymentEvent(event))
dispatch(slice.actions.handleReviewEvent(event))
dispatch(notifyDeploymentEvent(event))
dispatch(notifyReviewmentEvent(event))
})

return () => {
Expand Down