s
This commit is contained in:
@@ -370,6 +370,7 @@ export function LiveYoutubeUnmannedView({
|
||||
const urlListDirtyRef = useRef(false);
|
||||
const singleUrlDraftRef = useRef(urlConfig);
|
||||
const proUrlDraftRef = useRef(proUrlDraft);
|
||||
const channelsRef = useRef<YoutubeProChannel[]>([]);
|
||||
const currentProcRef = useRef(currentProc);
|
||||
const urlRequestSeqRef = useRef(0);
|
||||
const ytIniRequestSeqRef = useRef(0);
|
||||
@@ -403,6 +404,27 @@ export function LiveYoutubeUnmannedView({
|
||||
proUrlDraftRef.current = proUrlDraft;
|
||||
}, [proUrlDraft]);
|
||||
|
||||
useEffect(() => {
|
||||
channelsRef.current = channels;
|
||||
}, [channels]);
|
||||
|
||||
const updateActiveChannelUrlLines = useCallback((channelId: string, nextUrlLines: string) => {
|
||||
if (!channelId) return;
|
||||
setChannels((prev) => {
|
||||
let changed = false;
|
||||
const next = prev.map((row) => {
|
||||
if (row.id !== channelId) return row;
|
||||
if ((row.urlLines ?? "") === nextUrlLines) return row;
|
||||
changed = true;
|
||||
return { ...row, urlLines: nextUrlLines };
|
||||
});
|
||||
if (!changed) return prev;
|
||||
channelsRef.current = next;
|
||||
saveYoutubeProChannels(next);
|
||||
return next;
|
||||
});
|
||||
}, []);
|
||||
|
||||
const pullUrlConfigFromServer = useCallback(async () => {
|
||||
const targetProc = currentProcRef.current.trim();
|
||||
if (!targetProc) return;
|
||||
@@ -418,24 +440,18 @@ export function LiveYoutubeUnmannedView({
|
||||
if (urlListDirtyRef.current) return;
|
||||
if (mode === "single") applySingleUrlDraft(c);
|
||||
else {
|
||||
const localDraft = channels.find((row) => row.id === activeCh)?.urlLines ?? "";
|
||||
const localDraft = channelsRef.current.find((row) => row.id === activeCh)?.urlLines ?? "";
|
||||
if (shouldPreserveLocalUrlDraft(localDraft, c)) {
|
||||
applyProUrlDraft(localDraft);
|
||||
return;
|
||||
}
|
||||
applyProUrlDraft(c);
|
||||
if (activeCh) {
|
||||
setChannels((prev) => {
|
||||
const next = prev.map((x) => (x.id === activeCh ? { ...x, urlLines: c } : x));
|
||||
saveYoutubeProChannels(next);
|
||||
return next;
|
||||
});
|
||||
}
|
||||
updateActiveChannelUrlLines(activeCh, c);
|
||||
}
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}, [activeCh, applyProUrlDraft, applySingleUrlDraft, channels, fetchJson, mode]);
|
||||
}, [activeCh, applyProUrlDraft, applySingleUrlDraft, fetchJson, mode, updateActiveChannelUrlLines]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!currentProc || !urlAutoRefresh) return;
|
||||
@@ -467,27 +483,21 @@ export function LiveYoutubeUnmannedView({
|
||||
}
|
||||
if (mode === "single") applySingleUrlDraft(c);
|
||||
else {
|
||||
const localDraft = channels.find((row) => row.id === activeCh)?.urlLines ?? "";
|
||||
const localDraft = channelsRef.current.find((row) => row.id === activeCh)?.urlLines ?? "";
|
||||
if (shouldPreserveLocalUrlDraft(localDraft, c)) {
|
||||
applyProUrlDraft(localDraft);
|
||||
setUrlFetchNote(t("服务器暂无已保存 URL,已保留本地草稿", "Server has no saved URLs, kept the local draft"));
|
||||
return;
|
||||
}
|
||||
applyProUrlDraft(c);
|
||||
if (activeCh) {
|
||||
setChannels((prev) => {
|
||||
const next = prev.map((x) => (x.id === activeCh ? { ...x, urlLines: c } : x));
|
||||
saveYoutubeProChannels(next);
|
||||
return next;
|
||||
});
|
||||
}
|
||||
updateActiveChannelUrlLines(activeCh, c);
|
||||
}
|
||||
setUrlFetchNote(t("已从服务器读取 URL_config", "Reloaded URL_config from server"));
|
||||
} catch (e) {
|
||||
if (requestId !== urlRequestSeqRef.current || targetProc !== currentProcRef.current.trim()) return;
|
||||
setUrlFetchNote((e as Error).message);
|
||||
} finally {
|
||||
if (requestId === urlRequestSeqRef.current && targetProc === currentProcRef.current.trim()) {
|
||||
if (requestId === urlRequestSeqRef.current) {
|
||||
setUrlReloading(false);
|
||||
}
|
||||
}
|
||||
@@ -512,6 +522,7 @@ export function LiveYoutubeUnmannedView({
|
||||
try {
|
||||
const remote = await fetchYoutubeProChannelsRemote(fetchJson);
|
||||
if (remote.fetched && remote.channels.length && !cancelled) {
|
||||
channelsRef.current = remote.channels;
|
||||
setChannels(remote.channels);
|
||||
saveYoutubeProChannels(remote.channels);
|
||||
return;
|
||||
@@ -537,6 +548,7 @@ export function LiveYoutubeUnmannedView({
|
||||
},
|
||||
];
|
||||
})();
|
||||
channelsRef.current = initial;
|
||||
setChannels(initial);
|
||||
if (!list.length) saveYoutubeProChannels(initial);
|
||||
})();
|
||||
@@ -574,6 +586,7 @@ export function LiveYoutubeUnmannedView({
|
||||
}, [currentProc, currentProcLooksYoutube, mode, preferredSingleProc, setCurrentProc]);
|
||||
|
||||
const applyChannelsLocal = useCallback((next: YoutubeProChannel[]) => {
|
||||
channelsRef.current = next;
|
||||
setChannels(next);
|
||||
saveYoutubeProChannels(next);
|
||||
}, []);
|
||||
@@ -786,7 +799,7 @@ export function LiveYoutubeUnmannedView({
|
||||
if (requestId !== ytIniRequestSeqRef.current || targetProc !== currentProcRef.current.trim()) return;
|
||||
setYtIniStatus((e as Error).message);
|
||||
} finally {
|
||||
if (requestId === ytIniRequestSeqRef.current && targetProc === currentProcRef.current.trim()) {
|
||||
if (requestId === ytIniRequestSeqRef.current) {
|
||||
setYtIniLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user