Skip to content

Commit a42a50e

Browse files
committed
fix: ensure checkpoint restore popover works when portal container is not immediately available
- Updated useRooPortal hook to use MutationObserver to watch for portal element - Added fallback to undefined when portal container is null in CheckpointMenu - Fixes issue where clicking on checkpoints does nothing Fixes #8563
1 parent eeaafef commit a42a50e

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

webview-ui/src/components/chat/checkpoints/CheckpointMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const CheckpointMenu = ({
8686
</Button>
8787
</PopoverTrigger>
8888
</StandardTooltip>
89-
<PopoverContent align="end" container={portalContainer}>
89+
<PopoverContent align="end" container={portalContainer || undefined}>
9090
<div className="flex flex-col gap-2">
9191
{!isCurrent && (
9292
<div className="flex flex-col gap-1 group hover:text-foreground">
Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
1-
import { useState } from "react"
2-
import { useMount } from "react-use"
1+
import { useState, useEffect } from "react"
32

43
export const useRooPortal = (id: string) => {
5-
const [container, setContainer] = useState<HTMLElement>()
4+
const [container, setContainer] = useState<HTMLElement | null>(null)
65

7-
useMount(() => setContainer(document.getElementById(id) ?? undefined))
6+
useEffect(() => {
7+
// Try to find the element immediately
8+
const element = document.getElementById(id)
9+
if (element) {
10+
setContainer(element)
11+
return
12+
}
13+
14+
// If not found, set up a MutationObserver to watch for it
15+
const observer = new MutationObserver(() => {
16+
const element = document.getElementById(id)
17+
if (element) {
18+
setContainer(element)
19+
observer.disconnect()
20+
}
21+
})
22+
23+
// Start observing the document body for changes
24+
observer.observe(document.body, {
25+
childList: true,
26+
subtree: true,
27+
})
28+
29+
// Cleanup
30+
return () => {
31+
observer.disconnect()
32+
}
33+
}, [id])
834

935
return container
1036
}

0 commit comments

Comments
 (0)