Skip to content

Commit

Permalink
Adding a ShrinkSql component (#1058)
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Sep 6, 2016
1 parent 544b3f3 commit 62c7111
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
9 changes: 2 additions & 7 deletions caravel/assets/javascripts/SqlLab/components/QueryTable.jsx
Expand Up @@ -7,12 +7,9 @@ import * as Actions from '../actions';
import moment from 'moment';
import { Table } from 'reactable';
import { ProgressBar } from 'react-bootstrap';

import SyntaxHighlighter from 'react-syntax-highlighter';
import { github } from 'react-syntax-highlighter/dist/styles';

import Link from './Link';
import VisualizeModal from './VisualizeModal';
import SqlShrink from './SqlShrink';
import { STATE_BSSTYLE_MAP } from '../common';
import { fDuration } from '../../modules/dates';

Expand Down Expand Up @@ -47,9 +44,7 @@ class QueryTable extends React.Component {
q.started = moment.utc(q.startDttm).format('HH:mm:ss');
const source = q.ctas ? q.executedSql : q.sql;
q.sql = (
<SyntaxHighlighter language="sql" style={github}>
{source || ''}
</SyntaxHighlighter>
<SqlShrink sql={source} />
);
q.output = q.tempTable;
q.progress = (
Expand Down
38 changes: 38 additions & 0 deletions caravel/assets/javascripts/SqlLab/components/SqlShrink.jsx
@@ -0,0 +1,38 @@
import React from 'react';
import SyntaxHighlighter from 'react-syntax-highlighter';
import { github } from 'react-syntax-highlighter/dist/styles';

const SqlShrink = (props) => {
let lines = props.sql.split('\n');
if (lines.length >= props.maxLines) {
lines = lines.slice(0, props.maxLines);
lines.push('{...}');
}
const shrunk = lines.map((line) => {
if (line.length > props.maxWidth) {
return line.slice(0, props.maxWidth) + '{...}';
}
return line;
})
.join('\n');
return (
<div>
<SyntaxHighlighter language="sql" style={github}>
{shrunk}
</SyntaxHighlighter>
</div>
);
};

SqlShrink.defaultProps = {
maxWidth: 60,
maxLines: 6,
};

SqlShrink.propTypes = {
sql: React.PropTypes.string,
maxWidth: React.PropTypes.integer,
maxLines: React.PropTypes.integer,
};

export default SqlShrink;

0 comments on commit 62c7111

Please sign in to comment.