From 17092ed87b53bb7fe7b274decde835b8211d5ba9 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Wed, 12 Aug 2026 18:02:18 +0200 Subject: [PATCH] [Glitch] Update eslint (non-major) Port 3cc36b98c084f8e5c602e56a486f89cfaa34db0b to glitch-soc Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: diondiondion Signed-off-by: Claire --- .../components/carousel/carousel.stories.tsx | 7 ++- .../glitch/components/carousel/index.tsx | 48 ++++++++++--------- .../glitch/components/dropdown_menu.tsx | 2 + .../components/form_fields/combobox_field.tsx | 3 ++ .../scroll_container/scroll_context.tsx | 1 + .../flavours/glitch/features/audio/index.tsx | 17 ++----- .../flavours/glitch/features/video/index.tsx | 16 ++----- .../flavours/glitch/hooks/useMergedRefs.ts | 1 + .../flavours/glitch/hooks/useObserver.ts | 22 ++++----- .../flavours/glitch/hooks/useOverflow.ts | 2 + .../flavours/glitch/hooks/useRevealedMedia.ts | 33 +++++++++++++ 11 files changed, 89 insertions(+), 63 deletions(-) create mode 100644 app/javascript/flavours/glitch/hooks/useRevealedMedia.ts diff --git a/app/javascript/flavours/glitch/components/carousel/carousel.stories.tsx b/app/javascript/flavours/glitch/components/carousel/carousel.stories.tsx index 5117bc08e3..2b98458869 100644 --- a/app/javascript/flavours/glitch/components/carousel/carousel.stories.tsx +++ b/app/javascript/flavours/glitch/components/carousel/carousel.stories.tsx @@ -1,4 +1,4 @@ -import type { FC } from 'react'; +import type { CSSProperties, FC } from 'react'; import type { Meta, StoryObj } from '@storybook/react-vite'; import { fn, userEvent, expect } from 'storybook/test'; @@ -10,16 +10,19 @@ interface TestSlideProps { id: number; text: string; color: string; + style?: CSSProperties; } const TestSlide: FC = ({ active, text, color, + style, }) => (
@@ -114,7 +117,7 @@ export const DifferentHeights: Story = { args: { items: slides.map((props, index) => ({ ...props, - styles: { height: 100 + index * 100 }, + style: { height: 100 + index * 100 }, })), }, }; diff --git a/app/javascript/flavours/glitch/components/carousel/index.tsx b/app/javascript/flavours/glitch/components/carousel/index.tsx index b4ebf751ca..4fa5952a61 100644 --- a/app/javascript/flavours/glitch/components/carousel/index.tsx +++ b/app/javascript/flavours/glitch/components/carousel/index.tsx @@ -1,4 +1,4 @@ -import { useCallback, useLayoutEffect, useMemo, useRef, useState } from 'react'; +import { useCallback, useMemo, useState } from 'react'; import type { ComponentPropsWithoutRef, ComponentType, @@ -75,10 +75,13 @@ export const Carousel = < }: CarouselProps & ComponentPropsWithoutRef<'div'>) => { // Handle slide change const [slideIndex, setSlideIndex] = useState(0); - const wrapperRef = useRef(null); + const [wrapperElement, setWrapperElement] = useState( + null, + ); + // Handle slide heights - const [currentSlideHeight, setCurrentSlideHeight] = useState( - () => wrapperRef.current?.scrollHeight ?? 0, + const [currentSlideHeight, setCurrentSlideHeight] = useState( + null, ); const previousSlideHeight = usePrevious(currentSlideHeight); const handleSlideChange = useCallback( @@ -92,7 +95,7 @@ export const Carousel = < newIndex = 0; } - const slide = wrapperRef.current?.children[newIndex]; + const slide = wrapperElement?.children[newIndex]; if (slide) { setCurrentSlideHeight(slide.scrollHeight); if (slide instanceof HTMLElement) { @@ -103,26 +106,27 @@ export const Carousel = < return newIndex; }); }, - [items.length, onChangeSlide], + [wrapperElement, items.length, onChangeSlide], ); - const observerRef = useRef(null); - observerRef.current ??= new ResizeObserver(() => { + // Update slide height when the component mounts + if (wrapperElement && currentSlideHeight === null) { handleSlideChange(0); - }); + } + + const [observer] = useState( + () => + new ResizeObserver(() => { + handleSlideChange(0); + }), + ); const wrapperStyles = useSpring({ x: `-${slideIndex * 100}%`, - height: currentSlideHeight, + height: currentSlideHeight ?? 0, // Don't animate from zero to the height of the initial slide immediate: !previousSlideHeight, }); - useLayoutEffect(() => { - // Update slide height when the component mounts - if (currentSlideHeight === 0) { - handleSlideChange(0); - } - }, [currentSlideHeight, handleSlideChange]); // Handle swiping animations const bind = useDrag( @@ -135,12 +139,12 @@ export const Carousel = < handleSlideChange(-1); // We're focusing on the wrapper as the child slides can potentially be inert. // Because of that, only the active slide can be focused anyway. - wrapperRef.current?.focus(); - }, [handleSlideChange]); + wrapperElement?.focus(); + }, [handleSlideChange, wrapperElement]); const handleNext = useCallback(() => { handleSlideChange(1); - wrapperRef.current?.focus(); - }, [handleSlideChange]); + wrapperElement?.focus(); + }, [handleSlideChange, wrapperElement]); const intl = useIntl(); @@ -173,7 +177,7 @@ export const Carousel = < item={itemsProps} renderItem={renderItem} - observer={observerRef.current} + observer={observer} index={index} key={`slide-${itemsProps.id}`} className={classNames(`${classNamePrefix}__slide`, slideClassName, { diff --git a/app/javascript/flavours/glitch/components/dropdown_menu.tsx b/app/javascript/flavours/glitch/components/dropdown_menu.tsx index f96a33a995..52e18763d8 100644 --- a/app/javascript/flavours/glitch/components/dropdown_menu.tsx +++ b/app/javascript/flavours/glitch/components/dropdown_menu.tsx @@ -488,6 +488,8 @@ export const Dropdown = ({ }; if (children) { + // We're just assigning the ref to an element so this is safe + // eslint-disable-next-line react-hooks/refs button = cloneElement(Children.only(children), buttonProps); } else if (icon && iconComponent) { button = ( diff --git a/app/javascript/flavours/glitch/components/form_fields/combobox_field.tsx b/app/javascript/flavours/glitch/components/form_fields/combobox_field.tsx index 49f3f4fd16..399a73d4e6 100644 --- a/app/javascript/flavours/glitch/components/form_fields/combobox_field.tsx +++ b/app/javascript/flavours/glitch/components/form_fields/combobox_field.tsx @@ -317,6 +317,9 @@ const ComboboxWithRef = ( // Reset scroll & highlight when menu items change useEffect(() => { if (flatItems.length) { + // This only runs when the items change so should be safe from + // cascade renders. + // eslint-disable-next-line react-hooks/set-state-in-effect resetHighlight(); } }, [flatItems, resetHighlight]); diff --git a/app/javascript/flavours/glitch/containers/scroll_container/scroll_context.tsx b/app/javascript/flavours/glitch/containers/scroll_container/scroll_context.tsx index 8b45eb73ef..a85ce29347 100644 --- a/app/javascript/flavours/glitch/containers/scroll_container/scroll_context.tsx +++ b/app/javascript/flavours/glitch/containers/scroll_container/scroll_context.tsx @@ -74,6 +74,7 @@ export const ScrollContext: React.FC = ({ * than a ref to simplify the types and ensure it's defined immediately. */ const [scrollBehavior] = useState( + // eslint-disable-next-line react-hooks/refs (): ScrollBehaviorInstance => new ScrollBehavior({ addNavigationListener: history.listen.bind(history), diff --git a/app/javascript/flavours/glitch/features/audio/index.tsx b/app/javascript/flavours/glitch/features/audio/index.tsx index 70ca5e5222..8a622f95be 100644 --- a/app/javascript/flavours/glitch/features/audio/index.tsx +++ b/app/javascript/flavours/glitch/features/audio/index.tsx @@ -20,9 +20,11 @@ import { SpoilerButton } from 'flavours/glitch/components/spoiler_button'; import { formatTime, getPointerPosition } from 'flavours/glitch/features/video'; import { useAudioContext } from 'flavours/glitch/hooks/useAudioContext'; import { useAudioVisualizer } from 'flavours/glitch/hooks/useAudioVisualizer'; -import { displayMedia, useBlurhash } from 'flavours/glitch/initial_state'; +import { useBlurhash } from 'flavours/glitch/initial_state'; import { playerSettings } from 'flavours/glitch/settings'; +import { useRevealedMedia } from '../../hooks/useRevealedMedia'; + import { AudioVisualizer } from './visualizer'; const messages = defineMessages({ @@ -100,7 +102,7 @@ export const Audio: React.FC<{ const [volume, setVolume] = useState(0.5); const [hovered, setHovered] = useState(false); const [dragging, setDragging] = useState(false); - const [revealed, setRevealed] = useState(false); + const [revealed, setRevealed] = useRevealedMedia({ visible, sensitive }); const playerRef = useRef(null); const audioRef = useRef(null); @@ -177,17 +179,6 @@ export const Audio: React.FC<{ } }, [volume, muted, gainNodeRef]); - useEffect(() => { - if (typeof visible !== 'undefined') { - setRevealed(visible); - } else { - setRevealed( - displayMedia === 'show_all' || - (displayMedia !== 'hide_all' && !sensitive), - ); - } - }, [visible, sensitive]); - useEffect(() => { if (!revealed) { pauseAudio(); diff --git a/app/javascript/flavours/glitch/features/video/index.tsx b/app/javascript/flavours/glitch/features/video/index.tsx index d054e09a6b..5fffa2fa6a 100644 --- a/app/javascript/flavours/glitch/features/video/index.tsx +++ b/app/javascript/flavours/glitch/features/video/index.tsx @@ -8,6 +8,7 @@ import { useSpring, animated, config } from '@react-spring/web'; import { throttle } from 'lodash'; import type { DeployPictureInPictureCallback } from '@/flavours/glitch/actions/picture_in_picture'; +import { useRevealedMedia } from '@/flavours/glitch/hooks/useRevealedMedia'; import Forward5Icon from '@/material-icons/400-24px/forward_5-fill.svg?react'; import FullscreenIcon from '@/material-icons/400-24px/fullscreen.svg?react'; import FullscreenExitIcon from '@/material-icons/400-24px/fullscreen_exit.svg?react'; @@ -28,7 +29,7 @@ import { attachFullscreenListener, detachFullscreenListener, } from 'flavours/glitch/features/ui/util/fullscreen'; -import { displayMedia, useBlurhash } from 'flavours/glitch/initial_state'; +import { useBlurhash } from 'flavours/glitch/initial_state'; import { playerSettings } from 'flavours/glitch/settings'; import { HotkeyIndicator } from './components/hotkey_indicator'; @@ -218,7 +219,7 @@ export const Video: React.FC<{ const [fullscreen, setFullscreen] = useState(false); const [hovered, setHovered] = useState(false); const [muted, setMuted] = useState(false); - const [revealed, setRevealed] = useState(false); + const [revealed, setRevealed] = useRevealedMedia({ visible, sensitive }); const [hotkeyEvents, setHotkeyEvents] = useState([]); const playerRef = useRef(null); @@ -366,17 +367,6 @@ export const Video: React.FC<{ videoRef.current.muted = muted; }, [volume, muted]); - useEffect(() => { - if (typeof visible !== 'undefined') { - setRevealed(visible); - } else { - setRevealed( - displayMedia === 'show_all' || - (displayMedia !== 'hide_all' && !sensitive), - ); - } - }, [visible, sensitive]); - useEffect(() => { if (!revealed && videoRef.current) { videoRef.current.pause(); diff --git a/app/javascript/flavours/glitch/hooks/useMergedRefs.ts b/app/javascript/flavours/glitch/hooks/useMergedRefs.ts index 0ae63d6c3b..5fd126f8c7 100644 --- a/app/javascript/flavours/glitch/hooks/useMergedRefs.ts +++ b/app/javascript/flavours/glitch/hooks/useMergedRefs.ts @@ -24,5 +24,6 @@ export function useMergedRefs(...refs: React.Ref[]) { }, [refs], ); + // eslint-disable-next-line react-hooks/immutability return setRef; } diff --git a/app/javascript/flavours/glitch/hooks/useObserver.ts b/app/javascript/flavours/glitch/hooks/useObserver.ts index f7bd9454cf..d9c5c6b3ce 100644 --- a/app/javascript/flavours/glitch/hooks/useObserver.ts +++ b/app/javascript/flavours/glitch/hooks/useObserver.ts @@ -1,29 +1,25 @@ -import { useEffect, useRef } from 'react'; +import { useEffect, useState } from 'react'; export function useResizeObserver(callback: ResizeObserverCallback) { - const observerRef = useRef(null); - observerRef.current ??= new ResizeObserver(callback); + const [observer] = useState(() => new ResizeObserver(callback)); useEffect(() => { - const observer = observerRef.current; return () => { - observer?.disconnect(); + observer.disconnect(); }; - }, []); + }, [observer]); - return observerRef.current; + return observer; } export function useMutationObserver(callback: MutationCallback) { - const observerRef = useRef(null); - observerRef.current ??= new MutationObserver(callback); + const [observer] = useState(() => new MutationObserver(callback)); useEffect(() => { - const observer = observerRef.current; return () => { - observer?.disconnect(); + observer.disconnect(); }; - }, []); + }, [observer]); - return observerRef.current; + return observer; } diff --git a/app/javascript/flavours/glitch/hooks/useOverflow.ts b/app/javascript/flavours/glitch/hooks/useOverflow.ts index 40d9cd0775..8f5d3feee8 100644 --- a/app/javascript/flavours/glitch/hooks/useOverflow.ts +++ b/app/javascript/flavours/glitch/hooks/useOverflow.ts @@ -213,6 +213,7 @@ export function useOverflowObservers({ // Watch the wrapper for size changes, and recalculate when it resizes. const wrapperRef = useRef(null); const wrapperRefCallback = useCallback( + // eslint-disable-next-line react-hooks/immutability (node: HTMLElement | null) => { if (node) { wrapperRef.current = node; // eslint-disable-line react-hooks/immutability -- https://github.com/facebook/react/issues/34955 @@ -229,6 +230,7 @@ export function useOverflowObservers({ // If there are changes to the children, recalculate which are visible. const listRefCallback = useCallback( + // eslint-disable-next-line react-hooks/immutability (node: HTMLElement | null) => { if (node) { listRef.current = node; diff --git a/app/javascript/flavours/glitch/hooks/useRevealedMedia.ts b/app/javascript/flavours/glitch/hooks/useRevealedMedia.ts new file mode 100644 index 0000000000..4e6654c0f5 --- /dev/null +++ b/app/javascript/flavours/glitch/hooks/useRevealedMedia.ts @@ -0,0 +1,33 @@ +import { useState } from 'react'; + +import { displayMedia } from 'flavours/glitch/initial_state'; + +interface RevealedMediaProps { + visible?: boolean; + sensitive?: boolean; +} + +function getRevealedState({ visible, sensitive }: RevealedMediaProps) { + if (typeof visible !== 'undefined') { + return visible; + } else { + return ( + displayMedia === 'show_all' || (displayMedia !== 'hide_all' && !sensitive) + ); + } +} + +export function useRevealedMedia({ visible, sensitive }: RevealedMediaProps) { + const [revealed, setRevealed] = useState(() => + getRevealedState({ visible, sensitive }), + ); + + // Update `revealed` state when `sensitive` or `visible` props change + const [previous, setPrevious] = useState({ visible, sensitive }); + if (sensitive !== previous.sensitive || visible !== previous.visible) { + setRevealed(getRevealedState({ visible, sensitive })); + setPrevious({ visible, sensitive }); + } + + return [revealed, setRevealed] as const; +}