0
File size: 4,239 Bytes
ee48497
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import React, { useState, useCallback, useEffect } from 'react';
import { FeatureDock } from './FeatureDock.tsx';
import { Window } from './Window.tsx';
import { Taskbar } from './Taskbar.tsx';
import { ALL_FEATURES } from '../features/index.ts';
import type { Feature } from '../../types.ts';

interface WindowState {
  id: string;
  position: { x: number; y: number };
  size: { width: number; height: number };
  zIndex: number;
  isMinimized: boolean;
}

const Z_INDEX_BASE = 10;

export const DesktopView: React.FC<{ openFeatureId?: string }> = ({ openFeatureId }) => {
    const [windows, setWindows] = useState<Record<string, WindowState>>({});
    const [activeId, setActiveId] = useState<string | null>(null);
    const [nextZIndex, setNextZIndex] = useState(Z_INDEX_BASE);
    
    const openWindow = useCallback((featureId: string) => {
        const newZIndex = nextZIndex + 1;
        setNextZIndex(newZIndex);
        setActiveId(featureId);

        setWindows(prev => {
            const existingWindow = prev[featureId];
            if (existingWindow) {
                return {
                    ...prev,
                    [featureId]: {
                        ...existingWindow,
                        isMinimized: false,
                        zIndex: newZIndex,
                    }
                };
            }

            const openWindowsCount = Object.values(prev).filter(w => !w.isMinimized).length;
            const newWindow: WindowState = {
                id: featureId,
                position: { x: 50 + openWindowsCount * 30, y: 50 + openWindowsCount * 30 },
                size: { width: 800, height: 600 },
                zIndex: newZIndex,
                isMinimized: false,
            };
            return { ...prev, [featureId]: newWindow };
        });
    }, [nextZIndex]);
    
    useEffect(() => {
        if(openFeatureId) {
            openWindow(openFeatureId);
        }
    }, [openFeatureId, openWindow])

    const closeWindow = (id: string) => {
        setWindows(prev => {
            const newState = { ...prev };
            delete newState[id];
            return newState;
        });
    };

    const minimizeWindow = (id: string) => {
        setWindows(prev => ({
            ...prev,
            [id]: { ...prev[id], isMinimized: true }
        }));
        setActiveId(null);
    };

    const focusWindow = (id: string) => {
        if (id === activeId) return;
        const newZIndex = nextZIndex + 1;
        setNextZIndex(newZIndex);
        setActiveId(id);
        setWindows(prev => ({
            ...prev,
            [id]: { ...prev[id], zIndex: newZIndex }
        }));
    };
    
    const updateWindowState = (id: string, updates: Partial<WindowState>) => {
        setWindows(prev => ({
            ...prev,
            [id]: { ...prev[id], ...updates }
        }));
    }

    const openWindows = Object.values(windows).filter(w => !w.isMinimized);
    const minimizedWindows = Object.values(windows).filter(w => w.isMinimized);
    const featuresMap = new Map(ALL_FEATURES.map(f => [f.id, f]));

    return (
        <div className="h-full flex flex-col bg-transparent">
            <FeatureDock onOpen={openWindow} />
            <div className="flex-grow relative overflow-hidden">
                {openWindows.map(win => {
                    const feature = featuresMap.get(win.id);
                    if (!feature) return null;
                    return (
                        <Window
                            key={win.id}
                            feature={feature}
                            state={win}
                            isActive={win.id === activeId}
                            onClose={() => closeWindow(win.id)}
                            onMinimize={() => minimizeWindow(win.id)}
                            onFocus={() => focusWindow(win.id)}
                            onUpdate={updateWindowState}
                        />
                    );
                })}
            </div>
            <Taskbar
                minimizedWindows={minimizedWindows.map(w => featuresMap.get(w.id)).filter(Boolean) as Feature[]}
                onRestore={openWindow}
            />
        </div>
    );
};