SurfSense/surfsense_web/components/sidebar/AppSidebarProvider.tsx

257 lines
5.9 KiB
TypeScript
Raw Normal View History

2025-07-27 10:05:37 -07:00
"use client";
2025-04-07 23:47:06 -07:00
2025-08-08 11:17:43 -07:00
import { Trash2 } from "lucide-react";
import { useTranslations } from "next-intl";
import { useCallback, useEffect, useMemo, useState } from "react";
2025-07-27 10:05:37 -07:00
import { AppSidebar } from "@/components/sidebar/app-sidebar";
2025-07-27 10:41:15 -07:00
import { Button } from "@/components/ui/button";
2025-04-07 23:47:06 -07:00
import {
2025-07-27 10:05:37 -07:00
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
2025-04-07 23:47:06 -07:00
} from "@/components/ui/dialog";
import { useChats, useSearchSpace, useUser } from "@/hooks";
2025-04-07 23:47:06 -07:00
interface AppSidebarProviderProps {
2025-07-27 10:05:37 -07:00
searchSpaceId: string;
navSecondary: {
title: string;
url: string;
icon: string;
}[];
navMain: {
title: string;
url: string;
icon: string;
isActive?: boolean;
items?: {
title: string;
url: string;
}[];
}[];
2025-04-07 23:47:06 -07:00
}
export function AppSidebarProvider({
2025-07-27 10:05:37 -07:00
searchSpaceId,
navSecondary,
navMain,
2025-04-07 23:47:06 -07:00
}: AppSidebarProviderProps) {
const t = useTranslations("dashboard");
const tCommon = useTranslations("common");
// Use the new hooks
const {
chats,
loading: isLoadingChats,
error: chatError,
fetchChats: fetchRecentChats,
deleteChat,
} = useChats({ searchSpaceId, limit: 5, skip: 0 });
const {
searchSpace,
loading: isLoadingSearchSpace,
error: searchSpaceError,
fetchSearchSpace,
} = useSearchSpace({ searchSpaceId });
const { user } = useUser();
2025-07-27 10:05:37 -07:00
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
const [chatToDelete, setChatToDelete] = useState<{ id: number; name: string } | null>(null);
const [isDeleting, setIsDeleting] = useState(false);
const [isClient, setIsClient] = useState(false);
2025-04-07 23:47:06 -07:00
2025-07-27 10:05:37 -07:00
// Set isClient to true when component mounts on the client
useEffect(() => {
setIsClient(true);
}, []);
2025-04-07 23:47:06 -07:00
2025-08-02 21:20:36 -07:00
// Retry function
const retryFetch = useCallback(() => {
fetchRecentChats();
fetchSearchSpace();
}, [fetchRecentChats, fetchSearchSpace]);
// Transform API response to the format expected by AppSidebar
const recentChats = useMemo(() => {
return chats.map((chat) => ({
name: chat.title || `Chat ${chat.id}`,
url: `/dashboard/${chat.search_space_id}/researcher/${chat.id}`,
icon: "MessageCircleMore",
id: chat.id,
search_space_id: chat.search_space_id,
actions: [
{
name: "Delete",
icon: "Trash2",
onClick: () => {
setChatToDelete({ id: chat.id, name: chat.title || `Chat ${chat.id}` });
setShowDeleteDialog(true);
},
},
],
}));
}, [chats]);
2025-08-02 21:20:36 -07:00
// Handle delete chat with better error handling
const handleDeleteChat = useCallback(async () => {
2025-07-27 10:05:37 -07:00
if (!chatToDelete) return;
2025-07-27 10:05:37 -07:00
try {
setIsDeleting(true);
await deleteChat(chatToDelete.id);
2025-07-27 10:05:37 -07:00
} catch (error) {
console.error("Error deleting chat:", error);
2025-08-02 21:20:36 -07:00
// You could show a toast notification here
2025-07-27 10:05:37 -07:00
} finally {
setIsDeleting(false);
setShowDeleteDialog(false);
setChatToDelete(null);
}
}, [chatToDelete, deleteChat]);
2025-08-02 21:20:36 -07:00
// Memoized fallback chats
const fallbackChats = useMemo(() => {
if (chatError) {
return [
{
name: t("error_loading_chats"),
2025-08-02 21:20:36 -07:00
url: "#",
icon: "AlertCircle",
id: 0,
search_space_id: Number(searchSpaceId),
actions: [
{
name: tCommon("retry"),
2025-08-02 21:20:36 -07:00
icon: "RefreshCw",
onClick: retryFetch,
},
],
},
];
}
2025-04-07 23:47:06 -07:00
2025-08-02 21:20:36 -07:00
if (!isLoadingChats && recentChats.length === 0) {
return [
{
name: t("no_recent_chats"),
2025-08-02 21:20:36 -07:00
url: "#",
icon: "MessageCircleMore",
id: 0,
search_space_id: Number(searchSpaceId),
actions: [],
},
];
}
2025-04-07 23:47:06 -07:00
2025-08-02 21:20:36 -07:00
return [];
}, [chatError, isLoadingChats, recentChats.length, searchSpaceId, retryFetch, t, tCommon]);
2025-04-07 23:47:06 -07:00
2025-07-27 10:05:37 -07:00
// Use fallback chats if there's an error or no chats
const displayChats = recentChats.length > 0 ? recentChats : fallbackChats;
2025-04-07 23:47:06 -07:00
2025-08-02 21:20:36 -07:00
// Memoized updated navSecondary
const updatedNavSecondary = useMemo(() => {
const updated = [...navSecondary];
if (updated.length > 0 && isClient) {
updated[0] = {
...updated[0],
title:
searchSpace?.name ||
(isLoadingSearchSpace
? tCommon("loading")
2025-08-02 21:20:36 -07:00
: searchSpaceError
? t("error_loading_space")
: t("unknown_search_space")),
2025-08-02 21:20:36 -07:00
};
}
return updated;
}, [
navSecondary,
isClient,
searchSpace?.name,
isLoadingSearchSpace,
searchSpaceError,
t,
tCommon,
]);
2025-08-02 21:20:36 -07:00
// Prepare page usage data
const pageUsage = user
? {
pagesUsed: user.pages_used,
pagesLimit: user.pages_limit,
}
: undefined;
2025-08-02 21:20:36 -07:00
// Show loading state if not client-side
if (!isClient) {
return (
<AppSidebar
navSecondary={navSecondary}
navMain={navMain}
RecentChats={[]}
pageUsage={pageUsage}
/>
);
2025-07-27 10:05:37 -07:00
}
2025-04-07 23:47:06 -07:00
2025-07-27 10:05:37 -07:00
return (
<>
<AppSidebar
navSecondary={updatedNavSecondary}
navMain={navMain}
RecentChats={displayChats}
pageUsage={pageUsage}
/>
2025-08-02 21:20:36 -07:00
{/* Delete Confirmation Dialog */}
<Dialog open={showDeleteDialog} onOpenChange={setShowDeleteDialog}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<Trash2 className="h-5 w-5 text-destructive" />
<span>{t("delete_chat")}</span>
2025-08-02 21:20:36 -07:00
</DialogTitle>
<DialogDescription>
{t("delete_chat_confirm")} <span className="font-medium">{chatToDelete?.name}</span>?{" "}
{t("action_cannot_undone")}
2025-08-02 21:20:36 -07:00
</DialogDescription>
</DialogHeader>
<DialogFooter className="flex gap-2 sm:justify-end">
<Button
variant="outline"
onClick={() => setShowDeleteDialog(false)}
disabled={isDeleting}
>
{tCommon("cancel")}
2025-08-02 21:20:36 -07:00
</Button>
<Button
variant="destructive"
onClick={handleDeleteChat}
disabled={isDeleting}
className="gap-2"
>
{isDeleting ? (
<>
<span className="h-4 w-4 animate-spin rounded-full border-2 border-current border-t-transparent" />
{t("deleting")}
2025-08-02 21:20:36 -07:00
</>
) : (
<>
<Trash2 className="h-4 w-4" />
{tCommon("delete")}
2025-08-02 21:20:36 -07:00
</>
)}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
2025-07-27 10:05:37 -07:00
</>
);
}