-
-
Notifications
You must be signed in to change notification settings - Fork 9.2k
/
CoverageTool.tsx
58 lines (52 loc) · 1.51 KB
/
CoverageTool.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
import React, { useState } from 'react';
import { window } from 'global';
import { Icons, IconButton } from '@storybook/components';
import { useChannel, useAddonState } from '@storybook/api';
import { SELECT_STORY } from '@storybook/core-events';
import {
PARAM_KEY,
EVENTS,
CoverageMap,
CoverageSummary,
CoverageItem,
setCoverageSummary,
getCoverageSummary,
} from './shared';
const heatmap = ['#fecc5c', '#fd8d3c', '#f03b20', '#bd0026'].reverse();
function heatmapColor(val: number) {
if (!val && val !== 0) {
return null;
}
if (val === 100) {
return '#74c476';
}
const idx = Math.floor(heatmap.length * (val / 100));
return heatmap[idx];
}
const getColor = (storyId: string, active: boolean) => {
const coverageSummary = getCoverageSummary();
return active && storyId && coverageSummary && heatmapColor(coverageSummary[storyId]);
};
export const CoverageTool: React.FunctionComponent<{}> = () => {
const [active, setActive] = useState(true);
const icon = active ? 'eye' : 'eyeclose';
window.getStoryColor = (storyId: string) => getColor(storyId, active);
const emit = useChannel({
[EVENTS.COVERAGE_SUMMARY]: (summary: CoverageSummary) => {
setCoverageSummary(summary);
},
});
return (
<IconButton
key={PARAM_KEY}
active={active}
title="Story Coverage"
onClick={() => {
setActive(!active);
emit(SELECT_STORY, { kind: 'Coverage|Welcome', story: 'normal' });
}}
>
<Icons icon={icon} />
</IconButton>
);
};