Skip to content

Commit 0cd3741

Browse files
refactor: use useResizeObserver hook from usehooks-ts (#2346)
- Refactor useScrollState in body/index.tsx to use useResizeObserver with onResize callback - Refactor ProfileSection in sidebar/profile/index.tsx to use useResizeObserver with onResize callback - Remove manual ResizeObserver instantiation in favor of the hook Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: yujonglee <[email protected]>
1 parent 29361b4 commit 0cd3741

File tree

2 files changed

+20
-36
lines changed

2 files changed

+20
-36
lines changed

apps/desktop/src/components/main/body/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,18 +564,20 @@ function useScrollState(
564564
});
565565
}, [ref]);
566566

567+
useResizeObserver({
568+
ref: ref as React.RefObject<HTMLDivElement>,
569+
onResize: updateScrollState,
570+
});
571+
567572
useEffect(() => {
568573
const container = ref.current;
569574
if (!container) return;
570575

571576
updateScrollState();
572577
container.addEventListener("scroll", updateScrollState);
573-
const resizeObserver = new ResizeObserver(updateScrollState);
574-
resizeObserver.observe(container);
575578

576579
return () => {
577580
container.removeEventListener("scroll", updateScrollState);
578-
resizeObserver.disconnect();
579581
};
580582
}, [updateScrollState, ...deps]);
581583

apps/desktop/src/components/main/sidebar/profile/index.tsx

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ import {
1010
UsersIcon,
1111
} from "lucide-react";
1212
import { AnimatePresence, motion } from "motion/react";
13-
import {
14-
useCallback,
15-
useEffect,
16-
useLayoutEffect,
17-
useRef,
18-
useState,
19-
} from "react";
13+
import { useCallback, useEffect, useRef, useState } from "react";
14+
import { useResizeObserver } from "usehooks-ts";
2015

2116
import { Kbd, KbdGroup } from "@hypr/ui/components/ui/kbd";
2217
import { cn } from "@hypr/utils";
@@ -69,35 +64,22 @@ export function ProfileSection({ onExpandChange }: ProfileSectionProps = {}) {
6964
}
7065
}, [isExpanded]);
7166

72-
useLayoutEffect(() => {
73-
if (!isExpanded || currentView !== "main") {
74-
return;
75-
}
76-
77-
const element = mainViewRef.current;
78-
if (!element) {
79-
return;
80-
}
81-
82-
const updateHeight = () => {
83-
const height = element.getBoundingClientRect().height;
84-
if (height > 0) {
67+
const handleMainViewResize = useCallback(
68+
({ height }: { width?: number; height?: number }) => {
69+
if (!isExpanded || currentView !== "main") {
70+
return;
71+
}
72+
if (height && height > 0) {
8573
setMainViewHeight(height);
8674
}
87-
};
88-
89-
updateHeight();
90-
91-
const observer = new ResizeObserver(() => {
92-
updateHeight();
93-
});
94-
95-
observer.observe(element);
75+
},
76+
[isExpanded, currentView],
77+
);
9678

97-
return () => {
98-
observer.disconnect();
99-
};
100-
}, [isExpanded, currentView, isAuthenticated]);
79+
useResizeObserver({
80+
ref: mainViewRef as React.RefObject<HTMLDivElement>,
81+
onResize: handleMainViewResize,
82+
});
10183

10284
const profileRef = useAutoCloser(closeMenu, {
10385
esc: isExpanded,

0 commit comments

Comments
 (0)