-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add notification context for deletion messages
This could also be used for other purposes, but is currently only used for displaying a message on the "My series" table when a series gets deleted. The notification will clear when the user navigates away from the table. This commit also fixes the table layout on narrow screens. Previously, the table would squish the "Title" column and overlap it with the one right next to it. Now each column uses a fixed width on smaller screens and adds a scrollbar, it it gets too crammed.
- Loading branch information
Showing
8 changed files
with
105 additions
and
19 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
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 { bug, Card } from "@opencast/appkit"; | ||
import { createContext, useState, useContext, PropsWithChildren, useEffect, useMemo } from "react"; | ||
import { useRouter } from "../router"; | ||
|
||
export type NotificationMessage = { | ||
kind: "info" | "error"; | ||
// Making this a function helps the message to use the currently | ||
// selected language when changed. | ||
message: () => string; | ||
// The path where the notification should be displayed. If specified, the | ||
// notification will be cleared when the user navigates away from this path. | ||
scope?: string; | ||
} | ||
|
||
type NotificationContext = { | ||
notification?: NotificationMessage; | ||
setNotification: (msg?: NotificationMessage) => void; | ||
} | ||
|
||
const NotificationContext = createContext<NotificationContext | null>(null); | ||
|
||
export const NotificationProvider: React.FC<PropsWithChildren> = ({ children }) => { | ||
const [notification, setNotification] = useState<NotificationMessage>(); | ||
const router = useRouter(); | ||
const value = useMemo(() => ({ | ||
notification, | ||
setNotification, | ||
}), [notification]); | ||
|
||
useEffect(() => { | ||
const clear = router.listenAtNav(({ newUrl }) => { | ||
if (notification?.scope && newUrl.pathname !== notification.scope) { | ||
setNotification(undefined); | ||
} | ||
}); | ||
|
||
return () => clear(); | ||
}, [router, notification]); | ||
|
||
return <NotificationContext.Provider value={value}> | ||
{children} | ||
</NotificationContext.Provider>; | ||
}; | ||
|
||
export const useNotification = () => { | ||
const context = useContext(NotificationContext) ?? bug("Not initialized!"); | ||
const { notification, setNotification } = context; | ||
|
||
const Notification: React.FC = () => notification && ( | ||
<Card css={{ width: "fit-content", marginTop: 12 }} kind={notification.kind}> | ||
{notification.message()} | ||
</Card> | ||
); | ||
|
||
return { Notification, setNotification }; | ||
}; |