'use client'; import { useTranslation } from '@nice/i18n'; import { useSetPageInfo } from '@/components/providers/dashboard-provider'; import FileUpload from '@/components/common/file-upload'; import { useQuery, useQueryClient } from '@tanstack/react-query'; import { useTRPC } from '@fenghuo/client'; import { useMemo, useState } from 'react'; import { useAuth } from '@/components/providers/auth-provider'; import { Card, CardContent } from '@nice/ui/components/card'; import { Button } from '@nice/ui/components/button'; import { Folder, FileImage, Copy, Download } from 'lucide-react'; import { toast } from '@nice/ui/components/sonner'; import { Pagination, PaginationContent, PaginationItem, PaginationLink, PaginationNext, PaginationPrevious, PaginationEllipsis, } from '@nice/ui/components/pagination'; interface Resource { id: string; title: string | null; url: string | null; type: string | null; [key: string]: any; } export default function ProfilePage() { const { t } = useTranslation(); const { user, isSuperAdmin, isAdmin } = useAuth(); const trpc = useTRPC(); const queryClient = useQueryClient(); const [page, setPage] = useState(1); const [pageSize, setPageSize] = useState(14); // 设置页面信息 useSetPageInfo({ title: "文件媒体库", subtitle: '上传文件资源、音视频资源、图片资源,并进行管理' }); const searchConditions = { ownerId: isAdmin() || isSuperAdmin() ? undefined : user?.id, }; const { data: resources, isLoading: resourcesLoading } = useQuery({ ...trpc.resource.findManyWithPagination.queryOptions({ page, pageSize, where: { ...searchConditions, }, orderBy: { createdAt: 'desc' } }), }); const resourcesData = useMemo(() => { return (resources?.items || []) as Resource[]; }, [resources]); const isImageFile = (filename: string | null) => { if (!filename) return false; const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.svg']; return imageExtensions.some(ext => filename.toLowerCase().endsWith(ext)); }; const handleCopyLink = async (url: string | null, e: React.MouseEvent) => { e.stopPropagation(); if (!url) { toast("文件链接不存在"); return; } try { await navigator.clipboard.writeText(url); toast("复制成功"); } catch (err) { toast("复制链接失败"); } }; const handleDownload = (url: string | null, filename: string | null, e: React.MouseEvent) => { e.stopPropagation(); if (!url) { toast("文件链接不存在"); return; } const link = document.createElement('a'); link.href = url; link.download = filename || '下载文件'; document.body.appendChild(link); link.click(); document.body.removeChild(link); toast("文件正在下载..."); }; const handleUploadComplete = async () => await queryClient.invalidateQueries({ queryKey: trpc.resource.pathKey() }); return (
{/* 主要内容区域 */}
{resourcesData.map((resource) => ( {isImageFile(resource.title) && resource.url && resource.type === 'image' ? (
{resource.title
) : (
{resource.type === 'image' ? ( ) : ( )} {resource.title || '未命名文件'}
)} {/* 悬停时显示的按钮覆盖层 */}
))}
{/* 分页组件 */} {resources && resources.totalPages > 1 && (
{ e.preventDefault(); if (page > 1) setPage(page - 1); }} className={page <= 1 ? 'pointer-events-none opacity-50' : 'cursor-pointer'} /> {/* 页码显示逻辑 */} {(() => { const totalPages = resources.totalPages; const currentPage = page; const pages: React.ReactNode[] = []; // 显示第一页 if (currentPage > 3) { pages.push( { e.preventDefault(); setPage(1); }} className="cursor-pointer" > 1 ); if (currentPage > 4) { pages.push( ); } } // 显示当前页附近的页码 for (let i = Math.max(1, currentPage - 2); i <= Math.min(totalPages, currentPage + 2); i++) { pages.push( { e.preventDefault(); setPage(i); }} isActive={i === currentPage} className="cursor-pointer" > {i} ); } // 显示最后一页 if (currentPage < totalPages - 2) { if (currentPage < totalPages - 3) { pages.push( ); } pages.push( { e.preventDefault(); setPage(totalPages); }} className="cursor-pointer" > {totalPages} ); } return pages; })()} { e.preventDefault(); if (page < resources.totalPages) setPage(page + 1); }} className={page >= resources.totalPages ? 'pointer-events-none opacity-50' : 'cursor-pointer'} />
)}
); }