/
index.tsx
142 lines (134 loc) · 4.37 KB
/
index.tsx
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import AppWindow from '@/components/app_window';
import MoreButton from '@/components/more_button';
import useAppStore from '@/stores/app';
import { TApp } from '@/types';
import { Box, Flex, Grid, GridItem, Image, Text } from '@chakra-ui/react';
import { useTranslation } from 'next-i18next';
import dynamic from 'next/dynamic';
import { MouseEvent, useCallback, useEffect, useState } from 'react';
import { createMasterAPP, masterApp } from 'sealos-desktop-sdk/master';
import IframeWindow from './iframe_window';
import styles from './index.module.scss';
const TimeComponent = dynamic(() => import('./time'), {
ssr: false
});
const UserMenu = dynamic(() => import('@/components/user_menu'), {
ssr: false
});
export default function DesktopContent(props: any) {
const { t, i18n } = useTranslation();
const { installedApps: apps, runningInfo, openApp, setToHighestLayerById } = useAppStore();
const renderApps = apps.filter((item: TApp) => item?.displayType === 'normal');
const [maxItems, setMaxItems] = useState(10);
const handleDoubleClick = (e: MouseEvent<HTMLDivElement>, item: TApp) => {
e.preventDefault();
if (item?.name) {
openApp(item);
}
};
/**
* open app
*/
const openDesktopApp = useCallback(
({
appKey,
query = {},
messageData = {},
pathname = '/'
}: {
appKey: string;
query?: Record<string, string>;
messageData?: Record<string, any>;
pathname: string;
}) => {
const app = apps.find((item) => item.key === appKey);
const runningApp = runningInfo.find((item) => item.key === appKey);
if (!app) return;
openApp(app, { query, pathname });
if (runningApp) {
setToHighestLayerById(runningApp.pid);
}
// post message
const iframe = document.getElementById(`app-window-${appKey}`) as HTMLIFrameElement;
if (!iframe) return;
iframe.contentWindow?.postMessage(messageData, app.data.url);
},
[apps, openApp, runningInfo, setToHighestLayerById]
);
useEffect(() => {
return createMasterAPP();
}, []);
useEffect(() => {
return masterApp?.addEventListen('openDesktopApp', openDesktopApp);
}, [openDesktopApp]);
return (
<Box id="desktop" className={styles.desktop}>
<Flex w="100%" h="100%" alignItems={'center'} flexDirection={'column'}>
<Box mt="12vh" minW={'508px'}>
<TimeComponent />
</Box>
{/* desktop apps */}
<Grid
mt="50px"
minW={'508px'}
maxH={'300px'}
templateRows={'repeat(2, 100px)'}
templateColumns={'repeat(5, 72px)'}
gap={'36px'}
>
{renderApps &&
renderApps.slice(0, maxItems).map((item: TApp, index) => (
<GridItem
w="72px"
h="100px"
key={index}
userSelect="none"
cursor={'pointer'}
onClick={(e) => handleDoubleClick(e, item)}
>
<Box
w="72px"
h="72px"
p={'12px'}
border={'1px solid #FFFFFF'}
borderRadius={8}
boxShadow={'0px 1.16667px 2.33333px rgba(0, 0, 0, 0.2)'}
backgroundColor={'rgba(244, 246, 248, 0.9)'}
>
<Image
width="100%"
height="100%"
src={item?.icon}
fallbackSrc="/images/sealos.svg"
alt="user avator"
/>
</Box>
<Text
textShadow={'0px 1px 2px rgba(0, 0, 0, 0.4)'}
textAlign={'center'}
mt="8px"
color={'#FFFFFF'}
fontSize={'10px'}
lineHeight={'16px'}
>
{item?.i18n?.[i18n?.language]?.name
? item?.i18n?.[i18n?.language]?.name
: t(item?.name)}
</Text>
</GridItem>
))}
</Grid>
<MoreButton />
<UserMenu />
</Flex>
{/* opened apps */}
{runningInfo.map((process) => {
return (
<AppWindow key={process.pid} style={{ height: '100vh' }} pid={process.pid}>
<IframeWindow pid={process.pid} />
</AppWindow>
);
})}
</Box>
);
}