Skip to content

Commit 5da6ba2

Browse files
committed
Add a dashboard breadcrumb when user naviagte from dashboard
Signed-off-by: Joey Liu <[email protected]>
1 parent 42e9595 commit 5da6ba2

File tree

4 files changed

+120
-3
lines changed

4 files changed

+120
-3
lines changed

src/plugins/dataset_management/public/components/dataset_table/dataset_table_v2/hooks/use_dataset_selector.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const useDatasetSelector = ({
6262
'datasetManagement.dataset.titleExistsLabel',
6363
{
6464
values: { title: query.dataset.title },
65-
defaultMessage: "An index pattern with the title '{title}' already exists.",
65+
defaultMessage: "An dataset with the title '{title}' already exists.",
6666
}
6767
);
6868

src/plugins/explore/public/application/utils/hooks/use_page_initialization.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { ExploreFlavor } from '../../../../common';
2424
import { useSetEditorText } from '../../hooks';
2525
import { EditorMode } from '../state_management/types';
2626
import { getVisualizationBuilder } from '../../../components/visualizations/visualization_builder';
27+
import { getPreviousPageBreadcrumb } from '../../../utils/get_previous_page_breadcrumb';
2728

2829
export const useInitPage = () => {
2930
const dispatch = useDispatch();
@@ -42,7 +43,29 @@ export const useInitPage = () => {
4243

4344
// Update browser title and breadcrumbs
4445
chrome.docTitle.change(title);
45-
chrome.setBreadcrumbs([{ text: 'Explore', href: '#/' }, { text: title }]);
46+
47+
// Get previous page info for smart breadcrumb navigation
48+
const { breadcrumbConfig, previousPage } = getPreviousPageBreadcrumb({
49+
chrome,
50+
application: services.core.application,
51+
currentPageId: savedExplore.id,
52+
});
53+
54+
// Build breadcrumbs array
55+
const breadcrumbs = [];
56+
57+
// If user came from a dashboard, show dashboard name as a breadcrumb
58+
if (previousPage && previousPage.meta?.type === 'dashboard') {
59+
breadcrumbs.push({
60+
text: `Dashboard:${previousPage.label}`,
61+
...breadcrumbConfig,
62+
});
63+
}
64+
65+
// Always show the current explore title
66+
breadcrumbs.push({ text: title });
67+
68+
chrome.setBreadcrumbs(breadcrumbs);
4669

4770
// Sync query from saved object to data plugin (explore doesn't use filters)
4871
const searchSourceFields = savedExplore.kibanaSavedObjectMeta;

src/plugins/explore/public/helpers/save_explore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export async function saveSavedExplore({
8181
} else {
8282
// Update browser title and breadcrumbs
8383
chrome.docTitle.change(newTitle);
84-
chrome.setBreadcrumbs([...getRootBreadcrumbs(), { text: savedExplore.title }]);
84+
chrome.setBreadcrumbs([{ text: savedExplore.title }]);
8585
}
8686

8787
store.dispatch(setSavedSearch(id));
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { ChromeStart, ChromeRecentlyAccessedHistoryItem, ApplicationStart } from 'src/core/public';
7+
8+
export interface BreadcrumbConfig {
9+
href?: string;
10+
onClick?: () => void;
11+
}
12+
13+
export interface PreviousPageInfo {
14+
breadcrumbConfig: BreadcrumbConfig;
15+
previousPage?: ChromeRecentlyAccessedHistoryItem;
16+
}
17+
18+
export interface GetBreadcrumbOptions {
19+
chrome: ChromeStart;
20+
application?: ApplicationStart;
21+
currentPageId?: string;
22+
}
23+
24+
/**
25+
* Get breadcrumb configuration that navigates to the previous page
26+
* Uses chrome.recentlyAccessed to determine where the user came from
27+
*
28+
* @param options - Configuration options
29+
* @returns Previous page info with breadcrumb config
30+
*/
31+
export function getPreviousPageBreadcrumb(options: GetBreadcrumbOptions): PreviousPageInfo;
32+
export function getPreviousPageBreadcrumb(
33+
chrome: ChromeStart,
34+
currentPageId?: string
35+
): PreviousPageInfo;
36+
export function getPreviousPageBreadcrumb(
37+
optionsOrChrome: GetBreadcrumbOptions | ChromeStart,
38+
currentPageId?: string
39+
): PreviousPageInfo {
40+
// Handle both function signatures for backward compatibility
41+
const chrome =
42+
'chrome' in optionsOrChrome
43+
? (optionsOrChrome as GetBreadcrumbOptions).chrome
44+
: optionsOrChrome;
45+
const application =
46+
'application' in optionsOrChrome
47+
? (optionsOrChrome as GetBreadcrumbOptions).application
48+
: undefined;
49+
const pageId =
50+
'currentPageId' in optionsOrChrome
51+
? (optionsOrChrome as GetBreadcrumbOptions).currentPageId
52+
: currentPageId;
53+
const recentlyAccessed = chrome.recentlyAccessed.get();
54+
55+
// Get the most recently accessed page (first item, which is most recent)
56+
// Skip if it's the current page
57+
const previousPage =
58+
recentlyAccessed.length > 0 && recentlyAccessed[0].id !== pageId
59+
? recentlyAccessed[0]
60+
: undefined;
61+
62+
if (previousPage && previousPage.link) {
63+
// We found a previous page - construct the full URL with workspace ID
64+
let href = previousPage.link;
65+
66+
// Check if the current URL has a workspace ID and the previous page link doesn't
67+
const currentPathname = window.location.pathname;
68+
const workspaceMatch = currentPathname.match(/\/w\/([^/]+)\//);
69+
70+
if (workspaceMatch && !href.includes('/w/')) {
71+
// Add workspace ID to the href
72+
const workspaceId = workspaceMatch[1];
73+
href = `/w/${workspaceId}${href}`;
74+
}
75+
76+
// If application service is available, use onClick with navigateToUrl for SPA navigation
77+
if (application) {
78+
return {
79+
breadcrumbConfig: {
80+
onClick: () => {
81+
application.navigateToUrl(href);
82+
},
83+
},
84+
previousPage,
85+
};
86+
}
87+
88+
// Fallback to href (may cause hard refresh)
89+
return { breadcrumbConfig: { href }, previousPage };
90+
} else {
91+
// No previous page found, fallback to explore home
92+
return { breadcrumbConfig: { href: '#/' } };
93+
}
94+
}

0 commit comments

Comments
 (0)