-
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.
new-log-viewer: Add
NotificationContextProvider
for managing pop-up…
… messages; add pop-ups for errors and remove status bar dummy message. (#84)
- Loading branch information
1 parent
ae24669
commit f402065
Showing
13 changed files
with
444 additions
and
87 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
121 changes: 121 additions & 0 deletions
121
new-log-viewer/src/components/PopUps/PopUpMessageBox.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,121 @@ | ||
import { | ||
useContext, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
|
||
import { | ||
Alert, | ||
Box, | ||
CircularProgress, | ||
IconButton, | ||
Typography, | ||
} from "@mui/joy"; | ||
|
||
import CloseIcon from "@mui/icons-material/Close"; | ||
|
||
import { | ||
NotificationContext, | ||
PopUpMessage, | ||
} from "../../contexts/NotificationContextProvider"; | ||
import {WithId} from "../../typings/common"; | ||
import {LOG_LEVEL} from "../../typings/logs"; | ||
import {DO_NOT_TIMEOUT_VALUE} from "../../typings/notifications"; | ||
|
||
|
||
const AUTO_DISMISS_PERCENT_UPDATE_INTERVAL_MILLIS = 50; | ||
|
||
interface PopUpMessageProps { | ||
message: WithId<PopUpMessage>, | ||
} | ||
|
||
/** | ||
* Display a pop-up message in an alert box. | ||
* | ||
* @param props | ||
* @param props.message | ||
* @return | ||
*/ | ||
const PopUpMessageBox = ({message}: PopUpMessageProps) => { | ||
const {id, level, message: messageStr, title, timeoutMillis} = message; | ||
|
||
const {handlePopUpMessageClose} = useContext(NotificationContext); | ||
const [percentRemaining, setPercentRemaining] = useState<number>(100); | ||
const intervalCountRef = useRef<number>(0); | ||
|
||
const handleCloseButtonClick = () => { | ||
handlePopUpMessageClose(id); | ||
}; | ||
|
||
useEffect(() => { | ||
if (DO_NOT_TIMEOUT_VALUE === timeoutMillis) { | ||
return () => {}; | ||
} | ||
|
||
const totalIntervals = Math.ceil( | ||
timeoutMillis / AUTO_DISMISS_PERCENT_UPDATE_INTERVAL_MILLIS | ||
); | ||
const intervalId = setInterval(() => { | ||
intervalCountRef.current++; | ||
const newPercentRemaining = 100 - (100 * (intervalCountRef.current / totalIntervals)); | ||
if (0 >= newPercentRemaining) { | ||
handlePopUpMessageClose(id); | ||
} | ||
setPercentRemaining(newPercentRemaining); | ||
}, AUTO_DISMISS_PERCENT_UPDATE_INTERVAL_MILLIS); | ||
|
||
return () => { | ||
clearInterval(intervalId); | ||
}; | ||
}, [ | ||
timeoutMillis, | ||
handlePopUpMessageClose, | ||
id, | ||
]); | ||
|
||
const color = level >= LOG_LEVEL.ERROR ? | ||
"danger" : | ||
"primary"; | ||
|
||
return ( | ||
<Alert | ||
className={"pop-up-message-box-alert"} | ||
color={color} | ||
variant={"outlined"} | ||
> | ||
<div className={"pop-up-message-box-alert-layout"}> | ||
<Box className={"pop-up-message-box-title-container"}> | ||
<Typography | ||
className={"pop-up-message-box-title-text"} | ||
color={color} | ||
level={"title-md"} | ||
> | ||
{title} | ||
</Typography> | ||
<CircularProgress | ||
color={color} | ||
determinate={true} | ||
size={"sm"} | ||
thickness={3} | ||
value={percentRemaining} | ||
> | ||
<IconButton | ||
className={"pop-up-message-box-close-button"} | ||
color={color} | ||
size={"sm"} | ||
onClick={handleCloseButtonClick} | ||
> | ||
<CloseIcon/> | ||
</IconButton> | ||
</CircularProgress> | ||
</Box> | ||
<Typography level={"body-sm"}> | ||
{messageStr} | ||
</Typography> | ||
</div> | ||
</Alert> | ||
); | ||
}; | ||
|
||
export default PopUpMessageBox; |
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,47 @@ | ||
.pop-up-messages-container-snackbar { | ||
/* Disable pointer events on the transparent container to allow components underneath to be | ||
accessed. */ | ||
pointer-events: none; | ||
|
||
right: 14px !important; | ||
bottom: var(--ylv-status-bar-height) !important; | ||
|
||
padding: 0 !important; | ||
|
||
background: transparent !important; | ||
border: none !important; | ||
box-shadow: none !important; | ||
} | ||
|
||
.pop-up-messages-container-stack { | ||
scrollbar-width: none; | ||
overflow-y: auto; | ||
height: calc(100vh - var(--ylv-status-bar-height) - var(--ylv-menu-bar-height)); | ||
} | ||
|
||
.pop-up-message-box-alert { | ||
/* Restore pointer events on the pop-up messages. See above `pointer-events: none` in | ||
`.pop-up-messages-container-snackbar`. */ | ||
pointer-events: initial; | ||
padding-inline: 18px !important; | ||
} | ||
|
||
.pop-up-message-box-alert-layout { | ||
width: 300px; | ||
} | ||
|
||
.pop-up-message-box-title-container { | ||
display: flex; | ||
align-items: center; | ||
} | ||
|
||
.pop-up-message-box-title-text { | ||
flex-grow: 1; | ||
} | ||
|
||
.pop-up-message-box-close-button { | ||
/* stylelint-disable-next-line custom-property-pattern */ | ||
--IconButton-size: 18px !important; | ||
|
||
border-radius: 18px !important; | ||
} |
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,42 @@ | ||
import {useContext} from "react"; | ||
|
||
import { | ||
Snackbar, | ||
Stack, | ||
} from "@mui/joy"; | ||
|
||
import {NotificationContext} from "../../contexts/NotificationContextProvider"; | ||
import PopUpMessageBox from "./PopUpMessageBox"; | ||
|
||
import "./index.css"; | ||
|
||
|
||
/** | ||
* Displays pop-ups in a transparent container positioned on the right side of the viewport. | ||
* | ||
* @return | ||
*/ | ||
const PopUps = () => { | ||
const {popUpMessages} = useContext(NotificationContext); | ||
|
||
return ( | ||
<Snackbar | ||
className={"pop-up-messages-container-snackbar"} | ||
open={0 < popUpMessages.length} | ||
> | ||
<Stack | ||
className={"pop-up-messages-container-stack"} | ||
direction={"column-reverse"} | ||
gap={1} | ||
> | ||
{popUpMessages.map((message) => ( | ||
<PopUpMessageBox | ||
key={message.id} | ||
message={message}/> | ||
))} | ||
</Stack> | ||
</Snackbar> | ||
); | ||
}; | ||
|
||
export default PopUps; |
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 |
---|---|---|
|
@@ -15,5 +15,4 @@ | |
|
||
.status-message { | ||
flex-grow: 1; | ||
padding-left: 8px; | ||
} |
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
Oops, something went wrong.