-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathindex.tsx
More file actions
135 lines (126 loc) · 3.63 KB
/
Copy pathindex.tsx
File metadata and controls
135 lines (126 loc) · 3.63 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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
//
// AgentPrompt — a callout that surfaces a ready-made builder prompt at the top
// of a docs page. Readers can copy the prompt or open it directly in an AI
// agent (Claude, ChatGPT, or Gemini) with the prompt pre-filled.
//
// Usage in MDX (registered globally, no import needed):
//
// <AgentPrompt
// prompt="Explain the borrow checker in Move ..."
// />
import React, { useEffect, useRef, useState } from "react";
import styles from "./styles.module.css";
interface Agent {
id: string;
label: string;
url: (prompt: string) => string;
}
const AGENTS: Agent[] = [
{
id: "claude",
label: "Claude",
url: (p) => `https://claude.ai/new?q=${encodeURIComponent(p)}`,
},
{
id: "chatgpt",
label: "ChatGPT",
url: (p) => `https://chatgpt.com/?q=${encodeURIComponent(p)}`,
},
{
id: "gemini",
label: "Gemini",
url: (p) => `https://gemini.google.com/app?q=${encodeURIComponent(p)}`,
},
];
export default function AgentPrompt({ prompt }: { prompt: string }) {
const [open, setOpen] = useState(false);
const [copied, setCopied] = useState(false);
const menuRef = useRef<HTMLDivElement | null>(null);
const [pageName, setPageName] = useState("");
useEffect(() => {
setPageName(
window.location.pathname.replace(/\//g, "+").replace(/^\+/, ""),
);
}, []);
useEffect(() => {
if (!open) return;
const onClick = (e: MouseEvent) => {
if (
menuRef.current &&
!menuRef.current.contains(e.target as Node)
) {
setOpen(false);
}
};
const onKey = (e: KeyboardEvent) => {
if (e.key === "Escape") setOpen(false);
};
document.addEventListener("mousedown", onClick);
document.addEventListener("keydown", onKey);
return () => {
document.removeEventListener("mousedown", onClick);
document.removeEventListener("keydown", onKey);
};
}, [open]);
const copyPrompt = () => {
if (typeof navigator !== "undefined" && navigator.clipboard) {
navigator.clipboard.writeText(prompt);
}
setCopied(true);
setTimeout(() => setCopied(false), 1500);
};
const openAgent = (agent: Agent) => {
if (typeof window !== "undefined") {
window.open(agent.url(prompt), "_blank", "noopener");
}
setOpen(false);
};
return (
<aside className={styles.callout}>
<span className={styles.eyebrow}>
Using an agent? Try this prompt
</span>
<pre className={styles.prompt}>{prompt}</pre>
<div className={styles.actions}>
<button
type="button"
className={styles.copyBtn}
onClick={copyPrompt}
>
{copied ? "Copied" : "Copy prompt"}
</button>
<div className={styles.agentWrap} ref={menuRef}>
<button
type="button"
className={styles.agentBtn}
aria-haspopup="true"
aria-expanded={open}
onClick={() => setOpen((o) => !o)}
>
Open in agent
<span className={styles.caret} aria-hidden="true">
▾
</span>
</button>
{open && (
<div className={styles.dropdown} role="menu">
{AGENTS.map((agent) => (
<button
key={agent.id}
type="button"
role="menuitem"
className={styles.item}
onClick={() => openAgent(agent)}
>
{agent.label}
</button>
))}
</div>
)}
</div>
</div>
</aside>
);
}