|
| 1 | +import { createEffect, createMemo, createSignal, onCleanup, Show, For } from "solid-js" |
| 2 | +import { createStore, reconcile } from "solid-js/store" |
| 3 | +import { AsciiLogo } from "@opencode-ai/ui/logo" |
| 4 | +import { Button } from "@opencode-ai/ui/button" |
| 5 | +import { TextField } from "@opencode-ai/ui/text-field" |
| 6 | +import { Icon } from "@opencode-ai/ui/icon" |
| 7 | +import { normalizeServerUrl, serverDisplayName, useServer } from "@/context/server" |
| 8 | +import { usePlatform } from "@/context/platform" |
| 9 | +import { createOpencodeClient } from "@opencode-ai/sdk/v2/client" |
| 10 | +import { isHostedEnvironment, hasUrlQueryParam, getUrlQueryParam } from "@/utils/hosted" |
| 11 | + |
| 12 | +type ServerStatus = { healthy: boolean; version?: string } |
| 13 | + |
| 14 | +async function checkHealth(url: string, fetch?: typeof globalThis.fetch): Promise<ServerStatus> { |
| 15 | + const sdk = createOpencodeClient({ |
| 16 | + baseUrl: url, |
| 17 | + fetch, |
| 18 | + signal: AbortSignal.timeout(3000), |
| 19 | + }) |
| 20 | + return sdk.global |
| 21 | + .health() |
| 22 | + .then((x) => ({ healthy: x.data?.healthy === true, version: x.data?.version })) |
| 23 | + .catch(() => ({ healthy: false })) |
| 24 | +} |
| 25 | + |
| 26 | +export interface WelcomeScreenProps { |
| 27 | + attemptedUrl?: string |
| 28 | + onRetry?: () => void |
| 29 | +} |
| 30 | + |
| 31 | +export function WelcomeScreen(props: WelcomeScreenProps) { |
| 32 | + const server = useServer() |
| 33 | + const platform = usePlatform() |
| 34 | + const [store, setStore] = createStore({ |
| 35 | + url: "", |
| 36 | + connecting: false, |
| 37 | + error: "", |
| 38 | + status: {} as Record<string, ServerStatus | undefined>, |
| 39 | + }) |
| 40 | + |
| 41 | + const urlOverride = getUrlQueryParam() |
| 42 | + const isLocalhost = () => { |
| 43 | + const url = props.attemptedUrl || "" |
| 44 | + return url.includes("localhost") || url.includes("127.0.0.1") |
| 45 | + } |
| 46 | + |
| 47 | + const items = createMemo(() => { |
| 48 | + const list = server.list |
| 49 | + return list.filter((x) => x !== props.attemptedUrl) |
| 50 | + }) |
| 51 | + |
| 52 | + async function refreshHealth() { |
| 53 | + const results: Record<string, ServerStatus> = {} |
| 54 | + await Promise.all( |
| 55 | + items().map(async (url) => { |
| 56 | + results[url] = await checkHealth(url, platform.fetch) |
| 57 | + }), |
| 58 | + ) |
| 59 | + setStore("status", reconcile(results)) |
| 60 | + } |
| 61 | + |
| 62 | + createEffect(() => { |
| 63 | + if (items().length === 0) return |
| 64 | + refreshHealth() |
| 65 | + const interval = setInterval(refreshHealth, 10_000) |
| 66 | + onCleanup(() => clearInterval(interval)) |
| 67 | + }) |
| 68 | + |
| 69 | + async function handleConnect(url: string, persist = false) { |
| 70 | + const normalized = normalizeServerUrl(url) |
| 71 | + if (!normalized) return |
| 72 | + |
| 73 | + setStore("connecting", true) |
| 74 | + setStore("error", "") |
| 75 | + |
| 76 | + const result = await checkHealth(normalized, platform.fetch) |
| 77 | + setStore("connecting", false) |
| 78 | + |
| 79 | + if (!result.healthy) { |
| 80 | + setStore("error", "Could not connect to server") |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + if (persist) { |
| 85 | + server.add(normalized) |
| 86 | + } else { |
| 87 | + server.setActive(normalized) |
| 88 | + } |
| 89 | + props.onRetry?.() |
| 90 | + } |
| 91 | + |
| 92 | + async function handleSubmit(e: SubmitEvent) { |
| 93 | + e.preventDefault() |
| 94 | + const value = normalizeServerUrl(store.url) |
| 95 | + if (!value) return |
| 96 | + await handleConnect(value, true) |
| 97 | + } |
| 98 | + |
| 99 | + return ( |
| 100 | + <div class="relative flex-1 h-screen w-screen min-h-0 flex flex-col items-center justify-center bg-background-base font-sans p-4"> |
| 101 | + <div class="w-full max-w-lg flex flex-col items-center justify-center gap-6"> |
| 102 | + <AsciiLogo scale={1.0} class="opacity-40 shrink-0" /> |
| 103 | + |
| 104 | + <div class="flex flex-col items-center gap-2 text-center"> |
| 105 | + <h1 class="text-lg font-medium text-text-strong">Welcome to Shuvcode</h1> |
| 106 | + <p class="text-sm text-text-weak"> |
| 107 | + {urlOverride |
| 108 | + ? `Could not connect to the server at ${urlOverride}` |
| 109 | + : "Connect to a Shuvcode server to get started"} |
| 110 | + </p> |
| 111 | + </div> |
| 112 | + |
| 113 | + {/* Local Server Section */} |
| 114 | + <div class="w-full bg-background-subtle rounded-lg p-4 flex flex-col gap-3"> |
| 115 | + <div class="flex items-center gap-2"> |
| 116 | + <Icon name="console" class="text-icon-base" /> |
| 117 | + <h2 class="text-sm font-medium text-text-strong">Local Server</h2> |
| 118 | + </div> |
| 119 | + |
| 120 | + <Show when={isLocalhost()}> |
| 121 | + <div class="text-xs text-text-weak bg-background-base rounded p-3 font-mono"> |
| 122 | + <p class="mb-2">Start a local server by running:</p> |
| 123 | + <code class="text-text-interactive-base">shuvcode</code> |
| 124 | + <p class="mt-2 text-text-weak">or</p> |
| 125 | + <code class="text-text-interactive-base">npx shuvcode</code> |
| 126 | + </div> |
| 127 | + </Show> |
| 128 | + |
| 129 | + <Button |
| 130 | + variant="secondary" |
| 131 | + size="normal" |
| 132 | + onClick={() => handleConnect(props.attemptedUrl || "http://localhost:4096")} |
| 133 | + disabled={store.connecting} |
| 134 | + > |
| 135 | + {store.connecting ? "Connecting..." : "Retry Connection"} |
| 136 | + </Button> |
| 137 | + </div> |
| 138 | + |
| 139 | + {/* Remote Server Section */} |
| 140 | + <div class="w-full bg-background-subtle rounded-lg p-4 flex flex-col gap-3"> |
| 141 | + <div class="flex items-center gap-2"> |
| 142 | + <Icon name="square-arrow-top-right" class="text-icon-base" /> |
| 143 | + <h2 class="text-sm font-medium text-text-strong">Remote Server</h2> |
| 144 | + </div> |
| 145 | + |
| 146 | + <form onSubmit={handleSubmit} class="flex flex-col gap-2"> |
| 147 | + <div class="flex items-start gap-2"> |
| 148 | + <div class="flex-1 min-w-0"> |
| 149 | + <TextField |
| 150 | + type="text" |
| 151 | + label="Server URL" |
| 152 | + hideLabel |
| 153 | + placeholder="https://your-server.example.com" |
| 154 | + value={store.url} |
| 155 | + onChange={(v) => { |
| 156 | + setStore("url", v) |
| 157 | + setStore("error", "") |
| 158 | + }} |
| 159 | + validationState={store.error ? "invalid" : "valid"} |
| 160 | + error={store.error} |
| 161 | + /> |
| 162 | + </div> |
| 163 | + <Button type="submit" variant="primary" size="large" disabled={store.connecting || !store.url.trim()}> |
| 164 | + Connect |
| 165 | + </Button> |
| 166 | + </div> |
| 167 | + </form> |
| 168 | + |
| 169 | + <p class="text-xs text-text-weak"> |
| 170 | + Note: Connecting to a remote server means trusting that server with your data. |
| 171 | + </p> |
| 172 | + </div> |
| 173 | + |
| 174 | + {/* Saved Servers Section */} |
| 175 | + <Show when={items().length > 0}> |
| 176 | + <div class="w-full bg-background-subtle rounded-lg p-4 flex flex-col gap-3"> |
| 177 | + <h2 class="text-sm font-medium text-text-strong">Saved Servers</h2> |
| 178 | + <div class="flex flex-col gap-2"> |
| 179 | + <For each={items()}> |
| 180 | + {(url) => ( |
| 181 | + <button |
| 182 | + type="button" |
| 183 | + class="flex items-center gap-3 p-2 rounded hover:bg-background-base transition-colors text-left" |
| 184 | + onClick={() => handleConnect(url)} |
| 185 | + disabled={store.status[url]?.healthy === false} |
| 186 | + > |
| 187 | + <div |
| 188 | + classList={{ |
| 189 | + "size-2 rounded-full shrink-0": true, |
| 190 | + "bg-icon-success-base": store.status[url]?.healthy === true, |
| 191 | + "bg-icon-critical-base": store.status[url]?.healthy === false, |
| 192 | + "bg-border-weak-base": store.status[url] === undefined, |
| 193 | + }} |
| 194 | + /> |
| 195 | + <span |
| 196 | + class="truncate text-sm" |
| 197 | + classList={{ "text-text-weak": store.status[url]?.healthy === false }} |
| 198 | + > |
| 199 | + {serverDisplayName(url)} |
| 200 | + </span> |
| 201 | + <Show when={store.status[url]?.version}> |
| 202 | + <span class="text-xs text-text-weak ml-auto">{store.status[url]?.version}</span> |
| 203 | + </Show> |
| 204 | + </button> |
| 205 | + )} |
| 206 | + </For> |
| 207 | + </div> |
| 208 | + </div> |
| 209 | + </Show> |
| 210 | + |
| 211 | + {/* Troubleshooting Section */} |
| 212 | + <Show when={isHostedEnvironment()}> |
| 213 | + <details class="w-full text-xs text-text-weak"> |
| 214 | + <summary class="cursor-pointer hover:text-text-base">Troubleshooting</summary> |
| 215 | + <div class="mt-2 p-3 bg-background-subtle rounded-lg flex flex-col gap-2"> |
| 216 | + <p> |
| 217 | + <strong>Server not running:</strong> Make sure you have a Shuvcode server running locally or accessible |
| 218 | + remotely. |
| 219 | + </p> |
| 220 | + <p> |
| 221 | + <strong>CORS blocked:</strong> The server must allow requests from{" "} |
| 222 | + <code class="text-text-interactive-base">{location.origin}</code>. Local servers automatically allow |
| 223 | + this domain. |
| 224 | + </p> |
| 225 | + <p> |
| 226 | + <strong>Mixed content:</strong> If connecting to an <code>http://</code> server from this{" "} |
| 227 | + <code>https://</code> page, your browser may block the connection. Use <code>https://</code> for remote |
| 228 | + servers. |
| 229 | + </p> |
| 230 | + </div> |
| 231 | + </details> |
| 232 | + </Show> |
| 233 | + |
| 234 | + <Show when={platform.version}> |
| 235 | + <p class="text-xs text-text-weak">Version: {platform.version}</p> |
| 236 | + </Show> |
| 237 | + </div> |
| 238 | + </div> |
| 239 | + ) |
| 240 | +} |
0 commit comments