import React from 'react'; import PropTypes from 'prop-types'; import { ButtonGroup, Collapse, Well } from 'react-bootstrap'; import shortid from 'shortid'; import CopyToClipboard from '../../components/CopyToClipboard'; import Link from './Link'; import ColumnElement from './ColumnElement'; import ModalTrigger from '../../components/ModalTrigger'; const propTypes = { table: PropTypes.object, actions: PropTypes.object, timeout: PropTypes.number, // used for tests }; const defaultProps = { actions: {}, table: null, timeout: 500, }; class TableElement extends React.PureComponent { constructor(props) { super(props); this.state = { sortColumns: false, expanded: true, }; } popSelectStar() { const qe = { id: shortid.generate(), title: this.props.table.name, dbId: this.props.table.dbId, autorun: true, sql: this.props.table.selectStar, }; this.props.actions.addQueryEditor(qe); } toggleTable(e) { e.preventDefault(); if (this.props.table.expanded) { this.props.actions.collapseTable(this.props.table); } else { this.props.actions.expandTable(this.props.table); } } removeTable() { this.setState({ expanded: false }); this.props.actions.removeDataPreview(this.props.table); } toggleSortColumns() { this.setState({ sortColumns: !this.state.sortColumns }); } removeFromStore() { this.props.actions.removeTable(this.props.table); } renderHeader() { const table = this.props.table; let header; if (table.partitions) { let partitionQuery; let partitionClipBoard; if (table.partitions.partitionQuery) { partitionQuery = table.partitions.partitionQuery; const tt = 'Copy partition query to clipboard'; partitionClipBoard = ( } /> ); } let latest = []; for (const k in table.partitions.latest) { latest.push(`${k}=${table.partitions.latest[k]}`); } latest = latest.join('/'); header = (
latest partition: {latest} {partitionClipBoard}
); } return header; } renderMetadata() { const table = this.props.table; let cols; if (table.columns) { cols = table.columns.slice(); if (this.state.sortColumns) { cols.sort((a, b) => a.name.toUpperCase() > b.name.toUpperCase()); } } const metadata = (
{this.renderHeader()}
{cols && cols.map(col => ( ))}
); return metadata; } render() { const table = this.props.table; let keyLink; if (table.indexes && table.indexes.length > 0) { keyLink = ( Keys for table {table.name} } modalBody={table.indexes.map((ix, i) => (
{JSON.stringify(ix, null, '  ')}
))} triggerNode={ } /> ); } return (
{keyLink} {table.selectStar && } text={table.selectStar} shouldShowText={false} tooltipText="Copy SELECT statement to clipboard" /> }
{this.renderMetadata()}
); } } TableElement.propTypes = propTypes; TableElement.defaultProps = defaultProps; export default TableElement;