()
| 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 { |
| 95 | setLoading(false); |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | loadNamespaces(); |
| 100 | // eslint-disable-next-line react-hooks/exhaustive-deps |
no test coverage detected