Skip to content

Commit

Permalink
[Obs AI Assistant] Simplify routing (#176569)
Browse files Browse the repository at this point in the history
Minor simplification to how we link into the conversation app
  • Loading branch information
sorenlouv authored Feb 11, 2024
1 parent 15ea681 commit 955dd69
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ export function ChatHeader({

const handleNavigateToConversations = () => {
if (conversationId) {
router.navigateToConversationsApp('/conversations/{conversationId}', {
router.push('/conversations/{conversationId}', {
path: {
conversationId,
},
query: {},
});
} else {
router.navigateToConversationsApp('/conversations/new', { path: {}, query: {} });
router.push('/conversations/new', { path: {}, query: {} });
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import { PathsOf, TypeAsArgs, TypeOf } from '@kbn/typed-react-router-config';
import { useMemo } from 'react';
import { useHistory } from 'react-router-dom';
import { ObservabilityAIAssistantRouter, ObservabilityAIAssistantRoutes } from '../routes/config';
import { observabilityAIAssistantRouter } from '../routes/config';
import { useKibana } from './use_kibana';
Expand All @@ -21,15 +20,9 @@ interface StatefulObservabilityAIAssistantRouter extends ObservabilityAIAssistan
path: T,
...params: TypeAsArgs<TypeOf<ObservabilityAIAssistantRoutes, T>>
): void;
navigateToConversationsApp<T extends PathsOf<ObservabilityAIAssistantRoutes>>(
path: T,
...params: TypeAsArgs<TypeOf<ObservabilityAIAssistantRoutes, T>>
): void;
}

export function useObservabilityAIAssistantRouter(): StatefulObservabilityAIAssistantRouter {
const history = useHistory();

const {
services: {
http,
Expand All @@ -47,43 +40,16 @@ export function useObservabilityAIAssistantRouter(): StatefulObservabilityAIAssi
...observabilityAIAssistantRouter,
push: (...args) => {
const next = link(...args);

history.push(next);
},
navigateToConversationsApp: (path, ...args) => {
const [_, route, routeParam] = path.split('/');

const sanitized = routeParam.replace('{', '').replace('}', '');

const pathKey = args[0]?.path;

if (typeof pathKey !== 'object') {
return;
}

if (Object.keys(pathKey).length === 0) {
navigateToApp('observabilityAIAssistant', {
path: route,
});
return;
}

if (Object.keys(pathKey).length === 1) {
navigateToApp('observabilityAIAssistant', {
// @ts-expect-error
path: `${route}/${pathKey[sanitized]}`,
});
return;
}
navigateToApp('observabilityAIAssistant', { path: next, replace: false });
},
replace: (path, ...args) => {
const next = link(path, ...args);
history.replace(next);
navigateToApp('observabilityAIAssistant', { path: next, replace: true });
},
link: (path, ...args) => {
return http.basePath.prepend('/app/observabilityAIAssistant' + link(path, ...args));
},
}),
[history, navigateToApp, http.basePath]
[navigateToApp, http.basePath]
);
}

0 comments on commit 955dd69

Please sign in to comment.