// contexts/WindowManagerContext.tsx 'use client'; import React, { createContext,…
- User
// contexts/WindowManagerContext.tsx
'use client';
import React, { createContext, useContext, useState, useEffect } from 'react';
type WindowData = { name: string; Component: React.ComponentType; windowProps: { width?: string; height?: string; }; };
type WindowState = { visible: boolean; minimized: boolean; maximized: boolean; width: number; height: number; x: number; y: number; zIndex: number; prevWidth?: number; prevHeight?: number; prevX?: number; prevY?: number; };
type WindowManagerContextType = { windows: { [key: string]: WindowData }; windowStates: { [key: string]: WindowState }; openWindow: (name: string) => void; closeWindow: (name: string) => void; minimizeWindow: (name: string) => void; restoreWindow: (name: string) => void; maximizeWindow: (name: string) => void; updateWindowState: (name: string, newState: Partial) => void; bringToFront: (name: string) => void; };
const WindowManagerContext = createContext<WindowManagerContextType | undefined>(undefined);
export const useWindowManager = () => { const context = useContext(WindowManagerContext); if (!context) { throw new Error('useWindowManager must be used within a WindowManagerProvider'); } return context; };
export const WindowManagerProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { const [windows, setWindows] = useState<{ [key: string]: WindowData }>({}); const [windowStates, setWindowStates] = useState<{ [key: string]: WindowState }>({}); const [nextZIndex, setNextZIndex] = useState(1);
useEffect(() => { // Dynamically import window components const importWindows = async () => { const windowsContext = require.context('../windows', false, /.tsx$/); const windowFiles = windowsContext.keys();
let zIndexCounter = 1; const windowsDataPromises = windowFiles.map(async (file: string, index: number) => { const mod = await windowsContext(file); const name = file.replace('./', '').replace('.tsx', ''); const Component = mod.default; const windowProps = mod.windowProps || {}; // Parse initial size const width = windowProps.width ? parseSize(windowProps.width, 'width') : 0.35 * window.innerWidth; const height = windowProps.height ? parseSize(windowProps.height, 'height') : 0.2 * window.innerHeight; const windowData: WindowData = { name, Component, windowProps }; const windowState: WindowState = { visible: true, minimized: false, maximized: false, width, height, x: 100 + index * 50, y: 100 + index * 50, zIndex: zIndexCounter++, }; return { windowData, windowState }; }); const windowsDataArray = await Promise.all(windowsDataPromises); const newWindows: { [key: string]: WindowData } = {}; const newWindowStates: { [key: string]: WindowState } = {}; windowsDataArray.forEach(({ windowData, windowState }) => { newWindows[windowData.name] = windowData; newWindowStates[windowData.name] = windowState; }); setWindows(newWindows); setWindowStates(newWindowStates); setNextZIndex(zIndexCounter); }; importWindows(); - Assistant