-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NotificationContextProvider for publishing and subscribing notifi…
…cations.
- Loading branch information
1 parent
d185624
commit 61f4b6e
Showing
6 changed files
with
201 additions
and
57 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
134 changes: 134 additions & 0 deletions
134
new-log-viewer/src/contexts/NotificationContextProvider.tsx
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,134 @@ | ||
import React, { | ||
createContext, | ||
useCallback, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
|
||
import { | ||
Box, | ||
ModalClose, | ||
Snackbar, | ||
Stack, | ||
Typography, | ||
} from "@mui/joy"; | ||
|
||
import {Nullable} from "../typings/common"; | ||
import {LOG_LEVEL} from "../typings/logs"; | ||
|
||
|
||
interface NotificationContextType { | ||
statusMessage: string, | ||
|
||
postPopup: (level: LOG_LEVEL, message: string, title?: string) => void, | ||
postStatus: (level: LOG_LEVEL, message: string) => void, | ||
} | ||
|
||
const NotificationContext = createContext<NotificationContextType>({} as NotificationContextType); | ||
|
||
interface NotificationContextProvider { | ||
children: React.ReactNode; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
const NOTIFICATION_DEFAULT: Readonly<NotificationContextType> = Object.freeze({ | ||
statusMessage: "", | ||
|
||
postPopup: () => null, | ||
postStatus: () => null, | ||
}); | ||
|
||
interface PopupNotification { | ||
level: LOG_LEVEL, | ||
message: string, | ||
title: string | ||
} | ||
|
||
/** | ||
Check warning on line 49 in new-log-viewer/src/contexts/NotificationContextProvider.tsx GitHub Actions / lint-check
Check warning on line 49 in new-log-viewer/src/contexts/NotificationContextProvider.tsx GitHub Actions / lint-check
|
||
* | ||
* @param props | ||
* @param props.children | ||
*/ | ||
const NotificationContextProvider = ({children}: NotificationContextProvider) => { | ||
const [popupNotification, setPopupNotification] = useState<Nullable<PopupNotification>>(null); | ||
const [statusMessage, setStatusMessage] = useState<string>(NOTIFICATION_DEFAULT.statusMessage); | ||
const popupNotificationTimeoutRef = useRef<Nullable<ReturnType<typeof setTimeout>>>(null); | ||
const statusMsgTimeoutRef = useRef<Nullable<ReturnType<typeof setTimeout>>>(null); | ||
|
||
const postPopup = useCallback((level: LOG_LEVEL, message: string, title: string = "") => { | ||
if (null !== popupNotificationTimeoutRef.current) { | ||
clearTimeout(popupNotificationTimeoutRef.current); | ||
} | ||
setPopupNotification(null); | ||
setPopupNotification({ | ||
level: level, | ||
message: message, | ||
title: "" === title ? | ||
LOG_LEVEL[level] : | ||
title, | ||
}); | ||
|
||
if (LOG_LEVEL.INFO >= level) { | ||
popupNotificationTimeoutRef.current = setTimeout(() => { | ||
setPopupNotification(null); | ||
}, 5000); | ||
} | ||
}, []); | ||
|
||
const postStatus = useCallback((level: LOG_LEVEL, message: string) => { | ||
if (null !== statusMsgTimeoutRef.current) { | ||
clearTimeout(statusMsgTimeoutRef.current); | ||
} | ||
setStatusMessage(message); | ||
|
||
if (LOG_LEVEL.INFO >= level) { | ||
statusMsgTimeoutRef.current = setTimeout(() => { | ||
setStatusMessage(NOTIFICATION_DEFAULT.statusMessage); | ||
}, 5000); | ||
} | ||
}, []); | ||
|
||
|
||
return ( | ||
<NotificationContext.Provider | ||
value={{ | ||
statusMessage: statusMessage, | ||
postPopup: postPopup, | ||
postStatus: postStatus, | ||
}} | ||
> | ||
{children} | ||
{null !== popupNotification && <Snackbar | ||
open={true} | ||
sx={{right: "14px", bottom: "32px"}} | ||
color={popupNotification.level >= LOG_LEVEL.ERROR ? | ||
"danger" : | ||
"primary"} | ||
> | ||
<Stack> | ||
<Box> | ||
<Typography | ||
level={"title-sm"} | ||
color={popupNotification.level >= LOG_LEVEL.ERROR ? | ||
"danger" : | ||
"primary"} | ||
> | ||
{popupNotification.title} | ||
</Typography> | ||
<ModalClose | ||
size={"sm"} | ||
onClick={() => { setPopupNotification(null); }}/> | ||
</Box> | ||
<Typography level={"body-sm"}> | ||
{popupNotification.message} | ||
</Typography> | ||
</Stack> | ||
</Snackbar>} | ||
</NotificationContext.Provider> | ||
); | ||
}; | ||
|
||
export {NotificationContext}; | ||
export default NotificationContextProvider; |
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