feat: Synchronize environment-provided subscription sources with user settings.

This commit is contained in:
kuekhaoyang
2025-12-25 16:57:15 +08:00
parent 7c1f1db046
commit 95cee83bf4
3 changed files with 46 additions and 6 deletions
+2
View File
@@ -8,10 +8,12 @@ import { NextRequest, NextResponse } from 'next/server';
export const runtime = 'edge';
const ACCESS_PASSWORD = process.env.ACCESS_PASSWORD || '';
const SUBSCRIPTION_SOURCES = process.env.SUBSCRIPTION_SOURCES || process.env.NEXT_PUBLIC_SUBSCRIPTION_SOURCES || '';
export async function GET() {
return NextResponse.json({
hasEnvPassword: ACCESS_PASSWORD.length > 0,
subscriptionSources: SUBSCRIPTION_SOURCES,
});
}
+5
View File
@@ -25,6 +25,11 @@ export function PasswordGate({ children }: { children: React.ReactNode }) {
const res = await fetch('/api/config');
const data = await res.json();
setHasEnvPassword(data.hasEnvPassword);
// Sync subscription sources if provided by environment
if (data.subscriptionSources) {
settingsStore.syncEnvSubscriptions(data.subscriptionSources);
}
} catch {
// Silently fail - env password not available
}
+39 -6
View File
@@ -44,12 +44,8 @@ export const getDefaultAdultSources = (): VideoSource[] => ADULT_SOURCES;
function getEnvSubscriptions(): SourceSubscription[] {
if (typeof process === 'undefined' || !process.env.NEXT_PUBLIC_SUBSCRIPTION_SOURCES) {
return [];
}
const envValue = process.env.NEXT_PUBLIC_SUBSCRIPTION_SOURCES.trim();
function getEnvSubscriptions(customValue?: string): SourceSubscription[] {
const envValue = (customValue || process.env.SUBSCRIPTION_SOURCES || process.env.NEXT_PUBLIC_SUBSCRIPTION_SOURCES || '').trim();
if (!envValue) return [];
// 1. Try JSON
@@ -231,6 +227,43 @@ export const settingsStore = {
return importSettings(jsonString, (s) => this.saveSettings(s), this.getSettings());
},
syncEnvSubscriptions(rawEnvValue: string): void {
if (typeof window === 'undefined') return;
const currentSettings = this.getSettings();
const envSubs = getEnvSubscriptions(rawEnvValue);
if (envSubs.length === 0) return;
const mergedSubscriptions = [...currentSettings.subscriptions];
let changed = false;
envSubs.forEach(envSub => {
const existingIndex = mergedSubscriptions.findIndex(s => s.url === envSub.url);
if (existingIndex > -1) {
// Only update if something meaningful changed to avoid unnecessary re-renders
if (mergedSubscriptions[existingIndex].name !== envSub.name) {
mergedSubscriptions[existingIndex] = {
...mergedSubscriptions[existingIndex],
name: envSub.name,
autoRefresh: true
};
changed = true;
}
} else {
mergedSubscriptions.push(envSub);
changed = true;
}
});
if (changed) {
this.saveSettings({
...currentSettings,
subscriptions: mergedSubscriptions
});
}
},
resetToDefaults(): void {
if (typeof window !== 'undefined') {
localStorage.removeItem(SETTINGS_KEY);