import { useEffect, useState } from 'react'; import { httpGet, httpPost } from '../../lib/http'; import { useToast } from '../../hooks/use-toast'; import { isLoggedIn } from '../../lib/jwt'; import { GitFork, Loader2, Map } from 'lucide-react'; import { showLoginPopup } from '../../lib/popup'; import type { RoadmapDocument } from '../CustomRoadmap/CreateRoadmap/CreateRoadmapModal.tsx'; type CreateVersionProps = { roadmapId: string; }; export function CreateVersion(props: CreateVersionProps) { const { roadmapId } = props; const toast = useToast(); const [isLoading, setIsLoading] = useState(true); const [isCreating, setIsCreating] = useState(false); const [isConfirming, setIsConfirming] = useState(false); const [userVersion, setUserVersion] = useState(); async function loadMyVersion() { if (!isLoggedIn()) { return; } setIsLoading(true); const { response, error } = await httpGet( `${import.meta.env.PUBLIC_API_URL}/v1-get-my-version/${roadmapId}`, {}, ); if (error || !response) { setIsLoading(false); return; } setIsLoading(false); setUserVersion(response); } useEffect(() => { loadMyVersion().finally(() => { setIsLoading(false); }); }, []); async function createVersion() { if (isCreating || !roadmapId) { return; } if (!isLoggedIn()) { showLoginPopup(); return; } setIsCreating(true); const { response, error } = await httpPost<{ roadmapId: string }>( `${import.meta.env.PUBLIC_API_URL}/v1-create-version/${roadmapId}`, {}, ); if (error || !response) { setIsCreating(false); toast.error(error?.message || 'Failed to create version'); return; } window.location.href = `${ import.meta.env.PUBLIC_EDITOR_APP_URL }/${response?.roadmapId}`; } if (isLoading) { return (
); } if (!isLoading && userVersion?._id) { return (
Visit your own version of this Roadmap
); } if (isConfirming) { return (

Create and edit a custom roadmap from this roadmap?  / 

); } return ( ); }