From 5afb148ffa9e267e5b24e0dfae198d1cf46cc396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=BCchele?= Date: Fri, 3 May 2019 02:47:28 -0700 Subject: [PATCH] Migrate to custom plugin Summary: In this step, we want to visualize our mammals in a custom card view. For this reason, we need to replace `createTablePlugin` with a React component that extends `FlipperPlugin`. Reviewed By: jknoxville Differential Revision: D15183324 fbshipit-source-id: 329cdc5c6cbd7e1435538e8cdddf049d914ff221 --- .../tutorial/plugin/SeaMammalFlipperPlugin.kt | 4 +- src/plugins/seamammals/index.js | 155 ++++++++++++++---- 2 files changed, 121 insertions(+), 38 deletions(-) diff --git a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/plugin/SeaMammalFlipperPlugin.kt b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/plugin/SeaMammalFlipperPlugin.kt index 45f2f2be1..91f0d65e8 100644 --- a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/plugin/SeaMammalFlipperPlugin.kt +++ b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/plugin/SeaMammalFlipperPlugin.kt @@ -33,9 +33,9 @@ class SeaMammalFlipperPlugin : FlipperPlugin { connection = null } - override fun runInBackground(): Boolean = false + override fun runInBackground(): Boolean = true private fun newRow(row: FlipperObject) { connection?.send("newRow", row) } -} \ No newline at end of file +} diff --git a/src/plugins/seamammals/index.js b/src/plugins/seamammals/index.js index dd98fdbda..d2ed5c506 100644 --- a/src/plugins/seamammals/index.js +++ b/src/plugins/seamammals/index.js @@ -6,7 +6,18 @@ * @flow strict-local */ -import {Text, Panel, ManagedDataInspector, createTablePlugin} from 'flipper'; +import { + Text, + Panel, + ManagedDataInspector, + FlipperPlugin, + DetailSidebar, + FlexRow, + FlexColumn, + styled, + colors, +} from 'flipper'; +import React from 'react'; type Id = number; @@ -16,24 +27,6 @@ type Row = { url: string, }; -function buildRow(row: Row) { - return { - columns: { - title: { - value: {row.title}, - filterValue: row.title, - }, - url: { - value: {row.url}, - filterValue: row.url, - }, - }, - key: row.id, - copyText: JSON.stringify(row), - filterValue: `${row.title} ${row.url}`, - }; -} - function renderSidebar(row: Row) { return ( @@ -42,24 +35,114 @@ function renderSidebar(row: Row) { ); } -const columns = { - title: { - value: 'Title', - }, - url: { - value: 'URL', - }, +type State = { + selectedIndex: number, }; -const columnSizes = { - title: '15%', - url: 'flex', +type PersistedState = { + data: Array, }; -export default createTablePlugin({ - method: 'newRow', - columns, - columnSizes, - renderSidebar, - buildRow, -}); +export default class SeaMammals extends FlipperPlugin< + State, + void, + PersistedState, +> { + static defaultPersistedState = { + data: [], + }; + + static persistedStateReducer = ( + persistedState: PersistedState, + method: string, + payload: Row, + ) => { + if (method === 'newRow') { + return { + data: [...persistedState.data, payload], + }; + } + return persistedState; + }; + + static Container = styled(FlexRow)({ + backgroundColor: colors.macOSTitleBarBackgroundBlur, + flexWrap: 'wrap', + alignItems: 'flex-start', + alignContent: 'flex-start', + flexGrow: 1, + }); + + state = { + selectedIndex: -1, + }; + + render() { + const {selectedIndex} = this.state; + const {data} = this.props.persistedState; + + return ( + + {data.map((row, i) => ( + this.setState({selectedIndex: i})} + selected={i === selectedIndex} + key={i} + /> + ))} + + {this.state.selectedIndex > -1 && + renderSidebar(this.props.persistedState.data[selectedIndex])} + + + ); + } +} + +class Card extends React.Component<{ + ...Row, + onSelect: () => void, + selected: boolean, +}> { + static Container = styled(FlexColumn)(props => ({ + margin: 10, + borderRadius: 5, + border: '2px solid black', + backgroundColor: colors.white, + borderColor: props.selected + ? colors.macOSTitleBarIconSelected + : colors.white, + padding: 0, + width: 150, + overflow: 'hidden', + boxShadow: '1px 1px 4px rgba(0,0,0,0.1)', + cursor: 'pointer', + })); + + static Image = styled('div')({ + backgroundSize: 'cover', + width: '100%', + paddingTop: '100%', + }); + + static Title = styled(Text)({ + fontSize: 14, + fontWeight: 'bold', + padding: '10px 5px', + overflow: 'hidden', + textOverflow: 'ellipsis', + whiteSpace: 'nowrap', + }); + + render() { + return ( + + + {this.props.title} + + ); + } +}