import React, { Suspense, useState, useEffect } from "react"; import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; interface DynamicFileProps { id: string; } const DynamicFileRenderer: React.FC = ({ id }) => { const [DynamicComponent, setDynamicComponent] = useState(null); const [error, setError] = useState(false); const [loadingText, setLoadingText] = useState("Loading"); useEffect(() => { let failed = false; const loadComponent = async (canSetError: boolean) => { try { const Component = await import(`@/components/generated/${id}`); setDynamicComponent(() => Component.default || Component); setError(false); } catch (error) { failed = true; if (canSetError) { setDynamicComponent(() => null); setError(true); } } }; loadComponent(false); setTimeout(() => { failed && loadComponent(true); }, 2000); }, [id]); useEffect(() => { const interval = setInterval(() => { setLoadingText((prev) => (prev.endsWith("...") ? "Loading" : prev + ".")); }, 500); return () => clearInterval(interval); }, []); if (error) { return (
Error Loading Component We encountered an issue while loading the component. Please try the following steps:
  • Switch between the "Preview" and "Code" tabs.
  • If the issue persists, refresh the page.
); } if (!DynamicComponent) { return (
{loadingText}
); } return ( {loadingText} } > ); }; export default DynamicFileRenderer;