feat(prompt-sources): add custom script fetching functionality for prompt sources

This commit is contained in:
HouYunFei
2026-07-17 17:36:42 +08:00
parent 0e1edd1484
commit d4130bbb79
14 changed files with 851 additions and 229 deletions
@@ -0,0 +1,37 @@
import { useEffect } from "react";
import { useQueryClient } from "@tanstack/react-query";
import { refreshAllSources } from "@/services/api/prompts";
import { usePromptSourceStore } from "@/stores/use-prompt-source-store";
const CHECK_INTERVAL_MS = 60_000;
/** Periodically refetch all enabled prompt sources while the app is open, based on the global schedule. */
export function usePromptSourceScheduler() {
const queryClient = useQueryClient();
const intervalMinutes = usePromptSourceStore((state) => state.schedule.intervalMinutes);
useEffect(() => {
if (!intervalMinutes) return;
let running = false;
const tick = async () => {
if (running) return;
const { schedule, updateSchedule } = usePromptSourceStore.getState();
const last = schedule.lastFetchedAt ? new Date(schedule.lastFetchedAt).getTime() : 0;
if (Date.now() - last < intervalMinutes * 60_000) return;
running = true;
try {
await refreshAllSources();
updateSchedule("lastFetchedAt", new Date().toISOString());
await queryClient.invalidateQueries({ queryKey: ["prompts"] });
} catch {
// 拉取失败时静默重试,等待下一个检查周期。
} finally {
running = false;
}
};
void tick();
const timer = window.setInterval(() => void tick(), CHECK_INTERVAL_MS);
return () => window.clearInterval(timer);
}, [intervalMinutes, queryClient]);
}