"use client"; import { usePathname } from "next/navigation"; import { Fragment } from "react"; import * as z from "zod"; import { isKey, useTranslation } from "@turbostarter/i18n"; import { Breadcrumb, BreadcrumbItem, BreadcrumbLink, BreadcrumbList, BreadcrumbPage, BreadcrumbSeparator, } from "@turbostarter/ui-web/breadcrumb"; import { buttonVariants } from "@turbostarter/ui-web/button"; import { Icons } from "@turbostarter/ui-web/icons"; import { Separator } from "@turbostarter/ui-web/separator"; import { SidebarTrigger } from "@turbostarter/ui-web/sidebar"; import { pathsConfig } from "~/config/paths"; import { TurboLink } from "~/modules/common/turbo-link"; const ROOT_KEY = "home"; const indexSchema = z.object({ index: z.string(), }); const hasIndex = (value: unknown): value is z.infer => { return indexSchema.safeParse(value).success; }; const getDisplayKey = (rawKey: string, hasPrevious: boolean) => { if (rawKey === "index") return hasPrevious ? null : ROOT_KEY; return rawKey; }; const isSkippedKey = (key: string) => ["user", "organization"].includes(key); const addCrumb = ( trail: { key: string; path?: string }[], rawKey: string, path?: string, ) => { const displayKey = getDisplayKey(rawKey, trail.length > 0); if (!displayKey || isSkippedKey(rawKey)) return trail; return [...trail, { key: displayKey, path }]; }; const getPath = ( obj: unknown, target: string, current: { key: string; path?: string }[] = [], ): { key: string; path?: string }[] | null => { if (!obj || typeof obj !== "object") return null; for (const [rawKey, value] of Object.entries( obj as Record, )) { if (typeof value === "string") { if (value === target) return addCrumb(current, rawKey, value); continue; } if (typeof value === "function") { const candidates = target.split("/").filter(Boolean); for (const candidate of candidates) { try { const result = (value as (arg: string) => unknown)(candidate); if (typeof result === "string") { if (result === target) return addCrumb(current, rawKey, result); } else if (result && typeof result === "object") { const parentPath = addCrumb( current, rawKey, hasIndex(result) ? result.index : undefined, ); const found = getPath(result, target, parentPath); if (found) return found; } } catch { // Ignore callable errors and continue trying other candidates } } continue; } if (value && typeof value === "object") { const parentPath = addCrumb( current, rawKey, hasIndex(value) ? value.index : undefined, ); const found = getPath(value, target, parentPath); if (found) return found; } } return null; }; export const DashboardActionBar = () => { const { t, i18n } = useTranslation("common"); const pathname = usePathname(); const rawPath = getPath(pathsConfig, pathname); const path = rawPath?.length === 1 ? [{ ...rawPath[0], key: ROOT_KEY }] : rawPath; const last = path?.at(-1); return (
{path ? ( <> {path.length > 1 && path.slice(1, -1).map((item, index, array) => ( {isKey(item.key, i18n, "common") ? t(item.key) : item.key} {index < array.length - 1 && } ))} {last && ( <> {path.length > 2 && } {isKey(last.key, i18n, "common") ? t(last.key) : last.key} )} ) : null}
); };