|
| 1 | +import { createSignal, Show, onMount } from "solid-js" |
| 2 | +import { createStore } from "solid-js/store" |
| 3 | +import { TextareaRenderable, TextAttributes } from "@opentui/core" |
| 4 | +import { useKeyboard } from "@opentui/solid" |
| 5 | +import { useDialog } from "@tui/ui/dialog" |
| 6 | +import { useSDK } from "../context/sdk" |
| 7 | +import { useSync } from "@tui/context/sync" |
| 8 | +import { useTheme } from "../context/theme" |
| 9 | +import { AltimateApi } from "@/altimate/api/client" |
| 10 | +import { Filesystem } from "@/util/filesystem" |
| 11 | + |
| 12 | +export function DialogAltimateLogin() { |
| 13 | + const dialog = useDialog() |
| 14 | + const sdk = useSDK() |
| 15 | + const sync = useSync() |
| 16 | + const { theme } = useTheme() |
| 17 | + const [error, setError] = createSignal("") |
| 18 | + const [validating, setValidating] = createSignal(false) |
| 19 | + const [store, setStore] = createStore({ |
| 20 | + active: "instance" as "instance" | "key" | "url", |
| 21 | + }) |
| 22 | + |
| 23 | + let instanceRef: TextareaRenderable |
| 24 | + let keyRef: TextareaRenderable |
| 25 | + let urlRef: TextareaRenderable |
| 26 | + |
| 27 | + const fields = ["instance", "key", "url"] as const |
| 28 | + |
| 29 | + function focusActive() { |
| 30 | + setTimeout(() => { |
| 31 | + const ref = { instance: instanceRef, key: keyRef, url: urlRef }[store.active] |
| 32 | + if (ref && !ref.isDestroyed) ref.focus() |
| 33 | + }, 1) |
| 34 | + } |
| 35 | + |
| 36 | + useKeyboard((evt) => { |
| 37 | + if (evt.name === "tab") { |
| 38 | + const idx = fields.indexOf(store.active) |
| 39 | + const next = fields[(idx + 1) % fields.length] |
| 40 | + setStore("active", next) |
| 41 | + focusActive() |
| 42 | + evt.preventDefault() |
| 43 | + } |
| 44 | + if (evt.name === "return") { |
| 45 | + if (validating()) return |
| 46 | + void submit().catch((e) => setError(`Unexpected error: ${e?.message ?? e}`)) |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + onMount(() => { |
| 51 | + dialog.setSize("medium") |
| 52 | + focusActive() |
| 53 | + }) |
| 54 | + |
| 55 | + async function submit() { |
| 56 | + const instance = instanceRef.plainText.trim() |
| 57 | + const key = keyRef.plainText.trim() |
| 58 | + const url = urlRef.plainText.trim().replace(/\/+$/, "") |
| 59 | + |
| 60 | + if (!instance) { |
| 61 | + setError("Instance name is required") |
| 62 | + setStore("active", "instance") |
| 63 | + focusActive() |
| 64 | + return |
| 65 | + } |
| 66 | + if (!key) { |
| 67 | + setError("API key is required") |
| 68 | + setStore("active", "key") |
| 69 | + focusActive() |
| 70 | + return |
| 71 | + } |
| 72 | + if (!url) { |
| 73 | + setError("URL is required") |
| 74 | + setStore("active", "url") |
| 75 | + focusActive() |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + setError("") |
| 80 | + setValidating(true) |
| 81 | + try { |
| 82 | + const res = await fetch(`${url}/auth_health`, { |
| 83 | + method: "GET", |
| 84 | + headers: { |
| 85 | + Authorization: `Bearer ${key}`, |
| 86 | + "x-tenant": instance, |
| 87 | + }, |
| 88 | + }) |
| 89 | + if (!res.ok) { |
| 90 | + setError("Invalid credentials — check your instance name, API key, and URL") |
| 91 | + setValidating(false) |
| 92 | + return |
| 93 | + } |
| 94 | + const data = await res.json() |
| 95 | + if (data.status !== "auth_valid") { |
| 96 | + setError("Unexpected response from server") |
| 97 | + setValidating(false) |
| 98 | + return |
| 99 | + } |
| 100 | + } catch { |
| 101 | + setError(`Connection failed — could not reach ${url}`) |
| 102 | + setValidating(false) |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + try { |
| 107 | + const creds = { |
| 108 | + altimateUrl: url, |
| 109 | + altimateInstanceName: instance, |
| 110 | + altimateApiKey: key, |
| 111 | + } |
| 112 | + await Filesystem.writeJson(AltimateApi.credentialsPath(), creds, 0o600) |
| 113 | + await sdk.client.instance.dispose() |
| 114 | + await sync.bootstrap() |
| 115 | + dialog.clear() |
| 116 | + } finally { |
| 117 | + setValidating(false) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return ( |
| 122 | + <box paddingLeft={2} paddingRight={2} gap={1}> |
| 123 | + <box flexDirection="row" justifyContent="space-between"> |
| 124 | + <text attributes={TextAttributes.BOLD} fg={theme.text}> |
| 125 | + Connect to Altimate |
| 126 | + </text> |
| 127 | + <text fg={theme.textMuted} onMouseUp={() => dialog.clear()}> |
| 128 | + esc |
| 129 | + </text> |
| 130 | + </box> |
| 131 | + |
| 132 | + <text fg={theme.textMuted}>Find these in Settings > API Keys in your Altimate dashboard</text> |
| 133 | + |
| 134 | + <box> |
| 135 | + <text fg={store.active === "instance" ? theme.text : theme.textMuted}>Instance Name:</text> |
| 136 | + <text fg={theme.textMuted}> From your URL: https://<instance>.app.myaltimate.com</text> |
| 137 | + <textarea |
| 138 | + height={3} |
| 139 | + ref={(val: TextareaRenderable) => (instanceRef = val)} |
| 140 | + placeholder="your-instance" |
| 141 | + textColor={theme.text} |
| 142 | + focusedTextColor={theme.text} |
| 143 | + cursorColor={theme.text} |
| 144 | + onMouseUp={() => { |
| 145 | + setStore("active", "instance") |
| 146 | + focusActive() |
| 147 | + }} |
| 148 | + /> |
| 149 | + </box> |
| 150 | + |
| 151 | + <box> |
| 152 | + <text fg={store.active === "key" ? theme.text : theme.textMuted}>API Key:</text> |
| 153 | + <text fg={theme.textMuted}> Settings > API Keys > Copy</text> |
| 154 | + <textarea |
| 155 | + height={3} |
| 156 | + ref={(val: TextareaRenderable) => (keyRef = val)} |
| 157 | + placeholder="your-api-key" |
| 158 | + textColor={theme.text} |
| 159 | + focusedTextColor={theme.text} |
| 160 | + cursorColor={theme.text} |
| 161 | + onMouseUp={() => { |
| 162 | + setStore("active", "key") |
| 163 | + focusActive() |
| 164 | + }} |
| 165 | + /> |
| 166 | + </box> |
| 167 | + |
| 168 | + <box> |
| 169 | + <text fg={store.active === "url" ? theme.text : theme.textMuted}>URL:</text> |
| 170 | + <textarea |
| 171 | + height={3} |
| 172 | + ref={(val: TextareaRenderable) => (urlRef = val)} |
| 173 | + initialValue="https://api.myaltimate.com" |
| 174 | + placeholder="https://api.myaltimate.com" |
| 175 | + textColor={theme.text} |
| 176 | + focusedTextColor={theme.text} |
| 177 | + cursorColor={theme.text} |
| 178 | + onMouseUp={() => { |
| 179 | + setStore("active", "url") |
| 180 | + focusActive() |
| 181 | + }} |
| 182 | + /> |
| 183 | + </box> |
| 184 | + |
| 185 | + <Show when={error()}> |
| 186 | + <text fg={theme.error}>{error()}</text> |
| 187 | + </Show> |
| 188 | + <Show when={validating()}> |
| 189 | + <text fg={theme.textMuted}>Validating credentials...</text> |
| 190 | + </Show> |
| 191 | + |
| 192 | + <text fg={theme.textMuted} paddingBottom={1}> |
| 193 | + <span style={{ fg: theme.text }}>tab</span> next field{" "} |
| 194 | + <span style={{ fg: theme.text }}>enter</span> submit |
| 195 | + </text> |
| 196 | + </box> |
| 197 | + ) |
| 198 | +} |
0 commit comments