'use client' import { FakeWindow, useWindowManager } from "@/hooks/window-manager"; import { Icon } from "@iconify/react"; import clsx from "clsx"; import React, { useState, useRef, useEffect, useReducer } from "react"; export const RestoreWindowsButton = ({ onClick, className, ...props}: React.ComponentProps<"button">) => { const windowManager = useWindowManager(); const isWindowsDataDirty = !windowManager.windows.find(w => w.closed == true || w.minimized == true || (w.offset.x != 0 && w.offset.y != 0) ) return ( ) } export const FakeRelativeWindow = ({ windowText, className, draggable, withAnim, children }: { windowText: string, className?: string, draggable?: boolean, withAnim?: boolean, children: React.ReactNode }) => { const windowManager = useWindowManager(); const windowName = windowText.toLocaleLowerCase(); const currentWindow = windowManager.get(windowName); const [animation, setAnimation] = useState({ window: "animate-window-popup", content: "animate-[fade-in-half_1s]" }); const popupRef = useRef(null); const pos = useRef({ dragging: false, mouseX: 0, mouseY: 0, x: 0, y: 0 }); const populateWindow = () => { windowManager.add({ name: windowName, closed: false, minimized: false, offset: { x: 0, y: 0 } }) } useEffect(() => { if (!windowManager.isLocalDataExists) { if (!currentWindow) populateWindow(); return () => { windowManager.remove(windowName); } } }, []); useEffect(() => { if (popupRef.current) popupRef.current.style.transform = `translate(${currentWindow?.offset.x || 0}px, ${currentWindow?.offset.y || 0}px)`; }, [currentWindow?.offset]) useEffect(() => { if (!withAnim) return; const node = popupRef.current; if (!node) return; const observer = new window.IntersectionObserver( ([entry]) => { if (entry.isIntersecting) { setAnimation({ window: "animate-window-popup", content: "animate-[fade-in-half_1s]" }); } else { setAnimation({ window: "scale-x-0 scale-y-[0.03] animate-window-popdown", content: "animate-[fade-out-half_1s]" }); } }, { threshold: 0.3 } ); observer.observe(node); return () => observer.disconnect(); }, []); const toggleMinimize = () => windowManager.toggleMinimize(windowName); const handleClose = () => windowManager.close(windowName); const onMouseDown = (e: React.MouseEvent) => { if (!draggable || !currentWindow) return; if (popupRef.current) { pos.current.dragging = true; pos.current.mouseX = e.clientX; pos.current.mouseY = e.clientY; pos.current.x = currentWindow.offset.x; pos.current.y = currentWindow.offset.y; document.body.style.userSelect = "none"; } window.addEventListener("mousemove", onMouseMove); window.addEventListener("mouseup", onMouseUp); }; const onMouseUp = () => { pos.current.dragging = false; document.body.style.userSelect = ""; window.removeEventListener("mousemove", onMouseMove); window.removeEventListener("mouseup", onMouseUp); }; const onMouseMove = (e: MouseEvent) => { if (!currentWindow) return; if (pos.current.dragging && popupRef.current) { const dx = e.clientX - pos.current.mouseX; const dy = e.clientY - pos.current.mouseY; const newX = pos.current.x + dx; const newY = pos.current.y + dy; windowManager.move(windowName, { x: newX, y: newY }); } }; if (!currentWindow || currentWindow?.closed) return return ( {currentWindow ? currentWindow.name : "Error!"} {children} ) }