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 2020/07/30 07:27:11 UTC

[GitHub] [incubator-superset] etr2460 commented on a change in pull request #10455: chore: Migrate Timer component from jsx to tsx

etr2460 commented on a change in pull request #10455:
URL: https://github.com/apache/incubator-superset/pull/10455#discussion_r462639063



##########
File path: superset-frontend/src/components/Timer.tsx
##########
@@ -0,0 +1,76 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import React, { useEffect, useRef, useState } from 'react';
+import { Label } from 'react-bootstrap';
+
+import { now, fDuration } from '../modules/dates';
+
+interface TimerProps {
+  endTime?: number;
+  isRunning: boolean;
+  startTime?: number;
+  status?: string;
+  style?: object;
+}
+
+export default function Timer({
+  endTime,
+  isRunning,
+  startTime,
+  status = 'success',
+  style,
+}: TimerProps) {
+  const [clockStr, setClockStr] = useState<string>('');

Review comment:
       if we're defaulting this to empty string, then we shouldn't need to specify `<string>`

##########
File path: superset-frontend/src/components/Timer.tsx
##########
@@ -0,0 +1,76 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import React, { useEffect, useRef, useState } from 'react';
+import { Label } from 'react-bootstrap';
+
+import { now, fDuration } from '../modules/dates';
+
+interface TimerProps {
+  endTime?: number;
+  isRunning: boolean;
+  startTime?: number;
+  status?: string;
+  style?: object;

Review comment:
       A type should exist for styles here. possibly `React.CSSProperties`, although it might be something else (maybe from `react-bootstrap`. Let's see if we can use a more specific type

##########
File path: superset-frontend/src/components/Timer.tsx
##########
@@ -0,0 +1,76 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import React, { useEffect, useRef, useState } from 'react';
+import { Label } from 'react-bootstrap';
+
+import { now, fDuration } from '../modules/dates';
+
+interface TimerProps {
+  endTime?: number;
+  isRunning: boolean;
+  startTime?: number;
+  status?: string;
+  style?: object;
+}
+
+export default function Timer({
+  endTime,
+  isRunning,
+  startTime,
+  status = 'success',
+  style,
+}: TimerProps) {
+  const [clockStr, setClockStr] = useState<string>('');
+  const timer = useRef<NodeJS.Timeout>();
+
+  const stopwatch = () => {
+    if (startTime) {
+      const endDttm = endTime || now();
+      if (startTime < endDttm) {
+        setClockStr(fDuration(startTime, endDttm));
+      }
+      if (!isRunning) {
+        clearTimeout(timer.current!);
+      }
+    }
+  };
+
+  function stopTimer() {
+    clearInterval(timer.current!);

Review comment:
       if we get rid of the ref, then we wouldn't need the `!` here either (which would be a bit more type safe).
   
   Otherwise, it might be better to wrap this in an `if` statement anyway, instead of ignoring the types

##########
File path: superset-frontend/src/components/Timer.tsx
##########
@@ -0,0 +1,76 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import React, { useEffect, useRef, useState } from 'react';
+import { Label } from 'react-bootstrap';
+
+import { now, fDuration } from '../modules/dates';
+
+interface TimerProps {
+  endTime?: number;
+  isRunning: boolean;
+  startTime?: number;
+  status?: string;
+  style?: object;
+}
+
+export default function Timer({
+  endTime,
+  isRunning,
+  startTime,
+  status = 'success',
+  style,
+}: TimerProps) {
+  const [clockStr, setClockStr] = useState<string>('');
+  const timer = useRef<NodeJS.Timeout>();
+
+  const stopwatch = () => {
+    if (startTime) {
+      const endDttm = endTime || now();
+      if (startTime < endDttm) {
+        setClockStr(fDuration(startTime, endDttm));
+      }
+      if (!isRunning) {
+        clearTimeout(timer.current!);
+      }
+    }
+  };
+
+  function stopTimer() {

Review comment:
       we're defining functions here in 2 different ways:
   `const stopwatch = () => {`
   and
   `function stopTimer() {`
   
   Personally I prefer the former for inline function definitions, but consistency is more important to me

##########
File path: superset-frontend/src/components/Timer.tsx
##########
@@ -0,0 +1,76 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import React, { useEffect, useRef, useState } from 'react';
+import { Label } from 'react-bootstrap';
+
+import { now, fDuration } from '../modules/dates';
+
+interface TimerProps {
+  endTime?: number;
+  isRunning: boolean;
+  startTime?: number;
+  status?: string;
+  style?: object;
+}
+
+export default function Timer({
+  endTime,
+  isRunning,
+  startTime,
+  status = 'success',
+  style,
+}: TimerProps) {
+  const [clockStr, setClockStr] = useState<string>('');
+  const timer = useRef<NodeJS.Timeout>();

Review comment:
       any reason not to use state for this instead of a ref? The component should be rerendering super often anyway, so i don't think it should have perf implications




----------------------------------------------------------------
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