-
Notifications
You must be signed in to change notification settings - Fork 1
/
LibraryActualityWidget.tsx
64 lines (61 loc) · 1.99 KB
/
LibraryActualityWidget.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React from 'react'
import {
WidgetContainer,
WidgetTitle,
StatusWrapper,
StatusContainer,
Percent
} from './styled'
import DoughnutChart from './DoughnutChart'
import { useLibraries } from '../../data/Library'
import { SpaceProps } from 'styled-system'
import semverRegex from 'semver-regex'
import semverDiff from 'semver-diff'
import { Department } from '../../data/__generated-types'
const LibraryActualityWidget = ({
width,
mt,
department
}: LibraryActualityWidgetProps) => {
const { data: libraries } = useLibraries(department)
if (!libraries) return null
const { outDated, upToDate } = libraries.reduce(
(acc, { dependents, version }) => {
const libraryVersion = semverRegex().exec(version)![0]
const outDatedDependents = dependents.reduce((acc1, dependent) => {
const dependentVersion = semverRegex().exec(dependent.version)![0]
const diff = semverDiff(dependentVersion, libraryVersion)
return diff === 'major' ? acc1 + 1 : acc1
}, 0)
return {
outDated: acc.outDated + outDatedDependents,
upToDate: acc.upToDate + (dependents.length - outDatedDependents)
}
},
{ outDated: 0, upToDate: 0 }
)
const totalUsed = outDated + upToDate
const outDatedPercent = Math.round((outDated / totalUsed) * 100 * 10) / 10
const upToDatePercent = Math.round((upToDate / totalUsed) * 100 * 10) / 10
return (
<WidgetContainer mt={mt} width={width}>
<WidgetTitle>Libraries Status</WidgetTitle>
<StatusWrapper>
<StatusContainer>
Outdated
<Percent>{outDatedPercent}%</Percent>
</StatusContainer>
<DoughnutChart percent={outDatedPercent} />
<StatusContainer>
Up to Date
<Percent>{upToDatePercent}%</Percent>
</StatusContainer>
</StatusWrapper>
</WidgetContainer>
)
}
export interface LibraryActualityWidgetProps extends SpaceProps {
width?: string
department: Department
}
export default React.memo(LibraryActualityWidget)