🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
fix redemption (#764)
Bill Yang373535c60cdf
diff --git a/client/src/app/as/callback/page.tsx b/client/src/app/as/callback/page.tsxindex f0cb1b813..240dd90e8 100644--- a/client/src/app/as/callback/page.tsx+++ b/client/src/app/as/callback/page.tsx@@ -9,7 +9,7 @@ import { Card } from "@/components/ui/card";import { Input } from "@/components/ui/input";import { Label } from "@/components/ui/label";import { motion } from "framer-motion";-import { ArrowRight, Check } from "lucide-react";+import { ArrowRight, Check, Loader2 } from "lucide-react";import Link from "next/link";import { useRouter, useSearchParams } from "next/navigation";import React, { Suspense, useEffect, useState } from "react";@@ -60,6 +60,8 @@ export default function AppSumoSignupPage() {const [isLoading, setIsLoading] = useState(false);const [error, setError] = useState<string>("");const [appsumoCode, setAppsumoCode] = useState<string>("");+ const [isExistingUser, setIsExistingUser] = useState(false);+ const [isInitializing, setIsInitializing] = useState(true);const router = useRouter();// Step 1: Account creation@@ -75,6 +77,60 @@ export default function AppSumoSignupPage() {const [organizationId, setOrganizationId] = useState("");const [domain, setDomain] = useState("");+ // Track if license activation has been attempted for existing users+ const [licenseActivated, setLicenseActivated] = useState(false);++ // Use hooks to get auth state+ const { user, isPending: isUserPending } = userStore();+ const { data: activeOrganization, isPending: isOrgPending } =+ authClient.useActiveOrganization();++ // Initialize flow based on user auth state+ useEffect(() => {+ // Wait for both user and org state to resolve+ if (isUserPending || isOrgPending) {+ return;+ }++ if (!user) {+ // New user - start at step 1+ setIsInitializing(false);+ return;+ }++ // Existing user+ setIsExistingUser(true);++ if (activeOrganization?.id) {+ // Has active org - set it and go to step 3+ setOrganizationId(activeOrganization.id);+ setCurrentStep(3);+ } else {+ // No active org - need to create one (step 2)+ setCurrentStep(2);+ }++ setIsInitializing(false);+ }, [user, isUserPending, activeOrganization, isOrgPending]);++ // Activate license for existing users once code and organization are available+ useEffect(() => {+ const activateForExistingUser = async () => {+ if (+ isExistingUser &&+ appsumoCode &&+ organizationId &&+ !licenseActivated &&+ !isInitializing+ ) {+ setLicenseActivated(true);+ await activateLicenseForOrg(organizationId, appsumoCode);+ }+ };++ activateForExistingUser();+ }, [isExistingUser, appsumoCode, organizationId, licenseActivated, isInitializing]);+// Handle organization name change and generate slugconst handleOrgNameChange = (value: string) => {setOrgName(value);@@ -87,6 +143,40 @@ export default function AppSumoSignupPage() {}};+ // Helper function to activate AppSumo license for an organization+ const activateLicenseForOrg = async (orgId: string, code: string): Promise<boolean> => {+ if (!code) return true; // No code to activate, consider it success++ try {+ const response = await fetch("/api/as/activate", {+ method: "POST",+ headers: {+ "Content-Type": "application/json",+ },+ credentials: "include",+ body: JSON.stringify({+ code: code,+ organizationId: orgId,+ }),+ });++ if (!response.ok) {+ const errorData = await response.json();+ throw new Error(errorData.error || "Failed to activate AppSumo license");+ }++ const licenseData = await response.json();+ console.log("AppSumo license activated:", licenseData);+ return true;+ } catch (licenseError) {+ console.error("Error activating AppSumo license:", licenseError);+ setError(+ "License activation failed. Please contact support with your license key."+ );+ return false;+ }+ };+// Step 1: Account creation submissionconst handleAccountSubmit = async () => {setIsLoading(true);@@ -161,33 +251,8 @@ export default function AppSumoSignupPage() {// Activate AppSumo license if code is presentif (appsumoCode) {- try {- const response = await fetch("/api/as/activate", {- method: "POST",- headers: {- "Content-Type": "application/json",- },- credentials: "include",- body: JSON.stringify({- code: appsumoCode,- organizationId: data.id,- }),- });-- if (!response.ok) {- const errorData = await response.json();- throw new Error(errorData.error || "Failed to activate AppSumo license");- }-- const licenseData = await response.json();- console.log("AppSumo license activated:", licenseData);- } catch (licenseError) {- console.error("Error activating AppSumo license:", licenseError);- setError(- "Organization created, but license activation failed. Please contact support with your license key."- );- // Continue to next step even if license activation fails- }+ await activateLicenseForOrg(data.id, appsumoCode);+ // Continue to next step even if license activation fails (error is already set by helper)}setCurrentStep(3);@@ -296,9 +361,13 @@ export default function AppSumoSignupPage() {case 2:return (<motion.div initial="hidden" animate="visible" variants={contentVariants}>- <h2 className="text-2xl font-semibold mb-2">Create your organization</h2>+ <h2 className="text-2xl font-semibold mb-2">+ {isExistingUser ? "Create an organization" : "Create your organization"}+ </h2><p className="text-sm text-muted-foreground mb-6">- Your AppSumo license will be activated for this organization+ {isExistingUser+ ? "Create an organization to activate your AppSumo license"+ : "Your AppSumo license will be activated for this organization"}</p><div className="space-y-4"><div className="space-y-2">@@ -310,7 +379,7 @@ export default function AppSumoSignupPage() {value={orgName}onChange={e => handleOrgNameChange(e.target.value)}required- className="h-10 transition-all bg-neutral-800/50 border-neutral-700"+ className="h-10 transition-all bg-neutral-100 dark:bg-neutral-800/50 border-neutral-200 dark:border-neutral-700"/></div>@@ -340,7 +409,7 @@ export default function AppSumoSignupPage() {placeholder="example.com or sub.example.com"value={domain}onChange={e => setDomain(e.target.value.toLowerCase())}- className="h-10 transition-all bg-neutral-800/50 border-neutral-700"+ className="h-10 transition-all bg-neutral-100 dark:bg-neutral-800/50 border-neutral-200 dark:border-neutral-700"/><p className="text-xs text-muted-foreground">Enter the domain of the website you want to track</p></div>@@ -372,32 +441,44 @@ export default function AppSumoSignupPage() {}};+ // Show loading state while initializing+ if (isInitializing) {+ return (+ <div className="flex justify-center items-center h-dvh w-full p-4 bg-background">+ <div className="flex flex-col items-center gap-4">+ <Loader2 className="h-8 w-8 animate-spin text-emerald-600 dark:text-emerald-500" />+ <p className="text-muted-foreground">Loading...</p>+ </div>+ </div>+ );+ }+return (- <div className="flex justify-center items-center h-dvh w-full p-4 ">- <div className="flex flex-col items-center bg-background relative">+ <div className="flex justify-center items-center h-dvh w-full p-4 bg-background">+ <div className="flex flex-col items-center relative">{/* Suspense boundary for the URL parameter handler */}<Suspense fallback={null}><AppSumoCodeHandler onSetCode={setAppsumoCode} onSetStep={setCurrentStep} /></Suspense>{/* Background gradients similar to docs page */}- <div className="absolute top-0 left-0 w-[550px] h-[550px] bg-emerald-500/40 rounded-full blur-[80px] opacity-40"></div>- <div className="absolute top-20 left-20 w-[400px] h-[400px] bg-emerald-600/30 rounded-full blur-[70px] opacity-30"></div>+ <div className="absolute top-0 left-0 w-[550px] h-[550px] bg-emerald-500/40 rounded-full blur-[80px] opacity-20 dark:opacity-40"></div>+ <div className="absolute top-20 left-20 w-[400px] h-[400px] bg-emerald-600/30 rounded-full blur-[70px] opacity-15 dark:opacity-30"></div>- <div className="absolute bottom-0 right-0 w-[500px] h-[500px] bg-blue-500/40 rounded-full blur-[80px] opacity-30"></div>- <div className="absolute bottom-40 right-20 w-[350px] h-[350px] bg-indigo-500/30 rounded-full blur-[75px] opacity-30"></div>+ <div className="absolute bottom-0 right-0 w-[500px] h-[500px] bg-blue-500/40 rounded-full blur-[80px] opacity-15 dark:opacity-30"></div>+ <div className="absolute bottom-40 right-20 w-[350px] h-[350px] bg-indigo-500/30 rounded-full blur-[75px] opacity-15 dark:opacity-30"></div>- <div className="absolute top-1/4 right-0 w-[320px] h-[320px] bg-purple-500/40 rounded-full blur-[70px] opacity-20"></div>+ <div className="absolute top-1/4 right-0 w-[320px] h-[320px] bg-purple-500/40 rounded-full blur-[70px] opacity-10 dark:opacity-20"></div>{/* Logo and title above the card */}<div className="relative z-10 mb-6 text-center"><a href="https://rybbit.com" target="_blank" className="inline-block mb-2"><RybbitTextLogo /></a>- <h1 className="text-lg text-neutral-300">AppSumo License Activation</h1>+ <h1 className="text-lg text-neutral-600 dark:text-neutral-300">AppSumo License Activation</h1></div>- <Card className="w-full md:w-[500px] p-0 overflow-hidden shadow-2xl border-neutral-700/50 backdrop-blur-sm bg-neutral-800/20 z-10 p-8">+ <Card className="w-full md:w-[500px] p-0 overflow-hidden shadow-2xl border-neutral-200 dark:border-neutral-700/50 backdrop-blur-sm bg-white/80 dark:bg-neutral-800/20 z-10 p-8">{/* Horizontal step indicator */}<div className="flex items-center w-full mb-4">{[1, 2, 3].map((step, index) => (@@ -406,10 +487,10 @@ export default function AppSumoSignupPage() {className={cn("flex items-center justify-center w-10 h-10 rounded-full text-sm font-semibold transition-all duration-300",currentStep === step- ? "bg-emerald-600 text-primary-foreground"+ ? "bg-emerald-600 text-white shadow-lg shadow-emerald-600/30": currentStep > step- ? "bg-emerald-600/20 text-emerald-400 border-2 border-emerald-600/40"- : "bg-muted-foreground/20 text-muted-foreground border-2 border-muted-foreground/40"+ ? "bg-emerald-600 text-white"+ : "bg-neutral-200 dark:bg-neutral-800 text-neutral-500 dark:text-neutral-400")}>{currentStep > step ? <Check className="h-5 w-5" /> : step}@@ -418,7 +499,7 @@ export default function AppSumoSignupPage() {<divclassName={cn("flex-1 h-0.5 transition-all duration-300",- currentStep > step ? "bg-emerald-600" : "bg-muted-foreground/40"+ currentStep > step ? "bg-emerald-600" : "bg-neutral-200 dark:bg-neutral-800")}/>)}