Skip to content

Commit 1134586

Browse files
authored
✨ 在popup显示脚本数量 (#973)
1 parent dd80386 commit 1134586

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

src/pages/popup/App.tsx

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,40 @@ function App() {
5959
const { t } = useTranslation();
6060
const pageTabIdRef = useRef(0);
6161

62+
// 只随 script 数量和启动状态而改变的state
63+
const normalEnables = useMemo(() => {
64+
// 返回字串让 React 比对 state 有否改动
65+
return scriptList.map((script) => (script.enable ? 1 : 0)).join(",");
66+
}, [scriptList]);
67+
68+
// 只随 script 数量和启动状态而改变的state
69+
const backEnables = useMemo(() => {
70+
// 返回字串让 React 比对 state 有否改动
71+
return backScriptList.map((script) => (script.enable ? 1 : 0)).join(",");
72+
}, [backScriptList]);
73+
74+
const normalScriptCounts = useMemo(() => {
75+
// 拆回array
76+
const enables = normalEnables.split(",");
77+
// 计算已开启了的数量
78+
const running = enables.reduce((p, c) => p + (+c ? 1 : 0), 0);
79+
return {
80+
running,
81+
total: enables.length, // 总数
82+
};
83+
}, [normalEnables]);
84+
85+
const backScriptCounts = useMemo(() => {
86+
// 拆回array
87+
const enables = backEnables.split(",");
88+
// 计算已开启了的数量
89+
const running = enables.reduce((p, c) => p + (+c ? 1 : 0), 0);
90+
return {
91+
running,
92+
total: enables.length, // 总数
93+
};
94+
}, [backEnables]);
95+
6296
const urlHost = useMemo(() => {
6397
let url: URL | undefined;
6498
try {
@@ -443,7 +477,7 @@ function App() {
443477
style={{ maxWidth: 640, maxHeight: 500, overflow: "auto" }}
444478
>
445479
<CollapseItem
446-
header={t("current_page_scripts")}
480+
header={`${t("current_page_scripts")} (${normalScriptCounts.running}/${normalScriptCounts.total})`}
447481
name="script"
448482
style={{ padding: "0" }}
449483
contentStyle={{ padding: "0" }}
@@ -457,7 +491,7 @@ function App() {
457491
</CollapseItem>
458492

459493
<CollapseItem
460-
header={t("enabled_background_scripts")}
494+
header={`${t("enabled_background_scripts")} (${backScriptCounts.running}/${backScriptCounts.total})`}
461495
name="background"
462496
style={{
463497
padding: "0",

tests/pages/popup/App.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ describe("Popup App Component", () => {
167167
await waitFor(
168168
() => {
169169
// 检查是否存在折叠面板结构
170-
expect(screen.getByText("current_page_scripts")).toBeInTheDocument();
171-
expect(screen.getByText("enabled_background_scripts")).toBeInTheDocument();
170+
expect(screen.getByText("current_page_scripts (0/1)")).toBeInTheDocument();
171+
expect(screen.getByText("enabled_background_scripts (0/1)")).toBeInTheDocument();
172172
},
173173
{ timeout: 3000 }
174174
);
@@ -189,8 +189,8 @@ describe("Popup App Component", () => {
189189
await waitFor(
190190
() => {
191191
expect(screen.getByText("ScriptCat")).toBeInTheDocument();
192-
expect(screen.getByText("current_page_scripts")).toBeInTheDocument();
193-
expect(screen.getByText("enabled_background_scripts")).toBeInTheDocument();
192+
expect(screen.getByText("current_page_scripts (0/1)")).toBeInTheDocument();
193+
expect(screen.getByText("enabled_background_scripts (0/1)")).toBeInTheDocument();
194194
expect(screen.getByText("v" + ExtVersion)).toBeInTheDocument();
195195
},
196196
{ timeout: 3000 }

0 commit comments

Comments
 (0)