File size: 6,329 Bytes
d988ae4 3bbb98d d988ae4 3bbb98d d988ae4 3bbb98d d988ae4 3bbb98d d988ae4 3bbb98d d988ae4 |
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 |
'use client';
import { useState, useEffect } from 'react';
import { X } from 'lucide-react';
import { apiUrl } from '@/lib/constants';
import { appendTokenToUrl, withAdminTokenHeader } from '@/lib/adminAuth';
interface FilePreviewProps {
fileId: string;
filename: string;
mimetype: string;
roomCode: string;
onClose: () => void;
}
export default function FilePreview({ fileId, filename, mimetype, roomCode, onClose }: FilePreviewProps) {
const [loading, setLoading] = useState(true);
const fileUrl = appendTokenToUrl(`${apiUrl}/clipboard/${roomCode}/files/${fileId}`);
// Handle escape key to close preview
useEffect(() => {
const handleEsc = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
onClose();
}
};
window.addEventListener('keydown', handleEsc);
return () => {
window.removeEventListener('keydown', handleEsc);
};
}, [onClose]);
const renderPreview = () => {
if (mimetype.startsWith('image/')) {
return (
<div className="flex items-center justify-center h-full">
<img
src={fileUrl}
alt={filename}
className="max-w-full max-h-full object-contain"
onLoad={() => setLoading(false)}
/>
</div>
);
} else if (mimetype === 'application/pdf') {
return (
<iframe
src={`${fileUrl}#toolbar=0`}
className="w-full h-full"
title={filename}
onLoad={() => setLoading(false)}
/>
);
} else if (mimetype.startsWith('text/') || mimetype === 'application/json') {
return <TextFilePreview fileUrl={fileUrl} setLoading={setLoading} />;
} else if (mimetype.startsWith('video/')) {
return (
<video
controls
className="max-w-full max-h-full"
onLoadedData={() => setLoading(false)}
>
<source src={fileUrl} type={mimetype} />
Your browser does not support the video tag.
</video>
);
} else if (mimetype.startsWith('audio/')) {
return (
<div className="flex flex-col items-center justify-center h-full">
<div className="bg-surface/80 p-6 rounded-lg shadow-lg">
<p className="text-text-primary text-lg mb-4 text-center">{filename}</p>
<audio
controls
className="w-full"
onLoadedData={() => setLoading(false)}
>
<source src={fileUrl} type={mimetype} />
Your browser does not support the audio element.
</audio>
</div>
</div>
);
} else {
// For unsupported file types
setLoading(false);
return (
<div className="flex flex-col items-center justify-center h-full">
<div className="bg-surface/80 p-8 rounded-lg shadow-lg text-center">
<p className="text-text-primary text-xl mb-4">Preview not available</p>
<p className="text-text-secondary mb-6">This file type cannot be previewed.</p>
<a
href={appendTokenToUrl(`${apiUrl}/clipboard/${roomCode}/files/${fileId}?filename=${encodeURIComponent(filename)}`)}
target="_blank"
rel="noopener noreferrer"
className="px-4 py-2 bg-primary text-white rounded-md hover:bg-primary/90 transition-colors"
download={filename}
>
Download File
</a>
</div>
</div>
);
}
};
return (
<div className="fixed inset-0 bg-black/70 backdrop-blur-sm flex items-center justify-center z-50 p-4">
<div className="bg-background rounded-lg shadow-xl w-full max-w-4xl h-[80vh] flex flex-col">
{/* Header */}
<div className="flex items-center justify-between p-4 border-b border-surface-hover">
<h3 className="text-lg font-medium text-text-primary truncate max-w-[calc(100%-40px)]">
{filename}
</h3>
<button
onClick={onClose}
className="p-1 rounded-full hover:bg-surface-hover transition-colors"
aria-label="Close preview"
>
<X className="h-5 w-5 text-text-secondary" />
</button>
</div>
{/* Content */}
<div className="flex-1 overflow-auto relative">
{loading && (
<div className="absolute inset-0 flex items-center justify-center bg-background/80">
<div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-primary"></div>
</div>
)}
{renderPreview()}
</div>
{/* Footer */}
<div className="p-4 border-t border-surface-hover flex justify-end">
<a
href={appendTokenToUrl(`${apiUrl}/clipboard/${roomCode}/files/${fileId}?filename=${encodeURIComponent(filename)}`)}
download={filename}
className="px-4 py-2 bg-primary text-white rounded-md hover:bg-primary/90 transition-colors"
>
Download
</a>
</div>
</div>
</div>
);
}
// Component to handle text file preview
function TextFilePreview({ fileUrl, setLoading }: { fileUrl: string; setLoading: (loading: boolean) => void }) {
const [content, setContent] = useState<string>('');
const [error, setError] = useState<string | null>(null);
useEffect(() => {
async function fetchTextContent() {
try {
const response = await fetch(fileUrl, { headers: withAdminTokenHeader() });
if (!response.ok) {
throw new Error(`Failed to fetch file: ${response.status}`);
}
const text = await response.text();
setContent(text);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load file');
} finally {
setLoading(false);
}
}
fetchTextContent();
}, [fileUrl, setLoading]);
if (error) {
return (
<div className="flex items-center justify-center h-full">
<div className="bg-error/10 text-error p-4 rounded-md">
{error}
</div>
</div>
);
}
return (
<pre className="p-4 whitespace-pre-wrap break-words text-text-primary font-mono text-sm overflow-auto h-full bg-surface/30">
{content}
</pre>
);
}
|