UI Conversion: Use DataTable to show DB table structure
Summary: Use DataTable to show table structure Reviewed By: mweststrate Differential Revision: D28110485 fbshipit-source-id: 81f843a4763e1fbf5acf870621bc9049f719fd2c
This commit is contained in:
committed by
Facebook GitHub Bot
parent
763eda8f6b
commit
23102b1997
@@ -7,85 +7,69 @@
|
|||||||
* @format
|
* @format
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {
|
import {Value, renderValue} from 'flipper';
|
||||||
ManagedTable,
|
import {DataTable, DataTableColumn, Layout, useMemoize} from 'flipper-plugin';
|
||||||
TableBodyRow,
|
import React from 'react';
|
||||||
TableBodyColumn,
|
|
||||||
Value,
|
|
||||||
renderValue,
|
|
||||||
} from 'flipper';
|
|
||||||
import {Layout} from 'flipper-plugin';
|
|
||||||
import React, {useMemo} from 'react';
|
|
||||||
|
|
||||||
import {Structure} from './index';
|
import {Structure} from './index';
|
||||||
|
|
||||||
function transformRow(
|
function createRows(
|
||||||
columns: Array<string>,
|
columns: string[],
|
||||||
row: Array<Value>,
|
rows: Value[][],
|
||||||
index: number,
|
): {[key: string]: Value}[] {
|
||||||
): TableBodyRow {
|
return rows.map((values) =>
|
||||||
const transformedColumns: {[key: string]: TableBodyColumn} = {};
|
values.reduce((acc: {[key: string]: Value}, cur: Value, i: number) => {
|
||||||
for (let i = 0; i < columns.length; i++) {
|
acc[columns[i]] = cur;
|
||||||
transformedColumns[columns[i]] = {value: renderValue(row[i], true)};
|
return acc;
|
||||||
}
|
}, {}),
|
||||||
return {key: String(index), columns: transformedColumns};
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const DatabaseStructureManagedTable = React.memo(
|
function createColumnConfig(columns: string[]) {
|
||||||
(props: {columns: Array<string>; rows: Array<Array<Value>>}) => {
|
const columnObjs: DataTableColumn<{[key: string]: Value}>[] = columns.map(
|
||||||
const {columns, rows} = props;
|
(c) => ({
|
||||||
const renderRows = useMemo(
|
key: c,
|
||||||
() =>
|
title: c,
|
||||||
rows.map((row: Array<Value>, index: number) =>
|
onRender(row) {
|
||||||
transformRow(columns, row, index),
|
return renderValue(row[c]);
|
||||||
),
|
},
|
||||||
[rows, columns],
|
}),
|
||||||
);
|
);
|
||||||
const renderColumns = useMemo(
|
return columnObjs;
|
||||||
() =>
|
}
|
||||||
columns.reduce(
|
|
||||||
(acc, val) =>
|
|
||||||
Object.assign({}, acc, {[val]: {value: val, resizable: true}}),
|
|
||||||
{},
|
|
||||||
),
|
|
||||||
[columns],
|
|
||||||
);
|
|
||||||
const columnOrder = useMemo(
|
|
||||||
() =>
|
|
||||||
columns.map((name) => ({
|
|
||||||
key: name,
|
|
||||||
visible: true,
|
|
||||||
})),
|
|
||||||
[columns],
|
|
||||||
);
|
|
||||||
return (
|
|
||||||
<Layout.Horizontal grow>
|
|
||||||
<ManagedTable
|
|
||||||
floating={false}
|
|
||||||
columnOrder={columnOrder}
|
|
||||||
columns={renderColumns}
|
|
||||||
zebra={true}
|
|
||||||
rows={renderRows}
|
|
||||||
horizontallyScrollable={true}
|
|
||||||
/>
|
|
||||||
</Layout.Horizontal>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
export default React.memo((props: {structure: Structure | null}) => {
|
export default React.memo((props: {structure: Structure}) => {
|
||||||
const {structure} = props;
|
const {structure} = props;
|
||||||
if (!structure) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const {columns, rows, indexesColumns, indexesValues} = structure;
|
const {columns, rows, indexesColumns, indexesValues} = structure;
|
||||||
|
const rowObjs = useMemoize(
|
||||||
|
(columns: string[], rows: Value[][]) => createRows(columns, rows),
|
||||||
|
[columns, rows],
|
||||||
|
);
|
||||||
|
const columnObjs = useMemoize(
|
||||||
|
(columns: string[]) => createColumnConfig(columns),
|
||||||
|
[columns],
|
||||||
|
);
|
||||||
|
const indexRowObjs = useMemoize(
|
||||||
|
(indexesColumns: string[], indexesValues: Value[][]) =>
|
||||||
|
createRows(indexesColumns, indexesValues),
|
||||||
|
[indexesColumns, indexesValues],
|
||||||
|
);
|
||||||
|
const indexColumnObjs = useMemoize(
|
||||||
|
(indexesColumns: string[]) => createColumnConfig(indexesColumns),
|
||||||
|
[indexesColumns],
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<Layout.Top resizable height={400}>
|
||||||
<DatabaseStructureManagedTable columns={columns} rows={rows} />
|
<DataTable<{[key: string]: Value}>
|
||||||
<DatabaseStructureManagedTable
|
records={rowObjs}
|
||||||
columns={indexesColumns}
|
columns={columnObjs}
|
||||||
rows={indexesValues}
|
searchbar={false}
|
||||||
/>
|
/>
|
||||||
</>
|
<DataTable<{[key: string]: Value}>
|
||||||
|
records={indexRowObjs}
|
||||||
|
columns={indexColumnObjs}
|
||||||
|
searchbar={false}
|
||||||
|
/>
|
||||||
|
</Layout.Top>
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import {
|
|||||||
styled,
|
styled,
|
||||||
produce,
|
produce,
|
||||||
ManagedTable,
|
ManagedTable,
|
||||||
colors,
|
|
||||||
getStringFromErrorLike,
|
getStringFromErrorLike,
|
||||||
TableBodyColumn,
|
TableBodyColumn,
|
||||||
TableRows,
|
TableRows,
|
||||||
@@ -80,8 +79,8 @@ const BoldSpan = styled.span({
|
|||||||
textTransform: 'uppercase',
|
textTransform: 'uppercase',
|
||||||
});
|
});
|
||||||
const ErrorBar = styled.div({
|
const ErrorBar = styled.div({
|
||||||
backgroundColor: colors.cherry,
|
backgroundColor: theme.errorColor,
|
||||||
color: colors.white,
|
color: theme.textColorPrimary,
|
||||||
lineHeight: '26px',
|
lineHeight: '26px',
|
||||||
textAlign: 'center',
|
textAlign: 'center',
|
||||||
});
|
});
|
||||||
@@ -1185,7 +1184,9 @@ export function Component() {
|
|||||||
{tableOptions}
|
{tableOptions}
|
||||||
</Select>
|
</Select>
|
||||||
<div />
|
<div />
|
||||||
<Button onClick={onRefreshClicked}>Refresh</Button>
|
<Button onClick={onRefreshClicked} type="default">
|
||||||
|
Refresh
|
||||||
|
</Button>
|
||||||
</Toolbar>
|
</Toolbar>
|
||||||
) : null}
|
) : null}
|
||||||
{state.viewMode === 'SQL' ? (
|
{state.viewMode === 'SQL' ? (
|
||||||
@@ -1260,7 +1261,7 @@ export function Component() {
|
|||||||
currentStructure={state.currentStructure}
|
currentStructure={state.currentStructure}
|
||||||
/>
|
/>
|
||||||
) : null}
|
) : null}
|
||||||
{state.viewMode === 'structure' ? (
|
{state.viewMode === 'structure' && state.currentStructure ? (
|
||||||
<DatabaseStructure structure={state.currentStructure} />
|
<DatabaseStructure structure={state.currentStructure} />
|
||||||
) : null}
|
) : null}
|
||||||
{state.viewMode === 'SQL' ? (
|
{state.viewMode === 'SQL' ? (
|
||||||
|
|||||||
Reference in New Issue
Block a user