diff --git a/src/components/App.tsx b/src/components/App.tsx index 364baff..3663630 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -197,8 +197,13 @@ function AppShell(props: AppProps) { handleReplSubmit(current) return } - if (!key.ctrl && !key.meta && input && input.length === 1) { - setReplInput(prev => prev + input) + if (!key.ctrl && !key.meta && input) { + // Accept pasted multi-char chunks, not just single keystrokes + const clean = [...input].filter(ch => { + const code = ch.charCodeAt(0) + return code >= 32 && code !== 127 + }).join('') + if (clean) setReplInput(prev => prev + clean) return } } diff --git a/src/components/LoginForm.tsx b/src/components/LoginForm.tsx index cbddb80..d271850 100644 --- a/src/components/LoginForm.tsx +++ b/src/components/LoginForm.tsx @@ -9,9 +9,10 @@ type LoginStep = 'email' | 'password' | 'otp' | 'loading' | 'done' interface LoginFormProps { prefilledToken?: string // from --token flag + onDone?: (email: string) => void // login subcommand: print + exit } -export function LoginForm({ prefilledToken }: LoginFormProps) { +export function LoginForm({ prefilledToken, onDone }: LoginFormProps) { const { dispatch } = useAppState() const [step, setStep] = useState(prefilledToken ? 'loading' : 'email') const [email, setEmail] = useState('') @@ -53,6 +54,7 @@ export function LoginForm({ prefilledToken }: LoginFormProps) { await saveConfig({ ...config, token: prefilledToken! }) dispatch({ type: 'auth/set', payload: { user, token: prefilledToken! } }) setStep('done') + onDone?.(user.email) } catch (err) { setError(err instanceof Error ? err.message : 'Invalid token') setStep('email') @@ -88,6 +90,7 @@ export function LoginForm({ prefilledToken }: LoginFormProps) { await saveConfig({ ...config, token: response.access_token }) dispatch({ type: 'auth/set', payload: { user: sessionUser, token: response.access_token } }) setStep('done') + onDone?.(sessionUser.email) } catch (err) { setError(err instanceof Error ? err.message : 'Login failed') setStep('password') @@ -108,6 +111,7 @@ export function LoginForm({ prefilledToken }: LoginFormProps) { await saveConfig({ ...config, token: accessToken }) dispatch({ type: 'auth/set', payload: { user: sessionUser, token: accessToken } }) setStep('done') + onDone?.(sessionUser.email) } catch (err) { setError(err instanceof Error ? err.message : '2FA verification failed') setStep('otp') diff --git a/src/components/design-system/SmartTextInput.tsx b/src/components/design-system/SmartTextInput.tsx index e2c83d6..04c351b 100644 --- a/src/components/design-system/SmartTextInput.tsx +++ b/src/components/design-system/SmartTextInput.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react' +import React, { useEffect, useRef, useState } from 'react' import { Text, useInput } from 'ink' interface SmartTextInputProps { @@ -20,6 +20,13 @@ export function SmartTextInput({ }: SmartTextInputProps) { const [cursor, setCursor] = useState(value.length) + // Latest value, updated synchronously on edits: a paste followed immediately + // by Enter would otherwise submit the pre-paste value from a stale closure + const latestValue = useRef(value) + useEffect(() => { + latestValue.current = value + }, [value]) + useEffect(() => { setCursor(c => Math.min(c, value.length)) }, [value.length]) @@ -36,35 +43,57 @@ export function SmartTextInput({ if (key.ctrl && (input === 'u' || input === 'k')) { // Ctrl+U: clear to beginning from cursor; Ctrl+K: clear to end // Without true cursor-aware editing we treat both as clear-all for simplicity + latestValue.current = '' onChange('') setCursor(0) return } if (key.ctrl && input === 'w') { - const before = value.slice(0, cursor) + const base = latestValue.current + const at = Math.min(cursor, base.length) + const before = base.slice(0, at) const trimmed = before.trimEnd() const lastSpace = trimmed.lastIndexOf(' ') const newBefore = lastSpace >= 0 ? trimmed.slice(0, lastSpace + 1) : '' - onChange(newBefore + value.slice(cursor)) + const next = newBefore + base.slice(at) + latestValue.current = next + onChange(next) setCursor(newBefore.length) return } if (key.backspace || key.delete) { - if (cursor > 0) { - onChange(value.slice(0, cursor - 1) + value.slice(cursor)) - setCursor(c => c - 1) + const base = latestValue.current + const at = Math.min(cursor, base.length) + if (at > 0) { + const next = base.slice(0, at - 1) + base.slice(at) + latestValue.current = next + onChange(next) + setCursor(at - 1) } return } if (key.return) { - onSubmit?.(value) + onSubmit?.(latestValue.current) return } - if (!key.ctrl && !key.meta && input && input.length === 1) { - const next = value.slice(0, cursor) + input + value.slice(cursor) - onChange(next) - setCursor(c => c + 1) + if (!key.ctrl && !key.meta && input) { + // Pasted text arrives as one multi-char chunk — strip control chars and insert whole. + // A newline inside the chunk (paste of a full line) means insert-then-submit. + const hasNewline = input.includes('\r') || input.includes('\n') + const clean = [...input].filter(ch => { + const code = ch.charCodeAt(0) + return code >= 32 && code !== 127 + }).join('') + if (clean) { + const base = latestValue.current + const at = Math.min(cursor, base.length) + const next = base.slice(0, at) + clean + base.slice(at) + latestValue.current = next + onChange(next) + setCursor(c => c + clean.length) + } + if (hasNewline) onSubmit?.(latestValue.current) } }) diff --git a/src/main.tsx b/src/main.tsx index 413dd1c..14e8e61 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -40,8 +40,13 @@ program const { LoginForm } = await import('./components/LoginForm.js') const config = await loadConfig() + const onDone = (email: string) => { + console.log(chalk.green(`✓ Authenticated as ${email}`)) + process.exit(0) + } + if (options.token) { - const loginEl = React.createElement(LoginForm, { prefilledToken: options.token }) + const loginEl = React.createElement(LoginForm, { prefilledToken: options.token, onDone }) render(React.createElement(AppStateProvider, { initialTheme: config.theme, children: loginEl })) return } @@ -68,7 +73,7 @@ program console.warn(chalk.gray(' Set it with: plexicus config set webUrl https://your-app-domain')) } - const loginEl = React.createElement(LoginForm, {}) + const loginEl = React.createElement(LoginForm, { onDone }) render(React.createElement(AppStateProvider, { initialTheme: config.theme, children: loginEl })) }) diff --git a/src/services/plexicusApi.ts b/src/services/plexicusApi.ts index 4d8f1a7..ba49a72 100644 --- a/src/services/plexicusApi.ts +++ b/src/services/plexicusApi.ts @@ -179,12 +179,23 @@ export class PlexicusApi { envelope: EnvelopeMode = 'auto', ): Promise { const url = `${this.baseUrl}${path}` - const res = await fetch(url, { - method, - headers: this.headers(), - body: body !== undefined ? JSON.stringify(body) : undefined, - }) - if (res.status === 401) throw new PlexicusAuthError() + let res: Response + try { + res = await fetch(url, { + method, + headers: this.headers(), + body: body !== undefined ? JSON.stringify(body) : undefined, + }) + } catch (err) { + const msg = err instanceof Error ? err.message : String(err) + throw new PlexicusApiError(`${msg} (${url})`, 0, path) + } + if (res.status === 401) { + const detail = await res.text() + .then(t => JSON.parse(t).detail as unknown) + .catch(() => undefined) + throw new PlexicusAuthError(typeof detail === 'string' ? detail : undefined) + } if (!res.ok) { const text = await res.text().catch(() => '') throw new PlexicusApiError(`${method} ${path} failed: ${text}`, res.status, path)