From 524072991428dd9e614d6631f09ffedcfb45c17e Mon Sep 17 00:00:00 2001 From: Aman Varshney Date: Wed, 7 May 2025 18:10:44 +0530 Subject: [PATCH] add random and share button in stack architech --- .../app/(home)/_components/StackArchitech.tsx | 88 ++++++++++++++++++- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/apps/web/src/app/(home)/_components/StackArchitech.tsx b/apps/web/src/app/(home)/_components/StackArchitech.tsx index 54a42a9..8b482e2 100644 --- a/apps/web/src/app/(home)/_components/StackArchitech.tsx +++ b/apps/web/src/app/(home)/_components/StackArchitech.tsx @@ -27,6 +27,8 @@ import { InfoIcon, RefreshCw, Settings, + Share2, + Shuffle, Star, Terminal, } from "lucide-react"; @@ -275,7 +277,7 @@ const analyzeStackCompatibility = (stack: StackState): CompatibilityResult => { `No backend requires ${displayName} to be '${valueDisplay}'.`, ); notes[catKey].hasIssue = true; - notes.backend.hasIssue = true; + notes[catKey].hasIssue = true; (nextStack[catKey] as string | string[]) = value; changed = true; changes.push({ @@ -884,6 +886,70 @@ const StackArchitect = () => { const rules = useMemo(() => getCompatibilityRules(stack), [stack]); + const getRandomStack = () => { + const randomStack: Partial = { + projectName: `my-random-stack-${Math.random() + .toString(36) + .substring(2, 7)}`, + }; + + for (const category of CATEGORY_ORDER) { + const options = TECH_OPTIONS[category as keyof typeof TECH_OPTIONS] || []; + if (options.length === 0) continue; + + const catKey = category as keyof StackState; + + if (["frontend", "addons", "examples"].includes(catKey)) { + const currentValues: string[] = []; + randomStack[catKey as "frontend" | "addons" | "examples"] = + currentValues; + + if (catKey === "frontend") { + const randomIndex = Math.floor(Math.random() * options.length); + const selectedOption = options[randomIndex].id; + currentValues.push(selectedOption); + if (selectedOption === "none" && currentValues.length > 1) { + randomStack[catKey] = ["none"]; + } else if (selectedOption !== "none") { + randomStack[catKey] = currentValues.filter((id) => id !== "none"); + } + } else { + const numToPick = Math.floor( + Math.random() * Math.min(options.length + 1, 4), + ); + const shuffledOptions = [...options].sort(() => 0.5 - Math.random()); + for (let i = 0; i < numToPick; i++) { + currentValues.push(shuffledOptions[i].id); + } + } + } else { + const randomIndex = Math.floor(Math.random() * options.length); + (randomStack[catKey] as string) = options[randomIndex].id; + } + } + setStack(randomStack as StackState); + setShowHelp(false); + setShowPresets(false); + setActiveCategory(CATEGORY_ORDER[0]); + contentRef.current?.scrollTo(0, 0); + toast.success("Random stack generated!"); + }; + + const shareToTwitter = () => { + const text = encodeURIComponent( + "Check out this cool tech stack I configured with Create Better T-Stack!\n\n", + ); + if (typeof window !== "undefined") { + const url = encodeURIComponent(window.location.href); + window.open( + `https://twitter.com/intent/tweet?text=${text}&url=${url}`, + "_blank", + ); + } else { + toast.error("Could not generate share link."); + } + }; + const disabledReasons = useMemo(() => { const reasons = new Map(); @@ -1652,6 +1718,15 @@ const StackArchitect = () => { Reset + {lastSavedStack && ( +