Skip to content

Commit

Permalink
Change LogExplorerTabs to persist current tab state instead of state …
Browse files Browse the repository at this point in the history
…sharing
  • Loading branch information
davismcphee committed Nov 30, 2023
1 parent 9e081b1 commit 1cec6e2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import { withSuspense } from '@kbn/shared-ux-utility';
import { lazy } from 'react';

export type { LogExplorerTabsParams, LogExplorerTabsProps } from './log_explorer_tabs';
export type {
LogExplorerTabsParams,
LogExplorerTabsProps,
LogExplorerTab,
} from './log_explorer_tabs';

export const LogExplorerTabs = withSuspense(lazy(() => import('./log_explorer_tabs')));
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import { EuiTab, EuiTabs, useEuiTheme } from '@elastic/eui';
import type { DataViewSpec } from '@kbn/data-views-plugin/public';
import { AllDatasetsLocatorParams, ALL_DATASETS_LOCATOR_ID } from '@kbn/deeplinks-observability';
import { i18n } from '@kbn/i18n';
import React, { MouseEvent } from 'react';
import { camelCase } from 'lodash';
import React, { MouseEvent, useEffect, useMemo } from 'react';
import useLocalStorage from 'react-use/lib/useLocalStorage';
import useObservable from 'react-use/lib/useObservable';
import { map } from 'rxjs';
import { DiscoverAppLocatorParams, DISCOVER_APP_LOCATOR } from '../../../common';
Expand All @@ -22,39 +24,78 @@ export interface LogExplorerTabsParams {
dataViewSpec?: DataViewSpec;
}

export type LogExplorerTab = 'discover' | 'log-explorer';

export interface LogExplorerTabsProps {
services: Pick<DiscoverServices, 'share' | 'data'>;
params: LogExplorerTabsParams;
selectedTab: 'discover' | 'log-explorer';
selectedTab: LogExplorerTab;
}

export const LogExplorerTabs = ({ services, params, selectedTab }: LogExplorerTabsProps) => {
const { euiTheme } = useEuiTheme();
const { share, data } = services;
const locators = share?.url.locators;
const discoverLocator = locators?.get<DiscoverAppLocatorParams>(DISCOVER_APP_LOCATOR);
const logExplorerLocator = locators?.get<AllDatasetsLocatorParams>(ALL_DATASETS_LOCATOR_ID);

const {
time: timeRange,
refreshInterval,
query,
filters,
} = useObservable(data.query.state$.pipe(map(({ state }) => state)), data.query.getState());
const mergedParams = { ...params, timeRange, refreshInterval, query, filters };
const discoverLocator = locators?.get<DiscoverAppLocatorParams>(DISCOVER_APP_LOCATOR);
const logExplorerLocator = locators?.get<AllDatasetsLocatorParams>(ALL_DATASETS_LOCATOR_ID);
const discoverUrl = discoverLocator?.getRedirectUrl(mergedParams);
const logExplorerUrl = logExplorerLocator?.getRedirectUrl(mergedParams);

const navigateToDiscover = createNavigateHandler(() => {
if (selectedTab !== 'discover') {
discoverLocator?.navigate(mergedParams);
}
});
const mergedParams = useMemo(
() => ({ ...params, timeRange, refreshInterval, query, filters }),
[filters, params, query, refreshInterval, timeRange]
);

const navigateToLogExplorer = createNavigateHandler(() => {
if (selectedTab !== 'log-explorer') {
logExplorerLocator?.navigate(mergedParams);
const [discoverTabState, setDiscoverTabState] = useLocalStorage<typeof mergedParams>(
getTabStateKey('discover')
);

const discoverUrl = useMemo(
() => discoverLocator?.getRedirectUrl(discoverTabState ?? {}),
[discoverLocator, discoverTabState]
);

const navigateToDiscover = useMemo(
() =>
createNavigateHandler(() => {
if (selectedTab !== 'discover') {
discoverLocator?.navigate(discoverTabState ?? {});
}
}),
[discoverLocator, discoverTabState, selectedTab]
);

const [logExplorerTabState, setLogExplorerTabState] = useLocalStorage<typeof mergedParams>(
getTabStateKey('log-explorer')
);

const logExplorerUrl = useMemo(
() => logExplorerLocator?.getRedirectUrl(logExplorerTabState ?? {}),
[logExplorerLocator, logExplorerTabState]
);

const navigateToLogExplorer = useMemo(
() =>
createNavigateHandler(() => {
if (selectedTab !== 'log-explorer') {
logExplorerLocator?.navigate(logExplorerTabState ?? {});
}
}),
[logExplorerLocator, logExplorerTabState, selectedTab]
);

useEffect(() => {
if (selectedTab === 'discover') {
setDiscoverTabState(mergedParams);
} else {
setLogExplorerTabState(mergedParams);
}
});
}, [mergedParams, selectedTab, setDiscoverTabState, setLogExplorerTabState]);

return (
<EuiTabs bottomBorder={false} data-test-subj="logExplorerTabs">
Expand Down Expand Up @@ -87,6 +128,8 @@ export const LogExplorerTabs = ({ services, params, selectedTab }: LogExplorerTa
// eslint-disable-next-line import/no-default-export
export default LogExplorerTabs;

const getTabStateKey = (tab: LogExplorerTab) => `discover:logExplorerTabs.${camelCase(tab)}.state`;

const isModifiedEvent = (event: MouseEvent) =>
event.metaKey || event.altKey || event.ctrlKey || event.shiftKey;

Expand Down
1 change: 1 addition & 0 deletions src/plugins/discover/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ export {
LogExplorerTabs,
type LogExplorerTabsParams,
type LogExplorerTabsProps,
type LogExplorerTab,
} from './components/log_explorer_tabs';

0 comments on commit 1cec6e2

Please sign in to comment.