-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathResourcesView.tsx
More file actions
155 lines (139 loc) · 6.01 KB
/
Copy pathResourcesView.tsx
File metadata and controls
155 lines (139 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { useState, useMemo } from 'react'
import { useLocation, useNavigate } from 'react-router-dom'
import { useQuery } from '@tanstack/react-query'
import { ApiError, fetchJSON, isForbiddenError, useSecretCertExpiry, useTopPodMetrics, useTopNodeMetrics, useBulkDeleteResources } from '../../api/client'
import { useAPIResources } from '../../api/apiResources'
import { usePinnedKinds } from '../../hooks/useFavorites'
import { useOpenLogs, useOpenWorkloadLogs } from '../dock'
import {
ResourcesView as BaseResourcesView,
CORE_RESOURCES,
} from '@skyhook-io/k8s-ui'
import type { ResourceQueryResult } from '@skyhook-io/k8s-ui'
import type { SelectedResource } from '../../types'
import type { NavigateToResource } from '../../utils/navigation'
interface ResourceCountsResponse {
counts: Record<string, number>
forbidden?: string[]
}
interface ResourcesViewProps {
namespaces: string[]
selectedResource?: SelectedResource | null
onResourceClick?: (resource: SelectedResource | null) => void
onResourceClickYaml?: NavigateToResource
onKindChange?: () => void
}
export function ResourcesView({ namespaces, selectedResource, onResourceClick, onResourceClickYaml, onKindChange }: ResourcesViewProps) {
const location = useLocation()
const navigate = useNavigate()
// API resources discovery
const { data: apiResources } = useAPIResources()
// Track the selected kind from the k8s-ui component
const [selectedKind, setSelectedKind] = useState<{ name: string; kind: string; group: string } | null>(null)
// Lightweight resource counts for sidebar badges (~2KB instead of ~608MB)
const namespacesParam = namespaces.join(',')
const { data: countsData } = useQuery({
queryKey: ['resource-counts', namespacesParam],
queryFn: async () => {
const params = new URLSearchParams()
if (namespaces.length > 0) params.set('namespaces', namespacesParam)
return fetchJSON<ResourceCountsResponse>(`/resource-counts?${params}`)
},
staleTime: 10000,
refetchInterval: 15000,
})
// Determine if selected kind is a CRD (only CRDs should send ?group= to backend)
const isSelectedCrd = useMemo(() => {
if (!selectedKind) return false
// Check API resources first, fall back to CORE_RESOURCES
const match = apiResources?.find(r => r.name === selectedKind.name && r.group === selectedKind.group)
?? CORE_RESOURCES.find(r => r.name === selectedKind.name && r.group === selectedKind.group)
return match?.isCrd ?? (!!selectedKind.group) // default: has group = likely CRD
}, [selectedKind, apiResources])
// Fetch full data only for the selected kind
const selectedKindQuery = useQuery({
queryKey: ['resources', selectedKind?.name, isSelectedCrd ? selectedKind?.group : '', namespaces],
queryFn: async () => {
if (!selectedKind) return []
const params = new URLSearchParams()
if (namespaces.length > 0) params.set('namespaces', namespacesParam)
if (isSelectedCrd && selectedKind.group) params.set('group', selectedKind.group)
const res = await fetch(`/api/resources/${selectedKind.name}?${params}`)
if (!res.ok) {
const errorData = await res.json().catch(() => ({ error: `HTTP ${res.status}` }))
throw new ApiError(errorData.error || `Failed to fetch ${selectedKind.name}`, res.status, errorData)
}
return res.json()
},
enabled: !!selectedKind,
staleTime: 30000,
refetchInterval: 30000,
retry: (failureCount: number, error: Error) => {
if (isForbiddenError(error)) return false
return failureCount < 3
},
})
// Map to ResourceQueryResult shape
const selectedKindQueryResult: ResourceQueryResult | undefined = useMemo(() => {
if (!selectedKind) return undefined
return {
data: selectedKindQuery.data as any[] | undefined,
isLoading: selectedKindQuery.isLoading,
error: selectedKindQuery.error,
refetch: selectedKindQuery.refetch,
dataUpdatedAt: selectedKindQuery.dataUpdatedAt,
}
}, [selectedKind, selectedKindQuery.data, selectedKindQuery.isLoading, selectedKindQuery.error, selectedKindQuery.refetch, selectedKindQuery.dataUpdatedAt])
// Metrics
const { data: topPodMetrics } = useTopPodMetrics()
const { data: topNodeMetrics } = useTopNodeMetrics()
// Certificate expiry
const { data: certExpiry, isError: certExpiryError } = useSecretCertExpiry()
// Pinned kinds
const { pinned, togglePin, isPinned } = usePinnedKinds()
// Dock actions
const openLogs = useOpenLogs()
const openWorkloadLogs = useOpenWorkloadLogs()
// Bulk delete
const bulkDeleteMutation = useBulkDeleteResources()
// Navigation adapter
const handleNavigate = useMemo(() => {
return (path: string, options?: { replace?: boolean }) => {
navigate(path, { replace: options?.replace })
}
}, [navigate])
return (
<BaseResourcesView
namespaces={namespaces}
selectedResource={selectedResource}
onResourceClick={onResourceClick}
onResourceClickYaml={onResourceClickYaml}
onKindChange={onKindChange}
// Injected data
apiResources={apiResources}
// Lightweight counts for sidebar (replaces 233 parallel queries)
resourceCounts={countsData?.counts}
resourceForbidden={countsData?.forbidden}
selectedKindQuery={selectedKindQueryResult}
onSelectedKindChange={setSelectedKind}
topPodMetrics={topPodMetrics}
topNodeMetrics={topNodeMetrics}
certExpiry={certExpiry}
certExpiryError={certExpiryError}
// Pinned kinds
pinned={pinned}
togglePin={togglePin}
isPinned={(kind: string, group?: string) => isPinned(kind, group ?? '')}
// Navigation
locationSearch={location.search}
locationPathname={location.pathname}
onNavigate={handleNavigate}
// Dock actions
onOpenLogs={openLogs}
onOpenWorkloadLogs={openWorkloadLogs}
// Bulk operations
onBulkDelete={(items, options) => bulkDeleteMutation.mutate({ items, force: options?.force }, { onSuccess: options?.onSuccess })}
isBulkDeleting={bulkDeleteMutation.isPending}
/>
)
}