|
1 | 1 | <script lang="ts"> |
2 | 2 | import { Button } from '$lib/components/ui/button'; |
3 | | - import { AlertTriangle, RefreshCw } from '@lucide/svelte'; |
| 3 | + import { Input } from '$lib/components/ui/input'; |
| 4 | + import Label from '$lib/components/ui/label/label.svelte'; |
| 5 | + import { AlertTriangle, RefreshCw, Key, CheckCircle, XCircle } from '@lucide/svelte'; |
4 | 6 | import { ServerStatus } from '$lib/components/app'; |
5 | 7 | import { serverStore, serverLoading } from '$lib/stores/server.svelte'; |
6 | | - import { fade, fly } from 'svelte/transition'; |
| 8 | + import { config, updateConfig } from '$lib/stores/settings.svelte'; |
| 9 | + import { goto } from '$app/navigation'; |
| 10 | + import { fade, fly, scale } from 'svelte/transition'; |
7 | 11 |
|
8 | 12 | interface Props { |
9 | 13 | class?: string; |
|
18 | 22 | error, |
19 | 23 | onRetry, |
20 | 24 | showRetry = true, |
21 | | - showTroubleshooting = true |
| 25 | + showTroubleshooting = false |
22 | 26 | }: Props = $props(); |
23 | 27 |
|
24 | | - const isServerLoading = $derived(serverLoading()); |
| 28 | + let isServerLoading = $derived(serverLoading()); |
| 29 | + let isAccessDeniedError = $derived( |
| 30 | + error.toLowerCase().includes('access denied') || |
| 31 | + error.toLowerCase().includes('invalid api key') || |
| 32 | + error.toLowerCase().includes('unauthorized') || |
| 33 | + error.toLowerCase().includes('401') || |
| 34 | + error.toLowerCase().includes('403') |
| 35 | + ); |
| 36 | +
|
| 37 | + let apiKeyInput = $state(''); |
| 38 | + let showApiKeyInput = $state(false); |
| 39 | + let apiKeyState = $state<'idle' | 'validating' | 'success' | 'error'>('idle'); |
| 40 | + let apiKeyError = $state(''); |
25 | 41 |
|
26 | 42 | function handleRetryConnection() { |
27 | 43 | if (onRetry) { |
|
30 | 46 | serverStore.fetchServerProps(); |
31 | 47 | } |
32 | 48 | } |
| 49 | +
|
| 50 | + function handleShowApiKeyInput() { |
| 51 | + showApiKeyInput = true; |
| 52 | + // Pre-fill with current API key if it exists |
| 53 | + const currentConfig = config(); |
| 54 | + apiKeyInput = currentConfig.apiKey?.toString() || ''; |
| 55 | + } |
| 56 | +
|
| 57 | + async function handleSaveApiKey() { |
| 58 | + if (!apiKeyInput.trim()) return; |
| 59 | +
|
| 60 | + apiKeyState = 'validating'; |
| 61 | + apiKeyError = ''; |
| 62 | +
|
| 63 | + try { |
| 64 | + // Update the API key in settings first |
| 65 | + updateConfig('apiKey', apiKeyInput.trim()); |
| 66 | +
|
| 67 | + // Test the API key by making a real request to the server |
| 68 | + const response = await fetch('/props', { |
| 69 | + headers: { |
| 70 | + 'Content-Type': 'application/json', |
| 71 | + Authorization: `Bearer ${apiKeyInput.trim()}` |
| 72 | + } |
| 73 | + }); |
| 74 | +
|
| 75 | + if (response.ok) { |
| 76 | + // API key is valid - User Story B |
| 77 | + apiKeyState = 'success'; |
| 78 | +
|
| 79 | + // Show success state briefly, then navigate to home |
| 80 | + setTimeout(() => { |
| 81 | + goto('/'); |
| 82 | + }, 1000); |
| 83 | + } else { |
| 84 | + // API key is invalid - User Story A |
| 85 | + apiKeyState = 'error'; |
| 86 | +
|
| 87 | + if (response.status === 401 || response.status === 403) { |
| 88 | + apiKeyError = 'Invalid API key - please check and try again'; |
| 89 | + } else { |
| 90 | + apiKeyError = `Authentication failed (${response.status})`; |
| 91 | + } |
| 92 | +
|
| 93 | + // Reset to idle state after showing error (don't reload UI) |
| 94 | + setTimeout(() => { |
| 95 | + apiKeyState = 'idle'; |
| 96 | + }, 3000); |
| 97 | + } |
| 98 | + } catch (error) { |
| 99 | + // Network or other errors - User Story A |
| 100 | + apiKeyState = 'error'; |
| 101 | +
|
| 102 | + if (error instanceof Error) { |
| 103 | + if (error.message.includes('fetch')) { |
| 104 | + apiKeyError = 'Cannot connect to server - check if server is running'; |
| 105 | + } else { |
| 106 | + apiKeyError = error.message; |
| 107 | + } |
| 108 | + } else { |
| 109 | + apiKeyError = 'Connection error - please try again'; |
| 110 | + } |
| 111 | +
|
| 112 | + // Reset to idle state after showing error (don't reload UI) |
| 113 | + setTimeout(() => { |
| 114 | + apiKeyState = 'idle'; |
| 115 | + }, 3000); |
| 116 | + } |
| 117 | + } |
| 118 | +
|
| 119 | + function handleApiKeyKeydown(event: KeyboardEvent) { |
| 120 | + if (event.key === 'Enter') { |
| 121 | + handleSaveApiKey(); |
| 122 | + } |
| 123 | + } |
33 | 124 | </script> |
34 | 125 |
|
35 | 126 | <div class="flex h-full items-center justify-center {className}"> |
|
46 | 137 | <p class="mb-4 text-sm text-muted-foreground"> |
47 | 138 | {error} |
48 | 139 | </p> |
| 140 | + </div> |
49 | 141 |
|
50 | | - <div class="mb-4"> |
51 | | - <ServerStatus showActions={true} class="justify-center" /> |
| 142 | + {#if isAccessDeniedError && !showApiKeyInput} |
| 143 | + <div in:fly={{ y: 10, duration: 300, delay: 200 }} class="mb-4"> |
| 144 | + <Button onclick={handleShowApiKeyInput} variant="outline" class="w-full"> |
| 145 | + <Key class="h-4 w-4" /> |
| 146 | + Enter API Key |
| 147 | + </Button> |
52 | 148 | </div> |
53 | | - </div> |
| 149 | + {/if} |
| 150 | + |
| 151 | + {#if showApiKeyInput} |
| 152 | + <div in:fly={{ y: 10, duration: 300, delay: 200 }} class="mb-4 space-y-3 text-left"> |
| 153 | + <div class="space-y-2"> |
| 154 | + <Label for="api-key-input" class="text-sm font-medium">API Key</Label> |
| 155 | + |
| 156 | + <div class="relative"> |
| 157 | + <Input |
| 158 | + id="api-key-input" |
| 159 | + placeholder="Enter your API key..." |
| 160 | + bind:value={apiKeyInput} |
| 161 | + onkeydown={handleApiKeyKeydown} |
| 162 | + class="w-full pr-10 {apiKeyState === 'error' |
| 163 | + ? 'border-destructive' |
| 164 | + : apiKeyState === 'success' |
| 165 | + ? 'border-green-500' |
| 166 | + : ''}" |
| 167 | + disabled={apiKeyState === 'validating'} |
| 168 | + /> |
| 169 | + {#if apiKeyState === 'validating'} |
| 170 | + <div class="absolute top-1/2 right-3 -translate-y-1/2"> |
| 171 | + <RefreshCw class="h-4 w-4 animate-spin text-muted-foreground" /> |
| 172 | + </div> |
| 173 | + {:else if apiKeyState === 'success'} |
| 174 | + <div |
| 175 | + class="absolute top-1/2 right-3 -translate-y-1/2" |
| 176 | + in:scale={{ duration: 200, start: 0.8 }} |
| 177 | + > |
| 178 | + <CheckCircle class="h-4 w-4 text-green-500" /> |
| 179 | + </div> |
| 180 | + {:else if apiKeyState === 'error'} |
| 181 | + <div |
| 182 | + class="absolute top-1/2 right-3 -translate-y-1/2" |
| 183 | + in:scale={{ duration: 200, start: 0.8 }} |
| 184 | + > |
| 185 | + <XCircle class="h-4 w-4 text-destructive" /> |
| 186 | + </div> |
| 187 | + {/if} |
| 188 | + </div> |
| 189 | + {#if apiKeyError} |
| 190 | + <p class="text-sm text-destructive" in:fly={{ y: -10, duration: 200 }}> |
| 191 | + {apiKeyError} |
| 192 | + </p> |
| 193 | + {/if} |
| 194 | + {#if apiKeyState === 'success'} |
| 195 | + <p class="text-sm text-green-600" in:fly={{ y: -10, duration: 200 }}> |
| 196 | + ✓ API key validated successfully! Connecting... |
| 197 | + </p> |
| 198 | + {/if} |
| 199 | + </div> |
| 200 | + <div class="flex gap-2"> |
| 201 | + <Button |
| 202 | + onclick={handleSaveApiKey} |
| 203 | + disabled={!apiKeyInput.trim() || |
| 204 | + apiKeyState === 'validating' || |
| 205 | + apiKeyState === 'success'} |
| 206 | + class="flex-1" |
| 207 | + > |
| 208 | + {#if apiKeyState === 'validating'} |
| 209 | + <RefreshCw class="h-4 w-4 animate-spin" /> |
| 210 | + Validating... |
| 211 | + {:else if apiKeyState === 'success'} |
| 212 | + Success! |
| 213 | + {:else} |
| 214 | + Save & Retry |
| 215 | + {/if} |
| 216 | + </Button> |
| 217 | + <Button |
| 218 | + onclick={() => { |
| 219 | + showApiKeyInput = false; |
| 220 | + apiKeyState = 'idle'; |
| 221 | + apiKeyError = ''; |
| 222 | + }} |
| 223 | + variant="outline" |
| 224 | + class="flex-1" |
| 225 | + disabled={apiKeyState === 'validating'} |
| 226 | + > |
| 227 | + Cancel |
| 228 | + </Button> |
| 229 | + </div> |
| 230 | + </div> |
| 231 | + {/if} |
54 | 232 |
|
55 | 233 | {#if showRetry} |
56 | 234 | <div in:fly={{ y: 10, duration: 300, delay: 200 }}> |
57 | 235 | <Button onclick={handleRetryConnection} disabled={isServerLoading} class="w-full"> |
58 | 236 | {#if isServerLoading} |
59 | | - <RefreshCw class="mr-2 h-4 w-4 animate-spin" /> |
| 237 | + <RefreshCw class="h-4 w-4 animate-spin" /> |
60 | 238 |
|
61 | 239 | Connecting... |
62 | 240 | {:else} |
63 | | - <RefreshCw class="mr-2 h-4 w-4" /> |
| 241 | + <RefreshCw class="h-4 w-4" /> |
64 | 242 |
|
65 | 243 | Retry Connection |
66 | 244 | {/if} |
|
0 commit comments