-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CP-3325] Memory Usage Bar Component developed
- Loading branch information
Showing
9 changed files
with
175 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Copyright (c) Mudita sp. z o.o. All rights reserved. | ||
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md | ||
*/ | ||
|
||
import { z } from "zod" | ||
|
||
const dataValidator = z.undefined() | ||
|
||
const segmentBarItemSchema = z.object({ | ||
color: z.string(), | ||
label: z.string(), | ||
value: z.number(), | ||
minWidth: z.number(), | ||
}) | ||
|
||
export type SegmentBarItem = z.infer<typeof segmentBarItemSchema> | ||
|
||
const configValidator = z.object({ | ||
segments: z.array(segmentBarItemSchema), | ||
segmentBorderRadius: z.string().optional(), | ||
}) | ||
export type SegmentBarConfig = z.infer<typeof configValidator> | ||
|
||
export const segmentBar = { | ||
key: "segment-bar", | ||
dataValidator, | ||
configValidator, | ||
} as const |
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,11 @@ | ||
/** | ||
* Copyright (c) Mudita sp. z o.o. All rights reserved. | ||
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md | ||
*/ | ||
|
||
import { segmentBar as segmentBarWrapper } from "generic-view/models" | ||
import { SegmentBar } from "./segment-bar" | ||
|
||
export const segmentBar = { | ||
[segmentBarWrapper.key]: SegmentBar, | ||
} |
42 changes: 42 additions & 0 deletions
42
libs/generic-view/ui/src/lib/segment-bar/segment-bar-item.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,42 @@ | ||
/** | ||
* Copyright (c) Mudita sp. z o.o. All rights reserved. | ||
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md | ||
*/ | ||
|
||
import React from "react" | ||
import styled from "styled-components" | ||
import { ComputedSegmentBarItem } from "./compute-segment-bar-items.helper" | ||
import { BaseGenericComponent } from "generic-view/utils" | ||
|
||
interface SegmentBarItemProps extends ComputedSegmentBarItem { | ||
borderRadius: string | ||
isFirst: boolean | ||
} | ||
|
||
export const SegmentBarItem: BaseGenericComponent< | ||
undefined, | ||
undefined, | ||
SegmentBarItemProps | ||
> = React.memo(({ color, width, left, zIndex, label, ...props }) => ( | ||
<Wrapper | ||
style={{ | ||
width, | ||
left, | ||
zIndex, | ||
backgroundColor: color, | ||
}} | ||
{...props} | ||
/> | ||
)) | ||
|
||
const Wrapper = styled.div<{ | ||
borderRadius: string | ||
isFirst: boolean | ||
}>` | ||
position: absolute; | ||
height: 100%; | ||
border-radius: ${(props) => | ||
props.isFirst | ||
? `${props.borderRadius}` | ||
: `0 ${props.borderRadius} ${props.borderRadius} 0`}; | ||
` |
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,50 @@ | ||
/** | ||
* Copyright (c) Mudita sp. z o.o. All rights reserved. | ||
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md | ||
*/ | ||
|
||
import React from "react" | ||
import styled from "styled-components" | ||
import { APIFC } from "generic-view/utils" | ||
import { SegmentBarConfig } from "generic-view/models" | ||
import { computeSegmentBarItems } from "./compute-segment-bar-items.helper" | ||
import { SegmentBarItem } from "./segment-bar-item" | ||
import { useContainerWidth } from "./use-container-width.hook" | ||
|
||
export const SegmentBar: APIFC<undefined, SegmentBarConfig> = ({ | ||
config, | ||
data, | ||
...props | ||
}) => { | ||
const { ref, containerWidth } = useContainerWidth() | ||
|
||
const computeSegments = React.useCallback(() => { | ||
return computeSegmentBarItems(config.segments, containerWidth) | ||
}, [config.segments, containerWidth]) | ||
|
||
const computedSegments = computeSegments() | ||
|
||
const segmentBorderRadius = config.segmentBorderRadius || "56px" | ||
|
||
return ( | ||
<Wrapper ref={ref} width={"100%"} height={"14px"} {...props}> | ||
{computedSegments.map((segment, index) => ( | ||
<SegmentBarItem | ||
key={index} | ||
{...segment} | ||
borderRadius={segmentBorderRadius} | ||
isFirst={index === 0} | ||
/> | ||
))} | ||
</Wrapper> | ||
) | ||
} | ||
|
||
const Wrapper = styled.div<{ | ||
width: string | ||
height: string | ||
}>` | ||
position: relative; | ||
width: ${(props) => props.width}; | ||
height: ${(props) => props.height}; | ||
` |
19 changes: 19 additions & 0 deletions
19
libs/generic-view/ui/src/lib/segment-bar/use-container-width.hook.ts
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,19 @@ | ||
/** | ||
* Copyright (c) Mudita sp. z o.o. All rights reserved. | ||
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md | ||
*/ | ||
|
||
import { useLayoutEffect, useRef, useState } from "react" | ||
|
||
export const useContainerWidth = () => { | ||
const ref = useRef<HTMLDivElement>(null) | ||
const [containerWidth, setContainerWidth] = useState(0) | ||
|
||
useLayoutEffect(() => { | ||
if (ref.current) { | ||
setContainerWidth(ref.current.offsetWidth) | ||
} | ||
}, [ref]) | ||
|
||
return { ref, containerWidth } | ||
} |