Skip to content

Commit c46360a

Browse files
committed
fix: export tasks
1 parent 27c4551 commit c46360a

6 files changed

Lines changed: 80 additions & 65 deletions

File tree

docs/docs/en-US/guide/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,5 @@ interface Task {
169169
};
170170
// Custom function code
171171
code?: string;
172-
}
172+
}
173173
```

src/pages/options/sections/import-and-export/import-drawer/index.tsx

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ export default class ImportDrawer extends React.Component<
324324
dataSource={this.state.rules}
325325
pagination={false}
326326
loading={this.state.loading}
327+
size="small"
327328
columns={[
328329
{
329330
title: t('name'),
@@ -369,6 +370,7 @@ export default class ImportDrawer extends React.Component<
369370
render: (_v: any, item: ImportRuleInfo) => (
370371
<Select
371372
value={item.action}
373+
size="small"
372374
onChange={value =>
373375
this.handleActionChange(item, value as IMPORT_ACTION)
374376
}
@@ -386,10 +388,37 @@ export default class ImportDrawer extends React.Component<
386388
},
387389
]}
388390
/>
391+
<Space>
392+
<span>{t('save_to')}</span>
393+
<BoolRadioGroup
394+
onChange={this.handleRecommendChange}
395+
value={this.state.useRecommend}
396+
options={[
397+
{ label: t('suggested_group'), value: true },
398+
{
399+
label: (
400+
<span>
401+
<span>{this.state.group}</span>
402+
&nbsp;
403+
<Button
404+
className="select-group"
405+
size="small"
406+
onClick={this.handleSelectAll}
407+
>
408+
{t('choose')}
409+
</Button>
410+
</span>
411+
),
412+
value: false,
413+
},
414+
]}
415+
/>
416+
</Space>
389417
<Table
390418
dataSource={this.state.tasks}
391419
pagination={false}
392420
loading={this.state.loading}
421+
size="small"
393422
columns={[
394423
{
395424
title: t('name'),
@@ -400,6 +429,7 @@ export default class ImportDrawer extends React.Component<
400429
render: (_v: any, item: ImportTaskInfo) => (
401430
<Select
402431
value={item.action}
432+
size="small"
403433
onChange={value =>
404434
this.handleActionChange(item, value as IMPORT_ACTION)
405435
}
@@ -417,45 +447,6 @@ export default class ImportDrawer extends React.Component<
417447
},
418448
]}
419449
/>
420-
<div
421-
className={css`
422-
display: flex;
423-
flex-direction: row;
424-
align-items: center;
425-
margin-top: 8px;
426-
gap: 8px;
427-
.semi-radio-content {
428-
display: flex;
429-
flex-direction: row;
430-
align-items: center;
431-
}
432-
`}
433-
>
434-
<span>{t('save_to')}</span>
435-
<BoolRadioGroup
436-
onChange={this.handleRecommendChange}
437-
value={this.state.useRecommend}
438-
options={[
439-
{ label: t('suggested_group'), value: true },
440-
{
441-
label: (
442-
<span>
443-
<span>{this.state.group}</span>
444-
&nbsp;
445-
<Button
446-
className="select-group"
447-
size="small"
448-
onClick={this.handleSelectAll}
449-
>
450-
{t('choose')}
451-
</Button>
452-
</span>
453-
),
454-
value: false,
455-
},
456-
]}
457-
/>
458-
</div>
459450
</SideSheet>
460451
);
461452
}

src/pages/options/sections/import-and-export/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ export default class ImportAndExport extends React.Component<{}, IEState> {
100100

101101
async handleExport() {
102102
const result = await Api.getAllRules();
103-
batchShare(Object.values(result).flat());
103+
const tasks = await Api.getTasks();
104+
batchShare(Object.values(result).flat(), tasks);
104105
}
105106

106107
handleOpenThird() {

src/pages/options/sections/rules/utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TABLE_NAMES_ARR } from '@/share/core/constant';
2-
import type { Rule } from '@/share/core/types';
2+
import type { Rule, Task } from '@/share/core/types';
33
import { getTableName } from '@/share/core/utils';
44
import Api from '@/share/pages/api';
55
import file from '@/share/pages/file';
@@ -19,12 +19,16 @@ export function save(rule: Rule) {
1919
return Api.saveRule(rule);
2020
}
2121

22-
export async function batchShare(rules: Rule[]) {
22+
export async function batchShare(rules: Rule[], tasks?: Task[]) {
2323
const result: any = {};
2424
TABLE_NAMES_ARR.forEach(tb => {
2525
result[tb] = [];
2626
});
2727
rules.forEach(e => result[getTableName(e.ruleType)].push(e));
28+
if (tasks) {
29+
result.tasks = {};
30+
tasks.forEach(x => (result.tasks[x.key] = x));
31+
}
2832
file.save(
2933
JSON.stringify(await createExport(result), null, '\t'),
3034
getExportName(),

src/pages/options/utils/index.ts

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import dayjs from 'dayjs';
2+
import { TABLE_NAMES, TABLE_NAMES_ARR } from '@/share/core/constant';
23
import emitter from '@/share/core/emitter';
34
import { convertToBasicRule } from '@/share/core/rule-utils';
45
import { collectRulesUsedTasks } from '@/share/core/tasks';
@@ -10,35 +11,53 @@ export function getExportName(additional?: string) {
1011
return `HE_${date}${additional ? '_' + additional : ''}.json`;
1112
}
1213

13-
export async function createTaskExport(task: Task) {
14-
if (task.isFunction && task.code) {
15-
}
14+
export function createTaskExport(x: Task) {
15+
delete x.lastRun;
16+
delete x._func;
17+
return x;
1618
}
1719

18-
export async function createExport(arr: {
19-
[key: string]: Array<Rule | InitdRule>;
20-
}) {
20+
export async function createExport(
21+
arr: Partial<{
22+
[TABLE_NAMES.receiveHeader]: Array<Rule | InitdRule>;
23+
[TABLE_NAMES.receiveBody]: Array<Rule | InitdRule>;
24+
[TABLE_NAMES.request]: Array<Rule | InitdRule>;
25+
[TABLE_NAMES.sendHeader]: Array<Rule | InitdRule>;
26+
tasks: Record<string, Task>;
27+
}>,
28+
) {
2129
const result: any = {};
2230
const tasks = new Set<string>();
23-
Object.keys(arr).forEach(k => {
24-
result[k] = arr[k].map(e => convertToBasicRule(e));
25-
const t = collectRulesUsedTasks(arr[k]);
31+
32+
TABLE_NAMES_ARR.forEach(tb => {
33+
if (!arr[tb]) {
34+
return;
35+
}
36+
result[tb] = arr[tb].map(e => convertToBasicRule(e));
37+
const t = collectRulesUsedTasks(arr[tb]);
2638
t.forEach(e => tasks.add(e));
2739
});
40+
2841
// 一并导出任务
29-
if (tasks.size) {
42+
if (tasks.size || arr.tasks) {
3043
const allTasks = await Api.getTasks();
31-
result.tasks = Object.fromEntries(
32-
(
33-
Array.from(tasks)
34-
.map(x => allTasks.find(y => y.key === x))
35-
.filter(Boolean) as Task[]
36-
).map(x => {
37-
delete x.lastRun;
38-
delete x._func;
39-
return [x.key, x];
40-
}),
41-
);
44+
if (!result.tasks) {
45+
result.tasks = {};
46+
}
47+
if (arr.tasks) {
48+
Object.keys(arr.tasks).forEach(x => {
49+
result.tasks[x] = createTaskExport(arr.tasks![x]);
50+
});
51+
}
52+
const tasksArr = Array.from(tasks)
53+
.map(x => allTasks.find(y => y.key === x))
54+
.filter(Boolean) as Task[];
55+
tasksArr.forEach(x => {
56+
if (result.tasks[x.key]) {
57+
return;
58+
}
59+
result.tasks[x.key] = createTaskExport(x);
60+
});
4261
}
4362
return result;
4463
}

src/share/core/tasks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function collectTaskUsage(text: string) {
1010
if (!matches) {
1111
return [];
1212
}
13-
return matches.map(x => x.substring(7, x.indexOf('.')));
13+
return matches.map(x => x.substring(7, x.indexOf('.', 7)));
1414
}
1515

1616
export function collectTaskFromCode(code: string) {

0 commit comments

Comments
 (0)