Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notifications context #1839

Merged
merged 12 commits into from
Jan 6, 2022
29 changes: 16 additions & 13 deletions packages/files-ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { FilesApiProvider } from "./Contexts/FilesApiContext"
import { UserProvider } from "./Contexts/UserContext"
import { BillingProvider } from "./Contexts/BillingContext"
import { PosthogProvider } from "./Contexts/PosthogContext"
import { NotificationsProvider } from "./Contexts/NotificationsContext"

if (
process.env.NODE_ENV === "production" &&
Expand Down Expand Up @@ -121,19 +122,21 @@ const App = () => {
enableLogging={directAuthNetwork !== "mainnet"}
network={directAuthNetwork}
>
<UserProvider>
<FilesProvider>
<BillingProvider>
<Router>
<PosthogProvider>
<AppWrapper>
<FilesRoutes />
</AppWrapper>
</PosthogProvider>
</Router>
</BillingProvider>
</FilesProvider>
</UserProvider>
<NotificationsProvider>
<UserProvider>
<FilesProvider>
<BillingProvider>
<Router>
<PosthogProvider>
<AppWrapper>
<FilesRoutes />
</AppWrapper>
</PosthogProvider>
</Router>
</BillingProvider>
</FilesProvider>
</UserProvider>
</NotificationsProvider>
</ThresholdKeyProvider>
</FilesApiProvider>
</Web3Provider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react"
import { Button, BellIcon, MenuDropdown } from "@chainsafe/common-components"
import { createStyles, ITheme, makeStyles } from "@chainsafe/common-theme"
import NotificationList from "./NotificationList"
import { useNotifications } from "../../../Contexts/NotificationsContext"

const useStyles = makeStyles(({ palette, constants }: ITheme) =>
createStyles({
Expand Down Expand Up @@ -31,17 +32,15 @@ const useStyles = makeStyles(({ palette, constants }: ITheme) =>
)

export interface Notification {
id: string
title: string
createdAt: number
onClick?: () => void
}

interface INotificationsDropdownProps {
notifications: Notification[]
}

const NotificationsDropdown = ({ notifications }: INotificationsDropdownProps) => {
const NotificationsDropdown = () => {
const classes = useStyles()
const { notifications } = useNotifications()

return (
<MenuDropdown
Expand Down
4 changes: 1 addition & 3 deletions packages/files-ui/src/Components/Layouts/AppHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,7 @@ const AppHeader = ({ navOpen, setNavOpen }: IAppHeader) => {
</Button>
</section>
<section>
<NotificationsDropdown
notifications={[]}
/>
<NotificationsDropdown />
</section>
</section>
) : (
Expand Down
56 changes: 56 additions & 0 deletions packages/files-ui/src/Contexts/NotificationsContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React, { ReactNode, useCallback, useState } from "react"
import { Notification } from "../Components/Elements/Notifications/NotificationsDropdown"
import { v4 as uuidv4 } from "uuid"

type NotificationsContextProps = {
children: ReactNode | ReactNode[]
}

interface INotificationsContext {
notifications: Notification[]
addNotification: (notification: Omit<Notification, "id">) => string
removeNotification: (id: string) => void
}

const NotificationsContext = React.createContext<INotificationsContext | undefined>(
undefined
)

const NotificationsProvider = ({ children }: NotificationsContextProps) => {
const [notifications, setNotifications] = useState<Notification[]>([])

const addNotification = useCallback((notification: Omit<Notification, "id">) => {
const id = uuidv4()
setNotifications([...notifications, {
id,
...notification
}])
return id
}, [notifications])

const removeNotification = useCallback((id: string) => {
setNotifications(notifications.filter((notification) => notification.id !== id))
}, [notifications])

return (
<NotificationsContext.Provider
value={{
notifications,
addNotification,
removeNotification
}}
>
{children}
</NotificationsContext.Provider>
)
}

const useNotifications = () => {
const context = React.useContext(NotificationsContext)
if (context === undefined) {
throw new Error("useNotifications must be used within a NotificationsProvider")
}
return context
}

export { NotificationsProvider, useNotifications }