Spaces:
Running
Running
File size: 20,697 Bytes
968ca90 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 | "use client";
import React, { useMemo, useState } from "react";
import dynamic from "next/dynamic";
import {
Archive,
ChevronRight,
CloudUpload,
Download,
Edit3,
Eye,
FileCode2,
Film,
Folder,
Image as ImageIcon,
LayoutGrid,
List,
RefreshCw,
Search,
Share2,
Trash2,
} from "lucide-react";
import { useAuth } from "@/features/auth";
import { formatBytes } from "@/shared/lib/format";
import { buildDownloadUrl } from "@/shared/lib/http";
import { FileCategory, FileNode } from "@/shared/types/domain";
import { Badge, Button, EmptyState, Skeleton } from "@/shared/ui";
import { SortMode, useFileManager } from "../hooks/useFileManager";
const UploadModal = dynamic(
() => import("./UploadModal").then((m) => m.UploadModal),
{ ssr: false },
);
const FilePreviewModal = dynamic(
() => import("./FilePreviewModal").then((m) => m.FilePreviewModal),
{ ssr: false },
);
const ShareModal = dynamic(
() => import("./ShareModal").then((m) => m.ShareModal),
{ ssr: false },
);
const RenameModal = dynamic(
() => import("./RenameModal").then((m) => m.RenameModal),
{ ssr: false },
);
const CATEGORIES: readonly { value: "all" | FileCategory; label: string }[] = [
{ value: "all", label: "Tất cả" },
{ value: "video", label: "Video / Audio" },
{ value: "image", label: "Hình ảnh" },
{ value: "archive", label: "Tệp nén" },
{ value: "doc", label: "Tài liệu / Khác" },
];
function renderCategoryIcon(category: FileCategory) {
switch (category) {
case "video":
return <Film className="w-5 h-5 text-rose-400" aria-hidden="true" />;
case "image":
return <ImageIcon className="w-5 h-5 text-emerald-400" aria-hidden="true" />;
case "archive":
return <Archive className="w-5 h-5 text-amber-400" aria-hidden="true" />;
case "doc":
return <FileCode2 className="w-5 h-5 text-sky-400" aria-hidden="true" />;
}
}
export function FileExplorerView() {
const { token, canUpload, canRename, canDelete } = useAuth();
const {
files,
loading,
currentFolder,
setCurrentFolder,
searchQuery,
setSearchQuery,
categoryFilter,
setCategoryFilter,
sortBy,
setSortBy,
viewMode,
setViewMode,
uploadTasks,
refreshFiles,
handleUploadFiles,
handleRename,
handleDelete,
clearCompletedTasks,
} = useFileManager();
const [uploadOpen, setUploadOpen] = useState(false);
const [previewTarget, setPreviewTarget] = useState<FileNode | null>(null);
const [shareTarget, setShareTarget] = useState<FileNode | null>(null);
const [renameTarget, setRenameTarget] = useState<FileNode | null>(null);
const isFiltering = Boolean(searchQuery.trim() || categoryFilter !== "all");
const { subfolders, visibleFiles } = useMemo(() => {
const query = searchQuery.trim().toLowerCase();
const folderSet = new Set<string>();
const filtered = files.filter((item) => {
if (categoryFilter !== "all" && item.category !== categoryFilter) return false;
if (query) {
return (
item.name.toLowerCase().includes(query) ||
item.path.toLowerCase().includes(query)
);
}
if (!currentFolder) {
if (item.folder) {
folderSet.add(item.folder.split("/")[0]);
return false;
}
return true;
}
if (item.folder === currentFolder) return true;
if (item.folder.startsWith(`${currentFolder}/`)) {
const remainder = item.folder.slice(currentFolder.length + 1);
const child = remainder.split("/")[0];
if (child) folderSet.add(child);
}
return false;
});
const sorted = [...filtered].sort((a, b) => {
if (sortBy === "size") return b.size - a.size;
if (sortBy === "newest") return b.updatedAt.localeCompare(a.updatedAt);
return a.name.localeCompare(b.name);
});
return {
subfolders: isFiltering ? [] : Array.from(folderSet).sort(),
visibleFiles: sorted,
};
}, [categoryFilter, currentFolder, files, isFiltering, searchQuery, sortBy]);
const breadcrumbs = currentFolder ? currentFolder.split("/") : [];
const confirmDelete = async (file: FileNode) => {
if (!window.confirm(`Xóa vĩnh viễn tệp "${file.name}"?`)) return;
await handleDelete(file.path);
};
return (
<div className="p-4 sm:p-6 lg:p-8 max-w-7xl mx-auto w-full space-y-6">
{/* Toolbar */}
<div className="surface-panel rounded-2xl p-5 space-y-4">
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
<div>
<h1 className="text-xl sm:text-2xl font-extrabold text-white tracking-tight">
Trình duyệt Tệp Đám mây
</h1>
<p className="text-xs text-slate-400 mt-0.5">
Tổng {files.length} tệp • Tổng dung lượng{" "}
<span className="font-mono text-sky-300">
{formatBytes(files.reduce((acc, f) => acc + f.size, 0))}
</span>
</p>
</div>
<div className="flex flex-wrap items-center gap-2.5">
<Button variant="secondary" size="sm" onClick={refreshFiles}>
<RefreshCw className={`w-3.5 h-3.5 ${loading ? "animate-spin" : ""}`} aria-hidden="true" />
<span>Làm mới</span>
</Button>
{canUpload && (
<Button variant="primary" size="sm" onClick={() => setUploadOpen(true)}>
<CloudUpload className="w-4 h-4" aria-hidden="true" />
<span>Tải tệp lên</span>
</Button>
)}
</div>
</div>
{/* Search & Filters */}
<div className="grid grid-cols-1 md:grid-cols-12 gap-3 items-center">
<div className="md:col-span-5 relative">
<Search
className="w-4 h-4 text-slate-500 absolute left-3.5 top-1/2 -translate-y-1/2"
aria-hidden="true"
/>
<input
type="search"
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
placeholder="Tìm tên tệp hoặc đường dẫn..."
aria-label="Tìm kiếm tệp"
className="w-full pl-10 pr-4 py-2 rounded-xl bg-slate-950/90 border border-slate-800 text-xs sm:text-sm text-white placeholder:text-slate-600"
/>
</div>
<div className="md:col-span-5 flex flex-wrap gap-1.5">
{CATEGORIES.map((cat) => (
<button
key={cat.value}
type="button"
onClick={() => setCategoryFilter(cat.value)}
className={`px-3 py-1.5 rounded-lg text-xs font-semibold border transition ${
categoryFilter === cat.value
? "bg-sky-500/20 text-sky-300 border-sky-500/40"
: "bg-slate-900/80 text-slate-400 border-slate-800 hover:text-white"
}`}
>
{cat.label}
</button>
))}
</div>
<div className="md:col-span-2 flex items-center justify-end gap-2">
<select
aria-label="Sắp xếp tệp"
value={sortBy}
onChange={(e) => setSortBy(e.target.value as SortMode)}
className="px-2.5 py-2 rounded-xl bg-slate-950 border border-slate-800 text-xs text-slate-200"
>
<option value="newest">Mới nhất</option>
<option value="size">Dung lượng</option>
<option value="name">Tên A-Z</option>
</select>
<div className="flex rounded-xl bg-slate-950 border border-slate-800 p-0.5">
<button
type="button"
aria-label="Chế độ lưới"
onClick={() => setViewMode("grid")}
className={`p-1.5 rounded-lg ${
viewMode === "grid" ? "bg-sky-500/20 text-sky-300" : "text-slate-400"
}`}
>
<LayoutGrid className="w-3.5 h-3.5" aria-hidden="true" />
</button>
<button
type="button"
aria-label="Chế độ danh sách"
onClick={() => setViewMode("list")}
className={`p-1.5 rounded-lg ${
viewMode === "list" ? "bg-sky-500/20 text-sky-300" : "text-slate-400"
}`}
>
<List className="w-3.5 h-3.5" aria-hidden="true" />
</button>
</div>
</div>
</div>
</div>
{/* Breadcrumbs */}
<nav aria-label="Đường dẫn thư mục" className="flex flex-wrap items-center gap-1.5 text-xs font-mono">
<button
type="button"
onClick={() => setCurrentFolder("")}
className={`px-2.5 py-1 rounded-lg transition ${
!currentFolder ? "bg-sky-500/20 text-sky-300 font-bold" : "text-slate-400 hover:text-white"
}`}
>
root/
</button>
{breadcrumbs.map((part, idx) => {
const target = breadcrumbs.slice(0, idx + 1).join("/");
const isLast = idx === breadcrumbs.length - 1;
return (
<React.Fragment key={target}>
<ChevronRight className="w-3.5 h-3.5 text-slate-600" aria-hidden="true" />
<button
type="button"
onClick={() => setCurrentFolder(target)}
className={`px-2.5 py-1 rounded-lg transition ${
isLast ? "bg-sky-500/20 text-sky-300 font-bold" : "text-slate-400 hover:text-white"
}`}
>
{part}
</button>
</React.Fragment>
);
})}
</nav>
{/* Content */}
{loading ? (
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
{Array.from({ length: 6 }).map((_, idx) => (
<Skeleton key={idx} className="h-36 rounded-2xl" />
))}
</div>
) : subfolders.length === 0 && visibleFiles.length === 0 ? (
<EmptyState
icon={<Folder className="w-5 h-5" aria-hidden="true" />}
title="Chưa có tệp tin nào ở đây"
description={
isFiltering
? "Không tìm thấy tệp phù hợp với từ khóa hoặc bộ lọc hiện tại."
: "Tải lên tệp đầu tiên của bạn để bắt đầu lưu trữ và chia sẻ."
}
action={
canUpload ? (
<Button variant="primary" size="sm" onClick={() => setUploadOpen(true)}>
<CloudUpload className="w-4 h-4" aria-hidden="true" />
<span>Tải tệp lên ngay</span>
</Button>
) : undefined
}
/>
) : (
<div className="space-y-6">
{subfolders.length > 0 && (
<div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-5 gap-3">
{subfolders.map((folderName) => {
const nextFolder = currentFolder
? `${currentFolder}/${folderName}`
: folderName;
return (
<button
key={nextFolder}
type="button"
onClick={() => setCurrentFolder(nextFolder)}
className="surface-card rounded-xl p-4 flex items-center gap-3 text-left"
>
<Folder className="w-5 h-5 text-amber-400 shrink-0" aria-hidden="true" />
<span className="text-xs font-bold text-white truncate">{folderName}</span>
</button>
);
})}
</div>
)}
{viewMode === "grid" ? (
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
{visibleFiles.map((file) => (
<article
key={file.path}
className="surface-card rounded-2xl p-5 flex flex-col justify-between gap-4"
>
<div className="space-y-3">
<div className="flex items-start justify-between gap-3">
<div className="w-10 h-10 rounded-xl bg-slate-900 border border-slate-800 flex items-center justify-center shrink-0">
{renderCategoryIcon(file.category)}
</div>
<Badge tone="slate">@{file.owner}</Badge>
</div>
<div className="min-w-0">
<h2 className="text-sm font-bold text-white truncate" title={file.name}>
{file.name}
</h2>
<p className="text-[11px] font-mono text-slate-400 mt-1">
{formatBytes(file.size)}
{file.folder ? ` • ${file.folder}` : ""}
</p>
</div>
</div>
<div className="pt-3 border-t border-slate-800/80 flex items-center justify-between gap-1.5">
<div className="flex items-center gap-1">
<Button variant="ghost" size="sm" onClick={() => setPreviewTarget(file)}>
<Eye className="w-3.5 h-3.5" aria-hidden="true" />
<span>Xem</span>
</Button>
<a
href={buildDownloadUrl(file.path, token, false)}
download={file.name}
className="inline-flex items-center gap-1 px-2.5 py-1.5 rounded-lg text-xs font-medium text-slate-300 hover:text-white hover:bg-white/5"
>
<Download className="w-3.5 h-3.5" aria-hidden="true" />
<span>Tải</span>
</a>
</div>
<div className="flex items-center gap-1">
<button
type="button"
aria-label={`Chia sẻ ${file.name}`}
onClick={() => setShareTarget(file)}
className="p-1.5 rounded-lg text-slate-400 hover:text-sky-300 hover:bg-sky-500/10"
>
<Share2 className="w-3.5 h-3.5" aria-hidden="true" />
</button>
{canRename && (
<button
type="button"
aria-label={`Đổi tên ${file.name}`}
onClick={() => setRenameTarget(file)}
className="p-1.5 rounded-lg text-slate-400 hover:text-amber-300 hover:bg-amber-500/10"
>
<Edit3 className="w-3.5 h-3.5" aria-hidden="true" />
</button>
)}
{canDelete && (
<button
type="button"
aria-label={`Xóa ${file.name}`}
onClick={() => confirmDelete(file)}
className="p-1.5 rounded-lg text-slate-400 hover:text-rose-300 hover:bg-rose-500/10"
>
<Trash2 className="w-3.5 h-3.5" aria-hidden="true" />
</button>
)}
</div>
</div>
</article>
))}
</div>
) : (
<div className="surface-panel rounded-2xl overflow-x-auto">
<table className="w-full text-left text-xs">
<thead className="border-b border-slate-800 text-slate-400 font-mono uppercase">
<tr>
<th className="py-3.5 px-4">Tệp tin</th>
<th className="py-3.5 px-4">Kích thước</th>
<th className="py-3.5 px-4">Chủ sở hữu</th>
<th className="py-3.5 px-4 text-right">Thao tác</th>
</tr>
</thead>
<tbody className="divide-y divide-slate-800/70">
{visibleFiles.map((file) => (
<tr key={file.path} className="hover:bg-white/[0.02]">
<td className="py-3 px-4">
<div className="flex items-center gap-3 min-w-0">
{renderCategoryIcon(file.category)}
<div className="min-w-0">
<p className="font-semibold text-white truncate max-w-xs">{file.name}</p>
<p className="font-mono text-[11px] text-slate-500 truncate max-w-xs">
{file.path}
</p>
</div>
</div>
</td>
<td className="py-3 px-4 font-mono text-slate-300">{formatBytes(file.size)}</td>
<td className="py-3 px-4">
<Badge tone="slate">@{file.owner}</Badge>
</td>
<td className="py-3 px-4">
<div className="flex items-center justify-end gap-1.5">
<Button variant="ghost" size="sm" onClick={() => setPreviewTarget(file)}>
<Eye className="w-3.5 h-3.5" aria-hidden="true" />
</Button>
<a
href={buildDownloadUrl(file.path, token, false)}
download={file.name}
aria-label={`Tải ${file.name}`}
className="p-1.5 rounded-lg text-slate-400 hover:text-white hover:bg-white/5"
>
<Download className="w-3.5 h-3.5" aria-hidden="true" />
</a>
<button
type="button"
aria-label={`Chia sẻ ${file.name}`}
onClick={() => setShareTarget(file)}
className="p-1.5 rounded-lg text-slate-400 hover:text-sky-300"
>
<Share2 className="w-3.5 h-3.5" aria-hidden="true" />
</button>
{canRename && (
<button
type="button"
aria-label={`Đổi tên ${file.name}`}
onClick={() => setRenameTarget(file)}
className="p-1.5 rounded-lg text-slate-400 hover:text-amber-300"
>
<Edit3 className="w-3.5 h-3.5" aria-hidden="true" />
</button>
)}
{canDelete && (
<button
type="button"
aria-label={`Xóa ${file.name}`}
onClick={() => confirmDelete(file)}
className="p-1.5 rounded-lg text-slate-400 hover:text-rose-300"
>
<Trash2 className="w-3.5 h-3.5" aria-hidden="true" />
</button>
)}
</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
)}
{/* Lazy Modals */}
{uploadOpen && (
<UploadModal
open={uploadOpen}
defaultFolder={currentFolder}
tasks={uploadTasks}
onClose={() => setUploadOpen(false)}
onUpload={handleUploadFiles}
onClearDone={clearCompletedTasks}
/>
)}
{previewTarget && (
<FilePreviewModal
file={previewTarget}
token={token}
onClose={() => setPreviewTarget(null)}
/>
)}
{shareTarget && (
<ShareModal
file={shareTarget}
token={token}
onClose={() => setShareTarget(null)}
/>
)}
{renameTarget && (
<RenameModal
file={renameTarget}
onClose={() => setRenameTarget(null)}
onConfirm={handleRename}
/>
)}
</div>
);
}
|