1
1
import { useRef , useState , useMemo , useEffect } from "react" ;
2
- import {
3
- Container ,
4
- ContentContainer ,
5
- GrassBlock ,
6
- GrassColorsetInfoContainer ,
7
- GrassGrid ,
8
- } from "./GrassGraph.styled" ;
9
- import {
10
- IGrassColorsetInfoProps ,
11
- IGrassGraphProps ,
12
- IGrassProps ,
13
- } from "./GrassGraph.type" ;
2
+ import { Container , ContentContainer , GrassGrid } from "./GrassGraph.styled" ;
3
+ import { IGrassGraphProps } from "./GrassGraph.type" ;
14
4
import { languageOptionValueAtom as LOV } from "../../shared/atom" ;
15
5
import { useRecoilValue } from "recoil" ;
16
- import { Theme } from "../../styles/theme" ;
17
-
18
- const getGrassColorByValue = (
19
- value : number ,
20
- hex : string ,
21
- boundary : [ number , number , number , number ]
22
- ) => {
23
- if ( value < boundary [ 0 ] ) return `${ Theme . background . secondary } ` ;
24
- if ( value < boundary [ 1 ] ) return `${ hex } 66` ;
25
- if ( value < boundary [ 2 ] ) return `${ hex } aa` ;
26
- if ( value < boundary [ 3 ] ) return `${ hex } dd` ;
27
- return `${ hex } ff` ;
28
- } ;
29
-
30
- function Grass ( { value, color, colorBoundary } : IGrassProps ) {
31
- return (
32
- < GrassBlock
33
- backgroundColor = { getGrassColorByValue ( value , color , colorBoundary ) }
34
- > </ GrassBlock >
35
- ) ;
36
- }
37
-
38
- function GrassColorsetInfo ( {
39
- color,
40
- colorBoundary,
41
- leftText,
42
- rightText,
43
- } : IGrassColorsetInfoProps ) {
44
- return (
45
- < GrassColorsetInfoContainer >
46
- < span style = { { marginRight : 6 } } > { leftText } </ span >
47
- { [ 0 , ...colorBoundary ] . map ( ( aBoundary ) => (
48
- < div
49
- key = { aBoundary }
50
- className = "color"
51
- style = { {
52
- backgroundColor : getGrassColorByValue (
53
- aBoundary ,
54
- color ,
55
- colorBoundary
56
- ) ,
57
- } }
58
- > </ div >
59
- ) ) }
60
- < span style = { { marginLeft : 6 } } > { rightText } </ span >
61
- </ GrassColorsetInfoContainer >
62
- ) ;
63
- }
6
+ import GrassColorsetInfo from "./GrassColorsetInfo/GrassColorsetInfo" ;
7
+ import Grass from "./Grass/Grass" ;
64
8
65
9
export default function GrassGraph ( {
66
10
color,
67
11
recentDatas,
68
12
colorBoundary,
69
13
} : IGrassGraphProps ) {
70
14
const gridRef = useRef < HTMLDivElement > ( null ) ;
71
- const [ gridMaxItems , setGridMaxItems ] = useState ( 0 ) ;
72
15
const language = useRecoilValue ( LOV ) ;
16
+ const [ gridMaxItems , setGridMaxItems ] = useState ( 0 ) ;
73
17
74
- /*
75
- Memos
76
- */
77
18
const parsedDatas = useMemo ( ( ) => {
78
- const res : number [ ] = [ ] ;
79
- for ( let i = gridMaxItems - 1 ; i >= 0 ; i -- ) {
80
- if ( i < recentDatas . length ) {
81
- res . push ( recentDatas [ i ] ) ;
82
- } else {
83
- res . push ( 0 ) ;
84
- }
85
- }
86
- return res ;
19
+ const ZERO_SIZE = Math . max ( gridMaxItems - recentDatas . length , 0 ) ;
20
+ const zeros = Array ( ZERO_SIZE ) . fill ( 0 ) ;
21
+ const datas = recentDatas . slice ( 0 , gridMaxItems ) ;
22
+ return [ ...zeros , ...datas . reverse ( ) ] ;
87
23
} , [ recentDatas , gridMaxItems ] ) ;
88
24
89
- /*
90
- Effects
91
- */
25
+ const grassItems = useMemo ( ( ) => {
26
+ return parsedDatas . map ( ( v , i ) => {
27
+ return (
28
+ < Grass key = { i } color = { color } value = { v } colorBoundary = { colorBoundary } />
29
+ ) ;
30
+ } ) ;
31
+ } , [ parsedDatas ] ) ;
32
+
92
33
useEffect ( ( ) => {
93
34
const onResize = ( ) => {
94
- const { width : gridWidth } = gridRef . current ! . getBoundingClientRect ( ) ;
95
- const gridItemPixelSize = 10 ;
96
- const gridGap = 4 ;
97
-
35
+ const GRID_ROW_COUNT = 7 ;
36
+ const GRID_ITEM_SIZE = 10 ;
37
+ const GRID_GAP = 4 ;
38
+ const { width } = gridRef . current ! . getBoundingClientRect ( ) ;
98
39
const columns = Math . floor (
99
- ( gridWidth + gridGap ) / ( gridItemPixelSize + gridGap )
40
+ ( width + GRID_GAP ) / ( GRID_ITEM_SIZE + GRID_GAP )
100
41
) ;
101
42
102
- setGridMaxItems ( 7 * columns ) ;
43
+ setGridMaxItems ( GRID_ROW_COUNT * columns ) ;
103
44
} ;
104
45
105
46
if ( ! gridRef . current ) return ;
@@ -119,18 +60,7 @@ export default function GrassGraph({
119
60
< span > { language === "kor" ? "예전" : "Past" } </ span >
120
61
< span > { language === "kor" ? "최근" : "Present" } </ span >
121
62
</ div >
122
- < GrassGrid ref = { gridRef } >
123
- { parsedDatas . map ( ( v , i ) => {
124
- return (
125
- < Grass
126
- key = { i }
127
- color = { color }
128
- value = { v }
129
- colorBoundary = { colorBoundary }
130
- />
131
- ) ;
132
- } ) }
133
- </ GrassGrid >
63
+ < GrassGrid ref = { gridRef } > { grassItems } </ GrassGrid >
134
64
< GrassColorsetInfo
135
65
leftText = { language === "kor" ? "0분" : "0min" }
136
66
rightText = { language === "kor" ? "2시간" : "2hrs" }
0 commit comments