import { useState } from 'react'; import { CircleCheck, LoaderCircle, PlugZap, TriangleAlert } from 'lucide-react'; import { __, sprintf } from '@wordpress/i18n'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { ajax, errorMessage } from '@/lib/api'; import { cn } from '@/lib/utils'; import type { ModelsData } from './types'; /** A test as AI_Content_Generator_For_WooCommerce_Admin lists them. */ interface ConnectionTestItem { id: string; provider: string; model: string; source: string; features?: string[]; note?: string; } interface FixLinkData { url: string; label: string; external?: boolean; } interface TestResult { ok: boolean; error?: { title?: string; message?: string; details?: string; links?: FixLinkData[]; }; } interface Row { test: ConnectionTestItem; status: 'pending' | 'ok' | 'error'; result?: TestResult; } const STATUS_BORDER = { pending: 'wtofe:border-l-primary/30', ok: 'wtofe:border-l-green-600', error: 'wtofe:border-l-red-600', }; const failedTitle = () => __( 'Test failed', 'ai-content-generator-for-woocommerce' ); const requestFailed = () => __( "Your server didn't finish the test. It may have stopped the request before the provider replied. Try again, or lower the request timeout.", 'ai-content-generator-for-woocommerce' ); const listFailed = () => __( 'Could not start the connection test. Reload the page and try again.', 'ai-content-generator-for-woocommerce' ); /** A fix link from explain_provider_error(): external, a field anchor, or an admin URL. */ function FixLink( { link }: { link: FixLinkData } ) { return link.external ? ( { link.label } ) : ( { link.label } ); } function ResultRow( { row }: { row: Row } ) { const { test, status, result } = row; const error = result?.error ?? {}; let meta: string = test.features?.length ? sprintf( /** translators: %s: comma-separated feature names */ __( 'Used for: %s', 'ai-content-generator-for-woocommerce' ), test.features.join( ', ' ) ) : __( 'Not used by any feature yet', 'ai-content-generator-for-woocommerce' ); if ( test.note ) { meta += ` · ${ test.note }`; } return (
{ ( error.title || failedTitle() ) + '. ' } { error.message }
{ !! error.links?.length && (
{ error.links.map( ( link ) => (
{ sprintf( /** translators: %s: provider name, e.g. OpenAI */ __( '%s said:', 'ai-content-generator-for-woocommerce' ), test.provider ) }{ ' ' } { error.details }
) }{ __( 'Sends a tiny request to each provider and model in use. Tests your saved settings, so save any changes first.', 'ai-content-generator-for-woocommerce' ) }
{ ( rows.length > 0 || listError ) && (