|
| 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