opentui(ts): collapse prompt accessors into a generic narrow()
Replace the ~5 near-identical `as*()` accessors in
view/prompts/promptOverlay.tsx (one per ActivePrompt kind) with one generic
`narrow(kind)` helper that narrows the discriminated union via a typed type
guard (`p is Extract<ActivePrompt, { kind: K }>`) — no `as`. Each <Match>
branch keeps its precise typed payload. Behavior is identical.
This commit is contained in:
parent
b36001940a
commit
da07e67efd
@ -12,7 +12,7 @@
|
|||||||
import { Match, Switch } from 'solid-js'
|
import { Match, Switch } from 'solid-js'
|
||||||
|
|
||||||
import { deferClose } from '../../logic/defer.ts'
|
import { deferClose } from '../../logic/defer.ts'
|
||||||
import type { SessionStore } from '../../logic/store.ts'
|
import type { ActivePrompt, SessionStore } from '../../logic/store.ts'
|
||||||
import { ApprovalPrompt } from './approvalPrompt.tsx'
|
import { ApprovalPrompt } from './approvalPrompt.tsx'
|
||||||
import { ClarifyPrompt } from './clarifyPrompt.tsx'
|
import { ClarifyPrompt } from './clarifyPrompt.tsx'
|
||||||
import { ConfirmPrompt } from './confirmPrompt.tsx'
|
import { ConfirmPrompt } from './confirmPrompt.tsx'
|
||||||
@ -35,26 +35,20 @@ export function PromptOverlay(props: PromptOverlayProps) {
|
|||||||
clearSoon()
|
clearSoon()
|
||||||
}
|
}
|
||||||
|
|
||||||
const asApproval = () => {
|
// Reactive accessor that narrows the active-prompt union to one `kind`, giving
|
||||||
|
// each <Match> branch its precise typed payload (undefined when not that kind).
|
||||||
|
function narrow<K extends ActivePrompt['kind']>(kind: K): () => Extract<ActivePrompt, { kind: K }> | undefined {
|
||||||
|
const matches = (p: ActivePrompt): p is Extract<ActivePrompt, { kind: K }> => p.kind === kind
|
||||||
|
return () => {
|
||||||
const p = prompt()
|
const p = prompt()
|
||||||
return p && p.kind === 'approval' ? p : undefined
|
return p && matches(p) ? p : undefined
|
||||||
}
|
}
|
||||||
const asClarify = () => {
|
|
||||||
const p = prompt()
|
|
||||||
return p && p.kind === 'clarify' ? p : undefined
|
|
||||||
}
|
|
||||||
const asSudo = () => {
|
|
||||||
const p = prompt()
|
|
||||||
return p && p.kind === 'sudo' ? p : undefined
|
|
||||||
}
|
|
||||||
const asSecret = () => {
|
|
||||||
const p = prompt()
|
|
||||||
return p && p.kind === 'secret' ? p : undefined
|
|
||||||
}
|
|
||||||
const asConfirm = () => {
|
|
||||||
const p = prompt()
|
|
||||||
return p && p.kind === 'confirm' ? p : undefined
|
|
||||||
}
|
}
|
||||||
|
const asApproval = narrow('approval')
|
||||||
|
const asClarify = narrow('clarify')
|
||||||
|
const asSudo = narrow('sudo')
|
||||||
|
const asSecret = narrow('secret')
|
||||||
|
const asConfirm = narrow('confirm')
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Switch>
|
<Switch>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user