Skip to content

Commit 86bdf36

Browse files
committed
fix: upgrade
1 parent 0eef84f commit 86bdf36

8 files changed

Lines changed: 61 additions & 47 deletions

File tree

src/pages/background/core/rules.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ function remove(tableName: TABLE_NAMES, id: number): Promise<void> {
223223
updateCache(tableName);
224224
notify.other({ method: APIs.ON_EVENT, event: EVENTs.RULE_DELETE, table: tableName, id: Number(id) });
225225
emitter.emit(emitter.INNER_RULE_REMOVE, { table: tableName, id: Number(id) });
226+
getLocal().remove(`rule_switch_${tableName}-${id}`);
226227
// check common mark
227228
getLocal()
228229
.get('common_rule')

src/pages/background/upgrade.ts

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { getVirtualKey, isValidArray } from '@/share/core/utils';
77
import { getDatabase } from './core/db';
88
import { getAll, save, updateCache, waitLoad } from './core/rules';
99

10-
// Upgrade
1110
async function doUpgrade() {
1211
if (typeof localStorage !== 'undefined') {
1312
const downloadHistory = localStorage.getItem('dl_history');
@@ -24,7 +23,7 @@ async function doUpgrade() {
2423
version = 0;
2524
}
2625
if (version < 1) {
27-
// Upgrade group
26+
// Upgrade groups
2827
const rebindRuleWithGroup = (group) => {
2928
return new Promise((resolve) => {
3029
const cacheQueue: Array<Promise<void>> = [];
@@ -70,20 +69,17 @@ async function doUpgrade() {
7069
if (groups) {
7170
const g = JSON.parse(groups);
7271
localStorage.removeItem('groups');
73-
rebindRuleWithGroup(g);
72+
await rebindRuleWithGroup(g);
7473
} else {
75-
storage
76-
.getLocal()
77-
.get('groups')
78-
.then((r) => {
79-
if (r.groups !== undefined) {
80-
rebindRuleWithGroup(r.groups).then(() => storage.getLocal().remove('groups'));
81-
} else {
82-
const g = {};
83-
g[browser.i18n.getMessage('ungrouped')] = [];
84-
rebindRuleWithGroup(g);
85-
}
86-
});
74+
const r = await storage.getLocal().get('groups');
75+
if (r.groups !== undefined) {
76+
await rebindRuleWithGroup(r.groups);
77+
await storage.getLocal().remove('groups');
78+
} else {
79+
const g = {};
80+
g[browser.i18n.getMessage('ungrouped')] = [];
81+
await rebindRuleWithGroup(g);
82+
}
8783
}
8884
}
8985
}
@@ -120,27 +116,12 @@ async function doUpgrade() {
120116
});
121117
});
122118
await Promise.all(queue);
123-
tableToUpdate.forEach((table) => {
124-
updateCache(table);
125-
});
126-
// rule_switch_
119+
await Promise.all(tableToUpdate.map((table) => updateCache(table)));
127120
}
128121

129-
storage.getLocal().set({
122+
await storage.getLocal().set({
130123
version_mark: 2,
131124
});
132125
}
133126

134-
if (MANIFEST_VER === 'v3') {
135-
browser.runtime.onInstalled.addListener((details) => {
136-
if (IS_DEV) {
137-
console.log('chrome onInstalled', details);
138-
}
139-
if (details.reason !== 'install' && details.reason !== 'update') {
140-
return;
141-
}
142-
doUpgrade();
143-
});
144-
} else {
145-
doUpgrade();
146-
}
127+
doUpgrade();

src/pages/options/sections/rules/edit/form-content/exclude.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const Exclude = () => {
5757
<Form.Input
5858
label={t('match_regexp')}
5959
field="condition.excludeRegex"
60-
helpText={MANIFEST_VER === 'v3' ? <Text type="tertiary">{t('lite_not_supported')}</Text> : undefined}
60+
helpText={MANIFEST_VER === 'v3' ? <Text type="tertiary">{t('lite_not_support')}</Text> : undefined}
6161
/>
6262
)}
6363
{editExcludeType.includes('domain') && (

src/pages/options/sections/rules/edit/form-content/execution.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import {
2-
Form,
3-
useFormState,
4-
} from '@douyinfe/semi-ui';
1+
import { Form, useFormState } from '@douyinfe/semi-ui';
52
import * as React from 'react';
63
import { BoolRadioGroupField } from '@/pages/options/components/bool-radio';
74
import { RULE_TYPE } from '@/share/core/constant';
85
import { t } from '@/share/core/utils';
96
import HeaderField from '@/share/components/header-field';
7+
import usePref from '@/share/hooks/use-pref';
108
import { CodeEditorField } from '../code-editor';
119
import ENCODING_LIST from '../encoding';
1210
import { RuleInput } from '../utils';
1311

1412
const Execution = () => {
1513
const { values } = useFormState();
1614

15+
const [showCommonHeader] = usePref('show-common-header');
16+
1717
const { ruleType, isFunction } = values as RuleInput;
1818
const isHeaderSend = values.ruleType === 'modifySendHeader';
1919
const isHeaderReceive = values.ruleType === 'modifyReceiveHeader';
@@ -44,7 +44,11 @@ const Execution = () => {
4444
{/* Header modify */}
4545
{isHeader && !isFunction && (
4646
<Form.Slot label={t(isHeaderSend ? 'request_headers' : 'response_headers')}>
47-
<HeaderField field="editHeader" type={isHeaderSend ? 'request' : 'response'} />
47+
<HeaderField
48+
field="editHeader"
49+
// eslint-disable-next-line no-nested-ternary
50+
type={showCommonHeader ? (isHeaderSend ? 'request' : 'response') : undefined}
51+
/>
4852
</Form.Slot>
4953
)}
5054
{/* Custom function */}

src/pages/options/sections/rules/edit/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ const Edit = ({ visible, rule: ruleProp, onClose }: EditProps) => {
140140
}
141141
>
142142
<Form
143-
labelCol={{ fixedSpan: 6 }}
143+
labelCol={{ fixedSpan: 4 }}
144144
getFormApi={(api) => (formApi.current = api)}
145145
labelPosition="left"
146146
labelAlign="right"

src/share/core/prefs.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Prefs {
4949
});
5050
}
5151

52-
get<T = any>(key: string, defaultValue?: T): T | undefined {
52+
get<K extends keyof PrefValue>(key: K, defaultValue?: PrefValue[K]): PrefValue[K] {
5353
if (key in this.boundMethods) {
5454
if (key in this.boundWrappers) {
5555
return this.boundWrappers[key];
@@ -64,11 +64,7 @@ class Prefs {
6464
if (defaultValue !== undefined) {
6565
return defaultValue;
6666
}
67-
if (key in defaultPrefValue) {
68-
return defaultPrefValue[key];
69-
}
70-
console.warn(`No default preference for ${key}`);
71-
return defaultValue;
67+
return defaultPrefValue[key];
7268
}
7369

7470
getAll() {

src/share/core/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ export interface InitdRule extends Rule {
7373
}
7474

7575
export interface PrefValue {
76-
[key: string]: any;
7776
'disable-all': boolean;
7877
'manage-collapse-group': boolean; // Collapse groups
7978
'exclude-he': boolean; // rules take no effect on HE or not

src/share/hooks/use-pref.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { useCallback, useEffect, useState } from 'react';
2+
import { defaultPrefValue } from '../core/constant';
3+
import { PrefValue } from '../core/types';
4+
import { prefs } from '../core/prefs';
5+
import emitter from '../core/emitter';
6+
7+
const usePref = <K extends keyof PrefValue>(key: K) => {
8+
const [state, setState] = useState(defaultPrefValue[key]);
9+
10+
useEffect(() => {
11+
prefs.ready(() => {
12+
setState(prefs.get(key));
13+
});
14+
const handler = (k: keyof PrefValue, val: any) => {
15+
if (key === k) {
16+
setState(val);
17+
}
18+
};
19+
emitter.on(emitter.EVENT_PREFS_UPDATE, handler);
20+
return () => {
21+
emitter.off(emitter.EVENT_PREFS_UPDATE, handler);
22+
};
23+
}, []);
24+
25+
const set = useCallback((value: PrefValue[K]) => {
26+
prefs.set(key, value);
27+
setState(value);
28+
}, []);
29+
30+
return [state, set];
31+
};
32+
33+
export default usePref;

0 commit comments

Comments
 (0)