This commit is contained in:
2026-10-29 13:21:30 +06:00
parent 2ae9f84ddf
commit 3acc0de8de
132 changed files with 35007 additions and 1 deletions

View File

@@ -0,0 +1,229 @@
'use client';
import React, { useState, useEffect } from 'react';
import { useRouter } from 'next/navigation';
import { useAuth } from '@/context/auth-context';
import { api } from '@/lib/api-client';
import { Navbar } from '@/components/layout/Navbar';
import { Sidebar } from '@/components/layout/Sidebar';
import { FileCard } from '@/components/file-manager/FileCard';
import { FileRow } from '@/components/file-manager/FileRow';
import { FilePreviewModal } from '@/components/file-manager/FilePreviewModal';
import { EcohubWatermark } from '@/components/common/EcohubWatermark';
import { Share2, Users, LayoutGrid, List, Search, Loader2, RefreshCw } from 'lucide-react';
export default function SharedPage() {
const router = useRouter();
const { user, isLoading } = useAuth();
const [sharedFiles, setSharedFiles] = useState<any[]>([]);
const [isDataLoading, setIsDataLoading] = useState(true);
const [isSyncing, setIsSyncing] = useState(false);
const [lastSyncedAt, setLastSyncedAt] = useState<Date | null>(null);
const [search, setSearch] = useState('');
const [viewMode, setViewMode] = useState<'grid' | 'list'>('grid');
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
const [selectedFileForPreview, setSelectedFileForPreview] = useState<any | null>(null);
useEffect(() => {
if (!isLoading && !user) {
router.push('/login');
}
}, [user, isLoading, router]);
const loadSharedFiles = async (isSilent = false) => {
if (!user) return;
if (!isSilent) setIsDataLoading(true);
setIsSyncing(true);
try {
const files = await api.get<any[]>('/sharing/shared-with-me');
setSharedFiles(files || []);
setLastSyncedAt(new Date());
} catch (err) {
console.error('Failed to load shared files', err);
} finally {
if (!isSilent) setIsDataLoading(false);
setIsSyncing(false);
}
};
useEffect(() => {
loadSharedFiles();
}, [user]);
// Periodic Auto-Sync with Database (Silent every 8s + on Window Focus)
useEffect(() => {
if (!user) return;
const interval = setInterval(() => {
if (document.visibilityState === 'visible') {
loadSharedFiles(true);
}
}, 8000);
const handleFocus = () => {
loadSharedFiles(true);
};
window.addEventListener('focus', handleFocus);
return () => {
clearInterval(interval);
window.removeEventListener('focus', handleFocus);
};
}, [user]);
const filtered = sharedFiles.filter((f) =>
f.originalFilename.toLowerCase().includes(search.toLowerCase()),
);
return (
<div className="min-h-screen bg-[#f8fafc] text-slate-900 flex flex-col relative">
<EcohubWatermark variant="corner" />
<Navbar onToggleSidebar={() => setIsSidebarOpen(!isSidebarOpen)} />
<div className="flex-1 flex min-w-0">
<Sidebar isOpen={isSidebarOpen} onClose={() => setIsSidebarOpen(false)} />
<main className="flex-1 lg:pl-64 min-w-0 flex flex-col w-full">
<div className="flex-1 p-4 sm:p-6 lg:p-8 space-y-6 max-w-7xl w-full mx-auto">
{/* Header */}
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
<div>
<div className="flex items-center gap-2 flex-wrap">
<h1 className="text-xl font-bold text-slate-900 flex items-center gap-2.5">
<Share2 className="w-5 h-5 text-emerald-600" />
<span>Shared With Me</span>
</h1>
{/* Live Database Sync Badge */}
<div
className="inline-flex items-center gap-1.5 px-2.5 py-0.5 rounded-full bg-emerald-50 text-emerald-700 border border-emerald-200/80 text-[11px] font-semibold"
title={lastSyncedAt ? `Live Auto-Sync Active • Last synced: ${lastSyncedAt.toLocaleTimeString()}` : 'Live Auto-Sync Active'}
>
<span className={`w-2 h-2 rounded-full ${isSyncing ? 'bg-amber-500 animate-spin' : 'bg-emerald-500 animate-pulse'}`} />
<span>{isSyncing ? 'Syncing...' : 'Live DB Sync'}</span>
</div>
</div>
<p className="text-xs text-slate-500 mt-1">
Files and media shared with you by system administrators
</p>
</div>
<div className="flex items-center gap-2.5 flex-wrap sm:flex-nowrap">
{/* Manual Sync Button */}
<button
onClick={() => loadSharedFiles(false)}
disabled={isSyncing}
className="flex items-center gap-1.5 px-3 py-2 rounded-xl bg-white hover:bg-slate-50 border border-slate-200 text-slate-700 text-xs font-semibold shadow-xs transition-all active:scale-95 disabled:opacity-50"
title="Sync with database"
>
<RefreshCw className={`w-3.5 h-3.5 text-emerald-600 ${isSyncing ? 'animate-spin' : ''}`} />
<span>Sync DB</span>
</button>
{/* Search */}
<div className="relative w-48 sm:w-64 text-xs">
<Search className="w-3.5 h-3.5 text-slate-400 absolute left-3 top-2.5" />
<input
type="text"
placeholder="Search shared files..."
value={search}
onChange={(e) => setSearch(e.target.value)}
className="w-full pl-8 pr-3 py-1.5 rounded-xl bg-white border border-slate-200 text-slate-900 placeholder:text-slate-400 focus:outline-none focus:border-emerald-600 shadow-sm"
/>
</div>
{/* View toggle */}
<div className="flex items-center p-0.5 rounded-lg bg-white border border-slate-200 shadow-sm">
<button
onClick={() => setViewMode('grid')}
className={`p-1.5 rounded-md ${
viewMode === 'grid' ? 'bg-emerald-600 text-white' : 'text-slate-500 hover:text-slate-800'
}`}
>
<LayoutGrid className="w-3.5 h-3.5" />
</button>
<button
onClick={() => setViewMode('list')}
className={`p-1.5 rounded-md ${
viewMode === 'list' ? 'bg-emerald-600 text-white' : 'text-slate-500 hover:text-slate-800'
}`}
>
<List className="w-3.5 h-3.5" />
</button>
</div>
</div>
</div>
{/* Content */}
{isDataLoading ? (
<div className="p-16 flex items-center justify-center">
<Loader2 className="w-8 h-8 text-emerald-600 animate-spin" />
</div>
) : filtered.length === 0 ? (
<div className="p-16 text-center rounded-2xl bg-white border border-slate-200 shadow-sm">
<Users className="w-12 h-12 text-slate-400 mx-auto mb-3" />
<h3 className="text-sm font-medium text-slate-800">No shared files yet</h3>
<p className="text-xs text-slate-500 mt-1 max-w-sm mx-auto">
When an administrator grants you access to another user's files or videos, they will appear here.
</p>
</div>
) : viewMode === 'grid' ? (
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
{filtered.map((file) => (
<FileCard
key={file.id}
file={file}
isOwner={false}
onPreview={(f) => setSelectedFileForPreview(f)}
onRename={() => {}}
onMove={() => {}}
onDelete={() => {}}
/>
))}
</div>
) : (
<div className="bg-white rounded-2xl overflow-hidden border border-slate-200 shadow-sm">
<div className="overflow-x-auto">
<table className="w-full min-w-[640px] text-left border-collapse">
<thead>
<tr className="border-b border-slate-200 bg-slate-50 text-[11px] font-semibold text-slate-500 uppercase tracking-wider">
<th className="py-3 px-4">Filename</th>
<th className="py-3 px-4 hidden md:table-cell">Category</th>
<th className="py-3 px-4">Size</th>
<th className="py-3 px-4 hidden sm:table-cell">Uploaded</th>
<th className="py-3 px-4">Status</th>
<th className="py-3 px-4 text-right">Actions</th>
</tr>
</thead>
<tbody>
{filtered.map((file) => (
<FileRow
key={file.id}
file={file}
isOwner={false}
onPreview={(f) => setSelectedFileForPreview(f)}
onRename={() => {}}
onMove={() => {}}
onDelete={() => {}}
/>
))}
</tbody>
</table>
</div>
</div>
)}
</div>
</main>
</div>
{/* Preview Modal */}
<FilePreviewModal
isOpen={!!selectedFileForPreview}
onClose={() => setSelectedFileForPreview(null)}
file={selectedFileForPreview}
/>
</div>
);
}