From 47295572fe0b5212e0ef5c4f82d77dddfe272354 Mon Sep 17 00:00:00 2001 From: sudipnext Date: Thu, 26 Mar 2026 16:35:15 +0545 Subject: [PATCH] feat: enhance export functionality and error handling in presentation generation - Updated the export URL construction to include optional FastAPI URL from query parameters. - Improved error handling when applying themes during PDF export to prevent blocking rendering. - Added validation for theme font properties to ensure proper application. --- electron/app/ipc/export_handlers.ts | 6 +++- .../pdf-maker/PdfMakerPage.tsx | 8 ++++- electron/servers/nextjs/utils/api.ts | 29 ++++++++++++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/electron/app/ipc/export_handlers.ts b/electron/app/ipc/export_handlers.ts index da6910a3..d0691dda 100644 --- a/electron/app/ipc/export_handlers.ts +++ b/electron/app/ipc/export_handlers.ts @@ -22,7 +22,11 @@ export function setupExportHandlers() { ipcMain.handle("export-presentation", async (_, id: string, title: string, exportAs: "pptx" | "pdf" | "png") => { try { - const pptUrl = `${process.env.NEXT_PUBLIC_URL}/pdf-maker?id=${id}`; + const params = new URLSearchParams({ id }); + if (process.env.NEXT_PUBLIC_FAST_API) { + params.set("fastapiUrl", process.env.NEXT_PUBLIC_FAST_API); + } + const pptUrl = `${process.env.NEXT_PUBLIC_URL}/pdf-maker?${params.toString()}`; let exportTask = { type: "export", diff --git a/electron/servers/nextjs/app/(presentation-generator)/pdf-maker/PdfMakerPage.tsx b/electron/servers/nextjs/app/(presentation-generator)/pdf-maker/PdfMakerPage.tsx index 67792250..b324bae1 100644 --- a/electron/servers/nextjs/app/(presentation-generator)/pdf-maker/PdfMakerPage.tsx +++ b/electron/servers/nextjs/app/(presentation-generator)/pdf-maker/PdfMakerPage.tsx @@ -63,7 +63,12 @@ const PresentationPage = ({ presentation_id }: { presentation_id: string }) => { dispatch(setPresentationData(data)); setContentLoading(false); if (data?.theme) { - applyTheme(data.theme); + try { + applyTheme(data.theme); + } catch (themeError) { + // Theme issues should not block export rendering. + console.warn("Theme application skipped for pdf-maker:", themeError); + } } } catch (error) { setError(true); @@ -78,6 +83,7 @@ const PresentationPage = ({ presentation_id }: { presentation_id: string }) => { if (!element) return; if (!theme || !theme.data) { return; } if (!theme.data.colors['graph_0']) { return; } + if (!theme.data.fonts?.textFont?.name || !theme.data.fonts?.textFont?.url) { return; } const cssVariables = { '--primary-color': theme.data.colors['primary'], '--background-color': theme.data.colors['background'], diff --git a/electron/servers/nextjs/utils/api.ts b/electron/servers/nextjs/utils/api.ts index 6f8c3c6d..62c9ca28 100644 --- a/electron/servers/nextjs/utils/api.ts +++ b/electron/servers/nextjs/utils/api.ts @@ -1,5 +1,10 @@ // Utility to get the FastAPI base URL export function getFastAPIUrl(): string { + const queryFastApiUrl = getFastApiUrlFromQuery(); + if (queryFastApiUrl) { + return queryFastApiUrl; + } + // Prefer Electron-preload env when available if (typeof window !== "undefined" && (window as any).env?.NEXT_PUBLIC_FAST_API) { return (window as any).env.NEXT_PUBLIC_FAST_API; @@ -14,6 +19,23 @@ export function getFastAPIUrl(): string { return "http://127.0.0.1:8000"; } +function getFastApiUrlFromQuery(): string | null { + if (typeof window === "undefined") return null; + try { + const params = new URLSearchParams(window.location.search); + const value = params.get("fastapiUrl"); + if (!value) return null; + + const parsed = new URL(value); + if (parsed.protocol !== "http:" && parsed.protocol !== "https:") { + return null; + } + return parsed.origin; + } catch { + return null; + } +} + function isAbsoluteHttpUrl(path: string): boolean { return /^https?:\/\//i.test(path); } @@ -34,10 +56,15 @@ export function getApiUrl(path: string): string { const normalizedPath = withLeadingSlash(path); const isFastApiEndpoint = normalizedPath.startsWith("/api/v1/"); + const hasWindowFastApi = typeof window !== "undefined" && !!(window as any).env?.NEXT_PUBLIC_FAST_API; + const hasQueryFastApi = !!getFastApiUrlFromQuery(); // In web/docker, /api/v1 is typically reverse-proxied by the web server. // In Electron, Next and FastAPI run on different ports, so use FastAPI base URL. - if (isFastApiEndpoint && (isElectronRuntime() || !!process.env.NEXT_PUBLIC_FAST_API)) { + if ( + isFastApiEndpoint && + (isElectronRuntime() || !!process.env.NEXT_PUBLIC_FAST_API || hasWindowFastApi || hasQueryFastApi) + ) { return `${getFastAPIUrl()}${normalizedPath}`; }