This repository has been archived on 2026-06-16. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
headlamp-sealed-secrets-plugin/headlamp-sealed-secrets/src/components/VersionWarning.tsx
T
Chris Farhood 329d030c1a fix: add defensive error handling for API version detection
Ensure error messages are always strings before rendering to prevent
React error #310 (invalid React child - object).

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
2026-02-12 20:18:54 -05:00

141 lines
3.8 KiB
TypeScript

/**
* Version Warning Component
*
* Displays warnings about API version compatibility and issues.
*/
import { Alert, Box, Button, Link } from '@mui/material';
import React from 'react';
import { SealedSecret } from '../lib/SealedSecretCRD';
export interface VersionWarningProps {
/** Whether to auto-detect version on mount */
autoDetect?: boolean;
/** Whether to show detailed version information */
showDetails?: boolean;
}
/**
* Component that detects and displays API version information
*
* Shows warnings if:
* - CRD is not installed
* - Version detection fails
* - Using non-default version (informational)
*/
export function VersionWarning({ autoDetect = true, showDetails = false }: VersionWarningProps) {
const [loading, setLoading] = React.useState(true);
const [detectedVersion, setDetectedVersion] = React.useState<string | null>(null);
const [error, setError] = React.useState<string | null>(null);
const detectVersion = React.useCallback(async () => {
setLoading(true);
setError(null);
try {
const result = await SealedSecret.detectApiVersion();
if (result.ok) {
setDetectedVersion(result.value);
setError(null);
} else if (result.ok === false) {
setDetectedVersion(null);
// Ensure error is always a string
const errorMessage = typeof result.error === 'string'
? result.error
: String(result.error);
setError(errorMessage);
}
} catch (e) {
// Catch any unexpected errors
setDetectedVersion(null);
setError(e instanceof Error ? e.message : String(e));
}
setLoading(false);
}, []);
React.useEffect(() => {
if (autoDetect) {
detectVersion();
}
}, [autoDetect, detectVersion]);
// Don't show anything while loading
if (loading) {
return null;
}
// Show error if detection failed
if (error) {
return (
<Box mb={2}>
<Alert severity="error" action={
<Button color="inherit" size="small" onClick={detectVersion}>
Retry
</Button>
}>
<strong>API Version Detection Failed</strong>
<br />
{String(error)}
{String(error).includes('not found') && (
<>
<br />
<br />
Install Sealed Secrets with:{' '}
<code style={{ fontSize: '0.875em' }}>
kubectl apply -f https://github.com/bitnami-labs/sealed-secrets/releases/download/v0.24.0/controller.yaml
</code>
<br />
Or visit:{' '}
<Link
href="https://github.com/bitnami-labs/sealed-secrets"
target="_blank"
rel="noopener noreferrer"
color="inherit"
>
github.com/bitnami-labs/sealed-secrets
</Link>
</>
)}
</Alert>
</Box>
);
}
// Show informational message if using non-default version
if (detectedVersion && detectedVersion !== SealedSecret.DEFAULT_VERSION) {
return (
<Box mb={2}>
<Alert severity="info">
<strong>API Version Detected</strong>
<br />
Using API version: <code>{detectedVersion}</code>
{showDetails && (
<>
<br />
Default version: <code>{SealedSecret.DEFAULT_VERSION}</code>
</>
)}
</Alert>
</Box>
);
}
// Show success if explicitly showing details
if (showDetails && detectedVersion) {
return (
<Box mb={2}>
<Alert severity="success">
<strong>API Version Detected</strong>
<br />
Using API version: <code>{detectedVersion}</code>
</Alert>
</Box>
);
}
// Default: show nothing (version detected successfully)
return null;
}