From e66822e1b9cc2badf04e6503ece3179bbc1f84a8 Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Wed, 6 Mar 2024 03:57:32 +0600 Subject: [PATCH] fix: roadmap refetching --- .../GenerateRoadmap/GenerateRoadmap.tsx | 55 ++++++++++++++----- .../GenerateRoadmap/RoadmapSearch.tsx | 8 +-- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/src/components/GenerateRoadmap/GenerateRoadmap.tsx b/src/components/GenerateRoadmap/GenerateRoadmap.tsx index 7b1269fc9..d07a75a4e 100644 --- a/src/components/GenerateRoadmap/GenerateRoadmap.tsx +++ b/src/components/GenerateRoadmap/GenerateRoadmap.tsx @@ -23,6 +23,12 @@ import { cn } from '../../lib/classname.ts'; const ROADMAP_ID_REGEX = new RegExp('@ROADMAPID:(\\w+)@'); +type GetAIRoadmapResponse = { + id: string; + topic: string; + data: string; +}; + export function GenerateRoadmap() { const roadmapContainerRef = useRef(null); @@ -33,6 +39,8 @@ export function GenerateRoadmap() { const [isLoading, setIsLoading] = useState(false); const [roadmapTopic, setRoadmapTopic] = useState(''); const [generatedRoadmap, setGeneratedRoadmap] = useState(''); + const [currentRoadmap, setCurrentRoadmap] = + useState(null); const [roadmapLimit, setRoadmapLimit] = useState(0); const [roadmapLimitUsed, setRoadmapLimitUsed] = useState(0); @@ -51,6 +59,10 @@ export function GenerateRoadmap() { return; } + if (roadmapTopic === currentRoadmap?.topic) { + return; + } + setIsLoading(true); setHasSubmitted(true); @@ -104,6 +116,11 @@ export function GenerateRoadmap() { const roadmapId = result.match(ROADMAP_ID_REGEX)?.[1] || ''; setUrlParams({ id: roadmapId }); result = result.replace(ROADMAP_ID_REGEX, ''); + setCurrentRoadmap({ + id: roadmapId, + topic: roadmapTopic, + data: result, + }); } await renderRoadmap(result); @@ -150,11 +167,17 @@ export function GenerateRoadmap() { if (error || !response) { toast.error(error?.message || 'Something went wrong'); + pageProgressMessage.set(''); setIsLoading(false); return; } - window.location.href = `${import.meta.env.PUBLIC_EDITOR_APP_URL}/${response.roadmapId}`; + setIsLoading(false); + pageProgressMessage.set(''); + window.open( + `${import.meta.env.PUBLIC_EDITOR_APP_URL}/${response.roadmapId}`, + '_blank', + ); }; const downloadGeneratedRoadmap = async () => { @@ -208,6 +231,11 @@ export function GenerateRoadmap() { const { topic, data } = response; await renderRoadmap(data); + setCurrentRoadmap({ + id: roadmapId, + topic, + data, + }); setRoadmapTopic(topic); setGeneratedRoadmap(data); }; @@ -217,7 +245,7 @@ export function GenerateRoadmap() { }, []); useEffect(() => { - if (!roadmapId) { + if (!roadmapId || roadmapId === currentRoadmap?.id) { return; } @@ -225,7 +253,7 @@ export function GenerateRoadmap() { loadAIRoadmap(roadmapId).finally(() => { pageProgressMessage.set(''); }); - }, [roadmapId]); + }, [roadmapId, currentRoadmap]); if (!hasSubmitted) { return ( @@ -282,12 +310,12 @@ export function GenerateRoadmap() {
@@ -297,14 +325,15 @@ export function GenerateRoadmap() {