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:
committed by
Facebook GitHub Bot
parent
37a7b16774
commit
15e4ae5c24
@@ -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,86 +149,90 @@ 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>();
|
||||||
state: State = {
|
const setHighlightedRow = (keys: TableHighlightedRows) => {
|
||||||
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 = (
|
|
||||||
<Button onClick={this.clear} key="clear">
|
|
||||||
Clear Table
|
|
||||||
</Button>
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<FlexColumn grow={true}>
|
|
||||||
<SearchableTable
|
|
||||||
rowLineHeight={28}
|
|
||||||
floating={false}
|
|
||||||
multiline={true}
|
|
||||||
columnSizes={COLUMN_SIZES}
|
|
||||||
columns={COLUMNS}
|
|
||||||
onRowHighlighted={this.onRowHighlighted}
|
|
||||||
rows={this.props.persistedState.messageRows}
|
|
||||||
stickyBottom={true}
|
|
||||||
actions={[clearTableButton]}
|
|
||||||
/>
|
|
||||||
<DetailSidebar>{this.renderSidebar()}</DetailSidebar>
|
|
||||||
</FlexColumn>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
onRowHighlighted = (keys: TableHighlightedRows) => {
|
|
||||||
if (keys.length > 0) {
|
if (keys.length > 0) {
|
||||||
this.setState({
|
highlightedRow.set(keys[0]);
|
||||||
selectedId: keys[0],
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
const clear = () => {
|
||||||
|
state.set({messageRows: []});
|
||||||
|
highlightedRow.set(null);
|
||||||
|
};
|
||||||
|
|
||||||
renderSidebar() {
|
client.onMessage('newMessage', (payload) => {
|
||||||
const {selectedId} = this.state;
|
state.update((draft) => {
|
||||||
const {messageRows} = this.props.persistedState;
|
draft.messageRows = [...draft.messageRows, createRow(payload)].filter(
|
||||||
if (selectedId !== null) {
|
(row) => Date.now() - row.timestamp < 5 * 60 * 1000,
|
||||||
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 {
|
||||||
return (
|
state,
|
||||||
<Panel floating={false} grow={false} heading={'Payload'}>
|
highlightedRow,
|
||||||
<ManagedDataInspector data={extra} expandRoot={false} />
|
setHighlightedRow,
|
||||||
</Panel>
|
clear,
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
clear = () => {
|
|
||||||
this.setState({selectedId: null});
|
|
||||||
this.props.setPersistedState({messageRows: []});
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 = (
|
||||||
|
<Button onClick={instance.clear} key="clear">
|
||||||
|
Clear Table
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<FlexColumn grow={true}>
|
||||||
|
<SearchableTable
|
||||||
|
rowLineHeight={28}
|
||||||
|
floating={false}
|
||||||
|
multiline={true}
|
||||||
|
columnSizes={COLUMN_SIZES}
|
||||||
|
columns={COLUMNS}
|
||||||
|
onRowHighlighted={instance.setHighlightedRow}
|
||||||
|
rows={rows}
|
||||||
|
stickyBottom={true}
|
||||||
|
actions={[clearTableButton]}
|
||||||
|
/>
|
||||||
|
<DetailSidebar>
|
||||||
|
<Sidebar />
|
||||||
|
</DetailSidebar>
|
||||||
|
</FlexColumn>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"flipper": "*",
|
"flipper": "*",
|
||||||
"flipper-pkg": "*"
|
"flipper-pkg": "*",
|
||||||
|
"flipper-plugin": "*"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user