Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: sqleditorleftbar to typescript #17926

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,86 +17,100 @@
* under the License.
*/
import React from 'react';
import PropTypes from 'prop-types';
import Button from 'src/components/Button';
import { t, styled, css } from '@superset-ui/core';
import { t, styled, css, SupersetTheme } from '@superset-ui/core';
import Collapse from 'src/components/Collapse';
import Icons from 'src/components/Icons';
import TableSelector from 'src/components/TableSelector';
import { IconTooltip } from 'src/components/IconTooltip';
import TableElement from '../TableElement';
import { QueryEditor } from 'src/SqlLab/types';
import { DatabaseObject } from 'src/components/DatabaseSelector';
import TableElement, { Table, TableElementProps } from '../TableElement';

const propTypes = {
queryEditor: PropTypes.object.isRequired,
height: PropTypes.number,
tables: PropTypes.array,
actions: PropTypes.object,
database: PropTypes.object,
offline: PropTypes.bool,
};
interface ExtendedTable extends Table {
expanded: boolean;
}

const defaultProps = {
actions: {},
height: 500,
offline: false,
tables: [],
};
interface actionsTypes {
queryEditorSetDb: (queryEditor: QueryEditor, dbId: number) => void;
queryEditorSetFunctionNames: (queryEditor: QueryEditor, dbId: number) => void;
collapseTable: (table: Table) => void;
expandTable: (table: Table) => void;
addTable: (queryEditor: any, value: any, schema: any) => void;
setDatabases: (arg0: any) => {};
addDangerToast: (msg: string) => void;
queryEditorSetSchema: (schema?: string) => void;
queryEditorSetSchemaOptions: () => void;
queryEditorSetTableOptions: (options: Array<any>) => void;
resetState: () => void;
}

interface SqlEditorLeftBarProps {
queryEditor: QueryEditor;
height?: number;
tables?: ExtendedTable[];
actions: actionsTypes & TableElementProps['actions'];
database: DatabaseObject;
}

const StyledScrollbarContainer = styled.div`
flex: 1 1 auto;
overflow: auto;
`;

const collapseStyles = css`
const collapseStyles = (theme: SupersetTheme) => css`
.ant-collapse-item {
margin-bottom: ${({ theme }) => theme.gridUnit * 3}px;
margin-bottom: ${theme.gridUnit * 3}px;
}
.ant-collapse-header {
padding: 0px !important;
display: flex;
align-items: center;
}
.ant-collapse-content-box {
padding: 0px ${({ theme }) => theme.gridUnit * 4}px 0px 0px !important;
padding: 0px ${theme.gridUnit * 4}px 0px 0px !important;
}
.ant-collapse-arrow {
top: ${({ theme }) => theme.gridUnit * 2}px !important;
color: ${({ theme }) => theme.colors.primary.dark1} !important;
top: ${theme.gridUnit * 2}px !important;
color: ${theme.colors.primary.dark1} !important;
&: hover {
color: ${({ theme }) => theme.colors.primary.dark2} !important;
color: ${theme.colors.primary.dark2} !important;
}
}
`;

export default function SqlEditorLeftBar({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't offline be in here with a default variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Offline was removed in my functional conversion PR because the value was never read or used anywhere in the component.

actions,
database,
height,
queryEditor,
tables: tb,
}) {
const onDbChange = db => {
actions.queryEditorSetDb(queryEditor, db.id);
actions.queryEditorSetFunctionNames(queryEditor, db.id);
tables = [],
height = 500,
}: SqlEditorLeftBarProps) {
const onDbChange = ({ id: dbId }: { id: number }) => {
actions.queryEditorSetDb(queryEditor, dbId);
actions.queryEditorSetFunctionNames(queryEditor, dbId);
};

const onTableChange = (tableName, schemaName) => {
const onTableChange = (tableName: string, schemaName: string) => {
if (tableName && schemaName) {
actions.addTable(queryEditor, tableName, schemaName);
}
};

const onToggleTable = tables => {
tb.forEach(table => {
if (!tables.includes(table.id.toString()) && table.expanded) {
const onToggleTable = (updatedTables: string[]) => {
tables.forEach((table: ExtendedTable) => {
if (!updatedTables.includes(table.id.toString()) && table.expanded) {
actions.collapseTable(table);
} else if (tables.includes(table.id.toString()) && !table.expanded) {
} else if (
updatedTables.includes(table.id.toString()) &&
!table.expanded
) {
actions.expandTable(table);
}
});
};

const renderExpandIconWithTooltip = ({ isActive }) => (
const renderExpandIconWithTooltip = ({ isActive }: { isActive: boolean }) => (
<IconTooltip
css={css`
transform: rotate(90deg);
Expand All @@ -122,7 +136,6 @@ export default function SqlEditorLeftBar({
<div className="SqlEditorLeftBar">
<TableSelector
database={database}
dbId={queryEditor.dbId}
getDbList={actions.setDatabases}
handleError={actions.addDangerToast}
onDbChange={onDbChange}
Expand All @@ -137,12 +150,11 @@ export default function SqlEditorLeftBar({
<StyledScrollbarContainer>
<div
css={css`
height: ${props => props.contentHeight}px;
height: ${tableMetaDataHeight}px;
`}
contentHeight={tableMetaDataHeight}
>
<Collapse
activeKey={tb
activeKey={tables
.filter(({ expanded }) => expanded)
.map(({ id }) => id)}
css={collapseStyles}
Expand All @@ -151,7 +163,7 @@ export default function SqlEditorLeftBar({
onChange={onToggleTable}
expandIcon={renderExpandIconWithTooltip}
>
{tb.map(table => (
{tables.map(table => (
<TableElement table={table} key={table.id} actions={actions} />
))}
</Collapse>
Expand All @@ -169,6 +181,3 @@ export default function SqlEditorLeftBar({
</div>
);
}

SqlEditorLeftBar.propTypes = propTypes;
SqlEditorLeftBar.defaultProps = defaultProps;
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface Column {
type: string;
}

interface Table {
export interface Table {
id: string;
name: string;
partitions?: {
Expand All @@ -53,7 +53,7 @@ interface Table {
columns: Column[];
}

interface TableElementProps {
export interface TableElementProps {
table: Table;
actions: {
removeDataPreview: (table: Table) => void;
Expand Down