@xen-orchestra/web-core 0.12.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@
2
2
  <template>
3
3
  <div class="ui-stacked-bar">
4
4
  <StackedBarSegment
5
- v-for="(segment, index) in segments"
5
+ v-for="(segment, index) in parsedSegments"
6
6
  :key="index"
7
7
  :accent="segment.accent"
8
8
  :percentage="max === 0 ? 0 : (segment.value / max) * 100"
@@ -12,23 +12,36 @@
12
12
 
13
13
  <script lang="ts" setup>
14
14
  import StackedBarSegment, { type StackedBarSegmentProps } from '@core/components/ui/stacked-bar/StackedBarSegment.vue'
15
+ import { formatSizeParse } from '@core/utils/size.util'
15
16
  import { computed } from 'vue'
16
17
 
17
18
  type Segment = {
18
19
  value: number
19
20
  accent: StackedBarSegmentProps['accent']
21
+ unit?: string
20
22
  }
21
23
 
22
24
  export type StackedBarProps = {
23
25
  segments: Segment[]
24
- maxValue?: number
26
+ maxValue?: {
27
+ value?: number
28
+ unit?: string
29
+ }
25
30
  }
26
31
 
27
32
  const props = defineProps<StackedBarProps>()
28
33
 
29
- const totalValue = computed(() => props.segments.reduce((acc, segment) => acc + segment.value, 0))
34
+ const totalValue = computed(() =>
35
+ props.segments.reduce((acc, segment) => acc + (formatSizeParse(segment.value, segment.unit) ?? 0), 0)
36
+ )
30
37
 
31
- const max = computed(() => Math.max(props.maxValue ?? 0, totalValue.value))
38
+ const max = computed(() =>
39
+ Math.max(formatSizeParse(props.maxValue?.value, props.maxValue?.unit) ?? 0, totalValue.value)
40
+ )
41
+
42
+ const parsedSegments = computed(() =>
43
+ props.segments.map(segment => ({ ...segment, value: formatSizeParse(segment.value, segment.unit) ?? 0 }))
44
+ )
32
45
  </script>
33
46
 
34
47
  <style lang="postcss" scoped>
@@ -0,0 +1,30 @@
1
+ import { parse, Scale, raw } from 'human-format'
2
+
3
+ const scale = Scale.create(['', 'KiB', 'MiB', 'GiB', 'TiB'], 1024)
4
+
5
+ export const formatSizeRaw = (bytes: number | undefined, decimals: number) => {
6
+ if (bytes === undefined) {
7
+ return undefined
8
+ }
9
+
10
+ return raw(bytes, { maxDecimals: decimals, scale })
11
+ }
12
+
13
+ export const formatSizeParse = (size: number | undefined, unit?: string) => {
14
+ if (size === undefined) {
15
+ return undefined
16
+ }
17
+
18
+ let value = String(size)
19
+ if (unit !== undefined) {
20
+ value += ` ${unit}`
21
+ }
22
+
23
+ const result = parse(value, { scale })
24
+ if (isNaN(result)) {
25
+ console.error(`[formatSizeParse]: got ${result}, expected a number`)
26
+ return undefined
27
+ }
28
+
29
+ return result
30
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@xen-orchestra/web-core",
3
3
  "type": "module",
4
- "version": "0.12.0",
4
+ "version": "0.13.0",
5
5
  "private": false,
6
6
  "exports": {
7
7
  "./*": {
@@ -23,6 +23,7 @@
23
23
  "@vueuse/shared": "^10.7.1",
24
24
  "d3-time-format": "^4.1.0",
25
25
  "echarts": "^5.4.3",
26
+ "human-format": "^1.2.1",
26
27
  "iterable-backoff": "^0.1.0",
27
28
  "lodash-es": "^4.17.21",
28
29
  "placement.js": "^1.0.0-beta.5",