w-funnel/src/app/[funnelId]/page.tsx
2025-09-28 17:30:32 +02:00

77 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { notFound, redirect } from "next/navigation";
import {
listBakedFunnelIds,
peekBakedFunnelDefinition,
} from "@/lib/funnel/loadFunnelDefinition";
import type { FunnelDefinition } from "@/lib/funnel/types";
import { IS_FULL_SYSTEM_BUILD } from "@/lib/runtime/buildVariant";
// Функция для загрузки воронки из базы данных
async function loadFunnelFromDatabase(funnelId: string): Promise<FunnelDefinition | null> {
if (!IS_FULL_SYSTEM_BUILD) {
return null;
}
try {
// Пытаемся загрузить из базы данных через API
const response = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000'}/api/funnels/by-funnel-id/${funnelId}`, {
cache: 'no-store' // Не кешируем, т.к. воронки могут обновляться
});
if (response.ok) {
return await response.json();
}
return null;
} catch (error) {
console.error(`Failed to load funnel '${funnelId}' from database:`, error);
return null;
}
}
export const dynamic = "force-dynamic"; // Изменено на dynamic для поддержки базы данных
export function generateStaticParams() {
// Генерируем только для статических JSON файлов
return listBakedFunnelIds().map((funnelId) => ({ funnelId }));
}
interface FunnelRootPageProps {
params: Promise<{
funnelId: string;
}>;
}
export default async function FunnelRootPage({ params }: FunnelRootPageProps) {
const { funnelId } = await params;
let funnel: FunnelDefinition | null = null;
// Сначала пытаемся загрузить из базы данных
funnel = await loadFunnelFromDatabase(funnelId);
// Если не найдено в базе, пытаемся загрузить из JSON файлов
if (!funnel) {
try {
funnel = peekBakedFunnelDefinition(funnelId);
} catch (error) {
console.error(`Failed to load funnel '${funnelId}' from files:`, error);
}
}
// Если воронка не найдена ни в базе, ни в файлах
if (!funnel) {
notFound();
}
const firstScreenId =
funnel.meta.firstScreenId ?? funnel.screens.at(0)?.id ?? "";
if (!firstScreenId) {
redirect("/");
}
redirect(`/${funnel.meta.id}/${firstScreenId}`);
}