|
| 1 | +import React, { useState } from "react"; |
| 2 | +import { |
| 3 | + Button, |
| 4 | + Card, |
| 5 | + Collapse, |
| 6 | + Link, |
| 7 | + Message, |
| 8 | + Space, |
| 9 | + Typography, |
| 10 | +} from "@arco-design/web-react"; |
| 11 | +import IoC from "@App/app/ioc"; |
| 12 | +import { SystemConfig } from "@App/pkg/config/config"; |
| 13 | +import FileSystemFactory, { FileSystemType } from "@Pkg/filesystem/factory"; |
| 14 | +import FileSystemParams from "../FileSystemParams"; |
| 15 | + |
| 16 | +const CollapseItem = Collapse.Item; |
| 17 | + |
| 18 | +const GMApiSetting: React.FC = () => { |
| 19 | + const systemConfig = IoC.instance(SystemConfig) as SystemConfig; |
| 20 | + const [status, setStatus] = useState(systemConfig.catFileStorage.status); |
| 21 | + const [fileSystemType, setFilesystemType] = useState<FileSystemType>( |
| 22 | + systemConfig.catFileStorage.filesystem |
| 23 | + ); |
| 24 | + const [fileSystemParams, setFilesystemParam] = useState<{ |
| 25 | + [key: string]: any; |
| 26 | + }>(systemConfig.catFileStorage.params[fileSystemType] || {}); |
| 27 | + |
| 28 | + return ( |
| 29 | + <Card title="GM Api" bordered={false}> |
| 30 | + <Collapse bordered={false} defaultActiveKey={["storage"]}> |
| 31 | + <CollapseItem header="Storage API" name="storage"> |
| 32 | + <Space direction="vertical"> |
| 33 | + <FileSystemParams |
| 34 | + preNode={ |
| 35 | + <Typography.Text> |
| 36 | + 设置 |
| 37 | + <Link |
| 38 | + target="_black" |
| 39 | + href="https://github.com/scriptscat/scriptcat/blob/main/example/cat_file_storage.js" |
| 40 | + > |
| 41 | + CAT_fileStorage |
| 42 | + </Link> |
| 43 | + 使用的文件系统 |
| 44 | + </Typography.Text> |
| 45 | + } |
| 46 | + actionButton={[ |
| 47 | + <Button |
| 48 | + key="save" |
| 49 | + type="primary" |
| 50 | + onClick={async () => { |
| 51 | + try { |
| 52 | + await FileSystemFactory.create( |
| 53 | + fileSystemType, |
| 54 | + fileSystemParams |
| 55 | + ); |
| 56 | + } catch (e) { |
| 57 | + Message.error(`账号信息验证失败: ${e}`); |
| 58 | + return; |
| 59 | + } |
| 60 | + const params = { ...systemConfig.catFileStorage.params }; |
| 61 | + params[fileSystemType] = fileSystemParams; |
| 62 | + systemConfig.catFileStorage = { |
| 63 | + status: "success", |
| 64 | + filesystem: fileSystemType, |
| 65 | + params, |
| 66 | + }; |
| 67 | + setStatus("success"); |
| 68 | + Message.success("保存成功"); |
| 69 | + }} |
| 70 | + > |
| 71 | + 保存 |
| 72 | + </Button>, |
| 73 | + <Button |
| 74 | + key="reset" |
| 75 | + onClick={() => { |
| 76 | + const config = systemConfig.catFileStorage; |
| 77 | + config.status = "unset"; |
| 78 | + systemConfig.catFileStorage = config; |
| 79 | + setStatus("unset"); |
| 80 | + }} |
| 81 | + type="primary" |
| 82 | + status="danger" |
| 83 | + > |
| 84 | + 重置 |
| 85 | + </Button>, |
| 86 | + <Button |
| 87 | + key="open" |
| 88 | + type="secondary" |
| 89 | + onClick={async () => { |
| 90 | + try { |
| 91 | + let fs = await FileSystemFactory.create( |
| 92 | + fileSystemType, |
| 93 | + fileSystemParams |
| 94 | + ); |
| 95 | + fs = await fs.openDir("ScriptCat/app"); |
| 96 | + window.open(await fs.getDirUrl(), "_black"); |
| 97 | + } catch (e) { |
| 98 | + Message.error(`账号信息验证失败: ${e}`); |
| 99 | + } |
| 100 | + }} |
| 101 | + > |
| 102 | + 打开目录 |
| 103 | + </Button>, |
| 104 | + ]} |
| 105 | + fileSystemType={fileSystemType} |
| 106 | + fileSystemParams={fileSystemParams} |
| 107 | + onChangeFileSystemType={(type) => { |
| 108 | + setFilesystemType(type); |
| 109 | + }} |
| 110 | + onChangeFileSystemParams={(params) => { |
| 111 | + setFilesystemParam(params); |
| 112 | + }} |
| 113 | + /> |
| 114 | + {status === "unset" && ( |
| 115 | + <Typography.Text type="secondary">未设置</Typography.Text> |
| 116 | + )} |
| 117 | + {status === "success" && ( |
| 118 | + <Typography.Text type="success">使用中</Typography.Text> |
| 119 | + )} |
| 120 | + {status === "error" && ( |
| 121 | + <Typography.Text type="error">储存错误</Typography.Text> |
| 122 | + )} |
| 123 | + </Space> |
| 124 | + </CollapseItem> |
| 125 | + </Collapse> |
| 126 | + </Card> |
| 127 | + ); |
| 128 | +}; |
| 129 | + |
| 130 | +export default GMApiSetting; |
0 commit comments