1
- import { Flex , SubTab , TabList } from '@pluralsh/design-system'
1
+ import { mergeDeep } from '@apollo/client/utilities'
2
+ import { EmptyState , Flex , SubTab , TabList } from '@pluralsh/design-system'
3
+ import { POLL_INTERVAL } from 'components/cluster/constants.ts'
4
+ import { GqlError } from 'components/utils/Alert.tsx'
2
5
import {
3
6
Dispatch ,
4
7
ReactNode ,
@@ -11,7 +14,12 @@ import {
11
14
} from 'react'
12
15
import { Outlet , useMatch , useOutletContext } from 'react-router-dom'
13
16
import { useTheme } from 'styled-components'
14
- import { AiInsight , ClusterFragment } from '../../../generated/graphql.ts'
17
+ import {
18
+ AiInsight ,
19
+ ClusterFragment ,
20
+ ClusterInsightFragment ,
21
+ useClusterInsightQuery ,
22
+ } from '../../../generated/graphql.ts'
15
23
import {
16
24
CLUSTER_ABS_PATH ,
17
25
CLUSTER_INSIGHTS_COMPONENTS_PATH ,
@@ -24,11 +32,11 @@ import {
24
32
ChatWithAIButton ,
25
33
insightMessage ,
26
34
} from '../../ai/chatbot/ChatbotButton.tsx'
35
+ import { InsightDisplay } from '../../ai/insights/InsightDisplay.tsx'
27
36
import LoadingIndicator from '../../utils/LoadingIndicator.tsx'
28
37
import IconFrameRefreshButton from '../../utils/RefreshIconFrame.tsx'
29
38
import { LinkTabWrap } from '../../utils/Tabs.tsx'
30
39
import { useClusterContext } from './Cluster.tsx'
31
- import { InsightDisplay } from '../../ai/insights/InsightDisplay.tsx'
32
40
33
41
const DIRECTORY : Array < DirectoryEntry > = [
34
42
{ path : CLUSTER_INSIGHTS_SUMMARY_PATH , label : 'Insight summary' } ,
@@ -42,16 +50,44 @@ interface DirectoryEntry {
42
50
43
51
export default function ClusterInsights ( ) : ReactNode {
44
52
const theme = useTheme ( )
45
- const { cluster, refetch , clusterLoading } = useClusterContext ( )
53
+ const { cluster, clusterLoading } = useClusterContext ( )
46
54
const tabStateRef = useRef < any > ( null )
47
- const tab =
48
- useMatch ( `${ CLUSTER_ABS_PATH } /${ CLUSTER_INSIGHTS_PATH } /:tab/*` ) ?. params
49
- ?. tab || ''
55
+ const { tab } =
56
+ useMatch ( `${ CLUSTER_ABS_PATH } /${ CLUSTER_INSIGHTS_PATH } /:tab? /*` ) ?. params ??
57
+ { }
50
58
const currentTab = DIRECTORY . find ( ( { path } ) => path === tab )
51
59
60
+ const {
61
+ data,
62
+ loading : insightLoading ,
63
+ error,
64
+ refetch,
65
+ } = useClusterInsightQuery ( {
66
+ variables : { id : cluster ?. id ?? '' } ,
67
+ fetchPolicy : 'cache-and-network' ,
68
+ pollInterval : POLL_INTERVAL ,
69
+ } )
70
+
52
71
const [ navigationContent , setNavigationContent ] = useState < ReactNode > ( )
53
72
const [ actionContent , setActionContent ] = useState < ReactNode > ( )
73
+ const ctx : ClusterInsightsContextType = useMemo (
74
+ ( ) => ( {
75
+ cluster : mergeDeep ( cluster , data ?. cluster ) ,
76
+ clusterLoading : clusterLoading || insightLoading ,
77
+ refetch,
78
+ setNavigationContent,
79
+ setActionContent,
80
+ } ) ,
81
+ [ cluster , data ?. cluster , clusterLoading , insightLoading , refetch ]
82
+ )
54
83
84
+ if ( error ) return < GqlError error = { error } />
85
+ if ( ! data )
86
+ return insightLoading ? (
87
+ < LoadingIndicator />
88
+ ) : (
89
+ < EmptyState message = "Cluster insights not found." />
90
+ )
55
91
return (
56
92
< Flex
57
93
direction = "column"
@@ -104,17 +140,7 @@ export default function ClusterInsights(): ReactNode {
104
140
</ Flex >
105
141
</ Flex >
106
142
< Suspense fallback = { < LoadingIndicator /> } >
107
- < Outlet
108
- context = {
109
- {
110
- cluster,
111
- clusterLoading,
112
- refetch,
113
- setNavigationContent,
114
- setActionContent,
115
- } as ClusterInsightsContextType
116
- }
117
- />
143
+ < Outlet context = { ctx } />
118
144
</ Suspense >
119
145
</ Flex >
120
146
)
@@ -153,7 +179,7 @@ export function ClusterInsightsSummary(): ReactNode {
153
179
}
154
180
155
181
type ClusterInsightsContextType = {
156
- cluster : ClusterFragment
182
+ cluster : ClusterFragment & Nullable < ClusterInsightFragment >
157
183
clusterLoading : boolean
158
184
refetch : ( ) => void
159
185
setNavigationContent : Dispatch < SetStateAction < Nullable < ReactNode > > >
0 commit comments