Skip to content

Commit 99f6a38

Browse files
feat(spaces): right-click context menus in Shared Spaces (#5)
Shared Spaces had no right-click menu. Files, folders and the empty area now open a context menu (reusing the Drive's ContextMenu): open / download / rename / delete on a file, open / rename / delete on a folder, and new-folder / upload on the background — gated by the member's role (VIEWERs get open + download only). The personal Drive already had empty-area right-click, so #5 is now consistent across both.
1 parent 10e8134 commit 99f6a38

1 file changed

Lines changed: 46 additions & 4 deletions

File tree

  • apps/web/src/app/(app)/spaces/[id]

‎apps/web/src/app/(app)/spaces/[id]/page.tsx‎

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
UserPlus,
2727
Settings2,
2828
LogOut,
29+
Eye,
2930
File as FileIcon,
3031
} from 'lucide-react';
3132
import type {
@@ -41,6 +42,8 @@ import { useAuth } from '@/lib/auth';
4142
import { useT } from '@/lib/i18n';
4243
import { Select } from '@/components/ui/Select';
4344
import { FileViewer, type ViewerSource } from '@/components/FileViewer';
45+
import { ContextMenu } from '@/components/drive/ContextMenu';
46+
import type { MenuItem } from '@/components/ui/Menu';
4447
import { confirm, choose, prompt, toast } from '@/components/ui/overlays';
4548

4649
export default function SpacePage() {
@@ -59,6 +62,7 @@ export default function SpacePage() {
5962
const [uploadPct, setUploadPct] = useState<number | null>(null);
6063
const [showManage, setShowManage] = useState(false);
6164
const [viewing, setViewing] = useState<ViewerSource | null>(null);
65+
const [ctxMenu, setCtxMenu] = useState<{ x: number; y: number; items: MenuItem[] } | null>(null);
6266
const fileInput = useRef<HTMLInputElement>(null);
6367

6468
const canWrite = space ? space.myRole === 'OWNER' || space.myRole === 'EDITOR' : false;
@@ -210,6 +214,40 @@ export default function SpacePage() {
210214
}
211215
}
212216

217+
// ── Right-click menus ──────────────────────────────────────────────────────
218+
function fileMenu(f: PublicFile): MenuItem[] {
219+
const items: MenuItem[] = [
220+
{ label: t('space.openFile'), icon: Eye, onClick: () => openFile(f) },
221+
{ label: t('space.download'), icon: Download, onClick: () => download(f) },
222+
];
223+
if (canWrite) {
224+
items.push({ label: t('space.rename'), icon: Pencil, onClick: () => void renameFile(f) });
225+
items.push({ label: t('space.delete'), icon: Trash2, danger: true, onClick: () => void deleteFile(f) });
226+
}
227+
return items;
228+
}
229+
function folderMenu(f: PublicFolder): MenuItem[] {
230+
const items: MenuItem[] = [{ label: t('space.openFile'), icon: FolderIcon, onClick: () => setCwd(f.id) }];
231+
if (canWrite) {
232+
items.push({ label: t('space.rename'), icon: Pencil, onClick: () => void renameFolder(f) });
233+
items.push({ label: t('space.delete'), icon: Trash2, danger: true, onClick: () => void deleteFolder(f) });
234+
}
235+
return items;
236+
}
237+
function bgMenu(): MenuItem[] {
238+
if (!canWrite) return [];
239+
return [
240+
{ label: t('space.newFolder'), icon: FolderPlus, onClick: () => void newFolder() },
241+
{ label: t('space.upload'), icon: Upload, onClick: () => fileInput.current?.click() },
242+
];
243+
}
244+
function openCtx(ev: React.MouseEvent, items: MenuItem[]) {
245+
if (items.length === 0) return;
246+
ev.preventDefault();
247+
ev.stopPropagation();
248+
setCtxMenu({ x: ev.clientX, y: ev.clientY, items });
249+
}
250+
213251
// ── Member & lifecycle actions ─────────────────────────────────────────────
214252

215253
async function addMember() {
@@ -427,14 +465,17 @@ export default function SpacePage() {
427465

428466
{/* Listing */}
429467
{childFolders.length === 0 && files.length === 0 ? (
430-
<div className="card flex flex-col items-center gap-2 py-14 text-center">
468+
<div
469+
className="card flex flex-col items-center gap-2 py-14 text-center"
470+
onContextMenu={(ev) => openCtx(ev, bgMenu())}
471+
>
431472
<span className="grid h-12 w-12 place-items-center rounded-2xl bg-white/[0.04] text-zinc-500"><FolderIcon size={22} /></span>
432473
<p className="text-sm text-zinc-400">{t('space.emptyFolder')}</p>
433474
</div>
434475
) : (
435-
<div className="space-y-1.5">
476+
<div className="space-y-1.5" onContextMenu={(ev) => openCtx(ev, bgMenu())}>
436477
{childFolders.map((f) => (
437-
<div key={f.id} className="row">
478+
<div key={f.id} className="row" onContextMenu={(ev) => openCtx(ev, folderMenu(f))}>
438479
<button className="flex min-w-0 items-center gap-3" onClick={() => setCwd(f.id)}>
439480
<span className="grid h-9 w-9 shrink-0 place-items-center rounded-lg bg-white/[0.05] text-amber-200"><FolderIcon size={16} /></span>
440481
<span className="truncate font-medium text-zinc-100">{f.name}</span>
@@ -448,7 +489,7 @@ export default function SpacePage() {
448489
</div>
449490
))}
450491
{files.map((f) => (
451-
<div key={f.id} className="row">
492+
<div key={f.id} className="row" onContextMenu={(ev) => openCtx(ev, fileMenu(f))}>
452493
<button className="flex min-w-0 items-center gap-3 text-left" onClick={() => openFile(f)} title={t('space.openFile')}>
453494
<span className="grid h-9 w-9 shrink-0 place-items-center rounded-lg bg-accent-soft text-violet-300"><FileIcon size={16} /></span>
454495
<div className="min-w-0">
@@ -471,6 +512,7 @@ export default function SpacePage() {
471512
)}
472513

473514
{viewing && <FileViewer source={viewing} onClose={() => setViewing(null)} />}
515+
{ctxMenu && <ContextMenu x={ctxMenu.x} y={ctxMenu.y} items={ctxMenu.items} onClose={() => setCtxMenu(null)} />}
474516
</div>
475517
);
476518
}

0 commit comments

Comments
 (0)