Skip to content

Commit 83992ef

Browse files
authored
fix cluster details graphql bug (#2193)
1 parent 68a0be8 commit 83992ef

File tree

3 files changed

+134
-43
lines changed

3 files changed

+134
-43
lines changed

assets/src/components/cd/cluster/ClusterInsights.tsx

+45-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
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'
25
import {
36
Dispatch,
47
ReactNode,
@@ -11,7 +14,12 @@ import {
1114
} from 'react'
1215
import { Outlet, useMatch, useOutletContext } from 'react-router-dom'
1316
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'
1523
import {
1624
CLUSTER_ABS_PATH,
1725
CLUSTER_INSIGHTS_COMPONENTS_PATH,
@@ -24,11 +32,11 @@ import {
2432
ChatWithAIButton,
2533
insightMessage,
2634
} from '../../ai/chatbot/ChatbotButton.tsx'
35+
import { InsightDisplay } from '../../ai/insights/InsightDisplay.tsx'
2736
import LoadingIndicator from '../../utils/LoadingIndicator.tsx'
2837
import IconFrameRefreshButton from '../../utils/RefreshIconFrame.tsx'
2938
import { LinkTabWrap } from '../../utils/Tabs.tsx'
3039
import { useClusterContext } from './Cluster.tsx'
31-
import { InsightDisplay } from '../../ai/insights/InsightDisplay.tsx'
3240

3341
const DIRECTORY: Array<DirectoryEntry> = [
3442
{ path: CLUSTER_INSIGHTS_SUMMARY_PATH, label: 'Insight summary' },
@@ -42,16 +50,44 @@ interface DirectoryEntry {
4250

4351
export default function ClusterInsights(): ReactNode {
4452
const theme = useTheme()
45-
const { cluster, refetch, clusterLoading } = useClusterContext()
53+
const { cluster, clusterLoading } = useClusterContext()
4654
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+
{}
5058
const currentTab = DIRECTORY.find(({ path }) => path === tab)
5159

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+
5271
const [navigationContent, setNavigationContent] = useState<ReactNode>()
5372
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+
)
5483

84+
if (error) return <GqlError error={error} />
85+
if (!data)
86+
return insightLoading ? (
87+
<LoadingIndicator />
88+
) : (
89+
<EmptyState message="Cluster insights not found." />
90+
)
5591
return (
5692
<Flex
5793
direction="column"
@@ -104,17 +140,7 @@ export default function ClusterInsights(): ReactNode {
104140
</Flex>
105141
</Flex>
106142
<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} />
118144
</Suspense>
119145
</Flex>
120146
)
@@ -153,7 +179,7 @@ export function ClusterInsightsSummary(): ReactNode {
153179
}
154180

155181
type ClusterInsightsContextType = {
156-
cluster: ClusterFragment
182+
cluster: ClusterFragment & Nullable<ClusterInsightFragment>
157183
clusterLoading: boolean
158184
refetch: () => void
159185
setNavigationContent: Dispatch<SetStateAction<Nullable<ReactNode>>>

0 commit comments

Comments
 (0)