YASHK-arch commented on code in PR #40191: URL: https://github.com/apache/superset/pull/40191#discussion_r3254265662
########## 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: Thank You for your suggestions Bito. Applied the review fix: removed targetRef.current from the useEffect dependency array in useScrollToBottom.ts. ref.current is mutable and does not participate in React’s dependency tracking, so listing it only triggers react-hooks/exhaustive-deps noise without reliable re-subscription. The stable targetRef object plus updatePosition (and the existing useLayoutEffect that calls updatePosition) are sufficient. -- 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]
