Skip to content

Commit

Permalink
Notifications context (#1839)
Browse files Browse the repository at this point in the history
* hop

* lingui extract

* high res and bg color

* notification context ready

* Update packages/files-ui/src/Contexts/NotificationsContext.tsx

Co-authored-by: Thibaut Sardan <33178835+Tbaut@users.noreply.github.com>

* added callback

* add notification update

* notifications move to elements

Co-authored-by: Thibaut Sardan <github@thib.top>
Co-authored-by: GitHub Actions <actions@github.com>
Co-authored-by: Thibaut Sardan <33178835+Tbaut@users.noreply.github.com>
  • Loading branch information
4 people authored Jan 6, 2022
1 parent e6cb342 commit dd29146
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 21 deletions.
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 }

0 comments on commit dd29146

Please sign in to comment.