|
| 1 | +import { |
| 2 | + addTrailingSlash, |
| 3 | + isActive, |
| 4 | + useLocation, |
| 5 | + useSidebar, |
| 6 | + useSite, |
| 7 | +} from '@rspress/core/runtime' |
| 8 | +import { IconArrowRight, SvgWrapper } from '@rspress/core/theme' |
| 9 | +import type { NormalizedSidebarGroup, SidebarData } from '@rspress/shared' |
| 10 | +import virtual from 'doom-@global-virtual' |
| 11 | +import { use, useEffect, useMemo } from 'react' |
| 12 | +import { xfetch } from 'x-fetch' |
| 13 | +import { parse } from 'yaml' |
| 14 | + |
| 15 | +import type { BuildInfoGroup, BuildInfoItem } from '../../products/index.tsx' |
| 16 | +import { BuildInfoContext } from '../../shared/context.ts' |
| 17 | +import { X } from '../_X.ts' |
| 18 | + |
| 19 | +import { useLang, useSiteOverrides, useTranslation } from '@alauda/doom/runtime' |
| 20 | + |
| 21 | +export interface BreadcrumbItem { |
| 22 | + text: string |
| 23 | + link?: string |
| 24 | +} |
| 25 | + |
| 26 | +const isBuildInfoItem = (obj: object): obj is BuildInfoItem => |
| 27 | + 'base' in obj && 'version' in obj |
| 28 | + |
| 29 | +export const BreadCrumb = () => { |
| 30 | + const lang = useLang() |
| 31 | + |
| 32 | + const t = useTranslation() |
| 33 | + |
| 34 | + const { pathname } = useLocation() |
| 35 | + |
| 36 | + const sidebar = useSidebar() |
| 37 | + |
| 38 | + const site = useSiteOverrides() |
| 39 | + |
| 40 | + const { site: siteData } = useSite() |
| 41 | + |
| 42 | + const breadcrumbItems = useMemo(() => { |
| 43 | + function walk( |
| 44 | + sidebarItems: SidebarData, |
| 45 | + parents: NormalizedSidebarGroup[] = [], |
| 46 | + ): BreadcrumbItem[] | undefined { |
| 47 | + for (const sidebarItem of sidebarItems) { |
| 48 | + if ( |
| 49 | + 'link' in sidebarItem && |
| 50 | + sidebarItem.link && |
| 51 | + isActive(sidebarItem.link, pathname) |
| 52 | + ) { |
| 53 | + return [ |
| 54 | + ...parents.map((p) => ({ |
| 55 | + text: p.text, |
| 56 | + link: p.link, |
| 57 | + })), |
| 58 | + { |
| 59 | + text: sidebarItem.text, |
| 60 | + }, |
| 61 | + ] |
| 62 | + } |
| 63 | + if ('items' in sidebarItem && sidebarItem.items.length) { |
| 64 | + const found = walk(sidebarItem.items, [...parents, sidebarItem]) |
| 65 | + if (found) { |
| 66 | + return found |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + return walk(sidebar) |
| 72 | + }, [pathname, sidebar]) |
| 73 | + |
| 74 | + const prefix = addTrailingSlash(`/${lang === siteData.lang ? '' : lang}`) |
| 75 | + |
| 76 | + const { groups, setGroups: setBuildInfoGroups } = use(BuildInfoContext) |
| 77 | + |
| 78 | + const hasGroups = !!groups.length |
| 79 | + |
| 80 | + useEffect(() => { |
| 81 | + const fetchBuildInfo = async () => { |
| 82 | + let rawBuildInfo: Record< |
| 83 | + string, |
| 84 | + Record<string, BuildInfoItem> | BuildInfoItem |
| 85 | + > |
| 86 | + |
| 87 | + try { |
| 88 | + rawBuildInfo = parse( |
| 89 | + await xfetch((virtual.prefix || '') + '/build-info.yaml', { |
| 90 | + type: 'text', |
| 91 | + }), |
| 92 | + ) as Record<string, Record<string, BuildInfoItem> | BuildInfoItem> |
| 93 | + } catch { |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + const buildInfoGroups: BuildInfoGroup[] = [] |
| 98 | + for (const [base, items] of Object.entries(rawBuildInfo)) { |
| 99 | + const id = base |
| 100 | + .replace('alauda-build-of-', '') |
| 101 | + .replace('alauda-', '')[0] |
| 102 | + let group = buildInfoGroups.find((g) => g.id === id) |
| 103 | + if (!group) { |
| 104 | + group = { id, items: [] } |
| 105 | + buildInfoGroups.push(group) |
| 106 | + } |
| 107 | + if (isBuildInfoItem(items)) { |
| 108 | + group.items.push(items) |
| 109 | + } else { |
| 110 | + const latest = Object.values(items).at(-1) |
| 111 | + if (latest) { |
| 112 | + group.items.push(latest) |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + setBuildInfoGroups( |
| 117 | + buildInfoGroups.sort((a, b) => a.id.localeCompare(b.id)), |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + void fetchBuildInfo() |
| 122 | + }, [setBuildInfoGroups]) |
| 123 | + |
| 124 | + return ( |
| 125 | + <div className="breadcrumb-container"> |
| 126 | + <ul className="breadcrumb-content"> |
| 127 | + {hasGroups && ( |
| 128 | + <li className="breadcrumb-item rp-doc"> |
| 129 | + <X.a href={prefix + 'products'}>{t('products')}</X.a> |
| 130 | + </li> |
| 131 | + )} |
| 132 | + <li className="breadcrumb-item rp-doc"> |
| 133 | + {hasGroups && <SvgWrapper icon={IconArrowRight} />} |
| 134 | + <X.a href={prefix}>{site.title || siteData.title}</X.a> |
| 135 | + </li> |
| 136 | + {breadcrumbItems?.map((item, index) => ( |
| 137 | + // eslint-disable-next-line @eslint-react/no-array-index-key |
| 138 | + <li key={index} className="breadcrumb-item rp-doc"> |
| 139 | + <SvgWrapper icon={IconArrowRight} /> |
| 140 | + {item.link ? ( |
| 141 | + <X.a href={item.link}>{item.text}</X.a> |
| 142 | + ) : ( |
| 143 | + <span className="breadcrumb-item-text">{item.text}</span> |
| 144 | + )} |
| 145 | + </li> |
| 146 | + ))} |
| 147 | + </ul> |
| 148 | + </div> |
| 149 | + ) |
| 150 | +} |
| 151 | + |
| 152 | +export default BreadCrumb |
0 commit comments