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

[Search Playground] Change route on selector changed in between search/chat modes #197431

Merged
merged 8 commits into from
Nov 11, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import { KibanaLogic } from '../../../shared/kibana';

import { SearchPlaygroundPageTemplate } from './page_template';

export const Playground: React.FC = () => {
interface PlaygroundProps {
pageMode?: 'chat' | 'search';
}

export const Playground: React.FC<PlaygroundProps> = ({ pageMode = 'chat' }) => {
const { searchPlayground } = useValues(KibanaLogic);

if (!searchPlayground) {
Expand All @@ -35,7 +39,7 @@ export const Playground: React.FC = () => {
customPageSections
bottomBorder="extended"
>
<searchPlayground.Playground />
<searchPlayground.Playground pageMode={pageMode} />
</SearchPlaygroundPageTemplate>
</searchPlayground.PlaygroundProvider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import { Routes, Route } from '@kbn/shared-ux-router';
import { NotFound } from './components/not_found';
import { Playground } from './components/playground/playground';
import { SearchApplicationsRouter } from './components/search_applications/search_applications_router';
import { PLAYGROUND_PATH, ROOT_PATH, SEARCH_APPLICATIONS_PATH } from './routes';
import {
PLAYGROUND_CHAT_PATH,
PLAYGROUND_PATH,
PLAYGROUND_SEARCH_PATH,
ROOT_PATH,
SEARCH_APPLICATIONS_PATH,
} from './routes';

export const Applications = () => {
return (
Expand All @@ -22,8 +28,12 @@ export const Applications = () => {
<Route path={SEARCH_APPLICATIONS_PATH}>
<SearchApplicationsRouter />
</Route>
<Route path={PLAYGROUND_PATH}>
<Playground />
<Redirect exact from={PLAYGROUND_PATH} to={PLAYGROUND_CHAT_PATH} />
<Route path={PLAYGROUND_CHAT_PATH}>
<Playground pageMode={'chat'} />
</Route>
<Route path={PLAYGROUND_SEARCH_PATH}>
<Playground pageMode={'search'} />
</Route>
<Route>
<NotFound />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export enum SearchApplicationViewTabs {
export const SEARCH_APPLICATION_CREATION_PATH = `${SEARCH_APPLICATIONS_PATH}/new`;
export const SEARCH_APPLICATION_PATH = `${SEARCH_APPLICATIONS_PATH}/:searchApplicationName`;
export const SEARCH_APPLICATION_TAB_PATH = `${SEARCH_APPLICATION_PATH}/:tabId`;
export const PLAYGROUND_PATH = `${ROOT_PATH}playground`;
export const PLAYGROUND_PATH = `${ROOT_PATH}playground/`;
export const PLAYGROUND_CHAT_PATH = `${PLAYGROUND_PATH}chat`;
export const PLAYGROUND_SEARCH_PATH = `${PLAYGROUND_PATH}search`;

export const SEARCH_APPLICATION_CONNECT_PATH = `${SEARCH_APPLICATION_PATH}/${SearchApplicationViewTabs.CONNECT}/:connectTabId`;
export enum SearchApplicationConnectTabs {
Expand Down
11 changes: 8 additions & 3 deletions x-pack/plugins/search_playground/public/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import { Chat } from './chat';
import { SearchMode } from './search_mode/search_mode';
import { SearchPlaygroundSetupPage } from './setup_page/search_playground_setup_page';
import { usePageMode } from '../hooks/use_page_mode';
import { useKibana } from '../hooks/use_kibana';

export interface AppProps {
showDocs?: boolean;
pageMode?: PlaygroundPageMode;
pageMode?: 'chat' | 'search';
}

export enum ViewMode {
Expand All @@ -33,6 +34,7 @@ export const App: React.FC<AppProps> = ({
showDocs = false,
pageMode = PlaygroundPageMode.chat,
}) => {
const { services } = useKibana();
const [selectedMode, setSelectedMode] = useState<ViewMode>(ViewMode.chat);
const { data: connectors } = useLoadConnectors();
const hasSelectedIndices = Boolean(
Expand All @@ -41,15 +43,18 @@ export const App: React.FC<AppProps> = ({
}).length
);
const handleModeChange = (id: ViewMode) => setSelectedMode(id);
const handlePageModeChange = (mode: PlaygroundPageMode) => setSelectedPageMode(mode);
const handlePageModeChange = (mode: PlaygroundPageMode) => {
services.application?.navigateToUrl(`./${mode}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Wondering if it works in both ES3 and ESS?

Copy link
Contributor

Choose a reason for hiding this comment

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

good catch, I think if we're doing navigateToUrl we'd need to do the full URL and not make it relative.

and if we care doing navigateToUrl do we still need the setSelectedPageMode() call after this?

Copy link
Member Author

Choose a reason for hiding this comment

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

It does work both places, I tried on both.

Only the last paths differ in between ESS and ES3. When it is relative it works for both. Otherwise I would need to do isServerless check and pass full path unless I change some other things completely.

Ideally the routing should be done inside of the plugin completely and ESS wouldn't be able to break it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I will merge it as is for now, and will come back for a refactor to use single router later.

setSelectedPageMode(mode);
};
const {
showSetupPage,
pageMode: selectedPageMode,
setPageMode: setSelectedPageMode,
} = usePageMode({
hasSelectedIndices,
hasConnectors: Boolean(connectors?.length),
initialPageMode: pageMode,
initialPageMode: pageMode === 'chat' ? PlaygroundPageMode.chat : PlaygroundPageMode.search,
});

const restrictedWidth = selectedPageMode === PlaygroundPageMode.search && selectedMode === 'chat';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const SearchMode: React.FC = () => {
name={ChatFormFields.searchQuery}
render={({ field }) => (
<EuiFieldText
data-test-subj="searchPlaygroundSearchModeFieldText"
{...field}
value={searchBarValue}
icon="search"
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/search_playground/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ export function plugin(initializerContext: PluginInitializerContext) {
return new SearchPlaygroundPlugin(initializerContext);
}

export type { SearchPlaygroundPluginSetup, SearchPlaygroundPluginStart } from './types';
export type {
SearchPlaygroundPluginSetup,
SearchPlaygroundPluginStart,
PlaygroundPageMode,
} from './types';
2 changes: 1 addition & 1 deletion x-pack/plugins/search_playground/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@kbn/data-views-plugin",
"@kbn/discover-utils",
"@kbn/data-plugin",
"@kbn/search-index-documents"
"@kbn/search-index-documents",
],
"exclude": [
"target/**/*",
Expand Down