Skip to content

Commit 970da45

Browse files
author
Roman Snapko
committed
chore
1 parent 5768664 commit 970da45

1 file changed

Lines changed: 40 additions & 27 deletions

File tree

  • packages/react-ui/src/app/routes/settings/ai

packages/react-ui/src/app/routes/settings/ai/index.tsx

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
import { useMutation, useQueryClient } from '@tanstack/react-query';
2020
import { AxiosError } from 'axios';
2121
import { t } from 'i18next';
22-
import React from 'react';
22+
import React, { useCallback } from 'react';
2323

2424
const AiSettingsPage = () => {
2525
const { data: aiSettings, refetch: refetchAiSettings } =
@@ -52,17 +52,23 @@ const AiSettingsPage = () => {
5252
},
5353
onError: (error: AxiosError) => {
5454
let message = '';
55-
const apError = error.response?.data as ApplicationErrorParams;
55+
const apError = error.response?.data as
56+
| ApplicationErrorParams
57+
| undefined;
5658
if (
57-
apError.code ===
59+
apError?.code ===
5860
ErrorCode.OPENAI_COMPATIBLE_PROVIDER_BASE_URL_REQUIRED
5961
) {
6062
message = t('Base URL is required for OpenAI-compatible providers');
6163
} else {
62-
message =
63-
error.status === 400
64-
? (error.response?.data as { errorMessage: string })?.errorMessage
65-
: error.message;
64+
if (error.response?.status === 400) {
65+
const data = error.response?.data as
66+
| { errorMessage?: string }
67+
| undefined;
68+
message = data?.errorMessage ?? error.message;
69+
} else {
70+
message = error.message;
71+
}
6672
}
6773
toast({
6874
title: t('Error'),
@@ -84,7 +90,29 @@ const AiSettingsPage = () => {
8490
},
8591
});
8692

87-
const isAiConfigered = !!aiSettings?.[0]?.connection;
93+
const isAiConfigured = !!aiSettings?.[0]?.connection;
94+
95+
const handleSaveAi = useCallback(
96+
(connectionName: string) =>
97+
onSaveAiSettings({
98+
...aiSettings?.[0],
99+
enabled: !!connectionName,
100+
connection: connectionName,
101+
}),
102+
[onSaveAiSettings, aiSettings],
103+
);
104+
105+
const handleSaveMcp = useCallback(
106+
(connectionName: string) =>
107+
onSaveMcpSettings({
108+
...mcpSettings,
109+
awsCost: {
110+
enabled: !!connectionName,
111+
connectionName: removeConnectionBrackets(connectionName) ?? '',
112+
},
113+
}),
114+
[onSaveMcpSettings, mcpSettings],
115+
);
88116

89117
return (
90118
<div className="flex w-full flex-col items-center justify-center gap-4">
@@ -97,7 +125,7 @@ const AiSettingsPage = () => {
97125
'Enable OpenOps Assistant and other AI-powered features such as the CLI command generation.',
98126
)}
99127
</p>
100-
<AiConfigIndicator enabled={isAiConfigered} />
128+
<AiConfigIndicator enabled={isAiConfigured} />
101129
<div className="p-6 border rounded-[11px]">
102130
<div className="max-w-[648px] flex flex-col gap-4">
103131
<h3 className="text-base font-bold">{t('AI connection')}</h3>
@@ -106,13 +134,7 @@ const AiSettingsPage = () => {
106134
block={aiBlockModel}
107135
providerKey={'AI'}
108136
initialConnection={aiSettings?.[0]?.connection}
109-
onSave={(connectionName) =>
110-
onSaveAiSettings({
111-
...aiSettings?.[0],
112-
enabled: !!connectionName,
113-
connection: connectionName,
114-
})
115-
}
137+
onSave={handleSaveAi}
116138
disabled={isSavingAiSettings}
117139
displayName={t('Select Connection')}
118140
/>
@@ -126,16 +148,7 @@ const AiSettingsPage = () => {
126148
block={awsBlockModel}
127149
providerKey={'AWS'}
128150
initialConnection={mcpSettings?.awsCost?.connectionName}
129-
onSave={(connectionName: string) =>
130-
onSaveMcpSettings({
131-
...mcpSettings,
132-
awsCost: {
133-
enabled: !!connectionName,
134-
connectionName:
135-
removeConnectionBrackets(connectionName) ?? '',
136-
},
137-
})
138-
}
151+
onSave={handleSaveMcp}
139152
disabled={
140153
isSavingMcpSettings ||
141154
!aiSettings?.[0]?.connection ||
@@ -146,7 +159,7 @@ const AiSettingsPage = () => {
146159
) : (
147160
<Skeleton className="h-[78px]" />
148161
)}
149-
{!isAiConfigered && (
162+
{!isAiConfigured && (
150163
<p className="text-sm font-medium text-muted-foreground">
151164
{t('* Select AI connection in order to use the MCP tools')}
152165
</p>

0 commit comments

Comments
 (0)