Skip to content

Commit be818dc

Browse files
cyfung1031CodFrm
andauthored
🐛 修正按钮渲染逻辑,避免 render 阶段副作用,改用 JSX 条件渲染 & 修改 children (#1153)
* React Fix - No State Mutation * Omit Trash Code * Code Optimization * 改用 named slot * 英文修正:preNode -> headerContent * 修改为children模式 --------- Co-authored-by: 王一之 <yz@ggnb.top>
1 parent 5f42687 commit be818dc

File tree

4 files changed

+160
-168
lines changed

4 files changed

+160
-168
lines changed

src/pages/components/FileSystemParams/index.tsx

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@ import { useTranslation } from "react-i18next";
66
import { ClearNetDiskToken, netDiskTypeMap } from "@Packages/filesystem/auth";
77

88
const FileSystemParams: React.FC<{
9-
preNode: React.ReactNode | string;
9+
headerContent: React.ReactNode | string;
1010
onChangeFileSystemType: (type: FileSystemType) => void;
1111
onChangeFileSystemParams: (params: any) => void;
12-
actionButton: React.ReactNode[];
12+
children: React.ReactNode;
1313
fileSystemType: FileSystemType;
1414
fileSystemParams: any;
1515
}> = ({
1616
onChangeFileSystemType,
1717
onChangeFileSystemParams,
18-
preNode,
19-
actionButton,
18+
headerContent,
19+
children,
2020
fileSystemType,
2121
fileSystemParams,
2222
}) => {
2323
const fsParams = FileSystemFactory.params();
2424
const { t } = useTranslation();
25-
const actionButtons = [...actionButton];
2625

2726
const fileSystemList: {
2827
key: FileSystemType;
@@ -51,34 +50,12 @@ const FileSystemParams: React.FC<{
5150
];
5251

5352
const netDiskType = netDiskTypeMap[fileSystemType];
54-
55-
if (netDiskType) {
56-
const netDiskName = fileSystemList.find((item) => item.key === fileSystemType)?.name;
57-
58-
actionButtons.push(
59-
<Popconfirm
60-
key="netdisk-unbind"
61-
title={t("netdisk_unbind_confirm", { provider: netDiskName })}
62-
onOk={async () => {
63-
try {
64-
await ClearNetDiskToken(netDiskType);
65-
Message.success(t("netdisk_unbind_success", { provider: netDiskName })!);
66-
} catch (error) {
67-
Message.error(`${t("netdisk_unbind_error", { provider: netDiskName })}: ${String(error)}`);
68-
}
69-
}}
70-
>
71-
<Button type="primary" status="danger">
72-
{t("netdisk_unbind", { provider: netDiskName })}
73-
</Button>
74-
</Popconfirm>
75-
);
76-
}
53+
const netDiskName = netDiskType ? fileSystemList.find((item) => item.key === fileSystemType)?.name : null;
7754

7855
return (
7956
<>
8057
<Space>
81-
{preNode}
58+
{headerContent}
8259
<Select
8360
value={fileSystemType}
8461
style={{ width: 120 }}
@@ -92,7 +69,25 @@ const FileSystemParams: React.FC<{
9269
</Select.Option>
9370
))}
9471
</Select>
95-
{actionButtons.map((item) => item)}
72+
{children}
73+
{netDiskType && netDiskName && (
74+
<Popconfirm
75+
key="netdisk-unbind"
76+
title={t("netdisk_unbind_confirm", { provider: netDiskName })}
77+
onOk={async () => {
78+
try {
79+
await ClearNetDiskToken(netDiskType);
80+
Message.success(t("netdisk_unbind_success", { provider: netDiskName })!);
81+
} catch (error) {
82+
Message.error(`${t("netdisk_unbind_error", { provider: netDiskName })}: ${String(error)}`);
83+
}
84+
}}
85+
>
86+
<Button type="primary" status="danger">
87+
{t("netdisk_unbind", { provider: netDiskName })}
88+
</Button>
89+
</Popconfirm>
90+
)}
9691
</Space>
9792
<Space
9893
style={{

src/pages/components/RuntimeSetting/index.tsx

Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const RuntimeSetting: React.FC = () => {
7676
<CollapseItem header={t("storage_api")} name="storage">
7777
<Space direction="vertical">
7878
<FileSystemParams
79-
preNode={
79+
headerContent={
8080
<Typography.Text>
8181
{t("settings")}
8282
<Link
@@ -88,63 +88,6 @@ const RuntimeSetting: React.FC = () => {
8888
{t("use_file_system")}
8989
</Typography.Text>
9090
}
91-
actionButton={[
92-
<Button
93-
key="save"
94-
type="primary"
95-
onClick={async () => {
96-
try {
97-
await FileSystemFactory.create(fileSystemType, fileSystemParams);
98-
} catch (e) {
99-
Message.error(`${t("account_validation_failed")}: ${e}`);
100-
return;
101-
}
102-
const params = { ...fileSystemParams };
103-
params[fileSystemType] = fileSystemParams;
104-
systemConfig.setCatFileStorage({
105-
status: "success",
106-
filesystem: fileSystemType,
107-
params,
108-
});
109-
setStatus("success");
110-
Message.success(t("save_success")!);
111-
}}
112-
>
113-
{t("save")}
114-
</Button>,
115-
<Button
116-
key="reset"
117-
onClick={() => {
118-
systemConfig.setCatFileStorage({
119-
status: "unset",
120-
filesystem: "webdav",
121-
params: {},
122-
});
123-
setStatus("unset");
124-
setFilesystemParam({});
125-
setFilesystemType("webdav");
126-
}}
127-
type="primary"
128-
status="danger"
129-
>
130-
{t("reset")}
131-
</Button>,
132-
<Button
133-
key="open"
134-
type="secondary"
135-
onClick={async () => {
136-
try {
137-
let fs = await FileSystemFactory.create(fileSystemType, fileSystemParams);
138-
fs = await fs.openDir("ScriptCat/app");
139-
window.open(await fs.getDirUrl(), "_black");
140-
} catch (e) {
141-
Message.error(`${t("account_validation_failed")}: ${e}`);
142-
}
143-
}}
144-
>
145-
{t("open_directory")}
146-
</Button>,
147-
]}
14891
fileSystemType={fileSystemType}
14992
fileSystemParams={fileSystemParams}
15093
onChangeFileSystemType={(type) => {
@@ -153,7 +96,63 @@ const RuntimeSetting: React.FC = () => {
15396
onChangeFileSystemParams={(params) => {
15497
setFilesystemParam(params);
15598
}}
156-
/>
99+
>
100+
<Button
101+
key="save"
102+
type="primary"
103+
onClick={async () => {
104+
try {
105+
await FileSystemFactory.create(fileSystemType, fileSystemParams);
106+
} catch (e) {
107+
Message.error(`${t("account_validation_failed")}: ${e}`);
108+
return;
109+
}
110+
const params = { ...fileSystemParams };
111+
params[fileSystemType] = fileSystemParams;
112+
systemConfig.setCatFileStorage({
113+
status: "success",
114+
filesystem: fileSystemType,
115+
params,
116+
});
117+
setStatus("success");
118+
Message.success(t("save_success")!);
119+
}}
120+
>
121+
{t("save")}
122+
</Button>
123+
<Button
124+
key="reset"
125+
onClick={() => {
126+
systemConfig.setCatFileStorage({
127+
status: "unset",
128+
filesystem: "webdav",
129+
params: {},
130+
});
131+
setStatus("unset");
132+
setFilesystemParam({});
133+
setFilesystemType("webdav");
134+
}}
135+
type="primary"
136+
status="danger"
137+
>
138+
{t("reset")}
139+
</Button>
140+
<Button
141+
key="open"
142+
type="secondary"
143+
onClick={async () => {
144+
try {
145+
let fs = await FileSystemFactory.create(fileSystemType, fileSystemParams);
146+
fs = await fs.openDir("ScriptCat/app");
147+
window.open(await fs.getDirUrl(), "_black");
148+
} catch (e) {
149+
Message.error(`${t("account_validation_failed")}: ${e}`);
150+
}
151+
}}
152+
>
153+
{t("open_directory")}
154+
</Button>
155+
</FileSystemParams>
157156
{status === "unset" && <Typography.Text type="secondary">{t("not_set")}</Typography.Text>}
158157
{status === "success" && <Typography.Text type="success">{t("in_use")}</Typography.Text>}
159158
{status === "error" && <Typography.Text type="error">{t("storage_error")}</Typography.Text>}

src/pages/options/routes/Setting.tsx

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ function Setting() {
168168
</Checkbox>
169169
</Space>
170170
<FileSystemParams
171-
preNode={
171+
headerContent={
172172
<Checkbox
173173
checked={cloudSync.enable}
174174
onChange={(checked) => {
@@ -178,29 +178,6 @@ function Setting() {
178178
{t("enable_script_sync_to")}
179179
</Checkbox>
180180
}
181-
actionButton={[
182-
<Button
183-
key="save"
184-
type="primary"
185-
onClick={async () => {
186-
// Save to the configuration
187-
// Perform validation if enabled
188-
if (cloudSync.enable) {
189-
Message.info(t("cloud_sync_account_verification")!);
190-
try {
191-
await FileSystemFactory.create(cloudSync.filesystem, cloudSync.params[cloudSync.filesystem]);
192-
} catch (e) {
193-
Message.error(`${t("cloud_sync_verification_failed")}: ${JSON.stringify(Logger.E(e))}`);
194-
return;
195-
}
196-
}
197-
submitCloudSync();
198-
Message.success(t("save_success")!);
199-
}}
200-
>
201-
{t("save")}
202-
</Button>,
203-
]}
204181
fileSystemType={cloudSync.filesystem}
205182
fileSystemParams={cloudSync.params[cloudSync.filesystem] || {}}
206183
onChangeFileSystemType={(type) => {
@@ -212,7 +189,29 @@ function Setting() {
212189
params: { ...cloudSync.params, [cloudSync.filesystem]: params },
213190
}));
214191
}}
215-
/>
192+
>
193+
<Button
194+
key="save"
195+
type="primary"
196+
onClick={async () => {
197+
// Save to the configuration
198+
// Perform validation if enabled
199+
if (cloudSync.enable) {
200+
Message.info(t("cloud_sync_account_verification")!);
201+
try {
202+
await FileSystemFactory.create(cloudSync.filesystem, cloudSync.params[cloudSync.filesystem]);
203+
} catch (e) {
204+
Message.error(`${t("cloud_sync_verification_failed")}: ${JSON.stringify(Logger.E(e))}`);
205+
return;
206+
}
207+
}
208+
submitCloudSync();
209+
Message.success(t("save_success")!);
210+
}}
211+
>
212+
{t("save")}
213+
</Button>
214+
</FileSystemParams>
216215
</Space>
217216
</Card>
218217

0 commit comments

Comments
 (0)