bito-code-review[bot] commented on code in PR #40191: URL: https://github.com/apache/superset/pull/40191#discussion_r3253262655
########## 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: <div> <div id="suggestion"> <div id="issue"><b>Remove ref.current from deps</b></div> <div id="fix"> The `targetRef.current` value in the dependency array is unnecessary. `ref.current` is a mutable value that doesn't trigger re-renders when changed, so React cannot track changes to it. The `targetRef` object itself (already in deps) is stable across renders. Including `ref.current` triggers ESLint react-hooks/exhaustive-deps warnings. </div> <details> <summary> <b>Code suggestion</b> </summary> <blockquote>Check the AI-generated fix before applying</blockquote> <div id="code"> ``` --- a/superset-frontend/packages/superset-ui-core/src/components/ScrollToBottomButton/useScrollToBottom.ts +++ b/superset-frontend/packages/superset-ui-core/src/components/ScrollToBottomButton/useScrollToBottom.ts @@ -76,6 +76,6 @@ export function useScrollToBottom( resizeObserver.disconnect(); mutationObserver.disconnect(); }; - }, [targetRef, updatePosition, targetRef.current]); + }, [targetRef, updatePosition]); ``` </div> </details> </div> <small><i>Code Review Run #bc7b67</i></small> </div> --- Should Bito avoid suggestions like this for future reviews? (<a href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>) - [ ] Yes, avoid them -- 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]
