From 609fba7571b3c7b411b94ec518102eec8a5ca7e5 Mon Sep 17 00:00:00 2001 From: kuekhaoyang Date: Thu, 25 Dec 2025 12:11:46 +0800 Subject: [PATCH] feat: Implement automatic subscription source syncing, initialize subscriptions from environment, and integrate into home page. --- lib/hooks/useHomePage.ts | 2 + lib/hooks/useSubscriptionSync.ts | 63 ++++++++++++++++++++++++++++++++ lib/store/settings-store.ts | 4 +- 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 lib/hooks/useSubscriptionSync.ts diff --git a/lib/hooks/useHomePage.ts b/lib/hooks/useHomePage.ts index 2769a55..0189d59 100644 --- a/lib/hooks/useHomePage.ts +++ b/lib/hooks/useHomePage.ts @@ -2,9 +2,11 @@ import { useState, useRef, useEffect } from 'react'; import { useRouter, useSearchParams } from 'next/navigation'; import { useSearchCache } from '@/lib/hooks/useSearchCache'; import { useParallelSearch } from '@/lib/hooks/useParallelSearch'; +import { useSubscriptionSync } from '@/lib/hooks/useSubscriptionSync'; import { settingsStore } from '@/lib/store/settings-store'; export function useHomePage() { + useSubscriptionSync(); const router = useRouter(); const searchParams = useSearchParams(); const { loadFromCache, saveToCache } = useSearchCache(); diff --git a/lib/hooks/useSubscriptionSync.ts b/lib/hooks/useSubscriptionSync.ts new file mode 100644 index 0000000..483b069 --- /dev/null +++ b/lib/hooks/useSubscriptionSync.ts @@ -0,0 +1,63 @@ +import { useEffect, useRef } from 'react'; +import { settingsStore } from '@/lib/store/settings-store'; +import { fetchSourcesFromUrl, mergeSources } from '@/lib/utils/source-import-utils'; + +export function useSubscriptionSync() { + const hasSyncedRef = useRef(false); + + useEffect(() => { + if (hasSyncedRef.current) return; + hasSyncedRef.current = true; + + const sync = async () => { + const settings = settingsStore.getSettings(); + const subscriptions = settings.subscriptions.filter(s => s.autoRefresh !== false); + + if (subscriptions.length === 0) return; + + let anyChanged = false; + let currentSources = [...settings.sources]; + let currentAdultSources = [...settings.adultSources]; + let updatedSubscriptions = [...settings.subscriptions]; + + for (let i = 0; i < subscriptions.length; i++) { + const sub = subscriptions[i]; + try { + const result = await fetchSourcesFromUrl(sub.url); + + if (result.normalSources.length > 0) { + currentSources = mergeSources(currentSources, result.normalSources); + anyChanged = true; + } + + if (result.adultSources.length > 0) { + currentAdultSources = mergeSources(currentAdultSources, result.adultSources); + anyChanged = true; + } + + // Update timestamp + const subIdx = updatedSubscriptions.findIndex(s => s.id === sub.id); + if (subIdx !== -1) { + updatedSubscriptions[subIdx] = { + ...updatedSubscriptions[subIdx], + lastUpdated: Date.now() + }; + } + } catch (e) { + console.error(`Failed to sync subscription: ${sub.name}`, e); + } + } + + if (anyChanged) { + settingsStore.saveSettings({ + ...settings, + sources: currentSources, + adultSources: currentAdultSources, + subscriptions: updatedSubscriptions + }); + } + }; + + sync(); + }, []); +} diff --git a/lib/store/settings-store.ts b/lib/store/settings-store.ts index 17609f9..9f77af6 100644 --- a/lib/store/settings-store.ts +++ b/lib/store/settings-store.ts @@ -91,7 +91,7 @@ export const settingsStore = { return { sources: getDefaultSources(), adultSources: getDefaultAdultSources(), - subscriptions: [], + subscriptions: getEnvSubscriptions(), sortBy: 'default', searchHistory: true, watchHistory: true, @@ -111,7 +111,7 @@ export const settingsStore = { return { sources: getDefaultSources(), adultSources: getDefaultAdultSources(), - subscriptions: [], + subscriptions: getEnvSubscriptions(), sortBy: 'default', searchHistory: true, watchHistory: true,