From 15e4ae5c24891a0a73e4401a291cdfd217006876 Mon Sep 17 00:00:00 2001 From: Pascal Hartig Date: Tue, 27 Apr 2021 09:05:20 -0700 Subject: [PATCH] Convert flipper-messages to Sandy Summary: No logic change or UI update, just 1-to-1 conversion. Reviewed By: mweststrate Differential Revision: D27997791 fbshipit-source-id: 4cde0ef7d749d3117ef95cf67e9ce66e9674f747 --- .../plugins/public/flipper-messages/index.tsx | 160 +++++++++--------- .../public/flipper-messages/package.json | 3 +- 2 files changed, 82 insertions(+), 81 deletions(-) diff --git a/desktop/plugins/public/flipper-messages/index.tsx b/desktop/plugins/public/flipper-messages/index.tsx index adf8f436a..6d9bc127c 100644 --- a/desktop/plugins/public/flipper-messages/index.tsx +++ b/desktop/plugins/public/flipper-messages/index.tsx @@ -13,13 +13,13 @@ import { DetailSidebar, FlexCenter, FlexColumn, - FlipperPlugin, ManagedDataInspector, Panel, SearchableTable, styled, TableHighlightedRows, } from 'flipper'; +import {createState, PluginClient, usePlugin, useValue} from 'flipper-plugin'; import React from 'react'; type MessageInfo = { @@ -67,10 +67,6 @@ type MessageRow = { key: string; }; -type State = { - selectedId: string | null; -}; - type PersistedState = { messageRows: Array; }; @@ -153,86 +149,90 @@ function createRow(message: MessageInfo): MessageRow { }; } -export default class extends FlipperPlugin { - static defaultPersistedState = { +type Events = { + newMessage: MessageInfo; +}; + +export function plugin(client: PluginClient) { + const state = createState({ messageRows: [], - }; - - state: State = { - selectedId: null, - }; - - static persistedStateReducer = ( - persistedState: PersistedState, - method: string, - payload: any, - ): PersistedState => { - if (method === 'newMessage') { - return { - ...persistedState, - messageRows: [...persistedState.messageRows, createRow(payload)].filter( - (row) => Date.now() - row.timestamp < 5 * 60 * 1000, - ), - }; - } - return persistedState; - }; - - render() { - const clearTableButton = ( - - ); - - return ( - - - {this.renderSidebar()} - - ); - } - - onRowHighlighted = (keys: TableHighlightedRows) => { + }); + const highlightedRow = createState(); + const setHighlightedRow = (keys: TableHighlightedRows) => { if (keys.length > 0) { - this.setState({ - selectedId: keys[0], - }); + highlightedRow.set(keys[0]); } }; + const clear = () => { + state.set({messageRows: []}); + highlightedRow.set(null); + }; - renderSidebar() { - const {selectedId} = this.state; - const {messageRows} = this.props.persistedState; - if (selectedId !== null) { - const message = messageRows.find((row) => row.key == selectedId); - if (message != null) { - return this.renderExtra(message.payload); - } - } - return Select a message to view details; - } + client.onMessage('newMessage', (payload) => { + state.update((draft) => { + draft.messageRows = [...draft.messageRows, createRow(payload)].filter( + (row) => Date.now() - row.timestamp < 5 * 60 * 1000, + ); + }); + }); - renderExtra(extra: any) { - return ( - - - - ); - } - - clear = () => { - this.setState({selectedId: null}); - this.props.setPersistedState({messageRows: []}); + return { + state, + highlightedRow, + setHighlightedRow, + clear, }; } + +function Sidebar() { + const instance = usePlugin(plugin); + const rows = useValue(instance.state).messageRows; + const highlightedRow = useValue(instance.highlightedRow); + const message = rows.find((row) => row.key === highlightedRow); + + const renderExtra = (extra: any) => ( + + + + ); + + return ( + <> + {message != null ? ( + renderExtra(message.payload) + ) : ( + Select a message to view details + )} + + ); +} + +export function Component() { + const instance = usePlugin(plugin); + const rows = useValue(instance.state).messageRows; + + const clearTableButton = ( + + ); + + return ( + + + + + + + ); +} diff --git a/desktop/plugins/public/flipper-messages/package.json b/desktop/plugins/public/flipper-messages/package.json index 2bf874ca6..20c4fe70a 100644 --- a/desktop/plugins/public/flipper-messages/package.json +++ b/desktop/plugins/public/flipper-messages/package.json @@ -23,6 +23,7 @@ }, "peerDependencies": { "flipper": "*", - "flipper-pkg": "*" + "flipper-pkg": "*", + "flipper-plugin": "*" } }