Skip to content

Commit 99a5e2d

Browse files
committed
refactor: migrate hooks to kubernetesjs react
1 parent cf4a56f commit 99a5e2d

17 files changed

+613
-765
lines changed

ui/app/providers.tsx

Lines changed: 5 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/context'
4+
import { NamespaceProvider } from '@/contexts/NamespaceContext'
45

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

ui/components/dashboard-layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Link from 'next/link'
55
import { usePathname } from 'next/navigation'
66
import { Button } from '@/components/ui/button'
77
import { NamespaceSwitcher } from '@/components/namespace-switcher'
8-
import { useKubernetes } from '@/hooks'
8+
import { useKubernetes } from '@kubernetesjs/react/context'
99
import {
1010
Package,
1111
Server,

ui/components/namespace-switcher.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 { useKubernetes, useNamespaces } from '@/hooks'
3+
import { useNamespaces } from '@/hooks'
4+
import { usePreferredNamespace } from '@/contexts/NamespaceContext'
45
import {
56
Select,
67
SelectContent,
@@ -13,7 +14,7 @@ import { RefreshCw } from 'lucide-react'
1314
import { Button } from '@/components/ui/button'
1415

1516
export function NamespaceSwitcher() {
16-
const { namespace, setNamespace } = useKubernetes()
17+
const { namespace, setNamespace } = usePreferredNamespace()
1718
const { data, isLoading, error, refetch } = useNamespaces()
1819

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

ui/components/resources/pods.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ 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 } from '@/hooks'
21+
import { usePreferredNamespace } from '@/contexts/NamespaceContext'
2122

2223
interface Pod {
2324
name: string
@@ -34,7 +35,7 @@ export function PodsView() {
3435
const [selectedPod, setSelectedPod] = useState<Pod | null>(null)
3536

3637
// Use TanStack Query hooks
37-
const { namespace } = useKubernetes()
38+
const { namespace } = usePreferredNamespace()
3839
const { data, isLoading, error, refetch } = usePods()
3940
const deletePodMutation = useDeletePod()
4041

ui/components/templates/template-dialog.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ 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 } from '@kubernetesjs/react/context'
19+
import { usePreferredNamespace } from '@/contexts/NamespaceContext'
1920

2021
interface Template {
2122
id: string
@@ -36,7 +37,8 @@ interface TemplateDialogProps {
3637
}
3738

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

4143
// Deploy template function
4244
const deployTemplate = async (params: {

ui/contexts/KubernetesContext.tsx

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

ui/contexts/NamespaceContext.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use client'
2+
3+
import React, { createContext, useContext, useState } from 'react'
4+
5+
interface NamespaceContextValue {
6+
namespace: string
7+
setNamespace: (ns: 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<string>(initialNamespace)
19+
20+
return (
21+
<NamespaceContext.Provider value={{ namespace, setNamespace }}>
22+
{children}
23+
</NamespaceContext.Provider>
24+
)
25+
}
26+
27+
export function usePreferredNamespace(): NamespaceContextValue {
28+
const ctx = useContext(NamespaceContext)
29+
if (!ctx) throw new Error('usePreferredNamespace must be used within a NamespaceProvider')
30+
return ctx
31+
}

ui/hooks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ 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/context'

0 commit comments

Comments
 (0)