bito-code-review[bot] commented on code in PR #40191:
URL: https://github.com/apache/superset/pull/40191#discussion_r3254265856


##########
superset-frontend/packages/superset-ui-core/src/components/ScrollToBottomButton/useScrollToBottom.ts:
##########
@@ -0,0 +1,82 @@
+/**
+ * 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 { useCallback, useEffect, useLayoutEffect, useState } from 'react';
+import type { RefObject } from 'react';
+
+const DEFAULT_THRESHOLD = 32;
+
+function getDistanceFromBottom(element: HTMLElement): number {
+  return element.scrollHeight - element.scrollTop - element.clientHeight;
+}
+
+export function useScrollToBottom(
+  targetRef: RefObject<HTMLElement | null>,
+  threshold = DEFAULT_THRESHOLD,
+) {
+  const [isAtBottom, setIsAtBottom] = useState(true);
+
+  const updatePosition = useCallback(() => {
+    const element = targetRef.current;
+    if (!element) {
+      setIsAtBottom(true);
+      return;
+    }
+    setIsAtBottom(getDistanceFromBottom(element) <= threshold);
+  }, [targetRef, threshold]);
+
+  const scrollToBottom = useCallback(() => {
+    const element = targetRef.current;
+    if (!element) {
+      return;
+    }
+    element.scrollTo({
+      top: element.scrollHeight,
+      behavior: 'smooth',
+    });
+  }, [targetRef]);
+
+  useLayoutEffect(() => {
+    updatePosition();
+  }, [updatePosition]);
+
+  useEffect(() => {
+    const element = targetRef.current;
+    if (!element) {
+      return undefined;
+    }
+
+    updatePosition();
+
+    element.addEventListener('scroll', updatePosition, { passive: true });
+
+    const resizeObserver = new ResizeObserver(updatePosition);
+    resizeObserver.observe(element);
+
+    const mutationObserver = new MutationObserver(updatePosition);
+    mutationObserver.observe(element, { childList: true, subtree: true });
+
+    return () => {
+      element.removeEventListener('scroll', updatePosition);
+      resizeObserver.disconnect();
+      mutationObserver.disconnect();
+    };
+  }, [targetRef, updatePosition, targetRef.current]);

Review Comment:
   <!-- Bito Reply -->
   The suggestion to remove `targetRef.current` from the `useEffect` dependency 
array is valid and appropriate. The `ref.current` is a mutable value that React 
cannot track, so including it in the dependency array only triggers unnecessary 
ESLint warnings without providing any benefit. Removing it aligns with best 
practices and ensures the dependency array contains only stable and relevant 
values (`targetRef` and `updatePosition`).



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

To unsubscribe, e-mail: [email protected]

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