From 16ed255e2db713e773503651e4ea092fa667ee00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=BCchele?= Date: Fri, 3 May 2019 10:49:43 -0700 Subject: [PATCH] merge persistedState by keys Summary: Pushing all new rows into an array could result in duplicate entries. Now the data is stored in an object, using the row's id as a key. This deduplicates the data. Reviewed By: jknoxville Differential Revision: D15200794 fbshipit-source-id: 6afa2b7d02c1bdb796c250400938c38c51fdd207 --- src/plugins/seamammals/index.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/plugins/seamammals/index.js b/src/plugins/seamammals/index.js index d2ed5c506..26dcd49e8 100644 --- a/src/plugins/seamammals/index.js +++ b/src/plugins/seamammals/index.js @@ -36,11 +36,11 @@ function renderSidebar(row: Row) { } type State = { - selectedIndex: number, + selectedID: ?string, }; type PersistedState = { - data: Array, + [key: string]: Row, }; export default class SeaMammals extends FlipperPlugin< @@ -48,9 +48,7 @@ export default class SeaMammals extends FlipperPlugin< void, PersistedState, > { - static defaultPersistedState = { - data: [], - }; + static defaultPersistedState = {}; static persistedStateReducer = ( persistedState: PersistedState, @@ -59,7 +57,8 @@ export default class SeaMammals extends FlipperPlugin< ) => { if (method === 'newRow') { return { - data: [...persistedState.data, payload], + ...persistedState, + [payload.id]: payload, }; } return persistedState; @@ -71,29 +70,30 @@ export default class SeaMammals extends FlipperPlugin< alignItems: 'flex-start', alignContent: 'flex-start', flexGrow: 1, + overflow: 'scroll', }); state = { - selectedIndex: -1, + selectedID: null, }; render() { - const {selectedIndex} = this.state; - const {data} = this.props.persistedState; + const {selectedID} = this.state; + const {persistedState} = this.props; return ( - {data.map((row, i) => ( + {Object.entries(persistedState).map(([id, row]) => ( this.setState({selectedIndex: i})} - selected={i === selectedIndex} - key={i} + onSelect={() => this.setState({selectedID: id})} + selected={id === selectedID} + key={id} /> ))} - {this.state.selectedIndex > -1 && - renderSidebar(this.props.persistedState.data[selectedIndex])} + {typeof selectedID === 'string' && + renderSidebar(persistedState[selectedID])} );