Spaces:
Runtime error
Runtime error
File size: 11,619 Bytes
cd8bd0a | 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 | "use client";
import { useEffect, useState } from "react";
import { useTranslations } from "next-intl";
import { Button } from "@/shared/components";
function relativeTime(ts: number): string {
const diffMs = Date.now() - ts * 1000;
const diffSec = Math.round(diffMs / 1000);
if (diffSec < 60) return `${diffSec}s ago`;
const diffMin = Math.round(diffSec / 60);
if (diffMin < 60) return `${diffMin}m ago`;
const diffHr = Math.round(diffMin / 60);
if (diffHr < 24) return `${diffHr}h ago`;
const diffDays = Math.round(diffHr / 24);
return `${diffDays}d ago`;
}
function relativeExpiration(ts: number | null): string {
if (!ts) return "Never";
const diffMs = ts * 1000 - Date.now();
if (diffMs <= 0) return "Expired";
const diffSec = Math.round(diffMs / 1000);
if (diffSec < 60) return `${diffSec}s`;
const diffMin = Math.round(diffSec / 60);
if (diffMin < 60) return `${diffMin}m`;
const diffHr = Math.round(diffMin / 60);
if (diffHr < 24) return `${diffHr}h`;
const diffDays = Math.round(diffHr / 24);
return `${diffDays}d`;
}
function formatBytes(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / 1024 / 1024).toFixed(2)} MB`;
}
interface FileRecord {
id: string;
filename: string;
bytes: number;
purpose: string;
createdAt: number;
expiresAt?: number | null;
}
interface BatchRecord {
id: string;
endpoint: string;
status: string;
inputFileId: string;
outputFileId?: string | null;
errorFileId?: string | null;
model?: string | null;
}
interface FileDetailModalProps {
file: FileRecord;
contents: string | null;
loading: boolean;
batches?: BatchRecord[];
onClose: () => void;
}
export default function FileDetailModal({
file,
contents,
loading,
batches,
onClose,
}: Readonly<FileDetailModalProps>) {
const t = useTranslations("common");
const [copied, setCopied] = useState(false);
const relatedBatches = (batches ?? []).filter(
(b) => b.inputFileId === file.id || b.outputFileId === file.id || b.errorFileId === file.id
);
useEffect(() => {
const handler = (e: KeyboardEvent) => {
if (e.key === "Escape") onClose();
};
document.addEventListener("keydown", handler);
return () => document.removeEventListener("keydown", handler);
}, [onClose]);
const handleDownload = () => {
const a = document.createElement("a");
a.href = `/api/v1/files/${file.id}/content`;
a.download = file.filename;
document.body.appendChild(a);
a.click();
a.remove();
};
const handleCopy = () => {
if (contents) {
navigator.clipboard.writeText(contents);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
}
};
const createdAtTs = file.createdAt;
const expiresAtTs = file.expiresAt;
const lineCount = contents ? contents.split("\n").filter((l) => l.trim()).length : 0;
const isTruncated = lineCount > 1000;
const displayedLines = contents
? contents
.split("\n")
.filter((l) => l.trim())
.slice(0, 1000)
: [];
return (
<div className="fixed inset-0 z-50 flex items-end sm:items-center justify-center p-0 sm:p-4">
{/* Overlay */}
<div
className="absolute inset-0 bg-black/40 backdrop-blur-sm"
onClick={onClose}
aria-hidden="true"
/>
{/* Panel */}
<div className="relative w-full sm:max-w-3xl bg-[var(--color-surface)] border border-[var(--color-border)] rounded-t-2xl sm:rounded-xl shadow-2xl animate-in fade-in slide-in-from-bottom-4 duration-200 max-h-[90vh] flex flex-col">
{/* Header */}
<div className="flex items-center justify-between px-6 py-4 border-b border-[var(--color-border)] flex-shrink-0">
<div className="flex items-center gap-3">
<span className="material-symbols-outlined text-[20px] text-[var(--color-text-muted)]">
description
</span>
<div>
<h2 className="text-base font-semibold text-[var(--color-text-main)]">
File Contents
</h2>
<div className="flex items-center gap-2 mt-0.5">
<p className="text-xs text-[var(--color-text-muted)] font-mono">{file.id}</p>
<button
onClick={() => {
navigator.clipboard.writeText(file.id);
}}
className="text-[var(--color-text-muted)] hover:text-[var(--color-text-main)] transition-colors"
title={t("batchFileDetailCopyId")}
>
<span className="material-symbols-outlined text-[12px]">content_copy</span>
</button>
</div>
</div>
</div>
<button
onClick={onClose}
aria-label={t("batchFileDetailClose")}
className="p-1.5 rounded-lg text-[var(--color-text-muted)] hover:bg-[var(--color-bg-alt)] transition-colors"
>
<span className="material-symbols-outlined text-[20px]">close</span>
</button>
</div>
{/* Body */}
<div className="overflow-y-auto flex-1 p-6 flex flex-col gap-6">
{/* Metadata */}
<div className="grid grid-cols-2 sm:grid-cols-4 gap-4 p-4 rounded-xl bg-[var(--color-bg-alt)] border border-[var(--color-border)]">
<div className="flex flex-col gap-0.5">
<span className="text-[10px] uppercase tracking-wider font-medium text-[var(--color-text-muted)]">
Size
</span>
<span className="text-sm text-[var(--color-text-main)]">
{formatBytes(file.bytes)}
</span>
</div>
<div className="flex flex-col gap-0.5">
<span className="text-[10px] uppercase tracking-wider font-medium text-[var(--color-text-muted)]">
Purpose
</span>
<span className="text-sm text-[var(--color-text-main)]">{file.purpose}</span>
</div>
<div className="flex flex-col gap-0.5">
<span className="text-[10px] uppercase tracking-wider font-medium text-[var(--color-text-muted)]">
Created
</span>
<span className="text-sm text-[var(--color-text-main)]">
{createdAtTs ? relativeTime(createdAtTs) : "—"}
</span>
</div>
<div className="flex flex-col gap-0.5">
<span className="text-[10px] uppercase tracking-wider font-medium text-[var(--color-text-muted)]">
Expires
</span>
<span className="text-sm text-[var(--color-text-main)]">
{expiresAtTs ? relativeExpiration(expiresAtTs) : "Never"}
</span>
</div>
</div>
{/* Related batches */}
{relatedBatches.length > 0 && (
<div>
<h3 className="text-[11px] uppercase tracking-wider font-medium text-[var(--color-text-muted)] mb-2">
Used by {relatedBatches.length} batch{relatedBatches.length > 1 ? "es" : ""}
</h3>
<div className="space-y-1.5">
{relatedBatches.map((b) => (
<div
key={b.id}
className="flex items-center gap-3 px-3 py-2 rounded-lg bg-[var(--color-bg-alt)] border border-[var(--color-border)] text-xs"
>
<span className="material-symbols-outlined text-[14px] text-[var(--color-text-muted)]">
pending_actions
</span>
<span className="font-mono text-[var(--color-text-main)] truncate">{b.id}</span>
<span
className={`ml-auto px-1.5 py-0.5 rounded text-[10px] font-medium border ${
b.status === "completed"
? "bg-emerald-500/15 text-emerald-400 border-emerald-500/25"
: b.status === "failed"
? "bg-red-500/15 text-red-400 border-red-500/25"
: "bg-gray-500/15 text-gray-400 border-gray-500/25"
}`}
>
{b.status.replaceAll("_", " ")}
</span>
</div>
))}
</div>
</div>
)}
{/* Contents */}
<div className="flex-1 flex flex-col min-h-[300px]">
<div className="flex items-center justify-between mb-2">
<h3 className="text-xs font-semibold uppercase tracking-wider text-[var(--color-text-muted)]">
Preview
</h3>
{contents && (
<button
onClick={handleCopy}
className="flex items-center gap-1.5 px-2 py-1 text-xs font-medium rounded border border-[var(--color-border)] bg-[var(--color-bg-alt)] text-[var(--color-text-muted)] hover:text-[var(--color-text-main)] transition-colors"
>
<span className="material-symbols-outlined text-[14px]">
{copied ? "check" : "content_copy"}
</span>
{copied ? "Copied!" : "Copy"}
</button>
)}
</div>
<div className="flex-1 relative group">
{loading ? (
<div className="absolute inset-0 flex items-center justify-center bg-[var(--color-bg)]/50 rounded-lg">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-[var(--color-accent)]" />
</div>
) : contents ? (
<div className="h-full flex flex-col">
<pre className="flex-1 overflow-auto text-[11px] bg-[var(--color-bg)] rounded-lg p-4 border border-[var(--color-border)] text-[var(--color-text-muted)] font-mono leading-relaxed">
{displayedLines.join("\n")}
</pre>
{isTruncated && (
<div className="mt-3 p-3 bg-yellow-500/10 border border-yellow-500/25 rounded-lg text-xs text-yellow-400 flex items-center gap-2">
<span className="material-symbols-outlined text-[16px]">warning</span>
Showing first 1000 lines ({lineCount} total lines)
</div>
)}
</div>
) : (
<div className="flex flex-col items-center justify-center h-full py-12 rounded-lg border border-dashed border-[var(--color-border)] text-[var(--color-text-muted)]">
<span className="material-symbols-outlined text-[40px] mb-2 opacity-20">
find_in_page
</span>
<p className="text-sm">{t("batchFileDetailFailedToLoad")}</p>
</div>
)}
</div>
</div>
{/* Footer Action */}
<div className="flex justify-end gap-3 mt-2">
<Button
onClick={handleDownload}
className="flex items-center justify-center gap-2 px-4 py-2 text-sm font-medium rounded-lg bg-[var(--color-accent)] text-white hover:opacity-90 transition-opacity"
>
<span className="material-symbols-outlined text-[18px]">download</span>
Download Full File
</Button>
</div>
</div>
</div>
</div>
);
}
|