70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Shorts page: remove global busy lock; keep same API calls."""
|
|
from pathlib import Path
|
|
import re
|
|
|
|
path = Path(r"c:\Users\admin\Desktop\douyinWEB\frontend\src\app\shorts\page.tsx")
|
|
text = path.read_text(encoding="utf-8")
|
|
|
|
# Replace busy state with pending map
|
|
old = " const [busy, setBusy] = useState(false);"
|
|
new = """ const [pending, setPending] = useState<Record<string, true>>({});
|
|
const isPending = useCallback((key: string) => Boolean(pending[key]), [pending]);
|
|
const withPending = useCallback(async (key: string, fn: () => Promise<void>) => {
|
|
setPending((p) => (p[key] ? p : { ...p, [key]: true }));
|
|
try {
|
|
await fn();
|
|
} finally {
|
|
setPending((p) => {
|
|
const n = { ...p };
|
|
delete n[key];
|
|
return n;
|
|
});
|
|
}
|
|
}, []);"""
|
|
if old not in text:
|
|
raise SystemExit("busy state not found")
|
|
text = text.replace(old, new, 1)
|
|
|
|
# Ensure useCallback imported
|
|
if "useCallback" not in text.split("from \"react\"")[0]:
|
|
text = text.replace(
|
|
"import { useEffect, useMemo, useRef, useState",
|
|
"import { useCallback, useEffect, useMemo, useRef, useState",
|
|
1,
|
|
)
|
|
text = text.replace(
|
|
"import {\n useEffect,\n useMemo,\n useRef,\n useState,",
|
|
"import {\n useCallback,\n useEffect,\n useMemo,\n useRef,\n useState,",
|
|
1,
|
|
)
|
|
|
|
# Remove setBusy(true/false) lines
|
|
text = text.replace(" setBusy(true);\n", "")
|
|
text = text.replace(" setBusy(true);\n", "")
|
|
text = text.replace(" setBusy(true);\n", "")
|
|
text = re.sub(
|
|
r"\n(\s*)\} finally \{\n\1 setBusy\(false\);\n\1\}",
|
|
r"\n\1}",
|
|
text,
|
|
)
|
|
text = re.sub(
|
|
r"\n(\s*)finally \{\n\1 setBusy\(false\);\n\1\}\n",
|
|
"\n",
|
|
text,
|
|
)
|
|
text = re.sub(r"\n(\s*)setBusy\(false\);\n", "\n", text)
|
|
|
|
# disabled={busy || X} -> disabled={X}
|
|
text = text.replace("disabled={busy || ", "disabled={")
|
|
text = text.replace("disabled={busy||", "disabled={")
|
|
# disabled={busy} remove
|
|
text = re.sub(r"\s*disabled=\{busy\}", "", text)
|
|
|
|
path.write_text(text, encoding="utf-8")
|
|
print("setBusy", text.count("setBusy"), "disabled busy", text.count("disabled={busy}"))
|
|
# check useCallback import
|
|
import_line = [l for l in text.splitlines() if 'from "react"' in l or "from 'react'" in l][:3]
|
|
print("react imports:", import_line)
|
|
PY
|