@@ -7,11 +7,7 @@ import { Line, Bar } from "react-chartjs-2";
7
7
import dayjs from "dayjs" ;
8
8
import "./styles.scss" ;
9
9
10
- const subDirectories = [
11
- "rescuetime-time-tracking" ,
12
- "oura-activity" ,
13
- "oura-sleep" ,
14
- ] ;
10
+ const subDirectories = [ "rescuetime-time-tracking" , "oura-activity" , "oura-sleep" ] ;
15
11
16
12
const categoryColors : { [ index : string ] : string } = {
17
13
"Software Development" : "#00429d" ,
@@ -35,8 +31,7 @@ const itemNames: { [index: string]: string } = {
35
31
36
32
const getItemName = ( name : string ) => itemNames [ name ] ?? name ;
37
33
38
- export const zero = ( num : string ) =>
39
- parseInt ( num ) > 9 ? parseInt ( num ) : `0${ num } ` ;
34
+ export const zero = ( num : string ) => ( parseInt ( num ) > 9 ? parseInt ( num ) : `0${ num } ` ) ;
40
35
41
36
const changeLastPart = ( path : string , last : string ) => {
42
37
const key = path . split ( "/" ) ;
@@ -46,29 +41,20 @@ const changeLastPart = (path: string, last: string) => {
46
41
47
42
const cleanValues = ( items : number [ ] , api : string , path : string ) => {
48
43
// if (path.includes("/months/") || path.includes("/days/") || path.includes("/weeks/"))
49
- if ( path . includes ( "/months/" ) )
50
- return items . map ( ( val ) =>
51
- parseFloat ( ( val / ( 3600 * 30.4368499 ) ) . toFixed ( 2 ) )
52
- ) ;
44
+ if ( path . includes ( "/months/" ) ) return items . map ( ( val ) => parseFloat ( ( val / ( 3600 * 30.4368499 ) ) . toFixed ( 2 ) ) ) ;
53
45
return items . map ( ( val ) => parseFloat ( ( val / 3600 ) . toFixed ( 2 ) ) ) ;
54
46
// return items.map((val) => parseInt(String(val)));
55
47
} ;
56
48
const cleanKeys = ( items : string [ ] , api : string , path : string ) => {
57
- if ( path . includes ( "/months/" ) )
58
- return items . map ( ( val ) => dayjs ( `2020-${ zero ( val ) } -15` ) . format ( "MMMM" ) ) ;
59
- if ( path . includes ( "/weeks/" ) )
60
- return items . map ( ( val ) => dayjs ( val ) . format ( "dddd, MMM D" ) ) ;
49
+ if ( path . includes ( "/months/" ) ) return items . map ( ( val ) => dayjs ( `2020-${ zero ( val ) } -15` ) . format ( "MMMM" ) ) ;
50
+ if ( path . includes ( "/weeks/" ) ) return items . map ( ( val ) => dayjs ( val ) . format ( "dddd, MMM D" ) ) ;
61
51
return items ;
62
52
} ;
63
53
const cleanTitle = ( text ?: string , path ?: string ) => {
64
54
if ( ! text ) return "" ;
65
55
text = text . replace ( ".json" , "" ) ;
66
- if ( path ?. includes ( "/days/" ) )
67
- text = dayjs ( `${ path . split ( "/days/" ) [ 1 ] . split ( "/" ) [ 0 ] } -${ text } -10` ) . format (
68
- "MMMM YYYY"
69
- ) ;
70
- if ( path ?. includes ( "/weeks/" ) )
71
- return `Week ${ text } , ${ path . split ( "/weeks/" ) [ 1 ] . split ( "/" ) [ 0 ] } ` ;
56
+ if ( path ?. includes ( "/days/" ) ) text = dayjs ( `${ path . split ( "/days/" ) [ 1 ] . split ( "/" ) [ 0 ] } -${ text } -10` ) . format ( "MMMM YYYY" ) ;
57
+ if ( path ?. includes ( "/weeks/" ) ) return `Week ${ text } , ${ path . split ( "/weeks/" ) [ 1 ] . split ( "/" ) [ 0 ] } ` ;
72
58
return text ;
73
59
} ;
74
60
@@ -92,16 +78,10 @@ const getDatasets = (
92
78
} ) ;
93
79
}
94
80
} ) ;
95
- Object . keys ( total ) . forEach (
96
- ( key ) => ( total [ key ] = cleanValues ( total [ key ] , api , path ) )
97
- ) ;
81
+ Object . keys ( total ) . forEach ( ( key ) => ( total [ key ] = cleanValues ( total [ key ] , api , path ) ) ) ;
98
82
if ( ! allValuesAreNumbers )
99
83
return Object . keys ( total )
100
- . sort (
101
- ( a , b ) =>
102
- Object . keys ( categoryColors ) . indexOf ( a ) -
103
- Object . keys ( categoryColors ) . indexOf ( b )
104
- )
84
+ . sort ( ( a , b ) => Object . keys ( categoryColors ) . indexOf ( a ) - Object . keys ( categoryColors ) . indexOf ( b ) )
105
85
. map ( ( key ) => ( {
106
86
label : key ,
107
87
data : total [ key ] ,
@@ -135,21 +115,9 @@ const App: FunctionComponent<{}> = () => {
135
115
136
116
const getApiData = async ( repo : string , api : string , path : string ) => {
137
117
const key = `${ repo } ${ api } ${ path } ` ;
138
- const cachedValue = window . localStorage . getItem ( key ) ;
139
- if ( cachedValue ) {
140
- const val = JSON . parse ( cachedValue ) ;
141
- if ( dayjs ( val . expiry * 1000 ) . isBefore ( dayjs ( ) ) ) return val . value ;
142
- else window . localStorage . removeItem ( key ) ;
143
- }
144
- const response = await fetch (
145
- `https://raw.githubusercontent.com/${ repo } /master/data/${ api } /${ path } `
146
- ) ;
118
+ const response = await fetch ( `https://raw.githubusercontent.com/${ repo } /master/data/${ api } /${ path } ` ) ;
147
119
if ( ! response . ok ) throw new Error ( ) ;
148
120
const json = await response . json ( ) ;
149
- window . localStorage . setItem (
150
- key ,
151
- JSON . stringify ( { value : json , expiry : dayjs ( ) . add ( 1 , "hour" ) . unix ( ) } )
152
- ) ;
153
121
return json ;
154
122
} ;
155
123
const useMemoApiData = createMemo ( getApiData ) ;
@@ -162,26 +130,20 @@ const App: FunctionComponent<{}> = () => {
162
130
. then ( ( json ) => {
163
131
const items = pick ( latest , json ) ;
164
132
if ( Array . isArray ( items ) ) {
165
- window . location . href = `./?repo=${ encodeURIComponent (
166
- repo
167
- ) } &api= ${ encodeURIComponent ( api ) } & path=${ encodeURIComponent (
133
+ window . location . href = `./?repo=${ encodeURIComponent ( repo ) } &api= ${ encodeURIComponent (
134
+ api
135
+ ) } &path=${ encodeURIComponent (
168
136
`summary/${ latest . replace ( / \. / g, "/" ) } /${ items [ items . length - 1 ] } `
169
- ) } &color=${ encodeURIComponent ( color ) } &chart=${ encodeURIComponent (
170
- chart
171
- ) } `;
137
+ ) } &color=${ encodeURIComponent ( color ) } &chart=${ encodeURIComponent ( chart ) } `;
172
138
} else if ( typeof items === "object" ) {
173
139
const dotted = dot ( items ) ;
174
140
const lastKey = Object . keys ( dotted ) . pop ( ) ;
175
141
if ( lastKey ) {
176
- window . location . href = `./?repo=${ encodeURIComponent (
177
- repo
178
- ) } &api=${ encodeURIComponent ( api ) } &path=${ encodeURIComponent (
179
- `summary/${ latest . replace ( / \. / g, "/" ) } /${ lastKey
180
- . split ( "[" ) [ 0 ]
181
- . replace ( / \. / g, "/" ) } /${ dotted [ lastKey ] } `
182
- ) } &color=${ encodeURIComponent ( color ) } &chart=${ encodeURIComponent (
183
- chart
184
- ) } `;
142
+ window . location . href = `./?repo=${ encodeURIComponent ( repo ) } &api=${ encodeURIComponent (
143
+ api
144
+ ) } &path=${ encodeURIComponent (
145
+ `summary/${ latest . replace ( / \. / g, "/" ) } /${ lastKey . split ( "[" ) [ 0 ] . replace ( / \. / g, "/" ) } /${ dotted [ lastKey ] } `
146
+ ) } &color=${ encodeURIComponent ( color ) } &chart=${ encodeURIComponent ( chart ) } `;
185
147
}
186
148
}
187
149
} )
@@ -202,17 +164,10 @@ const App: FunctionComponent<{}> = () => {
202
164
} ) ;
203
165
if ( subDirectories . includes ( api ) ) {
204
166
if (
205
- JSON . stringify ( latestOptions ) !==
206
- JSON . stringify (
207
- Object . keys ( data [ path . split ( "summary/" ) [ 1 ] . split ( "/" ) [ 0 ] ] )
208
- )
167
+ JSON . stringify ( latestOptions ) !== JSON . stringify ( Object . keys ( data [ path . split ( "summary/" ) [ 1 ] . split ( "/" ) [ 0 ] ] ) )
209
168
)
210
- setLastestOptions (
211
- Object . keys ( data [ path . split ( "summary/" ) [ 1 ] . split ( "/" ) [ 0 ] ] )
212
- ) ;
213
- } else if (
214
- JSON . stringify ( latestOptions ) !== JSON . stringify ( Object . keys ( data ) )
215
- )
169
+ setLastestOptions ( Object . keys ( data [ path . split ( "summary/" ) [ 1 ] . split ( "/" ) [ 0 ] ] ) ) ;
170
+ } else if ( JSON . stringify ( latestOptions ) !== JSON . stringify ( Object . keys ( data ) ) )
216
171
setLastestOptions ( Object . keys ( data ) ) ;
217
172
} )
218
173
. catch ( ( ) => setError ( true ) ) ;
@@ -271,13 +226,9 @@ const App: FunctionComponent<{}> = () => {
271
226
< div >
272
227
{ previous ? (
273
228
< Link
274
- to = { `./?repo=${ encodeURIComponent ( repo ) } &api=${ encodeURIComponent (
275
- api
276
- ) } &path=${ encodeURIComponent (
229
+ to = { `./?repo=${ encodeURIComponent ( repo ) } &api=${ encodeURIComponent ( api ) } &path=${ encodeURIComponent (
277
230
changeLastPart ( path , previous )
278
- ) } &color=${ encodeURIComponent ( color ) } &chart=${ encodeURIComponent (
279
- chart
280
- ) } `}
231
+ ) } &color=${ encodeURIComponent ( color ) } &chart=${ encodeURIComponent ( chart ) } `}
281
232
>
282
233
← { cleanTitle ( previous . replace ( ".json" , "" ) , path ) }
283
234
</ Link >
@@ -287,13 +238,9 @@ const App: FunctionComponent<{}> = () => {
287
238
< div >
288
239
{ next ? (
289
240
< Link
290
- to = { `./?repo=${ encodeURIComponent ( repo ) } &api=${ encodeURIComponent (
291
- api
292
- ) } &path=${ encodeURIComponent (
241
+ to = { `./?repo=${ encodeURIComponent ( repo ) } &api=${ encodeURIComponent ( api ) } &path=${ encodeURIComponent (
293
242
changeLastPart ( path , next )
294
- ) } &color=${ encodeURIComponent ( color ) } &chart=${ encodeURIComponent (
295
- chart
296
- ) } `}
243
+ ) } &color=${ encodeURIComponent ( color ) } &chart=${ encodeURIComponent ( chart ) } `}
297
244
>
298
245
{ cleanTitle ( next , path ) } →
299
246
</ Link >
@@ -320,7 +267,7 @@ const App: FunctionComponent<{}> = () => {
320
267
{
321
268
stacked : true ,
322
269
ticks : {
323
- callback : ( label ) => `${ label } hours` ,
270
+ callback : ( label : string ) => `${ label } hours` ,
324
271
} ,
325
272
} ,
326
273
] ,
@@ -346,7 +293,7 @@ const App: FunctionComponent<{}> = () => {
346
293
{
347
294
stacked : true ,
348
295
ticks : {
349
- callback : ( label ) => `${ label } hours` ,
296
+ callback : ( label : string ) => `${ label } hours` ,
350
297
} ,
351
298
} ,
352
299
] ,
@@ -358,11 +305,7 @@ const App: FunctionComponent<{}> = () => {
358
305
< nav >
359
306
{ latestOptions
360
307
. filter ( ( item ) => ! item . endsWith ( "years" ) )
361
- . sort (
362
- ( a , b ) =>
363
- Object . keys ( itemNames ) . indexOf ( a ) -
364
- Object . keys ( itemNames ) . indexOf ( b )
365
- )
308
+ . sort ( ( a , b ) => Object . keys ( itemNames ) . indexOf ( a ) - Object . keys ( itemNames ) . indexOf ( b ) )
366
309
. map ( ( item ) => (
367
310
< Link
368
311
className = {
@@ -375,15 +318,9 @@ const App: FunctionComponent<{}> = () => {
375
318
: ""
376
319
}
377
320
key = { item }
378
- to = { `./?repo=${ encodeURIComponent ( repo ) } &api=${ encodeURIComponent (
379
- api
380
- ) } &latest=${ encodeURIComponent (
381
- subDirectories . includes ( api )
382
- ? `${ path . split ( "summary/" ) [ 1 ] . split ( "/" ) [ 0 ] } .${ item } `
383
- : item
384
- ) } &color=${ encodeURIComponent ( color ) } &chart=${ encodeURIComponent (
385
- chart
386
- ) } `}
321
+ to = { `./?repo=${ encodeURIComponent ( repo ) } &api=${ encodeURIComponent ( api ) } &latest=${ encodeURIComponent (
322
+ subDirectories . includes ( api ) ? `${ path . split ( "summary/" ) [ 1 ] . split ( "/" ) [ 0 ] } .${ item } ` : item
323
+ ) } &color=${ encodeURIComponent ( color ) } &chart=${ encodeURIComponent ( chart ) } `}
387
324
>
388
325
{ getItemName ( item ) }
389
326
</ Link >
0 commit comments