-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add log level selector component to StatusBar.
- Loading branch information
1 parent
d185624
commit 0e36cd9
Showing
8 changed files
with
239 additions
and
0 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
7 changes: 7 additions & 0 deletions
7
new-log-viewer/src/components/StatusBar/LogLevelSelect/LogLevelChip.css
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,7 @@ | ||
.log-level-chip { | ||
/* stylelint-disable-next-line custom-property-pattern */ | ||
--Chip-radius: 0; | ||
|
||
font-family: "Roboto Mono", monospace !important; | ||
font-weight: 600 !important; | ||
} |
66 changes: 66 additions & 0 deletions
66
new-log-viewer/src/components/StatusBar/LogLevelSelect/LogLevelChip.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,66 @@ | ||
import React from "react"; | ||
|
||
import { | ||
Chip, | ||
Tooltip, | ||
} from "@mui/joy"; | ||
import {DefaultColorPalette} from "@mui/joy/styles/types/colorSystem"; | ||
|
||
import {LOG_LEVEL} from "../../../typings/logs"; | ||
|
||
import "./LogLevelChip.css"; | ||
|
||
|
||
/** | ||
* Maps log levels with colors from JoyUI's default color palette. | ||
*/ | ||
const LOG_LEVEL_COLOR_MAP: Record<LOG_LEVEL, DefaultColorPalette> = Object.freeze({ | ||
[LOG_LEVEL.NONE]: "neutral", | ||
[LOG_LEVEL.TRACE]: "neutral", | ||
[LOG_LEVEL.DEBUG]: "neutral", | ||
[LOG_LEVEL.INFO]: "primary", | ||
[LOG_LEVEL.WARN]: "warning", | ||
[LOG_LEVEL.ERROR]: "danger", | ||
[LOG_LEVEL.FATAL]: "danger", | ||
}); | ||
|
||
interface LogLevelChipProps { | ||
name: string, | ||
value: LOG_LEVEL, | ||
|
||
onSelectedLogLevelsChange: (func: (value: LOG_LEVEL[]) => LOG_LEVEL[]) => void, | ||
} | ||
|
||
/** | ||
* Renders a log level chip. | ||
* | ||
* @param props | ||
* @param props.name | ||
* @param props.value | ||
* @param props.onSelectedLogLevelsChange Callback to handle changes to selected log levels. | ||
* @return | ||
*/ | ||
const LogLevelChip = ({name, value, onSelectedLogLevelsChange}: LogLevelChipProps) => { | ||
const handleChipClick = (ev: React.MouseEvent<HTMLButtonElement>) => { | ||
ev.stopPropagation(); | ||
onSelectedLogLevelsChange((oldValue) => oldValue.filter((v) => v !== value)); | ||
}; | ||
|
||
return ( | ||
<Tooltip | ||
color={LOG_LEVEL_COLOR_MAP[value]} | ||
key={value} | ||
title={name} | ||
> | ||
<Chip | ||
className={"log-level-chip"} | ||
color={LOG_LEVEL_COLOR_MAP[value]} | ||
variant={"outlined"} | ||
onClick={handleChipClick} | ||
> | ||
{name[0]} | ||
</Chip> | ||
</Tooltip> | ||
); | ||
}; | ||
export default LogLevelChip; |
4 changes: 4 additions & 0 deletions
4
new-log-viewer/src/components/StatusBar/LogLevelSelect/index.css
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,4 @@ | ||
.log-level-select-render-value-box { | ||
display: flex; | ||
gap: 2px; | ||
} |
102 changes: 102 additions & 0 deletions
102
new-log-viewer/src/components/StatusBar/LogLevelSelect/index.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,102 @@ | ||
import React, { | ||
useCallback, | ||
useState, | ||
} from "react"; | ||
|
||
import {SelectValue} from "@mui/base/useSelect"; | ||
import { | ||
Box, | ||
IconButton, | ||
MenuItem, | ||
Option, | ||
Select, | ||
SelectOption, | ||
} from "@mui/joy"; | ||
|
||
import CloseRoundedIcon from "@mui/icons-material/CloseRounded"; | ||
import KeyboardArrowUpIcon from "@mui/icons-material/KeyboardArrowUp"; | ||
|
||
import { | ||
LOG_LEVEL, | ||
LOG_LEVEL_NAMES, | ||
MAX_LOG_LEVEL, | ||
} from "../../../typings/logs"; | ||
import {range} from "../../../utils/data"; | ||
import LogLevelChip from "./LogLevelChip"; | ||
|
||
import "./index.css"; | ||
|
||
|
||
/** | ||
* Renders a dropdown box for selecting log levels. | ||
* | ||
* @return | ||
*/ | ||
const LogLevelSelect = () => { | ||
const [selectedLogLevels, setSelectedLogLevels] = useState<LOG_LEVEL[]>([]); | ||
|
||
const handleRenderValue = (selected: SelectValue<SelectOption<LOG_LEVEL>, true>) => ( | ||
<Box className={"log-level-select-render-value-box"}> | ||
{selected.map((selectedOption) => ( | ||
<LogLevelChip | ||
key={selectedOption.value} | ||
name={selectedOption.label as string} | ||
value={selectedOption.value} | ||
onSelectedLogLevelsChange={setSelectedLogLevels}/> | ||
))} | ||
</Box> | ||
); | ||
|
||
const handleSelectChange = useCallback(( | ||
_: React.MouseEvent | React.KeyboardEvent | React.FocusEvent | null, | ||
newValue: SelectValue<LOG_LEVEL, true> | ||
) => { | ||
if (0 === selectedLogLevels.length) { | ||
const [singleSelectValue] = newValue; | ||
setSelectedLogLevels(range(singleSelectValue as number, 1 + MAX_LOG_LEVEL)); | ||
} else { | ||
setSelectedLogLevels(newValue.sort()); | ||
} | ||
}, [selectedLogLevels]); | ||
|
||
const handleSelectClearButtonClick = () => { | ||
handleSelectChange(null, []); | ||
}; | ||
|
||
const handleSelectClearButtonMouseDown = (ev: React.MouseEvent<HTMLButtonElement>) => { | ||
ev.stopPropagation(); | ||
}; | ||
|
||
return ( | ||
<Select | ||
multiple={true} | ||
placeholder={"Log Level"} | ||
renderValue={handleRenderValue} | ||
size={"sm"} | ||
value={selectedLogLevels} | ||
variant={"soft"} | ||
indicator={0 === selectedLogLevels.length ? | ||
<KeyboardArrowUpIcon/> : | ||
<IconButton | ||
variant={"plain"} | ||
onClick={handleSelectClearButtonClick} | ||
onMouseDown={handleSelectClearButtonMouseDown} | ||
> | ||
<CloseRoundedIcon/> | ||
</IconButton>} | ||
onChange={handleSelectChange} | ||
> | ||
{/* Add a dummy MenuItem to avoid the first Option receiving focus. */} | ||
<MenuItem sx={{display: "none"}}/> | ||
{LOG_LEVEL_NAMES.map((logLevelName, index) => ( | ||
<Option | ||
key={logLevelName} | ||
value={index} | ||
> | ||
{logLevelName} | ||
</Option> | ||
))} | ||
</Select> | ||
); | ||
}; | ||
export default LogLevelSelect; |
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