algospaced-dsa / algospaced-ui /src /components /DesktopContextMenu.tsx
iamfebin's picture
feat: FSRS card manager, fantasy cartography art polish, and map progression effects
f0175ec
Raw History Blame Contribute Delete
6.54 kB
import { useEffect, useMemo } from 'react';
import {
useFloating,
shift,
flip,
offset,
autoUpdate,
useDismiss,
useRole,
useInteractions,
FloatingPortal,
} from '@floating-ui/react';
interface DesktopContextMenuProps {
x: number;
y: number;
isOpen: boolean;
onClose: () => void;
onAutoArrange: () => void;
onResetLayout: () => void;
onChangeWallpaper: () => void;
onOpenWindow: (id: string) => void;
onTriggerSync: () => void;
onToggleFullscreen: () => void;
isFullscreen: boolean;
}
export default function DesktopContextMenu({
x,
y,
isOpen,
onClose,
onAutoArrange,
onResetLayout,
onChangeWallpaper,
onOpenWindow,
onTriggerSync,
onToggleFullscreen,
isFullscreen,
}: DesktopContextMenuProps) {
// Virtual reference element for cursor click coordinates
const virtualReference = useMemo(
() => ({
getBoundingClientRect: () => ({
x,
y,
top: y,
left: x,
bottom: y,
right: x,
width: 0,
height: 0,
}),
}),
[x, y]
);
const {
refs: { setReference, setFloating },
floatingStyles,
context,
} = useFloating({
open: isOpen,
onOpenChange: (open) => {
if (!open) onClose();
},
placement: 'bottom-start',
middleware: [
offset(2),
flip({ padding: 12 }),
shift({ padding: 12 }),
],
whileElementsMounted: autoUpdate,
});
useEffect(() => {
setReference(virtualReference);
}, [virtualReference, setReference]);
const dismiss = useDismiss(context, { outsidePress: true, escapeKey: true });
const role = useRole(context, { role: 'menu' });
const { getFloatingProps } = useInteractions([dismiss, role]);
if (!isOpen) return null;
return (
<FloatingPortal>
<div
ref={setFloating}
style={floatingStyles}
{...getFloatingProps()}
className="z-[100] w-56 bg-paper-white border-[3px] border-ink-black gadget-shadow py-1.5 flex flex-col select-none text-ink-black animate-fade-in shadow-[4px_4px_0px_0px_#1E293B]"
>
<div className="px-3 py-1 bg-[#0ea5e9] text-white border-b-2 border-ink-black flex items-center justify-between mb-1 -mt-1.5 -mx-0">
<span className="font-window-title text-[11px] font-bold tracking-wider uppercase">Desktop Options</span>
<span className="material-symbols-outlined text-xs">desktop_windows</span>
</div>
{/* Icon Layout actions */}
<button
onClick={() => {
onAutoArrange();
onClose();
}}
className="px-3 py-1.5 hover:bg-[#fde047] hover:text-ink-black flex items-center gap-2 text-xs font-bold transition-colors text-left cursor-pointer"
>
<span className="material-symbols-outlined text-base">grid_view</span>
Auto Arrange Icons
</button>
<button
onClick={() => {
onResetLayout();
onClose();
}}
className="px-3 py-1.5 hover:bg-[#fde047] hover:text-ink-black flex items-center gap-2 text-xs font-bold transition-colors text-left cursor-pointer"
>
<span className="material-symbols-outlined text-base">restart_alt</span>
Reset Icon Layout
</button>
<div className="h-[2px] bg-ink-black/20 my-1 mx-2" />
{/* Wallpaper & View */}
<button
onClick={() => {
onChangeWallpaper();
onClose();
}}
className="px-3 py-1.5 hover:bg-[#39ff14] hover:text-ink-black flex items-center gap-2 text-xs font-bold transition-colors text-left cursor-pointer"
>
<span className="material-symbols-outlined text-base">wallpaper</span>
Change Wallpaper...
</button>
<button
onClick={() => {
onToggleFullscreen();
onClose();
}}
className="px-3 py-1.5 hover:bg-[#39ff14] hover:text-ink-black flex items-center gap-2 text-xs font-bold transition-colors text-left cursor-pointer"
>
<span className="material-symbols-outlined text-base">
{isFullscreen ? 'fullscreen_exit' : 'fullscreen'}
</span>
{isFullscreen ? 'Exit Fullscreen' : 'Enter Fullscreen'}
</button>
<div className="h-[2px] bg-ink-black/20 my-1 mx-2" />
{/* Quick Launchers */}
<button
onClick={() => {
onOpenWindow('queue');
onClose();
}}
className="px-3 py-1.5 hover:bg-[#4cc9f0] hover:text-ink-black flex items-center gap-2 text-xs font-bold transition-colors text-left cursor-pointer"
>
<span className="material-symbols-outlined text-base">view_list</span>
Daily Queue
</button>
<button
onClick={() => {
onOpenWindow('cardManager');
onClose();
}}
className="px-3 py-1.5 hover:bg-[#38bdf8] hover:text-ink-black flex items-center gap-2 text-xs font-bold transition-colors text-left cursor-pointer"
>
<span className="material-symbols-outlined text-base">style</span>
Card Manager
</button>
<button
onClick={() => {
onOpenWindow('terminal');
onClose();
}}
className="px-3 py-1.5 hover:bg-[#4cc9f0] hover:text-ink-black flex items-center gap-2 text-xs font-bold transition-colors text-left cursor-pointer"
>
<span className="material-symbols-outlined text-base">terminal</span>
Terminal Emulator
</button>
<button
onClick={() => {
onOpenWindow('computer');
onClose();
}}
className="px-3 py-1.5 hover:bg-[#4cc9f0] hover:text-ink-black flex items-center gap-2 text-xs font-bold transition-colors text-left cursor-pointer"
>
<span className="material-symbols-outlined text-base">folder</span>
My Computer
</button>
<div className="h-[2px] bg-ink-black/20 my-1 mx-2" />
<button
onClick={() => {
onTriggerSync();
onClose();
}}
className="px-3 py-1.5 hover:bg-[#f72585] hover:text-white flex items-center gap-2 text-xs font-bold transition-colors text-left cursor-pointer"
>
<span className="material-symbols-outlined text-base">cloud_sync</span>
Sync with Drive
</button>
</div>
</FloatingPortal>
);
}