2025-08-02 22:46:15 -07:00
|
|
|
"use client";
|
|
|
|
|
|
2025-12-17 00:09:43 -08:00
|
|
|
import { useQuery } from "@tanstack/react-query";
|
2025-08-02 22:46:15 -07:00
|
|
|
import { usePathname } from "next/navigation";
|
feat(i18n): Add next-intl framework with full bilingual support (EN/ZH)
- Implement next-intl framework for scalable i18n
- Add complete Chinese (Simplified) localization
- Support 400+ translated strings across all pages
- Add language switcher with persistent preference
- Zero breaking changes to existing functionality
Framework additions:
- i18n routing and middleware
- LocaleContext for client-side state
- LanguageSwitcher component
- Translation files (en.json, zh.json)
Translated components:
- Homepage: Hero, features, CTA, navbar
- Auth: Login, register
- Dashboard: Main page, layout
- Connectors: Management, add page (all categories)
- Documents: Upload, manage, filters
- Settings: LLM configs, role assignments
- Onboarding: Add provider, assign roles
- Logs: Task logs viewer
Adding a new language now requires only:
1. Create messages/<locale>.json
2. Add locale to i18n/routing.ts
2025-10-26 14:05:46 +08:00
|
|
|
import { useTranslations } from "next-intl";
|
2025-11-23 15:23:31 +05:30
|
|
|
import React, { useEffect, useState } from "react";
|
2025-08-02 22:46:15 -07:00
|
|
|
import {
|
|
|
|
|
Breadcrumb,
|
|
|
|
|
BreadcrumbItem,
|
|
|
|
|
BreadcrumbLink,
|
|
|
|
|
BreadcrumbList,
|
|
|
|
|
BreadcrumbPage,
|
|
|
|
|
BreadcrumbSeparator,
|
|
|
|
|
} from "@/components/ui/breadcrumb";
|
2025-12-12 09:18:16 +00:00
|
|
|
import { searchSpacesApiService } from "@/lib/apis/search-spaces-api.service";
|
2025-12-02 01:24:09 -08:00
|
|
|
import { authenticatedFetch, getBearerToken } from "@/lib/auth-utils";
|
2025-12-17 00:09:43 -08:00
|
|
|
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
2025-08-02 22:46:15 -07:00
|
|
|
|
|
|
|
|
interface BreadcrumbItemInterface {
|
|
|
|
|
label: string;
|
|
|
|
|
href?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function DashboardBreadcrumb() {
|
2025-10-27 20:30:10 -07:00
|
|
|
const t = useTranslations("breadcrumb");
|
2025-08-02 22:46:15 -07:00
|
|
|
const pathname = usePathname();
|
2025-11-11 18:07:32 -08:00
|
|
|
// Extract search space ID and chat ID from pathname
|
|
|
|
|
const segments = pathname.split("/").filter(Boolean);
|
|
|
|
|
const searchSpaceId = segments[0] === "dashboard" && segments[1] ? segments[1] : null;
|
|
|
|
|
|
2025-12-12 09:18:16 +00:00
|
|
|
const { data: searchSpace } = useQuery({
|
|
|
|
|
queryKey: cacheKeys.searchSpaces.detail(searchSpaceId || ""),
|
|
|
|
|
queryFn: () => searchSpacesApiService.getSearchSpace({ id: Number(searchSpaceId) }),
|
|
|
|
|
enabled: !!searchSpaceId,
|
2025-11-11 18:07:32 -08:00
|
|
|
});
|
|
|
|
|
|
2025-11-23 15:23:31 +05:30
|
|
|
// State to store document title for editor breadcrumb
|
|
|
|
|
const [documentTitle, setDocumentTitle] = useState<string | null>(null);
|
|
|
|
|
|
|
|
|
|
// Fetch document title when on editor page
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (segments[2] === "editor" && segments[3] && searchSpaceId) {
|
|
|
|
|
const documentId = segments[3];
|
2025-12-17 00:09:43 -08:00
|
|
|
|
2025-12-16 18:46:18 +05:30
|
|
|
// Skip fetch for "new" notes
|
|
|
|
|
if (documentId === "new") {
|
|
|
|
|
setDocumentTitle(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 01:24:09 -08:00
|
|
|
const token = getBearerToken();
|
2025-11-23 15:23:31 +05:30
|
|
|
|
|
|
|
|
if (token) {
|
2025-12-02 01:24:09 -08:00
|
|
|
authenticatedFetch(
|
2025-11-30 15:15:27 -08:00
|
|
|
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-spaces/${searchSpaceId}/documents/${documentId}/editor-content`,
|
2025-12-02 01:24:09 -08:00
|
|
|
{ method: "GET" }
|
2025-11-23 15:23:31 +05:30
|
|
|
)
|
|
|
|
|
.then((res) => res.json())
|
|
|
|
|
.then((data) => {
|
|
|
|
|
if (data.title) {
|
|
|
|
|
setDocumentTitle(data.title);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
// If fetch fails, just use the document ID
|
|
|
|
|
setDocumentTitle(null);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
setDocumentTitle(null);
|
|
|
|
|
}
|
|
|
|
|
}, [segments, searchSpaceId]);
|
|
|
|
|
|
2025-08-02 22:46:15 -07:00
|
|
|
// Parse the pathname to create breadcrumb items
|
|
|
|
|
const generateBreadcrumbs = (path: string): BreadcrumbItemInterface[] => {
|
|
|
|
|
const segments = path.split("/").filter(Boolean);
|
|
|
|
|
const breadcrumbs: BreadcrumbItemInterface[] = [];
|
|
|
|
|
|
2026-01-20 16:42:10 +05:30
|
|
|
// Handle search space (start directly with search space, skip "Dashboard")
|
2025-08-02 22:46:15 -07:00
|
|
|
if (segments[0] === "dashboard" && segments[1]) {
|
2025-11-11 18:07:32 -08:00
|
|
|
// Use the actual search space name if available, otherwise fall back to the ID
|
|
|
|
|
const searchSpaceLabel = searchSpace?.name || `${t("search_space")} ${segments[1]}`;
|
2025-10-27 20:30:10 -07:00
|
|
|
breadcrumbs.push({
|
2025-11-11 18:07:32 -08:00
|
|
|
label: searchSpaceLabel,
|
2025-10-27 20:30:10 -07:00
|
|
|
href: `/dashboard/${segments[1]}`,
|
|
|
|
|
});
|
2025-08-02 22:46:15 -07:00
|
|
|
|
|
|
|
|
// Handle specific sections
|
|
|
|
|
if (segments[2]) {
|
|
|
|
|
const section = segments[2];
|
|
|
|
|
let sectionLabel = section.charAt(0).toUpperCase() + section.slice(1);
|
|
|
|
|
|
|
|
|
|
// Map section names to more readable labels
|
|
|
|
|
const sectionLabels: Record<string, string> = {
|
2025-12-21 22:26:33 -08:00
|
|
|
"new-chat": t("chat") || "Chat",
|
2025-10-27 20:30:10 -07:00
|
|
|
documents: t("documents"),
|
|
|
|
|
logs: t("logs"),
|
|
|
|
|
settings: t("settings"),
|
2025-11-23 15:23:31 +05:30
|
|
|
editor: t("editor"),
|
2025-08-02 22:46:15 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sectionLabel = sectionLabels[section] || sectionLabel;
|
|
|
|
|
|
|
|
|
|
// Handle sub-sections
|
|
|
|
|
if (segments[3]) {
|
|
|
|
|
const subSection = segments[3];
|
2025-11-23 16:39:23 +05:30
|
|
|
|
2025-11-23 15:23:31 +05:30
|
|
|
// Handle editor sub-sections (document ID)
|
|
|
|
|
if (section === "editor") {
|
2025-12-16 18:46:18 +05:30
|
|
|
// Handle special cases for editor
|
|
|
|
|
let documentLabel: string;
|
|
|
|
|
if (subSection === "new") {
|
|
|
|
|
documentLabel = "New Note";
|
|
|
|
|
} else {
|
|
|
|
|
documentLabel = documentTitle || subSection;
|
|
|
|
|
}
|
2025-12-17 00:09:43 -08:00
|
|
|
|
2025-11-23 15:23:31 +05:30
|
|
|
breadcrumbs.push({
|
|
|
|
|
label: t("documents"),
|
|
|
|
|
href: `/dashboard/${segments[1]}/documents`,
|
|
|
|
|
});
|
|
|
|
|
breadcrumbs.push({
|
|
|
|
|
label: sectionLabel,
|
|
|
|
|
href: `/dashboard/${segments[1]}/documents`,
|
|
|
|
|
});
|
|
|
|
|
breadcrumbs.push({ label: documentLabel });
|
|
|
|
|
return breadcrumbs;
|
|
|
|
|
}
|
2025-08-02 22:46:15 -07:00
|
|
|
|
|
|
|
|
// Handle documents sub-sections
|
|
|
|
|
if (section === "documents") {
|
|
|
|
|
const documentLabels: Record<string, string> = {
|
2025-10-27 20:30:10 -07:00
|
|
|
upload: t("upload_documents"),
|
|
|
|
|
webpage: t("add_webpages"),
|
2025-08-02 22:46:15 -07:00
|
|
|
};
|
|
|
|
|
|
2025-11-23 15:23:31 +05:30
|
|
|
const documentLabel = documentLabels[subSection] || subSection;
|
2025-08-02 22:46:15 -07:00
|
|
|
breadcrumbs.push({
|
2025-10-27 20:30:10 -07:00
|
|
|
label: t("documents"),
|
2025-08-02 22:46:15 -07:00
|
|
|
href: `/dashboard/${segments[1]}/documents`,
|
|
|
|
|
});
|
|
|
|
|
breadcrumbs.push({ label: documentLabel });
|
|
|
|
|
return breadcrumbs;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-21 22:26:33 -08:00
|
|
|
// Handle new-chat sub-sections (thread IDs)
|
2025-12-25 12:18:45 +05:30
|
|
|
// Don't show thread ID in breadcrumb - users identify chats by content, not by ID
|
2025-12-21 22:26:33 -08:00
|
|
|
if (section === "new-chat") {
|
2025-11-11 18:07:32 -08:00
|
|
|
breadcrumbs.push({
|
2025-12-21 22:26:33 -08:00
|
|
|
label: t("chat") || "Chat",
|
2025-11-11 18:07:32 -08:00
|
|
|
});
|
|
|
|
|
return breadcrumbs;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-02 22:46:15 -07:00
|
|
|
// Handle other sub-sections
|
2025-11-23 15:23:31 +05:30
|
|
|
let subSectionLabel = subSection.charAt(0).toUpperCase() + subSection.slice(1);
|
2025-08-02 22:46:15 -07:00
|
|
|
const subSectionLabels: Record<string, string> = {
|
2025-10-27 20:30:10 -07:00
|
|
|
upload: t("upload_documents"),
|
|
|
|
|
youtube: t("add_youtube"),
|
|
|
|
|
webpage: t("add_webpages"),
|
|
|
|
|
manage: t("manage"),
|
2025-08-02 22:46:15 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
subSectionLabel = subSectionLabels[subSection] || subSectionLabel;
|
|
|
|
|
|
|
|
|
|
breadcrumbs.push({
|
|
|
|
|
label: sectionLabel,
|
|
|
|
|
href: `/dashboard/${segments[1]}/${section}`,
|
|
|
|
|
});
|
|
|
|
|
breadcrumbs.push({ label: subSectionLabel });
|
|
|
|
|
} else {
|
|
|
|
|
breadcrumbs.push({ label: sectionLabel });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return breadcrumbs;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const breadcrumbs = generateBreadcrumbs(pathname);
|
|
|
|
|
|
2026-01-22 12:26:37 -05:00
|
|
|
if (breadcrumbs.length === 0) {
|
2025-08-02 22:46:15 -07:00
|
|
|
return null; // Don't show breadcrumbs for root dashboard
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Breadcrumb>
|
|
|
|
|
<BreadcrumbList>
|
|
|
|
|
{breadcrumbs.map((item, index) => (
|
2025-12-31 19:32:44 -08:00
|
|
|
<React.Fragment key={`${index}-${item.href || item.label}`}>
|
2025-08-02 22:46:15 -07:00
|
|
|
<BreadcrumbItem>
|
|
|
|
|
{index === breadcrumbs.length - 1 ? (
|
|
|
|
|
<BreadcrumbPage>{item.label}</BreadcrumbPage>
|
|
|
|
|
) : (
|
|
|
|
|
<BreadcrumbLink href={item.href}>{item.label}</BreadcrumbLink>
|
|
|
|
|
)}
|
|
|
|
|
</BreadcrumbItem>
|
|
|
|
|
{index < breadcrumbs.length - 1 && <BreadcrumbSeparator />}
|
|
|
|
|
</React.Fragment>
|
|
|
|
|
))}
|
|
|
|
|
</BreadcrumbList>
|
|
|
|
|
</Breadcrumb>
|
|
|
|
|
);
|
|
|
|
|
}
|