You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2021/02/12 21:14:40 UTC

[GitHub] [superset] betodealmeida commented on a change in pull request #13098: refactor: HighligthedSQL into functional component with Typescript

betodealmeida commented on a change in pull request #13098:
URL: https://github.com/apache/superset/pull/13098#discussion_r575522878



##########
File path: superset-frontend/src/SqlLab/components/HighlightedSql.tsx
##########
@@ -27,93 +26,91 @@ import ModalTrigger from '../../components/ModalTrigger';
 
 SyntaxHighlighter.registerLanguage('sql', sql);
 
-const defaultProps = {
-  maxWidth: 50,
-  maxLines: 5,
-  shrink: false,
-};
+interface propTypes {
+  sql: string;
+  rawSql?: string;
+  maxWidth?: number;
+  maxLines?: number;
+  shrink?: any;
+}
 
-const propTypes = {
-  sql: PropTypes.string.isRequired,
-  rawSql: PropTypes.string,
-  maxWidth: PropTypes.number,
-  maxLines: PropTypes.number,
-  shrink: PropTypes.bool,
-};
+interface HighlightedSqlModalTypes {
+  rawSql?: string;
+  sql: string;
+}
 
-class HighlightedSql extends React.Component {
-  constructor(props) {
-    super(props);
-    this.state = {
-      modalBody: null,
-    };
-  }
+interface TriggerNodeProps {
+  shrink: boolean;
+  sql: string;
+  maxLines: number;
+  maxWidth: number;
+}
 
-  shrinkSql() {
-    const ssql = this.props.sql || '';
+function TriggerNode({ shrink, sql, maxLines, maxWidth }: TriggerNodeProps) {
+  const shrinkSql = () => {
+    const ssql = sql || '';
     let lines = ssql.split('\n');
-    if (lines.length >= this.props.maxLines) {
-      lines = lines.slice(0, this.props.maxLines);
+    if (lines.length >= maxLines) {
+      lines = lines.slice(0, maxLines);
       lines.push('{...}');
     }
     return lines
       .map(line => {
-        if (line.length > this.props.maxWidth) {
-          return `${line.slice(0, this.props.maxWidth)}{...}`;
+        if (line.length > maxWidth) {
+          return `${line.slice(0, maxWidth)}{...}`;
         }
         return line;
       })
       .join('\n');
-  }
+  };
 
-  triggerNode() {
-    const shownSql = this.props.shrink
-      ? this.shrinkSql(this.props.sql)
-      : this.props.sql;
-    return (
+  return (
+    <SyntaxHighlighter language="sql" style={github}>
+      {shrink ? shrinkSql() : sql}

Review comment:
       Just a nit, but this threw me off for a second because `shrinkSql` has implicit access to `sql`. I think it would be a bit clearer if `shrinkSql` was a function outside the module, so that you'd have here:
   
   ```js
   {shrink ? shrinkSql(sql) : sql}
   ```
   
   Which is easier to read — I can see that line and understand "ok, we either return the original sql or the shrunk sql, depending on the value of `shrink`". With the current approach it's clear that `shrinkSql()` must have access to _some form_ of the SQL, but I have to go and read the source to figure out if it's the same `sql`, or something else.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org