import { useState } from 'react'; import { AxiosInstance } from 'axios'; import { useTranslation } from 'react-i18next'; import { StatusCard } from './StatusCard'; import type { ActivityEntry, PluginStatus, SettingsResponse } from './types'; type DashboardTabProps = { client: AxiosInstance; status: PluginStatus | null; connected: boolean; connectionError: string | null; hasApiKey: boolean; activity: ActivityEntry[]; devMode: boolean; apiBaseUrl: string; onApiBaseUrlChange: (url: string) => void; onSettingsSaved: (data: SettingsResponse) => void; onStatusRefresh: () => Promise; }; export function DashboardTab({ client, status, connected, connectionError, hasApiKey, activity, devMode, apiBaseUrl, onApiBaseUrlChange, onSettingsSaved, onStatusRefresh, }: DashboardTabProps) { const { t } = useTranslation(); const [apiKey, setApiKey] = useState(''); const [isApiKeyVisible, setIsApiKeyVisible] = useState(false); const [saveState, setSaveState] = useState<'idle' | 'saving' | 'saved' | 'error'>('idle'); const [saveError, setSaveError] = useState(null); const [testState, setTestState] = useState<'idle' | 'testing'>('idle'); async function onSave() { setSaveState('saving'); setSaveError(null); try { const payload: Record = { api_base_url: apiBaseUrl }; if (apiKey.trim().length > 0) { payload.api_key = apiKey.trim(); } const { data } = await client.post('/settings', payload); onSettingsSaved(data); setApiKey(''); setSaveState('saved'); setTimeout(() => setSaveState('idle'), 1500); } catch (err) { setSaveState('error'); setSaveError(err instanceof Error ? err.message : 'Failed to save settings'); } } async function onTestConnection() { setTestState('testing'); try { await onStatusRefresh(); } finally { setTestState('idle'); } } return (
{/* Stat Cards */}
{/* Connection error */} {!connected && connectionError && (
{t('dashboard.connectionFailed')} {connectionError}
)} {/* No API key warning */} {!hasApiKey && !connectionError && (
{t('dashboard.noApiKey')}
)} {/* Connection Settings + CTA */}

{t('dashboard.connectionSettings')}

{devMode && (
onApiBaseUrlChange(e.target.value)} placeholder="https://api.xcscribe.com" />
)}
setApiKey(e.target.value)} placeholder={hasApiKey ? t('dashboard.apiKeyPlaceholderSaved') : t('dashboard.apiKeyPlaceholderNew')} autoComplete="off" style={{ flex: 1, maxWidth: 360 }} />

{hasApiKey ? t('dashboard.apiKeyHintSaved') : t('dashboard.apiKeyHintNew')}

{saveState === 'error' && (
{saveError}
)} {saveState === 'saved' && (
{t('dashboard.settingsSaved')}
)}

{t('dashboard.fullPlatform')}

{t('dashboard.fullPlatformDesc')}

{/* Recent Activity */}

{t('dashboard.recentActivity')}

{activity.length === 0 ? (

{t('dashboard.noActivity')}

) : (
    {activity.map((entry, i) => (
  • {entry.type} {entry.title} {t('activity.xct', { cost: entry.xct_cost })} {entry.date} {entry.edit_url && ( {t('activity.edit')} )}
  • ))}
)}
); }