From 9166b56af7494e9ded4f90282408f69f575473f7 Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Fri, 19 Apr 2024 02:05:08 +0600 Subject: [PATCH] wip --- .../GenerateRoadmap/GenerateRoadmap.tsx | 41 ++++++++++++++----- .../GenerateRoadmap/RoadmapTopicDetail.tsx | 29 +++++++------ src/pages/ai/[aiRoadmapSlug]/index.astro | 3 +- 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/src/components/GenerateRoadmap/GenerateRoadmap.tsx b/src/components/GenerateRoadmap/GenerateRoadmap.tsx index 48e021ddb..d3be2a2ce 100644 --- a/src/components/GenerateRoadmap/GenerateRoadmap.tsx +++ b/src/components/GenerateRoadmap/GenerateRoadmap.tsx @@ -50,6 +50,7 @@ export type GetAIRoadmapLimitResponse = { }; const ROADMAP_ID_REGEX = new RegExp('@ROADMAPID:(\\w+)@'); +const ROADMAP_SLUG_REGEX = new RegExp(/@ROADMAPSLUG:([\w-]+)@/); export type RoadmapNodeDetails = { nodeId: string; @@ -89,11 +90,11 @@ type GetAIRoadmapResponse = { type GenerateRoadmapProps = { roadmapId?: string; - t?: string; + slug?: string; }; export function GenerateRoadmap(props: GenerateRoadmapProps) { - const { roadmapId, t: term = '' } = props; + const { roadmapId: defaultRoadmapId, slug: defaultRoadmapSlug } = props; const roadmapContainerRef = useRef(null); @@ -102,13 +103,19 @@ export function GenerateRoadmap(props: GenerateRoadmapProps) { }; const toast = useToast(); + const [roadmapId, setRoadmapId] = useState( + defaultRoadmapId, + ); + const [roadmapSlug, setRoadmapSlug] = useState( + defaultRoadmapSlug, + ); const [hasSubmitted, setHasSubmitted] = useState(Boolean(roadmapId)); const [isLoading, setIsLoading] = useState(false); const [isLoadingResults, setIsLoadingResults] = useState(false); - const [roadmapTerm, setRoadmapTerm] = useState(term); - const [generatedRoadmapContent, setGeneratedRoadmapContent] = useState(''); + const [roadmapTerm, setRoadmapTerm] = useState(''); const [currentRoadmap, setCurrentRoadmap] = useState(null); + const [generatedRoadmapContent, setGeneratedRoadmapContent] = useState(''); const [selectedNode, setSelectedNode] = useState( null, ); @@ -140,6 +147,8 @@ export function GenerateRoadmap(props: GenerateRoadmapProps) { deleteUrlParam('id'); setCurrentRoadmap(null); + const origin = window.location.origin; + window.history.pushState(null, '', `${origin}/ai`); const response = await fetch( `${import.meta.env.PUBLIC_API_URL}/v1-generate-ai-roadmap`, { @@ -175,13 +184,21 @@ export function GenerateRoadmap(props: GenerateRoadmapProps) { await readAIRoadmapStream(reader, { onStream: async (result) => { - if (result.includes('@ROADMAPID')) { + if (result.includes('@ROADMAPID') || result.includes('@ROADMAPSLUG')) { // @ROADMAPID: is a special token that we use to identify the roadmap // @ROADMAPID:1234@ is the format, we will remove the token and the id // and replace it with a empty string const roadmapId = result.match(ROADMAP_ID_REGEX)?.[1] || ''; - setUrlParams({ id: roadmapId }); - result = result.replace(ROADMAP_ID_REGEX, ''); + const roadmapSlug = result.match(ROADMAP_SLUG_REGEX)?.[1] || ''; + + window.history.pushState(null, '', `${origin}/ai/${roadmapSlug}`); + result = result + .replace(ROADMAP_ID_REGEX, '') + .replace(ROADMAP_SLUG_REGEX, ''); + + setRoadmapId(roadmapId); + setRoadmapSlug(roadmapSlug); + const roadmapTitle = result.trim().split('\n')[0]?.replace('#', '')?.trim() || term; setRoadmapTerm(roadmapTitle); @@ -196,7 +213,10 @@ export function GenerateRoadmap(props: GenerateRoadmapProps) { await renderRoadmap(result); }, onStreamEnd: async (result) => { - result = result.replace(ROADMAP_ID_REGEX, ''); + result = result + .replace(ROADMAP_ID_REGEX, '') + .replace(ROADMAP_SLUG_REGEX, ''); + setGeneratedRoadmapContent(result); loadAIRoadmapLimit().finally(() => {}); }, @@ -391,7 +411,6 @@ export function GenerateRoadmap(props: GenerateRoadmapProps) { return; } - setHasSubmitted(true); loadAIRoadmap(roadmapId).finally(() => { pageProgressMessage.set(''); }); @@ -415,7 +434,7 @@ export function GenerateRoadmap(props: GenerateRoadmapProps) { ); } - const pageUrl = `https://roadmap.sh/ai?id=${roadmapId}`; + const pageUrl = `https://roadmap.sh/ai/${roadmapSlug}`; const canGenerateMore = roadmapLimitUsed < roadmapLimit; return ( @@ -530,7 +549,7 @@ export function GenerateRoadmap(props: GenerateRoadmapProps) { )} {!isAuthenticatedUser && ( - +
+ +

You must be logged in

+

+ Sign up or login to generate topic content. +

+ +
)} {!isLoading && !error && ( diff --git a/src/pages/ai/[aiRoadmapSlug]/index.astro b/src/pages/ai/[aiRoadmapSlug]/index.astro index 32c9b9591..7d1913c39 100644 --- a/src/pages/ai/[aiRoadmapSlug]/index.astro +++ b/src/pages/ai/[aiRoadmapSlug]/index.astro @@ -22,8 +22,9 @@ let errorMessage = ''; if (error || !roadmap) { errorMessage = error?.message || 'Error loading AI Roadmap'; } +const title = roadmap?.title || 'Roadmap AI'; --- - +