From e15935acb84771237fee072b58109153741ae61c Mon Sep 17 00:00:00 2001 From: Dawid Poliszak Date: Wed, 27 Nov 2024 14:31:23 +0100 Subject: [PATCH] fix: Maximum update depth exceeded error --- src/components/window/WindowFrame.tsx | 36 ++++++--------------------- src/themeHooks.tsx | 2 +- 2 files changed, 8 insertions(+), 30 deletions(-) diff --git a/src/components/window/WindowFrame.tsx b/src/components/window/WindowFrame.tsx index e6c0d4a1..3b23b867 100644 --- a/src/components/window/WindowFrame.tsx +++ b/src/components/window/WindowFrame.tsx @@ -1,5 +1,4 @@ import { css, cx } from "@emotion/css"; -import { isEqual } from "lodash"; import { mapValues } from "lodash/fp"; import React, { forwardRef, PropsWithChildren, RefObject, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"; import FocusLock from "react-focus-lock"; @@ -114,6 +113,7 @@ export const WindowFrame = forwardRef((props: PropsWithChildren { - if (ref.current) { + if (ref.current && !wasMaximized) { const randomize = mapValues((v: number) => Math.max(0, v + random(randomizePosition))); const { height, width } = ref.current.getBoundingClientRect(); const x = (viewport.width - width) / 2; - const y = (viewport.height * 0.75 - height) / 2; + const y = (viewport.height * 0.65 - height) / 2; const center = randomize({ x, y }); setPosition( roundCoords({ - x: initialPosition.x ?? center.x, - y: initialPosition.y ?? center.y, + x: initialPosition.x ?? calcCoord(center.x, center.x + width, width, viewport.width, windowMargin), + y: initialPosition.y ?? calcCoord(center.y, center.y + height, height, viewport.height, windowMargin), }), ); } }, - [randomizePosition, viewport.height, viewport.width], + [randomizePosition, viewport.height, viewport.width, wasMaximized, windowMargin], ); const onContentChanged = useCallback(() => { @@ -154,28 +154,6 @@ export const WindowFrame = forwardRef((props: PropsWithChildren { - const width = size?.width || box?.width || 0; - const height = size?.height || box?.height || 0; - return roundCoords({ - x: calcCoord(box.x, box.x + box.width, width, viewport.width, windowMargin), - y: calcCoord(box.y, box.y + box.height, height, viewport.height, windowMargin), - }); - }, - [size, windowMargin], - ); - - useLayoutEffect(() => { - if (contentAvailable && position && !(maximized || wasMaximized)) { - const newValue = calcEdgePosition(viewport); - setPosition((current) => (isEqual(newValue, current) ? current : newValue)); - } - }, [contentAvailable, wasMaximized, calcEdgePosition, maximized, position, viewport]); - const savePosition = useCallback((position: Position) => !maximized && setPosition(roundCoords(position)), [maximized]); const [side, setSide] = useState(Side.none); @@ -260,7 +238,7 @@ export const WindowFrame = forwardRef((props: PropsWithChildren ({ width: `calc(100% - ${position?.x <= windowMargin ? windowMargin * 2 : position?.x || 0}px)`, - height: viewport.height - (position?.y <= windowMargin ? windowMargin * 2 : position?.y || 0), + height: viewport.height - (position?.y <= windowMargin ? windowMargin * 2 : position?.y + windowMargin || 0), }), [windowMargin, position, viewport.height], ); diff --git a/src/themeHooks.tsx b/src/themeHooks.tsx index 5fa9f7cb..432c8b14 100644 --- a/src/themeHooks.tsx +++ b/src/themeHooks.tsx @@ -1,6 +1,6 @@ import { css, cx } from "@emotion/css"; import { useTheme } from "@emotion/react"; -import React, { useMemo } from "react"; +import { useMemo } from "react"; import { rgba } from "./rgba"; export function useFrameTheme() {