Fix bug 1: Only fetch Anthropic models when API key exists

getAnthropicModels() was called unconditionally on mount, causing a
console error when no API key was set. Now chains the call after
getAnthropicApiKeyExists() confirms a key is present.

Includes regression test added before the fix per bug workflow.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Dave
2026-02-20 12:01:47 +00:00
parent e12985f40c
commit 3e99929d03
3 changed files with 48 additions and 16 deletions

View File

@@ -174,26 +174,21 @@ export function Chat({ projectPath, onCloseProject }: ChatProps) {
.getAnthropicApiKeyExists()
.then((exists) => {
setHasAnthropicKey(exists);
if (!exists) return;
return api.getAnthropicModels().then((models) => {
if (models.length > 0) {
const sortedModels = models.sort((a, b) =>
a.toLowerCase().localeCompare(b.toLowerCase()),
);
setClaudeModels(sortedModels);
} else {
setClaudeModels([]);
}
});
})
.catch((err) => {
console.error(err);
setHasAnthropicKey(false);
});
api
.getAnthropicModels()
.then((models) => {
if (models.length > 0) {
const sortedModels = models.sort((a, b) =>
a.toLowerCase().localeCompare(b.toLowerCase()),
);
setClaudeModels(sortedModels);
} else {
setClaudeModels([]);
}
})
.catch((err) => {
console.error(err);
setClaudeModels([]);
});
}, []);