SurfSense/surfsense_web/components/sidebar/nav-main.tsx
Anish Sarkar bb971f89ba feat: enhance sidebar functionality with tooltips and improved sorting
- Added tooltips to chat and note items in the sidebars, displaying creation and update timestamps.
- Implemented sorting of chats and notes by their creation or update dates for better organization.
- Updated translation keys for new UI strings related to deletion and timestamps.
- Adjusted sidebar layout for improved user experience on mobile devices.
2025-12-19 21:40:40 +05:30

166 lines
5.1 KiB
TypeScript

"use client";
import { ChevronRight, type LucideIcon } from "lucide-react";
import { useTranslations } from "next-intl";
import { useCallback, useMemo, useState } from "react";
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible";
import {
SidebarGroup,
SidebarGroupLabel,
SidebarMenu,
SidebarMenuAction,
SidebarMenuButton,
SidebarMenuItem,
SidebarMenuSub,
SidebarMenuSubButton,
SidebarMenuSubItem,
} from "@/components/ui/sidebar";
interface NavItem {
title: string;
url: string;
icon: LucideIcon;
isActive?: boolean;
items?: {
title: string;
url: string;
}[];
}
interface NavMainProps {
items: NavItem[];
onSourcesExpandedChange?: (expanded: boolean) => void;
}
export function NavMain({ items, onSourcesExpandedChange }: NavMainProps) {
const t = useTranslations("nav_menu");
// Translation function that handles both exact matches and fallback to original
const translateTitle = (title: string): string => {
const titleMap: Record<string, string> = {
Researcher: "researcher",
"Manage LLMs": "manage_llms",
Sources: "sources",
"Add Sources": "add_sources",
"Manage Documents": "manage_documents",
"Manage Connectors": "manage_connectors",
Podcasts: "podcasts",
Logs: "logs",
Platform: "platform",
Team: "team",
};
const key = titleMap[title];
return key ? t(key) : title;
};
// Memoize items to prevent unnecessary re-renders
const memoizedItems = useMemo(() => items, [items]);
// Track expanded state for items with sub-menus (like Sources)
const [expandedItems, setExpandedItems] = useState<Record<string, boolean>>(() => {
const initial: Record<string, boolean> = {};
items.forEach((item) => {
if (item.items?.length) {
initial[item.title] = item.isActive ?? false;
}
});
return initial;
});
// Handle collapsible state change
const handleOpenChange = useCallback(
(title: string, isOpen: boolean) => {
setExpandedItems((prev) => ({ ...prev, [title]: isOpen }));
// Notify parent when Sources is expanded/collapsed
if (title === "Sources" && onSourcesExpandedChange) {
onSourcesExpandedChange(isOpen);
}
},
[onSourcesExpandedChange]
);
return (
<SidebarGroup>
<SidebarGroupLabel>{translateTitle("Platform")}</SidebarGroupLabel>
<SidebarMenu>
{memoizedItems.map((item, index) => {
const translatedTitle = translateTitle(item.title);
const hasSub = !!item.items?.length;
const isItemOpen = expandedItems[item.title] ?? item.isActive ?? false;
return (
<Collapsible
key={`${item.title}-${index}`}
asChild
open={hasSub ? isItemOpen : undefined}
onOpenChange={hasSub ? (open) => handleOpenChange(item.title, open) : undefined}
defaultOpen={!hasSub ? item.isActive : undefined}
>
<SidebarMenuItem>
{hasSub ? (
// When the item has children, make the whole row a collapsible trigger
<>
<CollapsibleTrigger asChild>
<SidebarMenuButton
asChild
tooltip={translatedTitle}
isActive={item.isActive}
aria-label={`${translatedTitle} with submenu`}
>
<button type="button" className="flex items-center gap-2 w-full text-left">
<item.icon />
<span>{translatedTitle}</span>
</button>
</SidebarMenuButton>
</CollapsibleTrigger>
<CollapsibleTrigger asChild>
<SidebarMenuAction
className="data-[state=open]:rotate-90 transition-transform duration-200"
aria-label={`Toggle ${translatedTitle} submenu`}
>
<ChevronRight />
<span className="sr-only">Toggle submenu</span>
</SidebarMenuAction>
</CollapsibleTrigger>
<CollapsibleContent className="data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:slide-out-to-top-2 data-[state=open]:slide-in-from-top-2 duration-200">
<SidebarMenuSub>
{item.items?.map((subItem, subIndex) => {
const translatedSubTitle = translateTitle(subItem.title);
return (
<SidebarMenuSubItem key={`${subItem.title}-${subIndex}`}>
<SidebarMenuSubButton asChild aria-label={translatedSubTitle}>
<a href={subItem.url}>
<span>{translatedSubTitle}</span>
</a>
</SidebarMenuSubButton>
</SidebarMenuSubItem>
);
})}
</SidebarMenuSub>
</CollapsibleContent>
</>
) : (
// Leaf item: treat as a normal link
<SidebarMenuButton
asChild
tooltip={translatedTitle}
isActive={item.isActive}
aria-label={translatedTitle}
>
<a href={item.url}>
<item.icon />
<span>{translatedTitle}</span>
</a>
</SidebarMenuButton>
)}
</SidebarMenuItem>
</Collapsible>
);
})}
</SidebarMenu>
</SidebarGroup>
);
}