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
87 changes: 37 additions & 50 deletions src/components/AIConfigModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,19 @@ const AIConfigModal: React.FC<AIConfigModalProps> = ({
setConnectionStatus({ kind: "idle" });
}, []);

// ── Connection test ────────────────────────────────────────────────────────
// ── Save & Verify ──────────────────────────────────────────────────────────

const handleSave = useCallback(async () => {
if (!useLLM) {
onSave({
...DEFAULT_LLM_CONFIG,
enabled: false,
apiKey: apiKey.trim(),
model: selectedModel,
});
return;
}

const handleTestConnection = useCallback(async () => {
if (!apiKey.trim()) {
setConnectionStatus({
kind: "error",
Expand All @@ -87,52 +97,29 @@ const AIConfigModal: React.FC<AIConfigModalProps> = ({
DEFAULT_LLM_CONFIG.apiUrl,
);

setConnectionStatus(
result.success
? {
kind: "success",
message: t("aiConfig.llm.connectedMsg", {
modelName:
MODELS.find((m) => m.id === selectedModel)?.name ??
selectedModel,
}),
}
: {
kind: "error",
message: t("aiConfig.llm.connectionError", {
error: result.message,
}),
},
);
}, [apiKey, selectedModel, t]);

// ── Save ───────────────────────────────────────────────────────────────────

const handleSave = useCallback(() => {
const newConfig: LLMConfig = {
...DEFAULT_LLM_CONFIG,
enabled: useLLM,
apiKey: apiKey.trim(),
model: selectedModel,
};
onSave(newConfig);
}, [useLLM, apiKey, selectedModel, onSave]);

// ── Test & Save (merged action) ────────────────────────────────────────────

const handleTestAndSave = useCallback(async () => {
if (!useLLM) {
handleSave();
return;
}
if (connectionStatus.kind === "success") {
// Already tested — save directly
handleSave();
return;
if (result.success) {
setConnectionStatus({
kind: "success",
message: t("aiConfig.llm.connectedMsg", {
modelName:
MODELS.find((m) => m.id === selectedModel)?.name ?? selectedModel,
}),
});
onSave({
...DEFAULT_LLM_CONFIG,
enabled: true,
apiKey: apiKey.trim(),
model: selectedModel,
});
} else {
setConnectionStatus({
kind: "error",
message: t("aiConfig.llm.connectionError", {
error: result.message,
}),
});
}
// First press: run the test; button will become "Save" on success
await handleTestConnection();
}, [useLLM, connectionStatus.kind, handleTestConnection, handleSave]);
}, [useLLM, apiKey, selectedModel, onSave, t]);

// ── Render ─────────────────────────────────────────────────────────────────

Expand Down Expand Up @@ -385,16 +372,16 @@ const AIConfigModal: React.FC<AIConfigModalProps> = ({
styles.saveBtn,
connectionStatus.kind === "testing" && styles.saveBtnDisabled,
]}
onPress={handleTestAndSave}
onPress={handleSave}
disabled={connectionStatus.kind === "testing"}
activeOpacity={0.8}
>
{connectionStatus.kind === "testing" ? (
<ActivityIndicator size="small" color="#fff" />
) : (
<Text style={styles.saveBtnText}>
{useLLM && connectionStatus.kind !== "success"
? t("aiConfig.buttons.verify")
{useLLM
? t("aiConfig.buttons.verifyAndSave")
: t("aiConfig.buttons.save")}
</Text>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/locales/en/modals.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
"buttons": {
"cancel": "Cancel",
"verify": "Verify",
"verifyAndSave": "Verify & Save",
"save": "Save"
},
"models": {
Expand Down
2 changes: 1 addition & 1 deletion src/locales/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export interface ModalsTranslations {
};
buttons: {
cancel: string;
verify: string;
verifyAndSave: string;
save: string;
};
models: {
Expand Down
2 changes: 1 addition & 1 deletion src/locales/zh/modals.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
"buttons": {
"cancel": "取消",
"verify": "验证",
"verifyAndSave": "验证并保存",
"save": "保存"
},
"models": {
Expand Down