11/* eslint-disable react/no-array-index-key */
2+ /* eslint-disable no-inner-declarations */
23import React from 'react' ;
4+ import { diff , formatters } from 'jsondiffpatch' ;
35import Action from '../components/Action' ;
46
57import { emptySnapshots } from '../actions/actions' ;
@@ -17,15 +19,56 @@ function ActionContainer() {
1719 let actionsArr = [ ] ;
1820 // build actions array
1921 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+ }
2055 actionsArr = snapshots . map ( ( snapshot , index ) => {
2156 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+ }
2264 return (
2365 < Action
2466 key = { `action${ index } ` }
2567 index = { index }
2668 selected = { selected }
2769 dispatch = { dispatch }
2870 sliderIndex = { sliderIndex }
71+ delta = { newDiff }
2972 />
3073 ) ;
3174 } ) ;
0 commit comments