forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole-trigger.tsx
More file actions
283 lines (260 loc) · 7.87 KB
/
Copy pathconsole-trigger.tsx
File metadata and controls
283 lines (260 loc) · 7.87 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"use client";
import React, { useState, useEffect, useRef } from "react";
import { useCopilotContext } from "../../context/copilot-context";
import { CopilotKitIcon } from "./icons";
import { DeveloperConsoleModal } from "./developer-console-modal";
// Storage key for hiding the Inspector trigger/modal
const INSPECTOR_HIDE_KEY = "cpk:inspector:hidden";
interface ConsoleTriggerProps {
position?: "bottom-left" | "bottom-right";
}
export function ConsoleTrigger({
position = "bottom-right",
}: ConsoleTriggerProps) {
const context = useCopilotContext();
const hasApiKey = Boolean(context.copilotApiConfig.publicApiKey);
const [isModalOpen, setIsModalOpen] = useState(false);
const [isHovered, setIsHovered] = useState(false);
const [isDragging, setIsDragging] = useState(false);
const [buttonPosition, setButtonPosition] = useState<{
x: number;
y: number;
} | null>(null);
const [mounted, setMounted] = useState(false);
const [isHidden, setIsHidden] = useState(false);
const dragRef = useRef<{
startX: number;
startY: number;
buttonX: number;
buttonY: number;
} | null>(null);
const buttonRef = useRef<HTMLButtonElement>(null);
// Initialize on client side only
useEffect(() => {
setMounted(true);
try {
const hidden =
typeof window !== "undefined"
? localStorage.getItem(INSPECTOR_HIDE_KEY)
: null;
if (hidden === "1" || hidden === "true") {
setIsHidden(true);
}
} catch {
// ignore
}
if (typeof window !== "undefined" && !buttonPosition) {
const buttonSize = 60;
const margin = 24;
const initialPosition = {
x: margin,
y: window.innerHeight - buttonSize - margin,
};
setButtonPosition(initialPosition);
}
}, [position]);
const handleMouseDown = (e: React.MouseEvent) => {
e.preventDefault();
if (!buttonPosition) return;
dragRef.current = {
startX: e.clientX,
startY: e.clientY,
buttonX: buttonPosition.x,
buttonY: buttonPosition.y,
};
setIsDragging(true);
};
useEffect(() => {
if (!isDragging) return;
const handleMouseMove = (e: MouseEvent) => {
e.preventDefault();
e.stopPropagation();
if (!dragRef.current) return;
const deltaX = e.clientX - dragRef.current.startX;
const deltaY = e.clientY - dragRef.current.startY;
// Calculate new position
let newX = dragRef.current.buttonX + deltaX;
let newY = dragRef.current.buttonY + deltaY;
// Keep button within viewport bounds
newX = Math.max(0, Math.min(newX, window.innerWidth - 60));
newY = Math.max(0, Math.min(newY, window.innerHeight - 60));
setButtonPosition({ x: newX, y: newY });
};
const handleMouseUp = (e: MouseEvent) => {
e.preventDefault();
e.stopPropagation();
setIsDragging(false);
dragRef.current = null;
};
// Use capture phase to intercept events before they reach other handlers
document.addEventListener("mousemove", handleMouseMove, {
capture: true,
passive: false,
});
document.addEventListener("mouseup", handleMouseUp, {
capture: true,
passive: false,
});
return () => {
document.removeEventListener("mousemove", handleMouseMove, {
capture: true,
});
document.removeEventListener("mouseup", handleMouseUp, { capture: true });
};
}, [isDragging]);
// Don't render until mounted and position is initialized
if (!mounted || !buttonPosition || isHidden) {
return null;
}
return (
<>
<button
ref={buttonRef}
onClick={(e) => {
if (!isDragging) {
// Modifier-click hides
if (e.metaKey || e.altKey) {
try {
localStorage.setItem(INSPECTOR_HIDE_KEY, "1");
} catch {}
setIsHidden(true);
return;
}
setIsModalOpen(true);
}
}}
onContextMenu={(e) => {
e.preventDefault();
try {
localStorage.setItem(INSPECTOR_HIDE_KEY, "1");
} catch {}
setIsHidden(true);
}}
onMouseDown={handleMouseDown}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
style={{
position: "fixed",
left: `${buttonPosition.x}px`,
top: `${buttonPosition.y}px`,
zIndex: 2147483647,
width: "60px",
height: "60px",
background: isDragging
? "#000000"
: isHovered
? "#111111"
: "#000000",
color: "white",
borderRadius: "50%",
boxShadow: isDragging
? "0 8px 32px rgba(0, 0, 0, 0.6), 0 4px 16px rgba(0, 0, 0, 0.4)"
: isHovered
? "0 12px 40px rgba(0, 0, 0, 0.7), 0 6px 20px rgba(0, 0, 0, 0.5)"
: "0 6px 20px rgba(0, 0, 0, 0.5), 0 3px 10px rgba(0, 0, 0, 0.3)",
transition: isDragging
? "none"
: "all 0.3s cubic-bezier(0.4, 0, 0.2, 1)",
display: "flex",
alignItems: "center",
justifyContent: "center",
border: "none",
cursor: isDragging ? "grabbing" : "grab",
opacity: 1,
userSelect: "none",
transform: isDragging
? "scale(1.05)"
: isHovered
? "scale(1.1)"
: "scale(1)",
backdropFilter: "blur(10px)",
pointerEvents: "auto",
isolation: "isolate",
}}
title={
hasApiKey
? "Open Inspector (Drag to move)"
: "Inspector (License Key Required, Drag to move)"
}
>
{/* Close (hide) control */}
<div
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
try {
localStorage.setItem(INSPECTOR_HIDE_KEY, "1");
} catch {
// ignore
}
setIsHidden(true);
}}
style={{
position: "absolute",
bottom: "2px",
right: "2px",
width: "20px",
height: "20px",
borderRadius: "50%",
background: "#ffffff",
color: "#ef4444",
fontSize: "14px",
lineHeight: "18px",
textAlign: "center",
boxShadow: "0 2px 6px rgba(0,0,0,0.35)",
cursor: "pointer",
border: "1px solid #e5e7eb",
display: "flex",
alignItems: "center",
justifyContent: "center",
zIndex: 1,
}}
title="Hide Inspector"
>
×
</div>
<div
style={{
width: "28px",
height: "28px",
display: "flex",
alignItems: "center",
justifyContent: "center",
filter: "drop-shadow(0 2px 4px rgba(0,0,0,0.2))",
}}
>
<CopilotKitIcon />
</div>
{!hasApiKey && (
<div
style={{
position: "absolute",
top: "-2px",
right: "-2px",
width: "18px",
height: "18px",
background: "linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%)",
borderRadius: "50%",
display: "flex",
alignItems: "center",
justifyContent: "center",
boxShadow: "0 2px 8px rgba(255, 107, 107, 0.4)",
border: "2px solid white",
}}
>
<span
style={{ fontSize: "10px", color: "white", fontWeight: "bold" }}
>
!
</span>
</div>
)}
</button>
<DeveloperConsoleModal
isOpen={isModalOpen}
onClose={() => setIsModalOpen(false)}
hasApiKey={hasApiKey}
/>
</>
);
}