1
1
/* eslint-disable react/no-array-index-key */
2
+ /* eslint-disable no-inner-declarations */
2
3
import React from 'react' ;
4
+ import { diff , formatters } from 'jsondiffpatch' ;
3
5
import Action from '../components/Action' ;
4
6
5
7
import { emptySnapshots } from '../actions/actions' ;
@@ -17,15 +19,56 @@ function ActionContainer() {
17
19
let actionsArr = [ ] ;
18
20
// build actions array
19
21
if ( snapshots . length > 0 ) {
22
+ // breadth first function - take in an delta obj
23
+ function breadthFirst ( delta ) {
24
+ // aux array = current and one called next
25
+ let current = Object . values ( delta . children ) ; // Object.keys(delta.children);
26
+ let next = [ ] ;
27
+ let result = '' ;
28
+ // return a string
29
+ while ( current . length > 0 ) {
30
+ const shifted = current . shift ( ) ;
31
+ if ( shifted . state ) {
32
+ // eslint-disable-next-line no-loop-func
33
+ Object . keys ( shifted . state ) . forEach ( key => {
34
+ result = result . concat ( key , ', ' ) ;
35
+ } ) ;
36
+ }
37
+ if ( shifted . children ) {
38
+ Object . keys ( shifted . children ) . forEach ( el => {
39
+ next . push ( shifted . children [ el ] ) ;
40
+ } ) ;
41
+ }
42
+ if ( current . length === 0 ) {
43
+ current = next ;
44
+ next = [ ] ;
45
+ }
46
+ }
47
+ return result ;
48
+ }
49
+
50
+ function truncate ( newDiff , num = 15 ) {
51
+ if ( newDiff . length <= num ) return newDiff . replace ( ',' , '' ) ;
52
+ const thisdiff = newDiff . slice ( 0 , num ) ;
53
+ return thisdiff . concat ( '...' ) ;
54
+ }
20
55
actionsArr = snapshots . map ( ( snapshot , index ) => {
21
56
const selected = index === viewIndex ;
57
+ let newDiff = '' ;
58
+ // if index is greater than 0
59
+ if ( index > 0 ) {
60
+ // calculate the diff
61
+ const delta = diff ( snapshots [ index - 1 ] , snapshot ) ;
62
+ newDiff = truncate ( breadthFirst ( delta ) ) ;
63
+ }
22
64
return (
23
65
< Action
24
66
key = { `action${ index } ` }
25
67
index = { index }
26
68
selected = { selected }
27
69
dispatch = { dispatch }
28
70
sliderIndex = { sliderIndex }
71
+ delta = { newDiff }
29
72
/>
30
73
) ;
31
74
} ) ;
0 commit comments