-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathindex.tsx
More file actions
171 lines (163 loc) · 4.81 KB
/
Copy pathindex.tsx
File metadata and controls
171 lines (163 loc) · 4.81 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 type { Resource } from "@App/app/repo/resource";
import type { Script } from "@App/app/repo/scripts";
import { ResourceClient } from "@App/app/service/service_worker/client";
import { message } from "@App/pages/store/global";
import { base64ToBlob } from "@App/pkg/utils/utils";
import { Button, Drawer, Input, Message, Popconfirm, Space, Table } from "@arco-design/web-react";
import type { ColumnProps } from "@arco-design/web-react/es/Table";
import { IconDelete, IconDownload, IconSearch } from "@arco-design/web-react/icon";
import React, { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
type ResourceListItem = {
key: string;
} & Resource;
const resourceClient = new ResourceClient(message);
const ScriptResource: React.FC<{
script?: Script;
visible: boolean;
onOk: () => void;
onCancel: () => void;
}> = ({ script, visible, onCancel, onOk }) => {
const [data, setData] = useState<ResourceListItem[]>([]);
const { t } = useTranslation();
useEffect(() => {
if (!script) {
return () => {};
}
resourceClient.getScriptResources(script).then((res) => {
const arr: ResourceListItem[] = [];
for (const key of Object.keys(res)) {
// @ts-ignore
const item: ResourceListItem = res[key];
item.key = key;
arr.push(item);
}
setData(arr);
});
return () => {};
}, [script]);
const columns: ColumnProps[] = [
{
title: t("key"),
dataIndex: "key",
key: "key",
filterIcon: <IconSearch />,
filterDropdown: ({ filterKeys, setFilterKeys, confirm }: any) => {
return (
<div className="arco-table-custom-filter">
<Input.Search
searchButton
autoFocus
placeholder={t("enter_key")!}
value={filterKeys[0] || ""}
onChange={(value) => {
setFilterKeys(value ? [value] : []);
}}
onSearch={() => {
confirm();
}}
/>
</div>
);
},
onFilter: (value, row) => !value || row.key.includes(value),
},
{
title: t("type"),
dataIndex: "contentType",
width: 140,
key: "type",
render(col, res: Resource) {
return `${res.type}/${col}`;
},
},
{
title: t("action"),
render(_col, value: Resource, index) {
return (
<Space>
<Button
type="text"
icon={<IconDownload />}
onClick={() => {
const url = URL.createObjectURL(base64ToBlob(value.base64));
setTimeout(() => {
URL.revokeObjectURL(url);
}, 60 * 1000);
const filename = value.url.split("/").pop();
chrome.downloads.download({
url,
saveAs: true,
filename,
});
}}
/>
<Popconfirm
focusLock
title={t("confirm_delete_resource")}
onOk={() => {
Message.info({
content: t("deleting"),
});
resourceClient
.deleteResource(value.url)
.then(() => {
Message.info({
content: t("delete_success"),
});
setData(data.filter((_, i) => i !== index));
})
.catch((e) => {
Message.error({
content: t("delete_failed") + ": " + e.message,
});
});
}}
>
<Button type="text" iconOnly icon={<IconDelete />} />
</Popconfirm>
</Space>
);
},
},
];
return (
<Drawer
width={600}
title={
<span>
{script?.name} {t("script_resource")}
</span>
}
visible={visible}
onOk={onOk}
onCancel={onCancel}
>
<Space className="w-full" direction="vertical">
<Space className="!flex justify-end">
<Popconfirm
focusLock
title={t("confirm_clear_resource")}
onOk={() => {
setData((prev) => {
prev.forEach((v) => {
resourceClient.deleteResource(v.url);
});
Message.info({
content: t("clear_success"),
});
return [];
});
}}
>
<Button type="primary" status="warning">
{t("clear")}
</Button>
</Popconfirm>
</Space>
<Table columns={columns} data={data} rowKey="id" />
</Space>
</Drawer>
);
};
export default ScriptResource;