|
| 1 | +import svg from "./icon.svg"; |
| 2 | +import styles from "./styles.css"; |
| 3 | + |
| 4 | +declare global { |
| 5 | + interface Window { |
| 6 | + _rivetkit_devtools_configs?: Array< |
| 7 | + Parameters<typeof import("rivetkit/client")["createClient"]>[0] |
| 8 | + >; |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +const root = document.createElement("div"); |
| 13 | + |
| 14 | +root.id = "rivetkit-devtools"; |
| 15 | +const shadow = root.attachShadow({ mode: "open" }); |
| 16 | + |
| 17 | +const div = document.createElement("div"); |
| 18 | + |
| 19 | +const img = document.createElement("img"); |
| 20 | +img.src = svg; |
| 21 | +div.appendChild(img); |
| 22 | + |
| 23 | +const btn = document.createElement("button"); |
| 24 | +btn.appendChild(div); |
| 25 | + |
| 26 | +const tooltip = document.createElement("div"); |
| 27 | +tooltip.className = "tooltip"; |
| 28 | +tooltip.textContent = "Open Inspector"; |
| 29 | + |
| 30 | +const style = document.createElement("style"); |
| 31 | +style.textContent = styles; |
| 32 | +shadow.appendChild(style); |
| 33 | +shadow.appendChild(btn); |
| 34 | +shadow.appendChild(tooltip); |
| 35 | + |
| 36 | +btn.addEventListener("mouseenter", () => { |
| 37 | + tooltip.classList.add("visible"); |
| 38 | +}); |
| 39 | + |
| 40 | +btn.addEventListener("mouseleave", () => { |
| 41 | + tooltip.classList.remove("visible"); |
| 42 | +}); |
| 43 | + |
| 44 | +btn.addEventListener("click", () => { |
| 45 | + const config = window._rivetkit_devtools_configs?.[0]; |
| 46 | + if (!config || typeof config !== "object") { |
| 47 | + console.error("RivetKit Devtools: No client config found"); |
| 48 | + return; |
| 49 | + } |
| 50 | + const url = new URL("https://inspect.rivet.dev/"); |
| 51 | + if (!config.endpoint) { |
| 52 | + console.error("RivetKit Devtools: No endpoint found in client config"); |
| 53 | + return; |
| 54 | + } |
| 55 | + url.searchParams.set("u", config.endpoint); |
| 56 | + if (config.token) { |
| 57 | + url.searchParams.set("t", config.token); |
| 58 | + } |
| 59 | + if (config.namespace) { |
| 60 | + url.searchParams.set("ns", config.namespace); |
| 61 | + } |
| 62 | + if (config.runnerName) { |
| 63 | + url.searchParams.set("r", config.runnerName); |
| 64 | + } |
| 65 | + window.open(url.toString(), "_blank"); |
| 66 | +}); |
| 67 | + |
| 68 | +document.body.appendChild(root); |
0 commit comments