Fix bug select model

This commit is contained in:
decolua
2026-01-21 11:45:19 +07:00
parent 7058b062e7
commit a01526642c
5 changed files with 124 additions and 62 deletions
@@ -1,6 +1,6 @@
"use client";
import { useState, useEffect } from "react";
import { useState, useEffect, useRef } from "react";
import { Card, Button, ModelSelectModal, ManualConfigModal } from "@/shared/components";
import Image from "next/image";
@@ -29,6 +29,8 @@ export default function ClaudeToolCard({
const [selectedApiKey, setSelectedApiKey] = useState("");
const [modelAliases, setModelAliases] = useState({});
const [showManualConfigModal, setShowManualConfigModal] = useState(false);
const [customBaseUrl, setCustomBaseUrl] = useState("");
const hasInitializedModels = useRef(false);
const getConfigStatus = () => {
if (!claudeStatus?.installed) return null;
@@ -66,12 +68,17 @@ export default function ClaudeToolCard({
};
useEffect(() => {
if (claudeStatus?.installed) {
if (claudeStatus?.installed && !hasInitializedModels.current) {
hasInitializedModels.current = true;
const env = claudeStatus.settings?.env || {};
tool.defaultModels.forEach((model) => {
if (model.envKey) {
const value = env[model.envKey] || model.defaultValue || "";
if (value) onModelMappingChange(model.alias, value);
// Only sync initial values from file once
if (value) {
onModelMappingChange(model.alias, value);
}
}
});
// Only set selectedApiKey if it exists in apiKeys list
@@ -95,11 +102,13 @@ export default function ClaudeToolCard({
}
};
const getEffectiveBaseUrl = () => customBaseUrl || baseUrl;
const handleApplySettings = async () => {
setApplying(true);
setMessage(null);
try {
const env = { ANTHROPIC_BASE_URL: baseUrl };
const env = { ANTHROPIC_BASE_URL: getEffectiveBaseUrl() };
// Get key from dropdown, fallback to first key or sk_9router for localhost
const keyToUse = selectedApiKey?.trim()
@@ -167,7 +176,7 @@ export default function ClaudeToolCard({
const keyToUse = (selectedApiKey && selectedApiKey.trim())
? selectedApiKey
: (!cloudEnabled ? "sk_9router" : "<API_KEY_FROM_DASHBOARD>");
const env = { ANTHROPIC_BASE_URL: baseUrl, ANTHROPIC_AUTH_TOKEN: keyToUse };
const env = { ANTHROPIC_BASE_URL: getEffectiveBaseUrl(), ANTHROPIC_AUTH_TOKEN: keyToUse };
tool.defaultModels.forEach((model) => {
const targetModel = modelMappings[model.alias];
if (targetModel && model.envKey) env[model.envKey] = targetModel;
@@ -240,26 +249,41 @@ export default function ClaudeToolCard({
{!checkingClaude && claudeStatus?.installed && (
<>
<div className="flex items-center gap-2">
<span className="material-symbols-outlined text-green-500 text-[16px]">check_circle</span>
<span className="text-xs text-text-muted shrink-0">URL:</span>
<code className="text-xs font-mono text-text-main truncate">{baseUrl}</code>
</div>
<div className="flex items-center gap-2">
<span className="text-xs text-text-muted shrink-0">Key:</span>
{apiKeys.length > 0 ? (
<select value={selectedApiKey} onChange={(e) => setSelectedApiKey(e.target.value)} className="flex-1 px-2 py-1.5 bg-surface rounded text-xs border border-border focus:outline-none focus:ring-1 focus:ring-primary/50">
{apiKeys.map((key) => <option key={key.id} value={key.key}>{key.key}</option>)}
</select>
) : (
<span className="text-xs text-text-muted">
{cloudEnabled ? "No API keys - Create one in Keys page" : "sk_9router"}
</span>
)}
</div>
<div className="flex flex-col gap-2">
{/* Base URL */}
<div className="flex items-center gap-2">
<span className="w-32 shrink-0 text-sm font-semibold text-text-main text-right">Base URL</span>
<span className="material-symbols-outlined text-text-muted text-[14px]">arrow_forward</span>
<input
type="text"
value={customBaseUrl || baseUrl}
onChange={(e) => setCustomBaseUrl(e.target.value)}
placeholder="https://..."
className="flex-1 px-2 py-1.5 bg-surface rounded border border-border text-xs focus:outline-none focus:ring-1 focus:ring-primary/50"
/>
{customBaseUrl && customBaseUrl !== baseUrl && (
<button onClick={() => setCustomBaseUrl("")} className="p-1 text-text-muted hover:text-primary rounded transition-colors" title="Reset to default">
<span className="material-symbols-outlined text-[14px]">restart_alt</span>
</button>
)}
</div>
{/* API Key */}
<div className="flex items-center gap-2">
<span className="w-32 shrink-0 text-sm font-semibold text-text-main text-right">API Key</span>
<span className="material-symbols-outlined text-text-muted text-[14px]">arrow_forward</span>
{apiKeys.length > 0 ? (
<select value={selectedApiKey} onChange={(e) => setSelectedApiKey(e.target.value)} className="flex-1 px-2 py-1.5 bg-surface rounded text-xs border border-border focus:outline-none focus:ring-1 focus:ring-primary/50">
{apiKeys.map((key) => <option key={key.id} value={key.key}>{key.key}</option>)}
</select>
) : (
<span className="flex-1 text-xs text-text-muted px-2 py-1.5">
{cloudEnabled ? "No API keys - Create one in Keys page" : "sk_9router (default)"}
</span>
)}
</div>
{/* Model Mappings */}
{tool.defaultModels.map((model) => (
<div key={model.alias} className="flex items-center gap-2">
<span className="w-32 shrink-0 text-sm font-semibold text-text-main text-right">{model.name}</span>