UI Conversion: Replace buttons and dropdowns to antd analogues
Summary: Replaced favorite query dropdown to antd analogue Reviewed By: mweststrate Differential Revision: D28089366 fbshipit-source-id: 0d6e01bf6169f100ba7c9e612f0da8240ca3218d
This commit is contained in:
committed by
Facebook GitHub Bot
parent
1a315c510e
commit
71d827288f
@@ -16,12 +16,13 @@ import {
|
|||||||
Value,
|
Value,
|
||||||
valueToNullableString,
|
valueToNullableString,
|
||||||
renderValue,
|
renderValue,
|
||||||
Button,
|
|
||||||
styled,
|
styled,
|
||||||
produce,
|
produce,
|
||||||
colors,
|
colors,
|
||||||
} from 'flipper';
|
} from 'flipper';
|
||||||
|
|
||||||
|
import {Button} from 'antd';
|
||||||
|
|
||||||
type TableRow = {
|
type TableRow = {
|
||||||
col: string;
|
col: string;
|
||||||
type: Value['type'];
|
type: Value['type'];
|
||||||
|
|||||||
@@ -13,12 +13,9 @@ import {
|
|||||||
Toolbar,
|
Toolbar,
|
||||||
ManagedTable,
|
ManagedTable,
|
||||||
Text,
|
Text,
|
||||||
Button,
|
|
||||||
ButtonGroup,
|
|
||||||
Input,
|
Input,
|
||||||
colors,
|
colors,
|
||||||
getStringFromErrorLike,
|
getStringFromErrorLike,
|
||||||
Spacer,
|
|
||||||
Textarea,
|
Textarea,
|
||||||
TableBodyColumn,
|
TableBodyColumn,
|
||||||
TableRows,
|
TableRows,
|
||||||
@@ -48,18 +45,30 @@ import {
|
|||||||
Layout,
|
Layout,
|
||||||
useMemoize,
|
useMemoize,
|
||||||
} from 'flipper-plugin';
|
} from 'flipper-plugin';
|
||||||
import {Select, Radio, RadioChangeEvent, Typography} from 'antd';
|
import {
|
||||||
|
Select,
|
||||||
|
Radio,
|
||||||
|
RadioChangeEvent,
|
||||||
|
Typography,
|
||||||
|
Button,
|
||||||
|
Menu,
|
||||||
|
Dropdown,
|
||||||
|
} from 'antd';
|
||||||
import {
|
import {
|
||||||
ConsoleSqlOutlined,
|
ConsoleSqlOutlined,
|
||||||
DatabaseOutlined,
|
DatabaseOutlined,
|
||||||
|
DownOutlined,
|
||||||
HistoryOutlined,
|
HistoryOutlined,
|
||||||
SettingOutlined,
|
SettingOutlined,
|
||||||
|
StarFilled,
|
||||||
|
StarOutlined,
|
||||||
TableOutlined,
|
TableOutlined,
|
||||||
} from '@ant-design/icons';
|
} from '@ant-design/icons';
|
||||||
|
|
||||||
const {Option} = Select;
|
const {Option} = Select;
|
||||||
|
|
||||||
const PAGE_SIZE = 50;
|
const PAGE_SIZE = 50;
|
||||||
|
const FAVORITES_LOCAL_STORAGE_KEY = 'plugin-database-favorites-sql-queries';
|
||||||
|
|
||||||
const BoldSpan = styled.span({
|
const BoldSpan = styled.span({
|
||||||
fontSize: 12,
|
fontSize: 12,
|
||||||
@@ -97,7 +106,6 @@ type DatabasesPluginState = {
|
|||||||
currentSort: TableRowSortOrder | null;
|
currentSort: TableRowSortOrder | null;
|
||||||
query: Query | null;
|
query: Query | null;
|
||||||
queryResult: QueryResult | null;
|
queryResult: QueryResult | null;
|
||||||
favorites: Array<string>;
|
|
||||||
executionTime: number;
|
executionTime: number;
|
||||||
tableInfo: string;
|
tableInfo: string;
|
||||||
queryHistory: Array<Query>;
|
queryHistory: Array<Query>;
|
||||||
@@ -174,11 +182,12 @@ const QueryHistory = React.memo(({history}: {history: Array<Query>}) => {
|
|||||||
};
|
};
|
||||||
const rows: TableRows = [];
|
const rows: TableRows = [];
|
||||||
if (history.length > 0) {
|
if (history.length > 0) {
|
||||||
for (const query of history) {
|
for (let i = 0; i < history.length; i++) {
|
||||||
|
const query = history[i];
|
||||||
const time = query.time;
|
const time = query.time;
|
||||||
const value = query.value;
|
const value = query.value;
|
||||||
rows.push({
|
rows.push({
|
||||||
key: value,
|
key: `${i}`,
|
||||||
columns: {time: {value: time}, query: {value: value}},
|
columns: {time: {value: time}, query: {value: value}},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -387,6 +396,29 @@ const QueryTable = React.memo(
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const FavoritesMenu = React.memo(
|
||||||
|
({
|
||||||
|
favorites,
|
||||||
|
onClick,
|
||||||
|
}: {
|
||||||
|
favorites: string[];
|
||||||
|
onClick: (value: string) => void;
|
||||||
|
}) => {
|
||||||
|
const onMenuClick = useCallback((p: any) => onClick(p.key as string), [
|
||||||
|
onClick,
|
||||||
|
]);
|
||||||
|
return (
|
||||||
|
<Menu>
|
||||||
|
{favorites.map((q) => (
|
||||||
|
<Menu.Item key={q} onClick={onMenuClick}>
|
||||||
|
{q}
|
||||||
|
</Menu.Item>
|
||||||
|
))}
|
||||||
|
</Menu>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
export function plugin(client: PluginClient<Events, Methods>) {
|
export function plugin(client: PluginClient<Events, Methods>) {
|
||||||
const pluginState = createState<DatabasesPluginState>({
|
const pluginState = createState<DatabasesPluginState>({
|
||||||
selectedDatabase: 0,
|
selectedDatabase: 0,
|
||||||
@@ -401,12 +433,19 @@ export function plugin(client: PluginClient<Events, Methods>) {
|
|||||||
currentSort: null,
|
currentSort: null,
|
||||||
query: null,
|
query: null,
|
||||||
queryResult: null,
|
queryResult: null,
|
||||||
favorites: [],
|
|
||||||
executionTime: 0,
|
executionTime: 0,
|
||||||
tableInfo: '',
|
tableInfo: '',
|
||||||
queryHistory: [],
|
queryHistory: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const favoritesState = createState<string[]>([], {persist: 'favorites'});
|
||||||
|
favoritesState.subscribe((favorites) => {
|
||||||
|
localStorage.setItem(
|
||||||
|
FAVORITES_LOCAL_STORAGE_KEY,
|
||||||
|
JSON.stringify(favorites),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
const updateDatabases = (event: {
|
const updateDatabases = (event: {
|
||||||
databases: Array<{name: string; id: number; tables: Array<string>}>;
|
databases: Array<{name: string; id: number; tables: Array<string>}>;
|
||||||
}) => {
|
}) => {
|
||||||
@@ -632,23 +671,15 @@ export function plugin(client: PluginClient<Events, Methods>) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateFavorites = (event: {favorites: Array<string> | undefined}) => {
|
const addOrRemoveQueryToFavorites = (query: string) => {
|
||||||
const state = pluginState.get();
|
favoritesState.update((favorites) => {
|
||||||
const newFavorites = [...(event.favorites || state.favorites)];
|
const index = favorites.indexOf(query);
|
||||||
if (state.query) {
|
|
||||||
const value = state.query.value;
|
|
||||||
const index = newFavorites.indexOf(value);
|
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
newFavorites.push(value);
|
favorites.push(query);
|
||||||
} else {
|
} else {
|
||||||
newFavorites.splice(index, 1);
|
favorites.splice(index, 1);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
pluginState.set({...state, favorites: newFavorites});
|
|
||||||
window.localStorage.setItem(
|
|
||||||
'plugin-database-favorites-sql-queries',
|
|
||||||
JSON.stringify(newFavorites),
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const sortByChanged = (event: {sortOrder: TableRowSortOrder}) => {
|
const sortByChanged = (event: {sortOrder: TableRowSortOrder}) => {
|
||||||
@@ -793,15 +824,21 @@ export function plugin(client: PluginClient<Events, Methods>) {
|
|||||||
databases,
|
databases,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
updateFavorites({
|
const loadedFavoritesJson = localStorage.getItem(
|
||||||
favorites: JSON.parse(
|
FAVORITES_LOCAL_STORAGE_KEY,
|
||||||
localStorage.getItem('plugin-database-favorites-sql-queries') || '[]',
|
);
|
||||||
),
|
if (loadedFavoritesJson) {
|
||||||
});
|
try {
|
||||||
|
favoritesState.set(JSON.parse(loadedFavoritesJson));
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to load favorite queries from local storage');
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
state: pluginState,
|
state: pluginState,
|
||||||
|
favoritesState,
|
||||||
updateDatabases,
|
updateDatabases,
|
||||||
updateSelectedDatabase,
|
updateSelectedDatabase,
|
||||||
updateSelectedDatabaseTable,
|
updateSelectedDatabaseTable,
|
||||||
@@ -817,7 +854,7 @@ export function plugin(client: PluginClient<Events, Methods>) {
|
|||||||
execute,
|
execute,
|
||||||
goToRow,
|
goToRow,
|
||||||
refresh,
|
refresh,
|
||||||
updateFavorites,
|
addOrRemoveQueryToFavorites,
|
||||||
sortByChanged,
|
sortByChanged,
|
||||||
updateQuery,
|
updateQuery,
|
||||||
pageHighlightedRowsChanged,
|
pageHighlightedRowsChanged,
|
||||||
@@ -828,6 +865,7 @@ export function plugin(client: PluginClient<Events, Methods>) {
|
|||||||
export function Component() {
|
export function Component() {
|
||||||
const instance = usePlugin(plugin);
|
const instance = usePlugin(plugin);
|
||||||
const state = useValue(instance.state);
|
const state = useValue(instance.state);
|
||||||
|
const favorites = useValue(instance.favoritesState);
|
||||||
|
|
||||||
const onViewModeChanged = useCallback(
|
const onViewModeChanged = useCallback(
|
||||||
(evt: RadioChangeEvent) => {
|
(evt: RadioChangeEvent) => {
|
||||||
@@ -863,11 +901,11 @@ export function Component() {
|
|||||||
instance.refresh();
|
instance.refresh();
|
||||||
}, [instance]);
|
}, [instance]);
|
||||||
|
|
||||||
const onFavoritesClicked = useCallback(() => {
|
const onFavoriteButtonClicked = useCallback(() => {
|
||||||
instance.updateFavorites({
|
if (state.query) {
|
||||||
favorites: instance.state.get().favorites,
|
instance.addOrRemoveQueryToFavorites(state.query.value);
|
||||||
});
|
}
|
||||||
}, [instance]);
|
}, [instance, state.query]);
|
||||||
|
|
||||||
const onDatabaseSelected = useCallback(
|
const onDatabaseSelected = useCallback(
|
||||||
(selected: string) => {
|
(selected: string) => {
|
||||||
@@ -933,6 +971,15 @@ export function Component() {
|
|||||||
[instance],
|
[instance],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const onFavoriteQuerySelected = useCallback(
|
||||||
|
(query: string) => {
|
||||||
|
instance.updateQuery({
|
||||||
|
value: query,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[instance],
|
||||||
|
);
|
||||||
|
|
||||||
const pageHighlightedRowsChanged = useCallback(
|
const pageHighlightedRowsChanged = useCallback(
|
||||||
(rows: TableHighlightedRows) => {
|
(rows: TableHighlightedRows) => {
|
||||||
instance.pageHighlightedRowsChanged(rows);
|
instance.pageHighlightedRowsChanged(rows);
|
||||||
@@ -1186,48 +1233,36 @@ export function Component() {
|
|||||||
<Toolbar
|
<Toolbar
|
||||||
position="top"
|
position="top"
|
||||||
style={{paddingLeft: 16, paddingTop: 24, paddingBottom: 24}}>
|
style={{paddingLeft: 16, paddingTop: 24, paddingBottom: 24}}>
|
||||||
<ButtonGroup>
|
<Layout.Right>
|
||||||
|
<Layout.Horizontal gap>
|
||||||
<Button
|
<Button
|
||||||
icon={'star'}
|
icon={
|
||||||
iconSize={12}
|
state.query && favorites.includes(state.query.value) ? (
|
||||||
iconVariant={
|
<StarFilled />
|
||||||
state.query !== null &&
|
) : (
|
||||||
typeof state.query !== 'undefined' &&
|
<StarOutlined />
|
||||||
state.favorites.includes(state.query.value)
|
)
|
||||||
? 'filled'
|
|
||||||
: 'outline'
|
|
||||||
}
|
}
|
||||||
onClick={onFavoritesClicked}
|
onClick={onFavoriteButtonClicked}
|
||||||
/>
|
/>
|
||||||
{state.favorites !== null ? (
|
<Dropdown
|
||||||
<Button
|
overlay={
|
||||||
dropdown={state.favorites.map((option) => {
|
<FavoritesMenu
|
||||||
return {
|
favorites={favorites}
|
||||||
click: () => {
|
onClick={onFavoriteQuerySelected}
|
||||||
instance.state.set({
|
/>
|
||||||
...instance.state.get(),
|
}>
|
||||||
query: {
|
<Button onClick={() => {}}>
|
||||||
value: option,
|
Choose from previous queries <DownOutlined />
|
||||||
time: dateFormat(new Date(), 'hh:MM:ss'),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
onQueryChanged;
|
|
||||||
},
|
|
||||||
label: option,
|
|
||||||
};
|
|
||||||
})}>
|
|
||||||
Choose from previous queries
|
|
||||||
</Button>
|
</Button>
|
||||||
) : null}
|
</Dropdown>
|
||||||
</ButtonGroup>
|
</Layout.Horizontal>
|
||||||
<Spacer />
|
|
||||||
<ButtonGroup>
|
|
||||||
<Button
|
<Button
|
||||||
onClick={onExecuteClicked}
|
onClick={onExecuteClicked}
|
||||||
title={'Execute SQL [Ctrl+Return]'}>
|
title={'Execute SQL [Ctrl+Return]'}>
|
||||||
Execute
|
Execute
|
||||||
</Button>
|
</Button>
|
||||||
</ButtonGroup>
|
</Layout.Right>
|
||||||
</Toolbar>
|
</Toolbar>
|
||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
|
|||||||
Reference in New Issue
Block a user