Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/components/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoginStep>(prefilledToken ? 'loading' : 'email')
const [email, setEmail] = useState('')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand Down
51 changes: 40 additions & 11 deletions src/components/design-system/SmartTextInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React, { useEffect, useRef, useState } from 'react'
import { Text, useInput } from 'ink'

interface SmartTextInputProps {
Expand All @@ -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])
Expand All @@ -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)
}
})

Expand Down
9 changes: 7 additions & 2 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 }))
})

Expand Down
23 changes: 17 additions & 6 deletions src/services/plexicusApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,23 @@ export class PlexicusApi {
envelope: EnvelopeMode = 'auto',
): Promise<T> {
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)
Expand Down
Loading