This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathCheckpointSaved.tsx
More file actions
110 lines (95 loc) · 3.02 KB
/
Copy pathCheckpointSaved.tsx
File metadata and controls
110 lines (95 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { useMemo, useRef, useState, useEffect } from "react"
import { useTranslation } from "react-i18next"
import { cn } from "@/lib/utils"
import { CheckpointMenu } from "./CheckpointMenu"
import { checkpointSchema } from "./schema"
import { GitCommitVertical } from "lucide-react"
type CheckpointSavedProps = {
ts: number
commitHash: string
currentHash?: string
checkpoint?: Record<string, unknown>
}
export const CheckpointSaved = ({ checkpoint, currentHash, ...props }: CheckpointSavedProps) => {
const { t } = useTranslation()
const isCurrent = currentHash === props.commitHash
const [isPopoverOpen, setIsPopoverOpen] = useState(false)
const [isClosing, setIsClosing] = useState(false)
const [isHovering, setIsHovering] = useState(false)
const closeTimer = useRef<number | null>(null)
useEffect(() => {
return () => {
if (closeTimer.current) {
window.clearTimeout(closeTimer.current)
closeTimer.current = null
}
}
}, [])
const handlePopoverOpenChange = (open: boolean) => {
setIsPopoverOpen(open)
if (open) {
setIsClosing(false)
if (closeTimer.current) {
window.clearTimeout(closeTimer.current)
closeTimer.current = null
}
} else {
setIsClosing(true)
closeTimer.current = window.setTimeout(() => {
setIsClosing(false)
closeTimer.current = null
}, 200) // keep menu visible briefly to avoid popover jump
}
}
const handleMouseEnter = () => {
setIsHovering(true)
}
const handleMouseLeave = () => {
setIsHovering(false)
}
// Menu is visible when hovering, popover is open, or briefly after popover closes
const menuVisible = isHovering || isPopoverOpen || isClosing
const metadata = useMemo(() => {
if (!checkpoint) {
return undefined
}
const result = checkpointSchema.safeParse(checkpoint)
if (!result.success) {
return undefined
}
return result.data
}, [checkpoint])
if (!metadata) {
return null
}
return (
<div
className="flex items-center justify-between gap-2 pt-2 pb-3"
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}>
<div className="flex items-center gap-2 text-blue-400 whitespace-nowrap">
<GitCommitVertical className="w-4" />
<span className="font-semibold">
{metadata.isInitial ? t("chat:checkpoint.initial") : t("chat:checkpoint.regular")}
</span>
{isCurrent && <span className="text-muted">({t("chat:checkpoint.current")})</span>}
</div>
<span
className="block w-full h-[2px] mt-[2px] text-xs"
style={{
backgroundImage:
"linear-gradient(90deg, rgba(0, 188, 255, .65), rgba(0, 188, 255, .65) 80%, rgba(0, 188, 255, 0) 99%)",
}}></span>
{/* Keep menu visible while hovering, popover is open, or briefly after close to prevent jump */}
<div data-testid="checkpoint-menu-container" className={cn("h-4 -mt-2", menuVisible ? "block" : "hidden")}>
<CheckpointMenu
ts={props.ts}
commitHash={props.commitHash}
checkpoint={metadata}
isInitial={metadata.isInitial ?? false}
onOpenChange={handlePopoverOpenChange}
/>
</div>
</div>
)
}