Skip to content

Commit 53afc67

Browse files
committed
feat: render AIChat in NavRoutes
1 parent 2ac6039 commit 53afc67

File tree

6 files changed

+36
-29
lines changed

6 files changed

+36
-29
lines changed

src/components/ApplicationGroup/AppGroupRoute.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ export default function AppGroupRoute({ isSuperAdmin }: AppGroupAdminType) {
2626
const { path } = useRouteMatch()
2727
const [environmentId, setEnvironmentId] = useState(null)
2828
const [currentEnvironmentName, setCurrentEnvironmentName] = useState<string>('')
29-
const [aiAgentContext, setAIAgentContext] = useState(null)
3029

3130
return (
3231
<AppContext.Provider
33-
value={{ environmentId, setEnvironmentId, currentEnvironmentName, setCurrentEnvironmentName, aiAgentContext, setAIAgentContext }}
32+
value={{ environmentId, setEnvironmentId, currentEnvironmentName, setCurrentEnvironmentName }}
3433
>
3534
<Switch>
3635
<Route path={`${path}/${URLS.APP_LIST}`}>

src/components/ResourceBrowser/ResourceList/ResourceList.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ const CompareClusterButton = importComponentFromFELibrary('CompareClusterButton'
7979
const isFELibAvailable = importComponentFromFELibrary('isFELibAvailable', null, 'function')
8080

8181
const ResourceList = () => {
82-
const { clusterId, namespace, nodeType, node, group } = useParams<URLParams>()
82+
const params = useParams<URLParams>()
83+
const { clusterId, namespace, nodeType, node, group } = params
8384
const { replace, push } = useHistory()
84-
const { url } = useRouteMatch()
85+
const { url, path } = useRouteMatch()
8586
const location = useLocation()
8687
const resourceBrowserRef = useRef<HTMLDivElement>()
8788
const {
@@ -101,7 +102,7 @@ const ResourceList = () => {
101102
const [isDataStale, setIsDataStale] = useState(false)
102103
const [selectedResource, setSelectedResource] = useState<ApiResourceGroupType>(null)
103104
const { targetK8sVersion } = useUrlFilters<never, ResourceListUrlFiltersType>({ parseSearchParams })
104-
const { setIntelligenceConfig } = useMainContext()
105+
const { setIntelligenceConfig, setAIAgentContext } = useMainContext()
105106

106107
const [rawGVKLoader, k8SObjectMapRaw, , reloadK8sObjectMapRaw] = useAsync(
107108
() => getResourceGroupListRaw(clusterId),
@@ -212,6 +213,7 @@ const ResourceList = () => {
212213

213214
return () => {
214215
setIntelligenceConfig(null)
216+
setAIAgentContext(null)
215217
}
216218
}, [])
217219

@@ -281,6 +283,17 @@ const ResourceList = () => {
281283
markTabActiveById(ResourceBrowserTabsId.k8s_Resources).catch(noop)
282284
}, [location.pathname])
283285

286+
useEffect(() => {
287+
setAIAgentContext({
288+
path,
289+
context: {
290+
...params,
291+
clusterName: selectedCluster.label,
292+
search: location.search,
293+
},
294+
})
295+
}, [location.pathname, location.search, selectedCluster.label])
296+
284297
const onClusterChange = (selected: ClusterOptionType) => {
285298
if (selected.value === selectedCluster?.value) {
286299
return
@@ -296,13 +309,13 @@ const ResourceList = () => {
296309
return
297310
}
298311

299-
const path = getClusterChangeRedirectionUrl(
312+
const newPath = getClusterChangeRedirectionUrl(
300313
selected.isClusterInCreationPhase,
301314
String(selected.isClusterInCreationPhase ? selected.installationId : selected.value),
302315
)
303316

304317
replace({
305-
pathname: path,
318+
pathname: newPath,
306319
})
307320
}
308321

src/components/app/details/appDetails/AppDetails.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
ToastManager,
4444
ToastVariantType,
4545
useAsync,
46+
useMainContext,
4647
} from '@devtron-labs/devtron-fe-common-lib'
4748

4849
import { ReactComponent as ForwardArrow } from '@Icons/ic-arrow-forward.svg'
@@ -207,7 +208,7 @@ const Details: React.FC<DetailsType> = ({
207208
const { replace } = useHistory()
208209
const { path, url } = useRouteMatch()
209210

210-
const { setAIAgentContext } = useAppContext()
211+
const { setAIAgentContext } = useMainContext()
211212

212213
useEffect(() => {
213214
setAIAgentContext({

src/components/app/details/main.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { lazy, PropsWithChildren, ReactNode, Suspense, useEffect, useRef, useState } from 'react'
17+
import { lazy, PropsWithChildren, Suspense, useEffect, useRef, useState } from 'react'
1818
import { Switch, Route, Redirect, useParams, useRouteMatch } from 'react-router-dom'
1919
import {
2020
showError,
@@ -38,7 +38,6 @@ import {
3838
importComponentFromFELibrary,
3939
setAppGroupFilterInLocalStorage,
4040
sortOptionsByLabel,
41-
useAppContext,
4241
} from '../../common'
4342
import { APP_TYPE, URLS } from '../../../config'
4443
import AppConfig from '../../../Pages/Applications/DevtronApps/Details/AppConfigurations/AppConfig'
@@ -61,10 +60,8 @@ const CIDetails = lazy(() => import('./cIDetails/CIDetails'))
6160
const AppDetails = lazy(() => import('./appDetails/AppDetails'))
6261
const CDDetails = lazy(() => import('./cdDetails/CDDetails'))
6362

64-
const AIChat = importComponentFromFELibrary('AIChat', null, 'function')
65-
6663
const AIAgentContextSetterWrapper = ({ children, appName }: PropsWithChildren<{ appName: string }>) => {
67-
const { setAIAgentContext } = useAppContext()
64+
const { setAIAgentContext } = useMainContext()
6865
const params = useParams()
6966
const { path, url } = useRouteMatch()
7067

@@ -78,7 +75,7 @@ const AIAgentContextSetterWrapper = ({ children, appName }: PropsWithChildren<{
7875
export default function AppDetailsPage() {
7976
const { path } = useRouteMatch()
8077
const { appId } = useParams<{ appId }>()
81-
const { setIntelligenceConfig } = useMainContext()
78+
const { setIntelligenceConfig, setAIAgentContext } = useMainContext()
8279
const [appName, setAppName] = useState('')
8380
const [appMetaInfo, setAppMetaInfo] = useState<AppMetaInfo>()
8481
const [reloadMandatoryProjects, setReloadMandatoryProjects] = useState<boolean>(true)
@@ -97,7 +94,7 @@ export default function AppDetailsPage() {
9794
const [apiError, setApiError] = useState(null)
9895
const [initLoading, setInitLoading] = useState<boolean>(false)
9996

100-
const { aiAgentContext } = useAppContext()
97+
const { aiAgentContext } = useMainContext()
10198

10299
const parentRef = useRef<HTMLDivElement>(null)
103100

@@ -156,6 +153,7 @@ export default function AppDetailsPage() {
156153
setSelectedGroupFilter([])
157154
setAppListOptions([])
158155
setIntelligenceConfig(null)
156+
setAIAgentContext(null)
159157
}
160158
}, [appId])
161159

@@ -400,8 +398,6 @@ export default function AppDetailsPage() {
400398
/>
401399
)}
402400

403-
{AIChat && window._env_?.FEATURE_AI_APP_DETAILS_ENABLE && <AIChat parentRef={parentRef} {...aiAgentContext} />}
404-
405401
<ErrorBoundary>
406402
<Suspense fallback={<Progressing pageLoader />}>
407403
<Switch>

src/components/common/Contexts/AppContext.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ export interface AppContextType {
3434
*/
3535
currentEnvironmentName?: string
3636
setCurrentEnvironmentName?: (currentEnvironmentName: string) => void
37-
aiAgentContext: {
38-
path: string
39-
context: Record<string, string>
40-
}
41-
setAIAgentContext: (aiAgentContext: AppContextType['aiAgentContext']) => void
4237
}
4338
export const AppContext = createContext<AppContextType>({
4439
environmentId: null,
@@ -47,8 +42,6 @@ export const AppContext = createContext<AppContextType>({
4742
setCurrentAppName: noop,
4843
currentEnvironmentName: '',
4944
setCurrentEnvironmentName: noop,
50-
aiAgentContext: null,
51-
setAIAgentContext: noop,
5245
})
5346

5447
export function useAppContext() {

src/components/common/navigation/NavigationRoutes.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ const SoftwareDistributionHubRenderProvider = importComponentFromFELibrary(
106106
)
107107
const migrateUserPreferences: (userPreferences: UserPreferencesType) => Promise<UserPreferencesType> =
108108
importComponentFromFELibrary('migrateUserPreferences', null, 'function')
109+
const AIChat = importComponentFromFELibrary('AIChat', null, 'function')
109110

110111
const ViewIsPipelineRBACConfigured: FunctionComponent<{
111112
userPreferences: UserPreferencesType
@@ -121,6 +122,7 @@ export default function NavigationRoutes({ reloadVersionConfig }: Readonly<Navig
121122
const location = useLocation()
122123
const match = useRouteMatch()
123124
const navRouteRef = useRef<HTMLDivElement>()
125+
const [aiAgentContext, setAIAgentContext] = useState<MainContext['aiAgentContext']>(null)
124126
const [serverMode, setServerMode] = useState<MainContext['serverMode']>(undefined)
125127
const [pageState, setPageState] = useState(ViewType.LOADING)
126128
const [currentServerInfo, setCurrentServerInfo] = useState<MainContext['currentServerInfo']>({
@@ -142,8 +144,9 @@ export default function NavigationRoutes({ reloadVersionConfig }: Readonly<Navig
142144
setHelpGettingStartedClicked(true)
143145
}
144146
const [environmentId, setEnvironmentId] = useState(null)
145-
const [aiAgentContext, setAIAgentContext] = useState(null)
146-
const contextValue = useMemo(() => ({ environmentId, setEnvironmentId, aiAgentContext, setAIAgentContext }), [environmentId])
147+
const contextValue = useMemo(() => ({ environmentId, setEnvironmentId }), [environmentId])
148+
149+
const parentRef = useRef<HTMLDivElement>(null)
147150

148151
const { showThemeSwitcherDialog, handleThemeSwitcherDialogVisibilityChange, appTheme } = useTheme()
149152

@@ -491,16 +494,19 @@ export default function NavigationRoutes({ reloadVersionConfig }: Readonly<Navig
491494
reloadVersionConfig,
492495
intelligenceConfig,
493496
setIntelligenceConfig,
497+
aiAgentContext,
498+
setAIAgentContext,
494499
}}
495500
>
496-
<main className={_isOnboardingPage ? 'no-nav' : ''} id={DEVTRON_BASE_MAIN_ID}>
501+
<main ref={parentRef} className={_isOnboardingPage ? 'no-nav' : ''} id={DEVTRON_BASE_MAIN_ID}>
497502
{showThemeSwitcherDialog && (
498503
<SwitchThemeDialog
499504
initialThemePreference={userPreferences?.themePreference}
500505
handleClose={handleCloseSwitchThemeDialog}
501506
handleUpdateUserThemePreference={handleUpdateUserThemePreference}
502507
/>
503508
)}
509+
{AIChat && window._env_?.FEATURE_AI_APP_DETAILS_ENABLE && <AIChat parentRef={parentRef} {...aiAgentContext} />}
504510
{renderAboutDevtronDialog()}
505511
{!_isOnboardingPage && (
506512
<Navigation
@@ -659,11 +665,10 @@ export const AppRouter = ({ isSuperAdmin, appListCount, loginCount }: AppRouterT
659665
const { path } = useRouteMatch()
660666
const [environmentId, setEnvironmentId] = useState(null)
661667
const [currentAppName, setCurrentAppName] = useState<string>('')
662-
const [aiAgentContext, setAIAgentContext] = useState<AppContextType['aiAgentContext']>(null)
663668

664669
return (
665670
<ErrorBoundary>
666-
<AppContext.Provider value={{ environmentId, setEnvironmentId, currentAppName, setCurrentAppName, aiAgentContext, setAIAgentContext }}>
671+
<AppContext.Provider value={{ environmentId, setEnvironmentId, currentAppName, setCurrentAppName }}>
667672
<Switch>
668673
<Route
669674
path={`${path}/${URLS.APP_LIST}`}

0 commit comments

Comments
 (0)