-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
e6cb342
commit dd29146
Showing
4 changed files
with
77 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |