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
This commit is contained in:
Pascal Hartig
2021-04-27 09:05:20 -07:00
committed by Facebook GitHub Bot
parent 37a7b16774
commit 15e4ae5c24
2 changed files with 82 additions and 81 deletions

View File

@@ -13,13 +13,13 @@ import {
DetailSidebar, DetailSidebar,
FlexCenter, FlexCenter,
FlexColumn, FlexColumn,
FlipperPlugin,
ManagedDataInspector, ManagedDataInspector,
Panel, Panel,
SearchableTable, SearchableTable,
styled, styled,
TableHighlightedRows, TableHighlightedRows,
} from 'flipper'; } from 'flipper';
import {createState, PluginClient, usePlugin, useValue} from 'flipper-plugin';
import React from 'react'; import React from 'react';
type MessageInfo = { type MessageInfo = {
@@ -67,10 +67,6 @@ type MessageRow = {
key: string; key: string;
}; };
type State = {
selectedId: string | null;
};
type PersistedState = { type PersistedState = {
messageRows: Array<MessageRow>; messageRows: Array<MessageRow>;
}; };
@@ -153,34 +149,70 @@ function createRow(message: MessageInfo): MessageRow {
}; };
} }
export default class extends FlipperPlugin<State, any, PersistedState> { type Events = {
static defaultPersistedState = { newMessage: MessageInfo;
};
export function plugin(client: PluginClient<Events, {}>) {
const state = createState<PersistedState>({
messageRows: [], messageRows: [],
});
const highlightedRow = createState<string | null>();
const setHighlightedRow = (keys: TableHighlightedRows) => {
if (keys.length > 0) {
highlightedRow.set(keys[0]);
}
};
const clear = () => {
state.set({messageRows: []});
highlightedRow.set(null);
}; };
state: State = { client.onMessage('newMessage', (payload) => {
selectedId: null, state.update((draft) => {
}; draft.messageRows = [...draft.messageRows, createRow(payload)].filter(
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, (row) => Date.now() - row.timestamp < 5 * 60 * 1000,
), );
});
});
return {
state,
highlightedRow,
setHighlightedRow,
clear,
}; };
} }
return persistedState;
};
render() { 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) => (
<Panel floating={false} grow={false} heading={'Payload'}>
<ManagedDataInspector data={extra} expandRoot={false} />
</Panel>
);
return (
<>
{message != null ? (
renderExtra(message.payload)
) : (
<Placeholder grow>Select a message to view details</Placeholder>
)}
</>
);
}
export function Component() {
const instance = usePlugin(plugin);
const rows = useValue(instance.state).messageRows;
const clearTableButton = ( const clearTableButton = (
<Button onClick={this.clear} key="clear"> <Button onClick={instance.clear} key="clear">
Clear Table Clear Table
</Button> </Button>
); );
@@ -193,46 +225,14 @@ export default class extends FlipperPlugin<State, any, PersistedState> {
multiline={true} multiline={true}
columnSizes={COLUMN_SIZES} columnSizes={COLUMN_SIZES}
columns={COLUMNS} columns={COLUMNS}
onRowHighlighted={this.onRowHighlighted} onRowHighlighted={instance.setHighlightedRow}
rows={this.props.persistedState.messageRows} rows={rows}
stickyBottom={true} stickyBottom={true}
actions={[clearTableButton]} actions={[clearTableButton]}
/> />
<DetailSidebar>{this.renderSidebar()}</DetailSidebar> <DetailSidebar>
<Sidebar />
</DetailSidebar>
</FlexColumn> </FlexColumn>
); );
} }
onRowHighlighted = (keys: TableHighlightedRows) => {
if (keys.length > 0) {
this.setState({
selectedId: keys[0],
});
}
};
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 <Placeholder grow>Select a message to view details</Placeholder>;
}
renderExtra(extra: any) {
return (
<Panel floating={false} grow={false} heading={'Payload'}>
<ManagedDataInspector data={extra} expandRoot={false} />
</Panel>
);
}
clear = () => {
this.setState({selectedId: null});
this.props.setPersistedState({messageRows: []});
};
}

View File

@@ -23,6 +23,7 @@
}, },
"peerDependencies": { "peerDependencies": {
"flipper": "*", "flipper": "*",
"flipper-pkg": "*" "flipper-pkg": "*",
"flipper-plugin": "*"
} }
} }