diff --git a/projects/frontend/src/routes.ts b/projects/frontend/src/routes.ts index e9559b0..ec7bc39 100644 --- a/projects/frontend/src/routes.ts +++ b/projects/frontend/src/routes.ts @@ -67,30 +67,54 @@ export const useRouteStore = defineStore('routeStore', { _breadcrumbs: [] as Link[] }), actions: { + /** + * Determines whether a route has showed a warning or not + * @param route the route to check + * @returns `true` if previously warned, `false` otherwise + */ doesRouteRememberWarning(route: string) { return this._routesAlreadyWarned[route] }, + + /** + * Remembers whether a route showed a warning or not + * @param route the route to remember + */ rememberRouteWarning(route: string) { this._routesAlreadyWarned[route] = true }, - setBreadcrumbs(currentRoute: RouteLocationNormalizedLoaded, resolvedTitle?: string) { - if (currentRoute.path === '/') { + + /** + * Sets the breadcrumbs array in the store + * @param route the current route + * @param resolvedTitle a title related to dynamic content loaded from a list-based template when in the view for that list + */ + setBreadcrumbs(route: RouteLocationNormalizedLoaded, resolvedTitle?: string) { + // breadcrumb shouldn't show on home page + if (route.path === '/') { this._breadcrumbs = [] return } - const split = currentRoute.path.split('/') + // split '/my/path' into '', 'my', 'path' + const split = route.path.split('/') + // adjust for root; '', 'my', 'path' becomes '/', 'my', 'path' split[0] = '/' + // compile paths from the split through some array functions const paths = [] as string[] split.forEach((val, i) => { + // get the paths leading up to the current index plus the next one const sliced = split.slice(0, i + 1) - if (sliced.length > 0) { - paths.push(sliced.reduce((prev, curr) => prev === '/' ? `/${curr}` : `${prev}/${curr}`)) - } + // reduce converts '/', 'my', 'path' to a breadcrumb trail '/', '/my', '/my/path' + // the conditional '/' check prevents accidentally prepending '//' to each breadcrumb + paths.push(sliced.reduce((prev, curr) => prev === '/' ? `/${curr}` : `${prev}/${curr}`)) }) + // map paths into breadcrumb info this._breadcrumbs = paths.map((path, index) => { return { href: path, + // the tail end should be the resolvedTitle from a view route if specified, + // otherwise use the title in the route definition caption: index === paths.length - 1 && !!resolvedTitle ? resolvedTitle : this._routes[path]?.title, } })