eschutho commented on a change in pull request #13032:
URL: https://github.com/apache/superset/pull/13032#discussion_r573360078



##########
File path: superset-frontend/src/SqlLab/components/HighlightedSql.jsx
##########
@@ -41,79 +35,86 @@ const propTypes = {
   shrink: PropTypes.bool,
 };
 
-class HighlightedSql extends React.Component {
-  constructor(props) {
-    super(props);
-    this.state = {
-      modalBody: null,
-    };
-  }
+function HighlightedSql({
+  sql,
+  rawSql,
+  maxWidth = 50,
+  maxLines = 5,
+  shrink = false,
+}) {
+  const [modalBody, setModalBody] = useState(null);
 
-  shrinkSql() {
-    const ssql = this.props.sql || '';
+  const shrinkSql = useCallback(() => {
+    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');
-  }
+  }, [maxLines, maxWidth, sql]);
 
-  triggerNode() {
-    const shownSql = this.props.shrink
-      ? this.shrinkSql(this.props.sql)
-      : this.props.sql;
+  const triggerNode = useCallback(() => {

Review comment:
       I don't think these useCallback method wrappers are going to work in 
this case because we're executing this method in the same file, so memoizing it 
doesn't help. 

##########
File path: superset-frontend/src/SqlLab/components/HighlightedSql.jsx
##########
@@ -41,79 +35,86 @@ const propTypes = {
   shrink: PropTypes.bool,
 };
 
-class HighlightedSql extends React.Component {
-  constructor(props) {
-    super(props);
-    this.state = {
-      modalBody: null,
-    };
-  }
+function HighlightedSql({
+  sql,
+  rawSql,
+  maxWidth = 50,
+  maxLines = 5,
+  shrink = false,
+}) {
+  const [modalBody, setModalBody] = useState(null);
 
-  shrinkSql() {
-    const ssql = this.props.sql || '';
+  const shrinkSql = useCallback(() => {
+    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');
-  }
+  }, [maxLines, maxWidth, sql]);
 
-  triggerNode() {
-    const shownSql = this.props.shrink
-      ? this.shrinkSql(this.props.sql)
-      : this.props.sql;
+  const triggerNode = useCallback(() => {
+    const shownSql = shrink ? shrinkSql(sql) : sql;
     return (
       <SyntaxHighlighter language="sql" style={github}>
         {shownSql}
       </SyntaxHighlighter>
     );
-  }
+  }, [shrink, shrinkSql, sql]);
 
-  generateModal() {
-    let rawSql;
-    if (this.props.rawSql && this.props.rawSql !== this.props.sql) {
-      rawSql = (
+  const generateModal = useCallback(() => {
+    let internalRawSql;
+    if (rawSql && rawSql !== sql) {
+      internalRawSql = (
         <div>
           <h4>{t('Raw SQL')}</h4>
           <SyntaxHighlighter language="sql" style={github}>
-            {this.props.rawSql}
+            {rawSql}
           </SyntaxHighlighter>
         </div>
       );
     }
-    this.setState({
-      modalBody: (
-        <div>
-          <h4>{t('Source SQL')}</h4>
-          <SyntaxHighlighter language="sql" style={github}>
-            {this.props.sql}
-          </SyntaxHighlighter>
-          {rawSql}
-        </div>
-      ),
-    });
-  }
+    setModalBody(
+      <div>
+        <h4>{t('Source SQL')}</h4>
+        <SyntaxHighlighter language="sql" style={github}>
+          {sql}
+        </SyntaxHighlighter>
+        {internalRawSql}
+      </div>,
+    );
+  }, [rawSql, sql]);
 
-  render() {
-    return (
+  const [modalTrigger, setModalTrigger] = useState(
+    <ModalTrigger
+      modalTitle={t('SQL')}
+      triggerNode={triggerNode()}
+      modalBody={modalBody}
+      beforeOpen={() => generateModal()}
+    />,
+  );
+
+  useEffect(() => {
+    setModalTrigger(
       <ModalTrigger
         modalTitle={t('SQL')}
-        triggerNode={this.triggerNode()}
-        modalBody={this.state.modalBody}
-        beforeOpen={this.generateModal.bind(this)}
-      />
+        triggerNode={triggerNode()}
+        modalBody={modalBody}
+        beforeOpen={generateModal}
+      />,

Review comment:
       I know we had this in state before, but this is causing some 
duplication. I think we can simplify this component by a few things: 
   1) beforeOpen and modalBody are essentially the same thing. Let's remove 
beforeOpen altogether
   2) instead of rerendering the triggerNode and modalBody on every render in 
this component, let's just just create a new functional component for each (you 
can keep them in this same file). That way we don't have to store anything in 
state or worry about what's rerendering when. 
   




----------------------------------------------------------------
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:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to