presenton/servers/nextjs/app/ConfigurationInitializer.tsx
sudipnext 2e7e31db8c feat: implement PDF/PPTX export functionality with dedicated routes and components
- Added new middleware to handle session authentication for presentation retrieval.
- Introduced new layout and page components for PDF export, ensuring no loading state is shown during headless rendering.
- Enhanced ConfigurationInitializer to manage loading state based on route.
- Updated DashboardApi to include credentials in requests for presentation data.
- Refactored hasValidLLMConfig function for cleaner validation logic.
2026-04-24 10:30:51 +05:45

175 lines
5.5 KiB
TypeScript

'use client';
import { useEffect, useState } from 'react';
import { setCanChangeKeys, setLLMConfig } from '@/store/slices/userConfig';
import { hasValidLLMConfig } from '@/utils/storeHelpers';
import { usePathname, useRouter } from 'next/navigation';
import { useDispatch } from 'react-redux';
import { checkIfSelectedOllamaModelIsPulled } from '@/utils/providerUtils';
import { LLMConfig } from '@/types/llm_config';
import { getApiUrl } from '@/utils/api';
export function ConfigurationInitializer({ children }: { children: React.ReactNode }) {
const dispatch = useDispatch();
const route = usePathname();
const [isLoading, setIsLoading] = useState(
() => !route?.startsWith("/pdf-maker")
);
const router = useRouter();
// Fetch user config state
useEffect(() => {
fetchUserConfigState();
}, []);
const setLoadingToFalseAfterNavigatingTo = (pathname: string) => {
const interval = setInterval(() => {
if (window.location.pathname === pathname) {
clearInterval(interval);
setIsLoading(false);
}
}, 500);
}
const fetchUserConfigState = async () => {
if (route.startsWith("/pdf-maker")) {
setIsLoading(false);
return;
}
setIsLoading(true);
let canChangeKeys = false;
try {
const res = await fetch('/api/can-change-keys');
const data = await res.json();
canChangeKeys = data.canChange ?? false;
} catch (e) {
console.error('Failed to fetch can-change-keys:', e);
canChangeKeys = false;
}
dispatch(setCanChangeKeys(canChangeKeys));
if (canChangeKeys) {
let llmConfig: LLMConfig = {};
try {
const res = await fetch('/api/user-config');
llmConfig = await res.json();
} catch (e) {
console.error('Failed to fetch user config:', e);
llmConfig = {};
}
if (!llmConfig.LLM) {
llmConfig.LLM = 'openai';
}
dispatch(setLLMConfig(llmConfig));
const isValid = hasValidLLMConfig(llmConfig);
if (route.startsWith('/pdf-maker')) {
setIsLoading(false);
return;
}
if (isValid) {
// Check if the selected Ollama model is pulled
if (llmConfig.LLM === 'ollama' && llmConfig.OLLAMA_MODEL) {
const isPulled = await checkIfSelectedOllamaModelIsPulled(llmConfig.OLLAMA_MODEL);
if (!isPulled) {
router.push('/');
setLoadingToFalseAfterNavigatingTo('/');
return;
}
}
if (llmConfig.LLM === 'custom') {
const isAvailable = await checkIfSelectedCustomModelIsAvailable(llmConfig);
if (!isAvailable) {
router.push('/');
setLoadingToFalseAfterNavigatingTo('/');
return;
}
}
if (route === '/') {
router.push('/upload');
setLoadingToFalseAfterNavigatingTo('/upload');
} else {
setIsLoading(false);
}
} else if (route !== '/') {
router.push('/');
setLoadingToFalseAfterNavigatingTo('/');
} else {
setIsLoading(false);
}
} else {
if (route === '/') {
router.push('/upload');
setLoadingToFalseAfterNavigatingTo('/upload');
} else {
setIsLoading(false);
}
}
}
const checkIfSelectedCustomModelIsAvailable = async (llmConfig: LLMConfig) => {
try {
const response = await fetch(getApiUrl('/api/v1/ppt/openai/models/available'), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
url: llmConfig.CUSTOM_LLM_URL,
api_key: llmConfig.CUSTOM_LLM_API_KEY,
}),
});
const data = await response.json();
return data.includes(llmConfig.CUSTOM_MODEL);
} catch (error) {
console.error('Error fetching custom models:', error);
return false;
}
}
if (isLoading) {
return (
<div className="min-h-screen bg-gradient-to-br from-[#E9E8F8] via-[#F5F4FF] to-[#E0DFF7] flex items-center justify-center p-4">
<div className="max-w-md w-full">
<div className="bg-white/80 backdrop-blur-sm rounded-2xl shadow-xl border border-white/20 p-8 text-center">
{/* Logo/Branding */}
<div className="mb-6">
<img
src="/Logo.png"
alt="PresentOn"
className="h-12 mx-auto mb-4 opacity-90"
/>
<div className="w-16 h-1 bg-gradient-to-r from-blue-500 to-purple-600 mx-auto rounded-full"></div>
</div>
{/* Loading Text */}
<div className="space-y-2">
<h3 className="text-lg font-semibold text-gray-800 font-inter">
Initializing Application
</h3>
<p className="text-sm text-gray-600 font-inter">
Loading configuration and checking model availability...
</p>
</div>
{/* Progress Indicator */}
<div className="mt-6">
<div className="flex space-x-1 justify-center">
<div className="w-2 h-2 bg-blue-500 rounded-full animate-pulse"></div>
<div className="w-2 h-2 bg-purple-500 rounded-full animate-pulse" style={{ animationDelay: '0.2s' }}></div>
<div className="w-2 h-2 bg-blue-500 rounded-full animate-pulse" style={{ animationDelay: '0.4s' }}></div>
</div>
</div>
</div>
</div>
</div>
);
}
return children;
}