import { useTranslation } from "react-i18next"; import { useDispatch, useSelector } from "react-redux"; import { selectAuthenticated, selectUsername } from "@/store/auth.ts"; import { closeMarket, selectCurrent, selectHistory } from "@/store/chat.ts"; import React, { useRef, useState } from "react"; import { ConversationInstance } from "@/api/types.ts"; import { useToast } from "@/components/ui/use-toast.ts"; import { extractMessage, filterMessage } from "@/utils/processor.ts"; import { copyClipboard } from "@/utils/dom.ts"; import { useEffectAsync, useAnimation } from "@/utils/hook.ts"; import { mobile } from "@/utils/device.ts"; import { deleteAllConversations, deleteConversation, toggleConversation, updateConversationList, } from "@/api/history.ts"; import { Button } from "@/components/ui/button.tsx"; import { selectMenu, setMenu } from "@/store/menu.ts"; import { Copy, Eraser, LogIn, MoreHorizontal, Plus, RotateCw, } from "lucide-react"; import ConversationSegment from "./ConversationSegment.tsx"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog.tsx"; import { getSharedLink, shareConversation } from "@/api/sharing.ts"; import { Input } from "@/components/ui/input.tsx"; import { login } from "@/conf.ts"; import MenuBar from "@/components/app/MenuBar.tsx"; import { Separator } from "@/components/ui/separator.tsx"; import { deeptrainApiEndpoint } from "@/utils/env.ts"; type Operation = { target: ConversationInstance | null; type: string; }; type SidebarActionProps = { setOperateConversation: (operation: Operation) => void; }; type ConversationListProps = { operateConversation: Operation; setOperateConversation: (operation: Operation) => void; }; function SidebarAction({ setOperateConversation }: SidebarActionProps) { const { t } = useTranslation(); const dispatch = useDispatch(); const { toast } = useToast(); const refresh = useRef(null); const [removeAll, setRemoveAll] = useState(false); async function handleDeleteAll(e: React.MouseEvent) { e.preventDefault(); e.stopPropagation(); if (await deleteAllConversations(dispatch)) toast({ title: t("conversation.delete-success"), description: t("conversation.delete-success-prompt"), }); else toast({ title: t("conversation.delete-failed"), description: t("conversation.delete-failed-prompt"), }); await updateConversationList(dispatch); setOperateConversation({ target: null, type: "" }); setRemoveAll(false); } return (
{t("conversation.remove-all-title")} {t("conversation.remove-all-description")} {t("conversation.cancel")} {t("conversation.delete")}
); } function SidebarConversationList({ operateConversation, setOperateConversation, }: ConversationListProps) { const { t } = useTranslation(); const dispatch = useDispatch(); const { toast } = useToast(); const history: ConversationInstance[] = useSelector(selectHistory); const [shared, setShared] = useState(""); const current = useSelector(selectCurrent); async function handleDelete(e: React.MouseEvent) { e.preventDefault(); e.stopPropagation(); if ( await deleteConversation(dispatch, operateConversation?.target?.id || -1) ) toast({ title: t("conversation.delete-success"), description: t("conversation.delete-success-prompt"), }); else toast({ title: t("conversation.delete-failed"), description: t("conversation.delete-failed-prompt"), }); setOperateConversation({ target: null, type: "" }); } async function handleShare(e: React.MouseEvent) { e.preventDefault(); e.stopPropagation(); const resp = await shareConversation(operateConversation?.target?.id || -1); if (resp.status) setShared(getSharedLink(resp.data)); else toast({ title: t("share.failed"), description: resp.message, }); setOperateConversation({ target: null, type: "" }); } return ( <>
{history.length ? ( history.map((conversation, i) => ( )) ) : (
{t("conversation.empty")}
)}
{ if (!open) setOperateConversation({ target: null, type: "" }); }} > {t("conversation.remove-title")} {t("conversation.remove-description")} {extractMessage( filterMessage(operateConversation?.target?.name || ""), )} {t("end")} {t("conversation.cancel")} {t("conversation.delete")} { if (!open) setOperateConversation({ target: null, type: "" }); }} > {t("share.title")} {t("share.description")} {extractMessage( filterMessage(operateConversation?.target?.name || ""), )} {t("end")} {t("conversation.cancel")} {t("share.title")} 0} onOpenChange={(open) => { if (!open) { setShared(""); setOperateConversation({ target: null, type: "" }); } }} > {t("share.success")}
{t("close")} { e.preventDefault(); e.stopPropagation(); window.open(shared, "_blank"); }} > {t("share.view")}
); } function SidebarMenu() { const username = useSelector(selectUsername); return (
); } function SideBar() { const { t } = useTranslation(); const dispatch = useDispatch(); const open = useSelector(selectMenu); const auth = useSelector(selectAuthenticated); const [operateConversation, setOperateConversation] = useState({ target: null, type: "", }); useEffectAsync(async () => { await updateConversationList(dispatch); }, []); return (
{auth ? (
) : ( )}
); } export default SideBar;