Spaces:
TwanAPI
/
Running

UploadFile / frontend /src /features /files /components /FilePreviewModal.tsx
ddosvn's picture
Deploy clean-slate Next.js FE (Feature-Based Architecture, Swiss-Obsidian UI/UX, CWV, SEO, AI Discovery)
968ca90 verified
Raw History Blame Contribute Delete
3.26 kB
"use client";
import React from "react";
import { Download, ExternalLink, FileText } from "lucide-react";
import { formatBytes } from "@/shared/lib/format";
import { buildDownloadUrl } from "@/shared/lib/http";
import { FileNode } from "@/shared/types/domain";
import { Badge, Modal } from "@/shared/ui";
interface FilePreviewModalProps {
file: FileNode | null;
token: string | null;
onClose: () => void;
}
export function FilePreviewModal({ file, token, onClose }: FilePreviewModalProps) {
if (!file) return null;
const inlineUrl = buildDownloadUrl(file.path, token, true);
const downloadUrl = buildDownloadUrl(file.path, token, false);
const isAudio = ["mp3", "wav", "ogg"].includes(file.ext);
return (
<Modal
open={Boolean(file)}
onClose={onClose}
title={file.name}
subtitle={`${formatBytes(file.size)} • @${file.owner}`}
maxWidthClass="max-w-3xl"
>
<div className="space-y-5">
<div className="rounded-2xl bg-slate-950 border border-slate-800 overflow-hidden flex items-center justify-center min-h-[220px]">
{file.category === "video" && !isAudio ? (
<video src={inlineUrl} controls playsInline className="w-full max-h-[60vh] bg-black" />
) : isAudio ? (
<div className="p-8 w-full max-w-md text-center space-y-4">
<Badge tone="cyan">HTTP 206 AUDIO STREAM</Badge>
<audio src={inlineUrl} controls className="w-full" />
</div>
) : file.category === "image" ? (
<img
src={inlineUrl}
alt={file.name}
className="max-h-[60vh] max-w-full object-contain"
/>
) : (
<div className="p-10 text-center space-y-3">
<FileText className="w-10 h-10 text-sky-400 mx-auto" aria-hidden="true" />
<p className="text-sm font-semibold text-white">
Tệp định dạng `.{file.ext || "bin"}` sẵn sàng tải xuống
</p>
<p className="text-xs text-slate-400 font-mono break-all">{file.path}</p>
</div>
)}
</div>
<div className="flex flex-wrap items-center justify-between gap-3 pt-1">
<div className="text-xs font-mono text-slate-400 truncate max-w-md">{file.path}</div>
<div className="flex items-center gap-2.5">
<a
href={inlineUrl}
target="_blank"
rel="noreferrer"
className="inline-flex items-center gap-1.5 px-3.5 py-2 rounded-xl bg-slate-900 border border-slate-800 text-xs font-semibold text-slate-200 hover:text-white"
>
<ExternalLink className="w-3.5 h-3.5" aria-hidden="true" />
<span>Mở tab mới</span>
</a>
<a
href={downloadUrl}
download={file.name}
className="inline-flex items-center gap-1.5 px-4 py-2 rounded-xl bg-gradient-to-r from-sky-400 to-emerald-400 text-xs font-bold text-slate-950"
>
<Download className="w-3.5 h-3.5" aria-hidden="true" />
<span>Tải xuống</span>
</a>
</div>
</div>
</div>
</Modal>
);
}