Skip to content

Commit

Permalink
Show error state if not validated input
Browse files Browse the repository at this point in the history
  • Loading branch information
yoziru committed Feb 7, 2024
1 parent a075cfb commit 50e8d4a
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/components/AppConfig/AppConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { lastValueFrom } from 'rxjs';
import { css } from '@emotion/css';
import { AppPluginMeta, GrafanaTheme2, KeyValue, PluginConfigPageProps, PluginMeta } from '@grafana/data';
import { FetchResponse, HealthCheckResult, getBackendSrv } from '@grafana/runtime';
import { Button, LoadingPlaceholder, useStyles2 } from '@grafana/ui';
import { Alert, Button, LoadingPlaceholder, useStyles2 } from '@grafana/ui';

import { testIds } from '../testIds';
import { ShowHealthCheckResult } from './HealthCheck';
Expand Down Expand Up @@ -88,17 +88,17 @@ export const AppConfig = ({ plugin }: AppConfigProps) => {
const [isUpdating, setIsUpdating] = useState(false);
const [healthCheck, setHealthCheck] = useState<HealthCheckResult | undefined>(undefined);

const validateInputs = (): boolean => {
const validateInputs = (): string | undefined => {
// Check if Grafana-provided OpenAI enabled, that it has been opted-in
if (settings?.openAI?.provider === 'grafana' && !settings?.llmGateway?.isOptIn) {
console.error("You must click the 'Enable OpenAI access via Grafana' button to use OpenAI provided by Grafana");
return false;
return "You must click the 'Enable OpenAI access via Grafana' button to use OpenAI provided by Grafana";
}
return true;
return;
};
const errorState = validateInputs();

const doSave = async () => {
if (!validateInputs()) {
if (errorState !== undefined) {
return;
}
// Push LLM opt-in state, will also check if the user is allowed to opt-in
Expand Down Expand Up @@ -183,13 +183,19 @@ export const AppConfig = ({ plugin }: AppConfigProps) => {
}}
/>

{errorState !== undefined && <Alert title={errorState} severity="error" />}
{isUpdating ? (
<LoadingPlaceholder text="Running health check..." />
) : (
healthCheck && <ShowHealthCheckResult {...healthCheck} />
)}
<div className={s.marginTop}>
<Button type="submit" data-testid={testIds.appConfig.submit} onClick={doSave} disabled={!updated || isUpdating}>
<Button
type="submit"
data-testid={testIds.appConfig.submit}
onClick={doSave}
disabled={!updated || isUpdating || errorState !== undefined}
>
Save &amp; test
</Button>
</div>
Expand Down

0 comments on commit 50e8d4a

Please sign in to comment.