Skip to content

Commit f3f9039

Browse files
committed
add namespace context and update hooks
1 parent cf4a56f commit f3f9039

File tree

16 files changed

+546
-770
lines changed

16 files changed

+546
-770
lines changed

ui/app/providers.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use client'
22

3-
import { KubernetesProvider } from '@/contexts/KubernetesContext'
3+
import { KubernetesProvider } from '@kubernetesjs/react'
4+
import { NamespaceProvider } from '@/contexts/NamespaceContext'
45

56
interface ProvidersProps {
67
children: React.ReactNode
@@ -9,7 +10,7 @@ interface ProvidersProps {
910
export function Providers({ children }: ProvidersProps) {
1011
return (
1112
<KubernetesProvider>
12-
{children}
13+
<NamespaceProvider>{children}</NamespaceProvider>
1314
</KubernetesProvider>
1415
)
1516
}

ui/components/namespace-switcher.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { useKubernetes, useNamespaces } from '@/hooks'
3+
import { usePreferredNamespace, useNamespaces } from '@/hooks'
44
import {
55
Select,
66
SelectContent,
@@ -13,7 +13,7 @@ import { RefreshCw } from 'lucide-react'
1313
import { Button } from '@/components/ui/button'
1414

1515
export function NamespaceSwitcher() {
16-
const { namespace, setNamespace } = useKubernetes()
16+
const { namespace, setNamespace } = usePreferredNamespace()
1717
const { data, isLoading, error, refetch } = useNamespaces()
1818

1919
const namespaces = data?.items?.map(item => item.metadata?.name).filter(Boolean) || []

ui/components/resources/pods.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
XCircle
1818
} from 'lucide-react'
1919
import { type Pod as K8sPod } from 'kubernetesjs'
20-
import { usePods, useDeletePod, usePodLogs, useKubernetes } from '@/hooks'
20+
import { usePods, useDeletePod, usePodLogs, usePreferredNamespace } from '@/hooks'
2121

2222
interface Pod {
2323
name: string
@@ -34,7 +34,7 @@ export function PodsView() {
3434
const [selectedPod, setSelectedPod] = useState<Pod | null>(null)
3535

3636
// Use TanStack Query hooks
37-
const { namespace } = useKubernetes()
37+
const { namespace } = usePreferredNamespace()
3838
const { data, isLoading, error, refetch } = usePods()
3939
const deletePodMutation = useDeletePod()
4040

ui/components/templates/template-dialog.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { Label } from '@/components/ui/label'
1515
import { Alert, AlertDescription } from '@/components/ui/alert'
1616
import { Loader2, CheckCircle, XCircle } from 'lucide-react'
1717
import { type Deployment, type Service } from 'kubernetesjs'
18-
import { useKubernetes } from '@/hooks'
18+
import { useKubernetes, usePreferredNamespace } from '@/hooks'
1919

2020
interface Template {
2121
id: string
@@ -36,7 +36,8 @@ interface TemplateDialogProps {
3636
}
3737

3838
export function TemplateDialog({ template, open, onOpenChange }: TemplateDialogProps) {
39-
const { client: k8sClient, namespace: contextNamespace } = useKubernetes()
39+
const { client: k8sClient } = useKubernetes()
40+
const { namespace: contextNamespace } = usePreferredNamespace()
4041

4142
// Deploy template function
4243
const deployTemplate = async (params: {

ui/contexts/KubernetesContext.tsx

Lines changed: 0 additions & 99 deletions
This file was deleted.

ui/contexts/NamespaceContext.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use client'
2+
3+
import React, { createContext, useContext, useState } from 'react'
4+
5+
interface NamespaceContextValue {
6+
namespace: string
7+
setNamespace: (namespace: string) => void
8+
}
9+
10+
const NamespaceContext = createContext<NamespaceContextValue | undefined>(undefined)
11+
12+
interface NamespaceProviderProps {
13+
children: React.ReactNode
14+
initialNamespace?: string
15+
}
16+
17+
export function NamespaceProvider({ children, initialNamespace = 'default' }: NamespaceProviderProps) {
18+
const [namespace, setNamespace] = useState(initialNamespace)
19+
return (
20+
<NamespaceContext.Provider value={{ namespace, setNamespace }}>
21+
{children}
22+
</NamespaceContext.Provider>
23+
)
24+
}
25+
26+
export function usePreferredNamespace(): NamespaceContextValue {
27+
const context = useContext(NamespaceContext)
28+
if (!context) {
29+
throw new Error('usePreferredNamespace must be used within a NamespaceProvider')
30+
}
31+
return context
32+
}

ui/hooks/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export * from './useDaemonSets'
99
export * from './useReplicaSets'
1010

1111
// Re-export context hook
12-
export { useKubernetes } from '../contexts/KubernetesContext'
12+
export { useKubernetes } from '@kubernetesjs/react'
13+
export { usePreferredNamespace } from '../contexts/NamespaceContext'

0 commit comments

Comments
 (0)