Files
claude-code/src/commands/plugin/PluginOptionsDialog.tsx
claude-code-best 5b1a52b8e0 更新大量 tsx 原始文件; 已经迁移 login panel; 部分 (#121)
* style(B1-1): 格式化 ink/buddy/cli/context/screens/tasks/services/keybindings/state (43 files)

纯格式化:移除分号、React Compiler import、import 多行展开。
修复了 Box.tsx 和 ScrollBox.tsx 中无效的 global.d.ts import。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* style(B1-2): 格式化 commands (79 files)

纯格式化:移除分号、React Compiler import、import 多行展开。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* style(B1-3): 格式化 components/messages,permissions,mcp,sandbox,shell (104 files)

纯格式化:移除分号、React Compiler import、import 多行展开。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* style(B1-4): 格式化 components/PromptInput,FeedbackSurvey,tasks,agents,skills,design-system,wizard (73 files)

纯格式化:移除分号、React Compiler import、import 多行展开。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* style(B1-5): 格式化 components其余 + hooks + tools (232 files)

纯格式化:移除分号、React Compiler import、import 多行展开。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* style(B1-6): 格式化 main/entrypoints/utils/moreright (21 files)

纯格式化:移除分号、React Compiler import、import 多行展开。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* docs: 更新 README,新增 Run.ps1/TODO.md,删除 V6.md

- README.md: 大幅重写,更详细版本历史和配置示例
- Run.ps1: 新增 Windows 启动脚本
- TODO.md: 新增包完成清单
- V6.md: 删除(架构重构规划已不适用)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: 修复以前的问题

* fix: 修复 login 面板的问题

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-04 23:24:27 +08:00

215 lines
6.4 KiB
TypeScript

import figures from 'figures'
import React, { useCallback, useState } from 'react'
import { Dialog } from '../../components/design-system/Dialog.js'
import { stringWidth } from '../../ink/stringWidth.js'
// eslint-disable-next-line custom-rules/prefer-use-keybindings -- raw text input for config dialog
import { Box, Text, useInput } from '../../ink.js'
import {
useKeybinding,
useKeybindings,
} from '../../keybindings/useKeybinding.js'
import { isEnvTruthy } from '../../utils/envUtils.js'
import type {
PluginOptionSchema,
PluginOptionValues,
} from '../../utils/plugins/pluginOptionsStorage.js'
/**
* Build the onSave payload from collected string inputs.
*
* Sensitive fields are never prepopulated in the text buffer (security), so
* by the time the user reaches the last field every sensitive field they
* stepped through contains '' in collected. To avoid silently wiping saved
* secrets on reconfigure: if a sensitive field is '' AND initialValues has
* a value for it, OMIT the key entirely. savePluginOptions only writes keys
* it receives, so omitting = keep existing.
*
* Exported for unit testing.
*/
export function buildFinalValues(
fields: string[],
collected: Record<string, string>,
configSchema: PluginOptionSchema,
initialValues: PluginOptionValues | undefined,
): PluginOptionValues {
const finalValues: PluginOptionValues = {}
for (const fieldKey of fields) {
const schema = configSchema[fieldKey]
const value = collected[fieldKey] ?? ''
if (
schema?.sensitive === true &&
value === '' &&
initialValues?.[fieldKey] !== undefined
) {
continue
}
if (schema?.type === 'number') {
// Number('') returns 0, not NaN — omit blank number inputs so
// validateUserConfig's required check actually catches them.
if (value.trim() === '') continue
const num = Number(value)
finalValues[fieldKey] = Number.isNaN(num) ? value : num
} else if (schema?.type === 'boolean') {
finalValues[fieldKey] = isEnvTruthy(value)
} else {
finalValues[fieldKey] = value
}
}
return finalValues
}
type Props = {
title: string
subtitle: string
configSchema: PluginOptionSchema
/** Pre-fill fields when reconfiguring. Sensitive fields are not prepopulated. */
initialValues?: PluginOptionValues
onSave: (config: PluginOptionValues) => void
onCancel: () => void
}
export function PluginOptionsDialog({
title,
subtitle,
configSchema,
initialValues,
onSave,
onCancel,
}: Props): React.ReactNode {
const fields = Object.keys(configSchema)
// Prepopulate from initialValues but skip sensitive fields — we don't
// want to echo secrets back into the text buffer.
const initialFor = useCallback(
(key: string): string => {
if (configSchema[key]?.sensitive === true) return ''
const v = initialValues?.[key]
return v === undefined ? '' : String(v)
},
[configSchema, initialValues],
)
const [currentFieldIndex, setCurrentFieldIndex] = useState(0)
const [values, setValues] = useState<Record<string, string>>({})
const [currentInput, setCurrentInput] = useState(() =>
fields[0] ? initialFor(fields[0]) : '',
)
const currentField = fields[currentFieldIndex]
const fieldSchema = currentField ? configSchema[currentField] : null
// Use Settings context so 'n' key doesn't cancel (allows typing 'n' in input).
// isCancelActive={false} on Dialog keeps its own confirm:no out of the way.
useKeybinding('confirm:no', onCancel, { context: 'Settings' })
// Tab to next field
const handleNextField = useCallback(() => {
if (currentFieldIndex < fields.length - 1 && currentField) {
setValues(prev => ({ ...prev, [currentField]: currentInput }))
setCurrentFieldIndex(prev => prev + 1)
const nextKey = fields[currentFieldIndex + 1]
setCurrentInput(nextKey ? initialFor(nextKey) : '')
}
}, [currentFieldIndex, fields, currentField, currentInput, initialFor])
// Enter to save current field and move to next, or save all if last
const handleConfirm = useCallback(() => {
if (!currentField) return
const newValues = { ...values, [currentField]: currentInput }
if (currentFieldIndex === fields.length - 1) {
onSave(buildFinalValues(fields, newValues, configSchema, initialValues))
} else {
// Move to next field
setValues(newValues)
setCurrentFieldIndex(prev => prev + 1)
const nextKey = fields[currentFieldIndex + 1]
setCurrentInput(nextKey ? initialFor(nextKey) : '')
}
}, [
currentField,
values,
currentInput,
currentFieldIndex,
fields,
configSchema,
onSave,
initialFor,
initialValues,
])
useKeybindings(
{
'confirm:nextField': handleNextField,
'confirm:yes': handleConfirm,
},
{ context: 'Confirmation' },
)
// Character input handling (backspace, typing)
useInput((char, key) => {
// Backspace
if (key.backspace || key.delete) {
setCurrentInput(prev => prev.slice(0, -1))
return
}
// Regular character input
if (char && !key.ctrl && !key.meta && !key.tab && !key.return) {
setCurrentInput(prev => prev + char)
}
})
if (!fieldSchema || !currentField) {
return null
}
const isSensitive = fieldSchema.sensitive === true
const isRequired = fieldSchema.required === true
const displayValue = isSensitive
? '*'.repeat(stringWidth(currentInput))
: currentInput
return (
<Dialog
title={title}
subtitle={subtitle}
onCancel={onCancel}
isCancelActive={false}
>
<Box flexDirection="column">
<Text bold={true}>
{fieldSchema.title || currentField}
{isRequired && <Text color="error"> *</Text>}
</Text>
{fieldSchema.description && (
<Text dimColor={true}>{fieldSchema.description}</Text>
)}
<Box marginTop={1}>
<Text>{figures.pointerSmall} </Text>
<Text>{displayValue}</Text>
<Text></Text>
</Box>
</Box>
<Box flexDirection="column">
<Text dimColor={true}>
Field {currentFieldIndex + 1} of {fields.length}
</Text>
{currentFieldIndex < fields.length - 1 && (
<Text dimColor={true}>
Tab: Next field · Enter: Save and continue
</Text>
)}
{currentFieldIndex === fields.length - 1 && (
<Text dimColor={true}>Enter: Save configuration</Text>
)}
</Box>
</Dialog>
)
}