forked from labring/sealos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
200 lines (192 loc) · 6.6 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import AppWindow from '@/components/app_window';
import MoreButton from '@/components/more_button';
import UserMenu from '@/components/user_menu';
import useDriver from '@/hooks/useDriver';
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';
import { useQuery } from '@tanstack/react-query';
import { getGlobalNotification } from '@/api/platform';
import { useMessage } from '@sealos/ui';
import { BackgroundImageUrl, ImageFallBackUrl } from '@/stores/config';
const TimeComponent = dynamic(() => import('./time'), {
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 { message } = useMessage();
const handleDoubleClick = (e: MouseEvent<HTMLDivElement>, item: TApp) => {
e.preventDefault();
if (item?.name) {
openApp(item);
}
};
/**
* Open Desktop Application
*
* @param {object} options - Options for opening the application
* @param {string} options.appKey - Unique identifier key for the application
* @param {object} [options.query={}] - Query parameter object
* @param {object} [options.messageData={}] - Message data to be sent to the application
* @param {string} options.pathname - Path when the application opens
*
* Logic:
* - Find information about the application and its running state
* - If the application does not exist, exit
* - If the application is not open (not running), call the openApp method to open it
* - If the application is already open (running), bring it to the highest layer
* - Send a postMessage to the application window to handle the message data
*/
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]);
const { UserGuide, showGuide } = useDriver({ openDesktopApp });
useQuery(['getGlobalNotification'], getGlobalNotification, {
onSuccess(data) {
const newID = data.data?.metadata?.uid;
if (!newID || newID === localStorage.getItem('GlobalNotification')) return;
localStorage.setItem('GlobalNotification', newID);
const title =
i18n.language === 'zh' && data.data?.spec?.i18ns?.zh?.message
? data.data?.spec?.i18ns?.zh?.message
: data.data?.spec?.message;
message({
title: title,
status: 'info',
duration: null
});
}
});
return (
<Box
id="desktop"
className={styles.desktop}
backgroundImage={`url(${BackgroundImageUrl})`}
backgroundRepeat={'no-repeat'}
backgroundSize={'cover'}
>
<Flex w="100%" h="100%" alignItems={'center'} flexDirection={'column'}>
<Box mt="12vh" minW={'508px'}>
<TimeComponent />
</Box>
{showGuide ? (
<>
<UserGuide />
<Box
position="fixed"
top="0"
left="0"
width="100%"
height="100%"
backgroundColor="rgba(0, 0, 0, 0.7)" // 半透明黑色背景
zIndex="11000" // 保证蒙层在最上层
/>
</>
) : (
<></>
)}
{/* 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
className={item.key}
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={ImageFallBackUrl}
draggable={false}
alt="user avator"
/>
</Box>
<Text
textShadow={'0px 1px 2px rgba(0, 0, 0, 0.4)'}
textAlign={'center'}
mt="8px"
color={'#FFFFFF'}
fontSize={'13px'}
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>
);
}