From 62c71110df5ea4df4720a7a2059b8328901e7161 Mon Sep 17 00:00:00 2001 From: Maxime Beauchemin Date: Tue, 6 Sep 2016 11:20:58 -0700 Subject: [PATCH] Adding a ShrinkSql component (#1058) --- .../SqlLab/components/QueryTable.jsx | 9 +---- .../SqlLab/components/SqlShrink.jsx | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 caravel/assets/javascripts/SqlLab/components/SqlShrink.jsx diff --git a/caravel/assets/javascripts/SqlLab/components/QueryTable.jsx b/caravel/assets/javascripts/SqlLab/components/QueryTable.jsx index 1572421691..576837a1f6 100644 --- a/caravel/assets/javascripts/SqlLab/components/QueryTable.jsx +++ b/caravel/assets/javascripts/SqlLab/components/QueryTable.jsx @@ -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'; @@ -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 = ( - - {source || ''} - + ); q.output = q.tempTable; q.progress = ( diff --git a/caravel/assets/javascripts/SqlLab/components/SqlShrink.jsx b/caravel/assets/javascripts/SqlLab/components/SqlShrink.jsx new file mode 100644 index 0000000000..9cd004ba25 --- /dev/null +++ b/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 ( +
+ + {shrunk} + +
+ ); +}; + +SqlShrink.defaultProps = { + maxWidth: 60, + maxLines: 6, +}; + +SqlShrink.propTypes = { + sql: React.PropTypes.string, + maxWidth: React.PropTypes.integer, + maxLines: React.PropTypes.integer, +}; + +export default SqlShrink;