import { useEffect, useState } from 'react'; import { SelectableValue } from '@grafana/data'; import { Select } from '@grafana/ui'; import { NamespaceContext, ResourceContext } from './plugins'; type Props = { value?: string; onChange: (v?: string) => void; // The wrapped element Original: React.ElementType; props: Record; }; export function K8sNameLookup(props: Props) { const [focused, setFocus] = useState(false); const [group, setGroup] = useState(); const [version, setVersion] = useState(); const [resource, setResource] = useState(); const [namespace, setNamespace] = useState(); const [namespaced, setNamespaced] = useState(); const [loading, setLoading] = useState(false); const [options, setOptions] = useState>>(); const [placeholder, setPlaceholder] = useState('Enter kubernetes name'); useEffect(() => { if (focused && group && version && resource) { setLoading(true); setPlaceholder('Enter kubernetes name'); const fn = async () => { const url = namespaced ? `apis/${group}/${version}/namespaces/${namespace}/${resource}` : `apis/${group}/${version}/${resource}`; const response = await fetch(url + '?limit=100', { headers: { Accept: 'application/json;as=Table;v=v1;g=meta.k8s.io,application/json;as=Table;v=v1beta1;g=meta.k8s.io,application/jso', }, }); if (!response.ok) { console.warn('error loading names'); setLoading(false); return; } const table = await response.json(); console.log('LIST', url, table); const options: Array> = []; if (table.rows?.length) { for (const row of table.rows) { const n = row.object?.metadata?.name; if (n) { options.push({ label: n, value: n }); } } } else { setPlaceholder('No items found'); } setLoading(false); setOptions(options); }; fn(); } }, [focused, namespace, group, version, resource, namespaced]); return ( {(namespace) => { return ( {(info) => { // delay avoids Cannot update a component setTimeout(() => { setNamespace(namespace); setGroup(info?.group); setVersion(info?.version); setResource(info?.resource); setNamespaced(info?.namespaced); }, 200); if (info) { const value = props.value ? { label: props.value, value: props.value } : undefined; return (