({
value,
onValueChange,
placeholder = "Select namespace…",
disabled = false,
includeAllNamespaces = false,
allNamespacesLabel = "All namespaces",
autoSelectDefault = true,
ariaLabel,
id: triggerId,
onError,
}: NamespaceComboboxProps)
| 35 | } |
| 36 | |
| 37 | export function NamespaceCombobox({ |
| 38 | value, |
| 39 | onValueChange, |
| 40 | placeholder = "Select namespace…", |
| 41 | disabled = false, |
| 42 | includeAllNamespaces = false, |
| 43 | allNamespacesLabel = "All namespaces", |
| 44 | autoSelectDefault = true, |
| 45 | ariaLabel, |
| 46 | id: triggerId, |
| 47 | onError, |
| 48 | }: NamespaceComboboxProps) { |
| 49 | const [open, setOpen] = useState(false); |
| 50 | const [namespaces, setNamespaces] = useState<NamespaceResponse[]>([]); |
| 51 | const [loading, setLoading] = useState(false); |
| 52 | const [error, setError] = useState<string | null>(null); |
| 53 | |
| 54 | useEffect(() => { |
| 55 | const loadNamespaces = async () => { |
| 56 | try { |
| 57 | setLoading(true); |
| 58 | setError(null); |
| 59 | const response = await listNamespaces(); |
| 60 | |
| 61 | if (!response.error) { |
| 62 | const sorted = [...(response.data || [])].sort((a, b) => |
| 63 | a.name.localeCompare(b.name, undefined, { sensitivity: "base" }) |
| 64 | ); |
| 65 | setNamespaces(sorted); |
| 66 | setError(null); |
| 67 | onError?.(null); |
| 68 | |
| 69 | // Set a default namespace if none is currently selected |
| 70 | if (autoSelectDefault && !value) { |
| 71 | const names = sorted.map((ns) => ns.name); |
| 72 | let defaultNamespace: string | undefined; |
| 73 | if (names.includes("kagent")) { |
| 74 | defaultNamespace = "kagent"; |
| 75 | } else if (names.includes("default")) { |
| 76 | defaultNamespace = "default"; |
| 77 | } else if (names.length > 0) { |
| 78 | defaultNamespace = names[0]; |
| 79 | } |
| 80 | if (defaultNamespace) { |
| 81 | onValueChange(defaultNamespace); |
| 82 | } |
| 83 | } |
| 84 | } else { |
| 85 | const errorMsg = response.error || 'Failed to load namespaces'; |
| 86 | setError(errorMsg); |
| 87 | onError?.(errorMsg); |
| 88 | } |
| 89 | } catch (err) { |
| 90 | console.error('Failed to load namespaces:', err); |
| 91 | const errorMsg = err instanceof Error ? err.message : 'Failed to load namespaces'; |
| 92 | setError(errorMsg); |
| 93 | onError?.(errorMsg); |
| 94 | } finally { |
nothing calls this directly
no test coverage detected