|
| 1 | +import { useState } from 'react' |
| 2 | +import { ChevronDown, Server, CheckCircle2, AlertTriangle, Loader } from 'lucide-react' |
| 3 | +import { useServerStore, normalizeServerUrl } from '../stores/server' |
| 4 | +import { testServerConnection } from '../services/api' |
| 5 | + |
| 6 | +type Status = |
| 7 | + | { kind: 'idle' } |
| 8 | + | { kind: 'testing' } |
| 9 | + | { kind: 'ok'; message: string } |
| 10 | + | { kind: 'warn'; message: string } |
| 11 | + | { kind: 'error'; message: string } |
| 12 | + |
| 13 | +// Bitwarden-style server selector shared by Login and Register: validates and |
| 14 | +// normalizes the URL, tests connectivity against the backend's public /info, and |
| 15 | +// saves regardless of the result (a server may be temporarily down) while showing |
| 16 | +// clear status. Empty means "use this site's own origin via the reverse proxy". |
| 17 | +export default function ServerSettings() { |
| 18 | + const { serverUrl, setServerUrl } = useServerStore() |
| 19 | + const [open, setOpen] = useState(false) |
| 20 | + const [input, setInput] = useState(serverUrl) |
| 21 | + const [status, setStatus] = useState<Status>({ kind: 'idle' }) |
| 22 | + |
| 23 | + const handleSave = async () => { |
| 24 | + const raw = input.trim() |
| 25 | + if (raw && !normalizeServerUrl(raw)) { |
| 26 | + setStatus({ kind: 'error', message: 'Enter a full URL, e.g. http://192.168.1.10:3000' }) |
| 27 | + return |
| 28 | + } |
| 29 | + const normalized = normalizeServerUrl(raw) // '' = same origin (reverse proxy) |
| 30 | + // Save immediately (warn-but-save): the choice is authoritative right away; |
| 31 | + // the connection probe below is advisory and may lag on a slow/down server. |
| 32 | + setServerUrl(raw) |
| 33 | + setInput(normalized) |
| 34 | + setStatus({ kind: 'testing' }) |
| 35 | + const result = await testServerConnection(normalized) |
| 36 | + setStatus( |
| 37 | + result.ok |
| 38 | + ? { kind: 'ok', message: `Connected · ${result.info.name} v${result.info.version}` } |
| 39 | + : { kind: 'warn', message: `Saved, but ${result.message}` }, |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + return ( |
| 44 | + <div className="mb-4"> |
| 45 | + <button |
| 46 | + type="button" |
| 47 | + onClick={() => setOpen(o => !o)} |
| 48 | + className="flex items-center gap-2 px-3 py-2 w-full text-xs text-tx-muted hover:text-tx-secondary rounded-lg hover:bg-surface-muted/40 transition-colors" |
| 49 | + > |
| 50 | + <Server className="w-3.5 h-3.5" /> |
| 51 | + <span>Server settings</span> |
| 52 | + <ChevronDown className={`w-3 h-3 ml-auto transition-transform ${open ? 'rotate-180' : ''}`} /> |
| 53 | + </button> |
| 54 | + |
| 55 | + {open && ( |
| 56 | + <div className="mt-2 p-3 bg-surface-muted/30 border border-surface-border rounded-lg space-y-2"> |
| 57 | + <label className="block text-xs font-medium text-tx-secondary uppercase tracking-wider">Server URL</label> |
| 58 | + <input |
| 59 | + type="text" |
| 60 | + value={input} |
| 61 | + onChange={e => { setInput(e.target.value); setStatus({ kind: 'idle' }) }} |
| 62 | + placeholder="Leave blank to use this site" |
| 63 | + className="input text-sm" |
| 64 | + autoComplete="off" |
| 65 | + autoCapitalize="off" |
| 66 | + spellCheck={false} |
| 67 | + /> |
| 68 | + |
| 69 | + {status.kind === 'error' && <p className="text-xs text-error-400">{status.message}</p>} |
| 70 | + {status.kind === 'warn' && ( |
| 71 | + <p className="flex items-start gap-1.5 text-xs text-warning-400"> |
| 72 | + <AlertTriangle className="w-3.5 h-3.5 mt-0.5 flex-shrink-0" /> |
| 73 | + <span>{status.message}</span> |
| 74 | + </p> |
| 75 | + )} |
| 76 | + {status.kind === 'ok' && ( |
| 77 | + <p className="flex items-center gap-1.5 text-xs text-success-500"> |
| 78 | + <CheckCircle2 className="w-3.5 h-3.5 flex-shrink-0" /> |
| 79 | + <span>{status.message}</span> |
| 80 | + </p> |
| 81 | + )} |
| 82 | + |
| 83 | + <div className="flex gap-2 pt-1"> |
| 84 | + <button |
| 85 | + type="button" |
| 86 | + onClick={handleSave} |
| 87 | + disabled={status.kind === 'testing'} |
| 88 | + className="flex-1 px-2 py-1.5 text-xs bg-brand-500 hover:bg-brand-600 disabled:opacity-40 disabled:cursor-not-allowed text-white font-medium rounded-lg transition-colors flex items-center justify-center gap-1.5" |
| 89 | + > |
| 90 | + {status.kind === 'testing' |
| 91 | + ? (<><Loader className="w-3.5 h-3.5 animate-spin" /> Testing…</>) |
| 92 | + : 'Test & Save'} |
| 93 | + </button> |
| 94 | + <button |
| 95 | + type="button" |
| 96 | + onClick={() => setOpen(false)} |
| 97 | + className="flex-1 px-2 py-1.5 text-xs bg-surface-border text-tx-secondary hover:bg-surface-border/80 rounded-lg transition-colors" |
| 98 | + > |
| 99 | + Close |
| 100 | + </button> |
| 101 | + </div> |
| 102 | + |
| 103 | + <p className="text-xs text-tx-muted pt-1"> |
| 104 | + {serverUrl ? `Current: ${serverUrl}` : 'Using this site’s address (reverse proxy)'} |
| 105 | + </p> |
| 106 | + </div> |
| 107 | + )} |
| 108 | + </div> |
| 109 | + ) |
| 110 | +} |
0 commit comments