diff --git a/src/renderer/components/metric-bar/index.ts b/src/renderer/components/metric-bar/index.ts new file mode 100644 index 0000000000..909091c960 --- /dev/null +++ b/src/renderer/components/metric-bar/index.ts @@ -0,0 +1,5 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +export * from "./metric-bar"; diff --git a/src/renderer/components/metric-bar/metric-bar.module.scss b/src/renderer/components/metric-bar/metric-bar.module.scss new file mode 100644 index 0000000000..ee51f6f758 --- /dev/null +++ b/src/renderer/components/metric-bar/metric-bar.module.scss @@ -0,0 +1,10 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +.metricBar { + display: flex; + gap: 0.5rem; + align-items: center; +} \ No newline at end of file diff --git a/src/renderer/components/metric-bar/metric-bar.tsx b/src/renderer/components/metric-bar/metric-bar.tsx new file mode 100644 index 0000000000..7eff967d80 --- /dev/null +++ b/src/renderer/components/metric-bar/metric-bar.tsx @@ -0,0 +1,43 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import styles from "./metric-bar.module.scss"; + +import React, { HTMLAttributes, ReactNode, useEffect, useRef, useState } from "react"; +import { cssNames, cssVar } from "../../utils"; +import { VerticalBar } from "../vertical-bar"; + +interface BarProps extends HTMLAttributes { + value: number; + max: number; + min?: number; + tooltip?: ReactNode; + showPercent?: ReactNode; + changeColorOnWarning?: boolean; + warningPercentage?: number; + metricName?: "cpu" | "memory" | "disk"; // Used for color setting + customColor?: string; +} + +export function MetricBar(props: BarProps) { + const elem = useRef(); + const { max, min, tooltip, showPercent, changeColorOnWarning = true, warningPercentage = 85, value, metricName, customColor } = props; + const percents = Math.min(100, value / (max - min) * 100); + const [metricColor, setMetricColor] = useState("var(--colorVague)"); + const color = (percents > warningPercentage && changeColorOnWarning) ? "pink" : customColor || metricColor; + + useEffect(() => { + const cssVars = cssVar(elem.current); + + setMetricColor(cssVars.get(`--color-${metricName}`).toString()); + }); + + return ( +
+ + {showPercent && } +
+ ); +} diff --git a/src/renderer/components/vertical-bar/vertical-bar.module.scss b/src/renderer/components/vertical-bar/vertical-bar.module.scss index 06cfd0cafa..039480a166 100644 --- a/src/renderer/components/vertical-bar/vertical-bar.module.scss +++ b/src/renderer/components/vertical-bar/vertical-bar.module.scss @@ -7,8 +7,8 @@ display: flex; flex-direction: column; justify-content: flex-end; - width: 12px; - height: 22px; + inline-size: 12px; + block-size: 22px; background-color: #313235; border-radius: 2px; overflow: hidden;