139 lines
5.0 KiB
Python
139 lines
5.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Refactor YtAssignPanel busy → per-action pending (UI only)."""
|
|
from pathlib import Path
|
|
import re
|
|
|
|
path = Path(r"c:\Users\admin\Desktop\douyinWEB\frontend\src\components\YtAssignPanel.tsx")
|
|
text = path.read_text(encoding="utf-8")
|
|
|
|
old_state = " const [busy, setBusy] = useState(false);"
|
|
new_state = """ /** Per-action pending — never freeze the whole panel on one toggle. */
|
|
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) => {
|
|
if (p[key]) return p;
|
|
return { ...p, [key]: true };
|
|
});
|
|
try {
|
|
await fn();
|
|
} finally {
|
|
setPending((p) => {
|
|
const n = { ...p };
|
|
delete n[key];
|
|
return n;
|
|
});
|
|
}
|
|
}, []);"""
|
|
if old_state not in text:
|
|
raise SystemExit("state not found")
|
|
text = text.replace(old_state, new_state, 1)
|
|
|
|
old_load = """ const loadGroups = useCallback(async () => {
|
|
setBusy(true);
|
|
try {
|
|
const [data] = await Promise.all([
|
|
fetchJson("/api/youtube/assign/groups"),
|
|
loadSchedule(),
|
|
]);
|
|
const list: AssignGroup[] = data.groups || [];
|
|
setGroups(list);
|
|
setGroupId((prev) => {
|
|
if (focusGroupId && list.some((g) => g.id === focusGroupId)) return focusGroupId;
|
|
if (prev && list.some((g) => g.id === prev)) return prev;
|
|
return list[0]?.id || "";
|
|
});
|
|
} catch (e) {
|
|
toast.push((e as Error).message, "err");
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}, [toast, loadSchedule, focusGroupId]);"""
|
|
|
|
new_load = """ const loadGroups = useCallback(async () => {
|
|
try {
|
|
const [data] = await Promise.all([
|
|
fetchJson("/api/youtube/assign/groups"),
|
|
loadSchedule(),
|
|
]);
|
|
const list: AssignGroup[] = data.groups || [];
|
|
setGroups(list);
|
|
setGroupId((prev) => {
|
|
if (focusGroupId && list.some((g) => g.id === focusGroupId)) return focusGroupId;
|
|
if (prev && list.some((g) => g.id === prev)) return prev;
|
|
return list[0]?.id || "";
|
|
});
|
|
} catch (e) {
|
|
toast.push((e as Error).message, "err");
|
|
}
|
|
}, [toast, loadSchedule, focusGroupId]);"""
|
|
if old_load not in text:
|
|
raise SystemExit("loadGroups not found")
|
|
text = text.replace(old_load, new_load, 1)
|
|
|
|
# Wrap remaining setBusy(true) ... finally setBusy(false) blocks by converting to withPending
|
|
# Simpler: replace setBusy(true) with begin via unique auto keys is hard.
|
|
# Instead strip setBusy calls and use withPending only for key handlers; remove disabled={busy}.
|
|
|
|
# refreshAllAvatars
|
|
text2 = text
|
|
text2 = text2.replace(" setBusy(true);\n try {\n const r = await fetchJson(\"/api/youtube/manage/profiles/refresh",
|
|
" await withPending(\"avatars\", async () => {\n try {\n const r = await fetchJson(\"/api/youtube/manage/profiles/refresh", 1)
|
|
# fix avatars finally
|
|
old = """ } catch (e) {
|
|
toast.push((e as Error).message, "err");
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}, [loadAllProfiles, toast]);"""
|
|
new = """ } catch (e) {
|
|
toast.push((e as Error).message, "err");
|
|
}
|
|
});
|
|
}, [loadAllProfiles, toast, withPending]);"""
|
|
if old not in text2:
|
|
# maybe already partially changed
|
|
print("avatars block pattern missing, continue")
|
|
else:
|
|
text2 = text2.replace(old, new, 1)
|
|
|
|
# Remove all remaining setBusy(true)/setBusy(false) — actions stay async without global lock
|
|
text2 = text2.replace(" setBusy(true);\n", "")
|
|
text2 = text2.replace(" setBusy(true);\n", "")
|
|
# remove finally { setBusy(false); } blocks that only clear busy
|
|
text2 = re.sub(
|
|
r"\n(\s*)\} finally \{\n\1 setBusy\(false\);\n\1\}",
|
|
r"\n\1}",
|
|
text2,
|
|
)
|
|
text2 = re.sub(
|
|
r"\n(\s*)finally \{\n\1 setBusy\(false\);\n\1\}\n",
|
|
r"\n",
|
|
text2,
|
|
)
|
|
# leftover setBusy
|
|
if "setBusy" in text2:
|
|
print("leftover setBusy lines:")
|
|
for i, line in enumerate(text2.splitlines(), 1):
|
|
if "setBusy" in line or "busy" == line.strip() or "busy," in line:
|
|
if "setBusy" in line or "busy={busy}" in line or "disabled={busy}" in line or "Boolean(busy)" in line:
|
|
print(i, line.strip()[:120])
|
|
|
|
# Hero busy prop: only avatars pending
|
|
text2 = text2.replace(" busy={busy}", ' busy={isPending("avatars")}')
|
|
|
|
# disabled={busy} → remove (keep clickable). For create/save use specific pending later.
|
|
# disabled={busy || ...} → disabled={...}
|
|
text2 = text2.replace(" disabled={busy || ", " disabled={")
|
|
text2 = text2.replace("disabled={busy || ", "disabled={")
|
|
# plain disabled={busy} remove attribute (leave space cleanup)
|
|
text2 = re.sub(r"\s*disabled=\{busy\}", "", text2)
|
|
|
|
# time_slot armed / category: add scoped disable + optimistic (manual patch below if needed)
|
|
path.write_text(text2, encoding="utf-8")
|
|
print("written", path)
|
|
print("setBusy left", text2.count("setBusy"))
|
|
print("disabled busy left", text2.count("disabled={busy}"))
|
|
print("busy={busy} left", text2.count("busy={busy}"))
|
|
PY
|