From 94f766d199ea17fd8d7f403e59d80691123d402f Mon Sep 17 00:00:00 2001 From: behrooz Date: Thu, 6 Nov 2025 19:50:01 +0330 Subject: [PATCH] fix namespace page --- src/pages/Namespaces.tsx | 62 +++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/src/pages/Namespaces.tsx b/src/pages/Namespaces.tsx index 428f757..80fcb8d 100644 --- a/src/pages/Namespaces.tsx +++ b/src/pages/Namespaces.tsx @@ -7,6 +7,7 @@ interface Namespace { } export default function Namespaces() { const [namespaces, setNamespace] = useState([]) + const [clusterName, setClusterName] = useState('') // const namespaces = [ // { name: 'default', status: 'Active', age: '2d', labels: 'app=web' }, // { name: 'kube-system', status: 'Active', age: '2d', labels: 'system' }, @@ -14,34 +15,41 @@ export default function Namespaces() { // { name: 'ingress-nginx', status: 'Active', age: '1d', labels: 'ingress' }, // ] - const fetchNamespaces = async () => { - try { - const response = await fetch('http://localhost:8082/cluster_namespaces?Name=test-cluster', { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - 'Authorization': `${localStorage.getItem('auth:token') || ''}` - }, - }) - - if (response.ok) { - const data = await response.json() - setNamespace(data || []) - - - } else { - const data = await response.json() - console.error(data.message || 'Failed to fetch clusters') - } - } catch (error) { - console.error('Failed to fetch clusters', error) - } - } + - useEffect(() => { - fetchNamespaces() - - }, []) + useEffect(() => { + const storedClusterName = localStorage.getItem('selectedCluster') + if (storedClusterName) { + setClusterName(storedClusterName) + } + + fetchNamespaces() + }, [clusterName]) + + const fetchNamespaces = async () => { + if (!clusterName) return + try { + const response = await fetch(`http://localhost:8082/cluster_namespaces?Name=${encodeURIComponent(clusterName)}`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `${localStorage.getItem('auth:token') || ''}` + }, + }) + + if (response.ok) { + const data = await response.json() + setNamespace(data || []) + + + } else { + const data = await response.json() + console.error(data.message || 'Failed to fetch clusters') + } + } catch (error) { + console.error('Failed to fetch clusters', error) + } + } return (