bgRemoval.tsx
5.8 KB
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
import chatStyles from "@/app/components/chat.module.scss";
import homeStyles from "@/app/components/home.module.scss";
import styles from "./bgRemoval.module.scss";
import { WindowContent } from "@/app/components/home";
import { useMobileScreen } from "@/app/utils";
import { IconButton } from "../button";
import Locale from "@/app/locales";
import { Path } from "@/app/constant";
import { useNavigate } from "react-router-dom";
import clsx from "clsx";
import { getClientConfig } from "@/app/config/client";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { useAppConfig } from "@/app/store";
import { BgSiderBar } from "./bg-siderBar";
import { Button, Flex, Upload, message } from "antd";
import { UploadOutlined } from "@ant-design/icons";
import type { UploadFile } from "antd";
import ReturnIcon from "@/app/icons/return.svg";
import MinIcon from "@/app/icons/min.svg";
import MaxIcon from "@/app/icons/max.svg";
import SDIcon from "@/app/icons/sd.svg";
import LoadingIcon from "@/app/icons/three-dots.svg";
import BotIcon from "@/app/icons/bot.svg";
import CloseIcon from "@/app/icons/close.svg";
export function BgRemoval() {
const isMobileScreen = useMobileScreen();
const navigate = useNavigate();
const clientConfig = useMemo(() => getClientConfig(), []);
const showMaxIcon = !isMobileScreen && !clientConfig?.isApp;
const config = useAppConfig();
const scrollRef = useRef<HTMLDivElement>(null);
const isBgRemoval = location.pathname === Path.BgRemoval;
const [isLoading, setIsLoading] = useState(false);
const [fileData, setFileData] = useState<Blob | null>(null); //储存上传图片
const [previewUrl, setPreviewUrl] = useState<string | null>("");
// 关闭图片
const closePic = () => {
setFileData(null);
setPreviewUrl("");
};
//上传图片
const onChange = (info: any) => {
if (info.file.status === "uploading") {
return;
}
if (info.file.status === "done") {
// 获取上传的文件对象
const file = info.file as UploadFile;
const blob = file.originFileObj as Blob;
setFileData(blob);
} else if (info.file.status === "error") {
message.error(`${info.file.name} file upload failed.`);
}
};
// 修改 useEffect 监听 fileData
useEffect(() => {
if (fileData) {
// 创建 Blob 的临时 URL
const objectUrl = URL.createObjectURL(fileData);
setPreviewUrl(objectUrl);
// 组件卸载时释放内存
return () => URL.revokeObjectURL(objectUrl);
}
}, [fileData]);
return (
<>
<BgSiderBar
className={clsx({ [homeStyles["sidebar-show"]]: isBgRemoval })}
fileData={fileData}
setFileData={setFileData}
previewUrl={previewUrl}
setPreviewUrl={setPreviewUrl}
isLoading={isLoading}
setIsLoading={setIsLoading}
/>
<WindowContent>
<div className={chatStyles.chat} key={"1"}>
<div className="window-header" data-tauri-drag-region>
{isMobileScreen && (
<div className="window-actions">
<div className={"window-action-button"}>
<IconButton
icon={<ReturnIcon />}
bordered
title={Locale.Chat.Actions.ChatList}
onClick={() => navigate(Path.BgRemoval)}
/>
</div>
</div>
)}
<div
className={clsx(
"window-header-title",
chatStyles["chat-body-title"],
)}
>
<div className={`window-header-main-title`}>智能抠图</div>
</div>
<div className="window-actions">
{showMaxIcon && (
<div className="window-action-button">
<IconButton
aria={Locale.Chat.Actions.FullScreen}
icon={config.tightBorder ? <MinIcon /> : <MaxIcon />}
bordered
onClick={() => {
config.update(
(config) => (config.tightBorder = !config.tightBorder),
);
}}
/>
</div>
)}
{isMobileScreen && <SDIcon width={50} height={50} />}
</div>
</div>
<div className={chatStyles["chat-body"]} ref={scrollRef}>
<Flex
vertical
justify="center"
align="center"
gap="middle"
className={styles["panelFlex"]}
>
{isLoading ? (
<div className={clsx("no-dark", styles["loading-content"])}>
<BotIcon />
<LoadingIcon />
</div>
) : previewUrl ? (
<div className={styles["preview"]}>
<img
src={previewUrl}
alt="Preview"
className={styles["previewImage"]}
/>
<IconButton
icon={<CloseIcon />}
bordered
onClick={closePic}
className={styles["icon"]}
/>
</div>
) : (
<Upload
onChange={onChange}
beforeUpload={(file) => {
setFileData(file);
return false;
}}
showUploadList={false}
accept="image/*"
>
<Button icon={<UploadOutlined />} size="large">
上传图片
</Button>
</Upload>
)}
</Flex>
</div>
</div>
</WindowContent>
</>
);
}