-
Notifications
You must be signed in to change notification settings - Fork 556
/
Copy pathBrowserView.tsx
242 lines (221 loc) · 6.37 KB
/
BrowserView.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { DeleteIcon } from "@/assets/svg";
import DownloadForm, { DownloadFormRef } from "@/components/DownloadForm";
import { IconButton } from "@/components/IconButton";
import { Button } from "@/components/ui/button";
import WebView from "@/components/WebView";
import useElectron from "@/hooks/electron";
import {
addSource,
BrowserStatus,
deleteSource,
PageMode,
selectBrowserStore,
setBrowserStore,
} from "@/store";
import { generateUrl, randomName } from "@/utils";
import { useMemoizedFn } from "ahooks";
import { Empty, Space, Spin, message, Splitter } from "antd";
import React, { useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { useDispatch, useSelector } from "react-redux";
export function BrowserView() {
const {
webviewLoadURL,
addIpcListener,
removeIpcListener,
webviewGoHome,
downloadNow,
addDownloadItem,
showDownloadDialog: ipcShowDownloadDialog,
} = useElectron();
const downloadForm = useRef<DownloadFormRef>(null);
const store = useSelector(selectBrowserStore);
const { t } = useTranslation();
const [placeHolder, setPlaceHolder] = useState<string>("");
const dispatch = useDispatch();
const [messageApi, contextHolder] = message.useMessage();
useEffect(() => {
const onShowDownloadDialog = async (
e: unknown,
data: DownloadItem[],
image: string,
) => {
if (image) {
setPlaceHolder(image);
}
const item = data[0];
downloadForm.current.openModal({
batch: false,
type: item.type,
url: item.url,
name: item.name,
headers: item.headers,
});
};
const onWebviewLinkMessage = async (e: unknown, data: any) => {
dispatch(addSource(data));
};
addIpcListener("show-download-dialog", onShowDownloadDialog);
addIpcListener("webview-link-message", onWebviewLinkMessage);
return () => {
removeIpcListener("show-download-dialog", onShowDownloadDialog);
removeIpcListener("webview-link-message", onWebviewLinkMessage);
};
}, [store.status]);
const onClickGoHome = async () => {
await webviewGoHome();
dispatch(
setBrowserStore({
url: "",
title: "",
mode: PageMode.Default,
}),
);
};
const confirmDownload = async (now?: boolean) => {
try {
const { name, url, headers, type, folder } =
downloadForm.current.getFieldsValue();
const item = {
name: name || randomName(),
url,
headers,
type,
folder,
};
if (now) {
await downloadNow(item);
} else {
await addDownloadItem(item);
}
return true;
} catch (e) {
messageApi.error((e as any).message);
return false;
}
};
const loadUrl = (url: string) => {
dispatch(
setBrowserStore({
url,
mode: PageMode.Browser,
status: BrowserStatus.Loading,
}),
);
webviewLoadURL(url);
};
const goto = () => {
const link = generateUrl(store.url);
loadUrl(link);
};
const handleFormVisibleChange = useMemoizedFn((visible: boolean) => {
if (!visible) {
setPlaceHolder("");
}
});
const renderContent = () => {
// Loaded state
if (store.status === BrowserStatus.Loading) {
return (
<div className="flex h-full w-full flex-row items-center justify-center">
<Spin />
</div>
);
}
// Modal box
if (placeHolder) {
return <img src={placeHolder} className="h-full w-full" />;
}
// Load failure
if (store.status === BrowserStatus.Failed) {
return (
<div className="flex h-full w-full flex-row items-center justify-center">
<Empty
description={`${store.errMsg || t("loadFailed")} (${store.errCode})`}
>
<Space>
<Button onClick={onClickGoHome}>{t("backToHome")}</Button>
<Button onClick={goto}>{t("refresh")}</Button>
</Space>
</Empty>
</div>
);
}
// Load successfully
if (store.status === BrowserStatus.Loaded) {
return <WebView className="h-full w-full flex-1" />;
}
return null;
};
const renderSidePanel = () => {
if (store.sources.length === 0) {
return null;
}
return (
<div className="flex h-full flex-col gap-3 overflow-y-auto bg-white p-3 dark:bg-[#1F2024]">
{store.sources.map((item, index) => {
return (
<div
className="flex flex-col gap-2 rounded-lg bg-[#FAFCFF] p-2 dark:bg-[#27292F]"
key={index}
>
<span
className="line-clamp-2 cursor-default break-words text-sm text-[#343434] dark:text-[#B4B4B4]"
title={item.name}
>
{item.name}
</span>
<span
className="line-clamp-2 cursor-default break-words text-xs dark:text-[#515257]"
title={item.url}
>
{item.url}
</span>
<div className="flex flex-row items-center justify-between gap-3">
<div>
<IconButton
icon={<DeleteIcon />}
onClick={() => {
dispatch(deleteSource(item.url));
}}
/>
</div>
<Button
size="sm"
onClick={() => {
ipcShowDownloadDialog([item]);
}}
>
{t("addToDownloadList")}
</Button>
</div>
</div>
);
})}
</div>
);
};
return (
<div className="flex flex-1 flex-col overflow-hidden">
<Splitter className="flex h-full flex-1 gap-2">
<Splitter.Panel>{renderContent()}</Splitter.Panel>
{store.sources.length && (
<Splitter.Panel defaultSize={240} min="20%" max="70%">
{renderSidePanel()}
</Splitter.Panel>
)}
</Splitter>
<DownloadForm
id="browser"
isEdit
usePrevData
ref={downloadForm}
onDownloadNow={() => confirmDownload(true)}
onAddToList={() => confirmDownload()}
destroyOnClose
onFormVisibleChange={handleFormVisibleChange}
/>
{contextHolder}
</div>
);
}