diff --git a/app/src/lib/auth/billing.ts b/app/src/lib/auth/billing.ts index 519e18f..2d54b9a 100644 --- a/app/src/lib/auth/billing.ts +++ b/app/src/lib/auth/billing.ts @@ -1,6 +1,8 @@ -export const APP_BILLING_PLAN_SLUGS = [ +const DEFAULT_APP_BILLING_PLAN_SLUGS = [ 'early_access', + 'early-access', 'starter', + 'homesteader', 'producer', 'professional', 'commercial', @@ -12,6 +14,28 @@ export const AGENT_MONITOR_FEATURE_SLUGS = [ 'agent_dashboard', // Legacy slug kept for backward compatibility ] as const +const DEFAULT_APP_BILLING_FEATURE_SLUGS = AGENT_MONITOR_FEATURE_SLUGS + +function parseSlugList(raw: string | undefined): string[] { + if (!raw) return [] + return raw + .split(',') + .map((s) => s.trim()) + .filter((s) => s.length > 0) +} + +export function getAppBillingPlanSlugs(): string[] { + const fromEnv = parseSlugList(import.meta.env.VITE_BILLING_PLAN_SLUGS as string | undefined) + if (fromEnv.length === 0) return [...DEFAULT_APP_BILLING_PLAN_SLUGS] + return Array.from(new Set([...DEFAULT_APP_BILLING_PLAN_SLUGS, ...fromEnv])) +} + +export function getAppBillingFeatureSlugs(): string[] { + const fromEnv = parseSlugList(import.meta.env.VITE_BILLING_FEATURE_SLUGS as string | undefined) + if (fromEnv.length === 0) return [...DEFAULT_APP_BILLING_FEATURE_SLUGS] + return Array.from(new Set([...DEFAULT_APP_BILLING_FEATURE_SLUGS, ...fromEnv])) +} + function safeCheck(check: (value: string) => boolean, value: string): boolean { try { return check(value) @@ -22,7 +46,7 @@ function safeCheck(check: (value: string) => boolean, value: string): boolean { export function hasAnyPlan( hasPlan: (plan: string) => boolean, - plans: readonly string[] = APP_BILLING_PLAN_SLUGS + plans: readonly string[] = getAppBillingPlanSlugs() ): boolean { return plans.some((plan) => safeCheck(hasPlan, plan)) } @@ -33,3 +57,14 @@ export function hasAnyFeature( ): boolean { return features.some((feature) => safeCheck(hasFeature, feature)) } + +export function hasBillingAccess(args: { + hasPlan: (plan: string) => boolean + hasFeature: (feature: string) => boolean + planSlugs?: readonly string[] + featureSlugs?: readonly string[] +}): boolean { + const planSlugs = args.planSlugs ?? getAppBillingPlanSlugs() + const featureSlugs = args.featureSlugs ?? getAppBillingFeatureSlugs() + return hasAnyPlan(args.hasPlan, planSlugs) || hasAnyFeature(args.hasFeature, featureSlugs) +} diff --git a/app/src/routes/app.tsx b/app/src/routes/app.tsx index 664850d..769ef8b 100644 --- a/app/src/routes/app.tsx +++ b/app/src/routes/app.tsx @@ -10,14 +10,23 @@ import { useEffect } from 'react' import { TutorialProvider, TutorialOverlay } from '@/components/onboarding/tutorial' import { SatelliteAnimationProvider } from '@/lib/satellite-animation' import { SatelliteCollapseAnimation } from '@/components/layout/SatelliteCollapseAnimation' -import { APP_BILLING_PLAN_SLUGS, hasAnyPlan } from '@/lib/auth/billing' +import { hasBillingAccess } from '@/lib/auth/billing' export const Route = createFileRoute('/app')({ component: AppLayout, }) function AppLayout() { - const { isLoaded, isSignedIn, isDevAuth, needsOnboarding, isOrgLoaded, hasPlan: checkPlan, isPlanLoaded } = useAppAuth() + const { + isLoaded, + isSignedIn, + isDevAuth, + needsOnboarding, + isOrgLoaded, + hasPlan: checkPlan, + hasFeature: checkFeature, + isPlanLoaded, + } = useAppAuth() const navigate = useNavigate() const location = useRouterState({ select: (s) => s.location }) const isOnboarding = location.pathname === '/app/onboarding' @@ -30,7 +39,7 @@ function AppLayout() { const isSubscriptionLoaded = isPlanLoaded const hasPlan = paywallEnabled ? (isSubscriptionLoaded - ? hasAnyPlan(checkPlan, APP_BILLING_PLAN_SLUGS) + ? hasBillingAccess({ hasPlan: checkPlan, hasFeature: checkFeature }) : false) : true diff --git a/app/src/routes/subscribe.tsx b/app/src/routes/subscribe.tsx index d913931..d98a447 100644 --- a/app/src/routes/subscribe.tsx +++ b/app/src/routes/subscribe.tsx @@ -1,12 +1,12 @@ import { createFileRoute, useNavigate, useSearch } from '@tanstack/react-router' import { useUser, useClerk, useAuth, PricingTable } from '@clerk/clerk-react' -import { useEffect, useCallback } from 'react' +import { useEffect, useCallback, useMemo } from 'react' import { Button } from '@/components/ui/button' import { Terminal, LogOut } from 'lucide-react' import { LoadingSpinner } from '@/components/ui/loading/LoadingSpinner' import { useAppAuth } from '@/lib/auth' import { useAnalytics } from '@/lib/analytics' -import { APP_BILLING_PLAN_SLUGS, hasAnyPlan } from '@/lib/auth/billing' +import { getAppBillingFeatureSlugs, getAppBillingPlanSlugs, hasBillingAccess } from '@/lib/auth/billing' import { z } from 'zod' export const Route = createFileRoute('/subscribe')({ @@ -47,15 +47,19 @@ function SubscribePageContent() { const { signOut } = useClerk() const navigate = useNavigate() const { trackSubscriptionStarted } = useAnalytics() + const billingPlanSlugs = useMemo(() => getAppBillingPlanSlugs(), []) + const billingFeatureSlugs = useMemo(() => getAppBillingFeatureSlugs(), []) // B2C billing source of truth: Clerk plans/features via has(). const checkHasPlan = useCallback(() => { if (!has) return false - return hasAnyPlan( - (plan) => has({ plan }), - APP_BILLING_PLAN_SLUGS - ) - }, [has]) + return hasBillingAccess({ + hasPlan: (plan) => has({ plan }), + hasFeature: (feature) => has({ feature }), + planSlugs: billingPlanSlugs, + featureSlugs: billingFeatureSlugs, + }) + }, [has, billingPlanSlugs, billingFeatureSlugs]) // Debug: log active plan checks useEffect(() => { @@ -63,10 +67,14 @@ function SubscribePageContent() { console.log('[Subscribe] User ID:', user.id) console.log( '[Subscribe] Clerk plan checks:', - APP_BILLING_PLAN_SLUGS.map((plan) => [plan, has?.({ plan })]) + billingPlanSlugs.map((plan) => [plan, has?.({ plan })]) + ) + console.log( + '[Subscribe] Clerk feature checks:', + billingFeatureSlugs.map((feature) => [feature, has?.({ feature })]) ) } - }, [isUserLoaded, user, has]) + }, [isUserLoaded, user, has, billingPlanSlugs, billingFeatureSlugs]) // Paywall is enabled by default, can be disabled via env var (useful for dev) const paywallDisabled = import.meta.env.VITE_PAYWALL_DISABLED === 'true'