Skip to content

Refactor namespace management and hooks #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ui/app/providers.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client'

import { KubernetesProvider } from '@/contexts/KubernetesContext'
import { KubernetesProvider } from '@kubernetesjs/react'
import { NamespaceProvider } from '@/contexts/NamespaceContext'

interface ProvidersProps {
children: React.ReactNode
Expand All @@ -9,7 +10,7 @@ interface ProvidersProps {
export function Providers({ children }: ProvidersProps) {
return (
<KubernetesProvider>
{children}
<NamespaceProvider>{children}</NamespaceProvider>
</KubernetesProvider>
)
}
4 changes: 2 additions & 2 deletions ui/components/namespace-switcher.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import { useKubernetes, useNamespaces } from '@/hooks'
import { usePreferredNamespace, useNamespaces } from '@/hooks'
import {
Select,
SelectContent,
Expand All @@ -13,7 +13,7 @@ import { RefreshCw } from 'lucide-react'
import { Button } from '@/components/ui/button'

export function NamespaceSwitcher() {
const { namespace, setNamespace } = useKubernetes()
const { namespace, setNamespace } = usePreferredNamespace()
const { data, isLoading, error, refetch } = useNamespaces()

const namespaces = data?.items?.map(item => item.metadata?.name).filter(Boolean) || []
Expand Down
4 changes: 2 additions & 2 deletions ui/components/resources/pods.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
XCircle
} from 'lucide-react'
import { type Pod as K8sPod } from 'kubernetesjs'
import { usePods, useDeletePod, usePodLogs, useKubernetes } from '@/hooks'
import { usePods, useDeletePod, usePodLogs, usePreferredNamespace } from '@/hooks'

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

// Use TanStack Query hooks
const { namespace } = useKubernetes()
const { namespace } = usePreferredNamespace()
const { data, isLoading, error, refetch } = usePods()
const deletePodMutation = useDeletePod()

Expand Down
5 changes: 3 additions & 2 deletions ui/components/templates/template-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Label } from '@/components/ui/label'
import { Alert, AlertDescription } from '@/components/ui/alert'
import { Loader2, CheckCircle, XCircle } from 'lucide-react'
import { type Deployment, type Service } from 'kubernetesjs'
import { useKubernetes } from '@/hooks'
import { useKubernetes, usePreferredNamespace } from '@/hooks'

interface Template {
id: string
Expand All @@ -36,7 +36,8 @@ interface TemplateDialogProps {
}

export function TemplateDialog({ template, open, onOpenChange }: TemplateDialogProps) {
const { client: k8sClient, namespace: contextNamespace } = useKubernetes()
const { client: k8sClient } = useKubernetes()
const { namespace: contextNamespace } = usePreferredNamespace()

// Deploy template function
const deployTemplate = async (params: {
Expand Down
99 changes: 0 additions & 99 deletions ui/contexts/KubernetesContext.tsx

This file was deleted.

32 changes: 32 additions & 0 deletions ui/contexts/NamespaceContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use client'

import React, { createContext, useContext, useState } from 'react'

interface NamespaceContextValue {
namespace: string
setNamespace: (namespace: string) => void
}

const NamespaceContext = createContext<NamespaceContextValue | undefined>(undefined)

interface NamespaceProviderProps {
children: React.ReactNode
initialNamespace?: string
}

export function NamespaceProvider({ children, initialNamespace = 'default' }: NamespaceProviderProps) {
const [namespace, setNamespace] = useState(initialNamespace)
return (
<NamespaceContext.Provider value={{ namespace, setNamespace }}>
{children}
</NamespaceContext.Provider>
)
}

export function usePreferredNamespace(): NamespaceContextValue {
const context = useContext(NamespaceContext)
if (!context) {
throw new Error('usePreferredNamespace must be used within a NamespaceProvider')
}
return context
}
3 changes: 2 additions & 1 deletion ui/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export * from './useDaemonSets'
export * from './useReplicaSets'

// Re-export context hook
export { useKubernetes } from '../contexts/KubernetesContext'
export { useKubernetes } from '@kubernetesjs/react'
export { usePreferredNamespace } from '../contexts/NamespaceContext'
Loading
Loading