import { ApiProxy } from '@kinvolk/headlamp-plugin/lib'; import { NameValueTable, SectionBox, StatusLabel, } from '@kinvolk/headlamp-plugin/lib/CommonComponents'; import { useTheme } from '@mui/material/styles'; import React from 'react'; import { AuditData, getDashboardUrl, getPolarisApiPath, getRefreshInterval, INTERVAL_OPTIONS, isFullUrl, setDashboardUrl, setRefreshInterval, } from '../api/polaris'; interface PluginSettingsProps { data?: { [key: string]: string | number | boolean }; onDataChange?: (data: { [key: string]: string | number | boolean }) => void; } export default function PolarisSettings(props: PluginSettingsProps) { const theme = useTheme(); const { data, onDataChange } = props; const currentInterval = (data?.refreshInterval as number) ?? getRefreshInterval(); const currentUrl = (data?.dashboardUrl as string) ?? getDashboardUrl(); const [testing, setTesting] = React.useState(false); const [testResult, setTestResult] = React.useState<{ success: boolean; message: string } | null>( null ); function handleIntervalChange(e: React.ChangeEvent) { const seconds = Number(e.target.value); setRefreshInterval(seconds); onDataChange?.({ ...data, refreshInterval: seconds }); } function handleUrlChange(e: React.ChangeEvent) { const url = e.target.value; setDashboardUrl(url); onDataChange?.({ ...data, dashboardUrl: url }); } async function testConnection() { setTesting(true); setTestResult(null); try { const apiPath = getPolarisApiPath(); let result: AuditData; if (isFullUrl(apiPath)) { const response = await fetch(apiPath); if (!response.ok) { throw new Error(`HTTP ${response.status}: ${response.statusText}`); } result = await response.json(); } else { result = await ApiProxy.request(apiPath, { method: 'GET', }); } setTestResult({ success: true, message: `Connected successfully! Version: ${ result.PolarisOutputVersion }, Last audit: ${new Date(result.AuditTime).toLocaleString()}`, }); } catch (err) { setTestResult({ success: false, message: `Connection failed: ${String(err)}`, }); } finally { setTesting(false); } } return ( {INTERVAL_OPTIONS.map(opt => ( ))} ), }, { name: 'Dashboard URL', value: (
Examples:
• K8s proxy:{' '} /api/v1/namespaces/polaris/services/http:polaris-dashboard:80/proxy/
• Full URL: https://my-polaris.example.com
), }, { name: 'Connection Test', value: (
{testResult && (
{testResult.message}
)}
), }, ]} />
); }