Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Obs AI Assistant] Simplify routing #176569

Merged
merged 2 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, does this not cause a full page refresh?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. I was also a little worried about using this internally when the name suggests it should be used when navigating between apps. But I don't observe any downsides like full page refreshes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace does mean you can't go use browser back anymore no?

Copy link
Member Author

@sorenlouv sorenlouv Feb 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace does mean you can't go use browser back anymore no?

Yes, instead of adding a new item to the history stack (like push) replace overwrites the existing item.

},
link: (path, ...args) => {
return http.basePath.prepend('/app/observabilityAIAssistant' + link(path, ...args));
},
}),
[history, navigateToApp, http.basePath]
[navigateToApp, http.basePath]
);
}