"use client";
import {
ChevronRight,
Folder as Fd,
FolderPen,
FolderPlus,
Trash2,
} from "lucide-react";
import { CircleButton, LightButton } from "@/design-system/base/button";
import { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import { useTranslations } from "next-intl";
import { toast } from "sonner";
import { PageLayout } from "@/components/ui/PageLayout";
import { PageHeader } from "@/components/ui/PageHeader";
import { CardList } from "@/components/ui/CardList";
import { actionCreateFolder, actionDeleteFolderById, actionGetFoldersWithTotalPairsByUserId, actionRenameFolderById } from "@/modules/folder/folder-aciton";
import { TSharedFolderWithTotalPairs } from "@/shared/folder-type";
interface FolderProps {
folder: TSharedFolderWithTotalPairs;
refresh: () => void;
}
const FolderCard = ({ folder, refresh }: FolderProps) => {
const router = useRouter();
const t = useTranslations("folders");
return (
{
router.push(`/folders/${folder.id}`);
}}
>
{folder.name}
{t("folderInfo", {
id: folder.id,
name: folder.name,
totalPairs: folder.total,
})}
{
e.stopPropagation();
const newName = prompt("Input a new name.")?.trim();
if (newName && newName.length > 0) {
actionRenameFolderById(folder.id, newName)
.then(result => {
if (result.success) {
refresh();
}
else {
toast.error(result.message);
}
});
}
}}
>
{
e.stopPropagation();
const confirm = prompt(t("confirmDelete", { name: folder.name }));
if (confirm === folder.name) {
actionDeleteFolderById(folder.id)
.then(result => {
if (result.success) {
refresh();
}
else {
toast.error(result.message);
}
});
}
}}
className="text-gray-400 hover:text-red-500 hover:bg-red-50"
>
);
};
export function FoldersClient({ userId }: { userId: string; }) {
const t = useTranslations("folders");
const [folders, setFolders] = useState(
[],
);
const [loading, setLoading] = useState(false);
useEffect(() => {
setLoading(true);
actionGetFoldersWithTotalPairsByUserId(userId)
.then((folders) => {
if (folders.success && folders.data) {
setFolders(folders.data);
setLoading(false);
}
});
}, [userId]);
const updateFolders = async () => {
setLoading(true);
await actionGetFoldersWithTotalPairsByUserId(userId)
.then(async result => {
if (!result.success) toast.error(result.message);
else await actionGetFoldersWithTotalPairsByUserId(userId)
.then((folders) => {
if (folders.success && folders.data) {
setFolders(folders.data);
}
});
});
setLoading(false);
};
return (
{/* 新建文件夹按钮 */}
{
const folderName = prompt(t("enterFolderName"));
if (!folderName) return;
setLoading(true);
try {
await actionCreateFolder(userId, folderName)
.then(result => {
if (result.success) {
updateFolders();
} else {
toast.error(result.message);
}
});
} finally {
setLoading(false);
}
}}
disabled={loading}
className="w-full border-dashed"
>
{loading ? t("creating") : t("newFolder")}
{/* 文件夹列表 */}
{folders.length === 0 ? (
// 空状态
) : (
// 文件夹卡片列表
{folders
.toSorted((a, b) => a.id - b.id)
.map((folder) => (
))}
)}
);
}