diff --git a/src/api/polaris.ts b/src/api/polaris.ts index 808b3d8..f40d5d5 100644 --- a/src/api/polaris.ts +++ b/src/api/polaris.ts @@ -93,6 +93,13 @@ export function countResults(data: AuditData): ResultCounts { // --- Settings --- +export const INTERVAL_OPTIONS = [ + { label: '1 minute', value: 60 }, + { label: '5 minutes', value: 300 }, + { label: '10 minutes', value: 600 }, + { label: '30 minutes', value: 1800 }, +]; + const STORAGE_KEY = 'polaris-plugin-refresh-interval'; const DEFAULT_INTERVAL_SECONDS = 300; // 5 minutes diff --git a/src/components/PolarisSettings.tsx b/src/components/PolarisSettings.tsx new file mode 100644 index 0000000..be80a22 --- /dev/null +++ b/src/components/PolarisSettings.tsx @@ -0,0 +1,40 @@ +import { NameValueTable, SectionBox } from '@kinvolk/headlamp-plugin/lib/CommonComponents'; +import React from 'react'; +import { getRefreshInterval, INTERVAL_OPTIONS, 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 { data, onDataChange } = props; + const currentInterval = (data?.refreshInterval as number) ?? getRefreshInterval(); + + function handleChange(e: React.ChangeEvent) { + const seconds = Number(e.target.value); + setRefreshInterval(seconds); + onDataChange?.({ ...data, refreshInterval: seconds }); + } + + return ( + + + {INTERVAL_OPTIONS.map(opt => ( + + ))} + + ), + }, + ]} + /> + + ); +} diff --git a/src/components/PolarisView.tsx b/src/components/PolarisView.tsx index 16f0127..9e4efd2 100644 --- a/src/components/PolarisView.tsx +++ b/src/components/PolarisView.tsx @@ -1,5 +1,4 @@ import { - HeaderLabel, Loader, NameValueTable, SectionBox, @@ -13,32 +12,15 @@ import { countResults, getRefreshInterval, ResultCounts, - setRefreshInterval, usePolarisData, } from '../api/polaris'; -const INTERVAL_OPTIONS = [ - { label: '1 minute', value: 60 }, - { label: '5 minutes', value: 300 }, - { label: '10 minutes', value: 600 }, - { label: '30 minutes', value: 1800 }, -]; - function scoreStatus(score: number): 'success' | 'warning' | 'error' { if (score >= 80) return 'success'; if (score >= 50) return 'warning'; return 'error'; } -function RefreshSettings(props: { interval: number; onChange: (seconds: number) => void }) { - return ( - o.value === props.interval)?.label ?? ''} - /> - ); -} - function OverviewSection(props: { data: AuditData; counts: ResultCounts }) { const score = computeScore(props.counts); const status = scoreStatus(score); @@ -105,13 +87,7 @@ function OverviewSection(props: { data: AuditData; counts: ResultCounts }) { } export default function PolarisView() { - const [interval, setInterval] = React.useState(getRefreshInterval); - - function handleIntervalChange(seconds: number) { - setInterval(seconds); - setRefreshInterval(seconds); - } - + const interval = getRefreshInterval(); const { data, loading, error } = usePolarisData(interval); if (loading) { @@ -122,12 +98,7 @@ export default function PolarisView() { return ( <> - , - ]} - /> + {error && ( diff --git a/src/index.tsx b/src/index.tsx index 3b30f3f..fdce70e 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,5 +1,10 @@ -import { registerRoute, registerSidebarEntry } from '@kinvolk/headlamp-plugin/lib'; +import { + registerPluginSettings, + registerRoute, + registerSidebarEntry, +} from '@kinvolk/headlamp-plugin/lib'; import React from 'react'; +import PolarisSettings from './components/PolarisSettings'; import PolarisView from './components/PolarisView'; registerSidebarEntry({ @@ -17,3 +22,5 @@ registerRoute({ exact: true, component: () => , }); + +registerPluginSettings('polaris-headlamp-plugin', PolarisSettings, true);