5
5
6
6
import { queryOptions , useSuspenseQuery } from "@tanstack/react-query" ;
7
7
import { Navigate , createFileRoute , redirect } from "@tanstack/react-router" ;
8
- import { useEffect , useMemo , useRef } from "react" ;
8
+ import { useCallback , useEffect , useMemo , useRef } from "react" ;
9
9
import { preload } from "react-dom" ;
10
10
import { graphql } from "../gql" ;
11
11
import { graphqlRequest } from "../graphql" ;
@@ -50,7 +50,7 @@ function Plan(): React.ReactElement {
50
50
51
51
// Query the size of the iframe content and set the height
52
52
// This will only work where the iframe is served from the same origin
53
- const calculateHeight = ( ) => {
53
+ const calculateHeight = useCallback ( ( ) => {
54
54
const iframe = ref . current ;
55
55
if ( ! iframe ) {
56
56
return ;
@@ -63,21 +63,21 @@ function Plan(): React.ReactElement {
63
63
} else {
64
64
iframe . height = "500px" ;
65
65
}
66
- } ;
66
+ } , [ ] ) ;
67
67
68
68
const observer = useMemo (
69
69
( ) =>
70
70
new MutationObserver ( ( _mutationsList ) => {
71
71
// we calculate the height immediately when the observer is triggered
72
72
calculateHeight ( ) ;
73
- // then we recalculate the height after a short timeout
74
- // to ensure that any layout changes have settled
73
+ // then we recalculate the height after a short timeout to allow for any rendering
74
+ // that doesn't trigger a mutation. e.g. an iframe
75
75
setTimeout ( ( ) => {
76
76
calculateHeight ( ) ;
77
77
} , 1000 ) ;
78
78
// n.b. we don't worry about the timeout happening after the component is unmounted
79
79
} ) ,
80
- [ ] ,
80
+ [ calculateHeight ] ,
81
81
) ;
82
82
83
83
useEffect ( ( ) => {
@@ -87,7 +87,7 @@ function Plan(): React.ReactElement {
87
87
}
88
88
// Cleanup observer when the component unmounts
89
89
return ( ) => observer . disconnect ( ) ;
90
- } , [ ] ) ;
90
+ } , [ observer ] ) ;
91
91
92
92
const attachObserver = ( iframe : HTMLIFrameElement ) => {
93
93
const iframeBody = iframe . contentWindow ?. document . body ;
0 commit comments