|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Loader2, Lock } from "lucide-react"; |
| 4 | +import Link from "next/link"; |
| 5 | +import { Button } from "@/components/ui/button"; |
| 6 | +import { |
| 7 | + Card, |
| 8 | + CardContent, |
| 9 | + CardDescription, |
| 10 | + CardHeader, |
| 11 | + CardTitle, |
| 12 | +} from "@/components/ui/card"; |
| 13 | +import { api } from "@/utils/api"; |
| 14 | + |
| 15 | +interface EnterpriseFeatureLockedProps { |
| 16 | + /** Optional title override */ |
| 17 | + title?: string; |
| 18 | + /** Optional description override */ |
| 19 | + description?: string; |
| 20 | + /** Optional custom CTA label */ |
| 21 | + ctaLabel?: string; |
| 22 | + /** Optional CTA href (default: /dashboard/settings/license) */ |
| 23 | + ctaHref?: string; |
| 24 | + /** Compact variant (less padding, smaller icon) */ |
| 25 | + compact?: boolean; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Displays a locked state for enterprise features when the user has no valid license. |
| 30 | + * Use standalone or via EnterpriseFeatureGate. |
| 31 | + */ |
| 32 | +export function EnterpriseFeatureLocked({ |
| 33 | + title = "Enterprise feature", |
| 34 | + description = "This feature is part of Dokploy Enterprise. Add a valid license to use it.", |
| 35 | + ctaLabel = "Go to License", |
| 36 | + ctaHref = "/dashboard/settings/license", |
| 37 | + compact = false, |
| 38 | +}: EnterpriseFeatureLockedProps) { |
| 39 | + return ( |
| 40 | + <Card className="border-dashed bg-transparent"> |
| 41 | + <CardHeader className={compact ? "pb-2" : undefined}> |
| 42 | + <div className="flex flex-col items-center gap-3 text-center"> |
| 43 | + <div |
| 44 | + className={ |
| 45 | + compact |
| 46 | + ? "rounded-full bg-muted p-3" |
| 47 | + : "rounded-full bg-muted p-4" |
| 48 | + } |
| 49 | + > |
| 50 | + <Lock |
| 51 | + className={ |
| 52 | + compact |
| 53 | + ? "size-6 text-muted-foreground" |
| 54 | + : "size-8 text-muted-foreground" |
| 55 | + } |
| 56 | + /> |
| 57 | + </div> |
| 58 | + <div className="space-y-1"> |
| 59 | + <CardTitle className="text-lg">{title}</CardTitle> |
| 60 | + <CardDescription className="max-w-sm mx-auto"> |
| 61 | + {description} |
| 62 | + </CardDescription> |
| 63 | + </div> |
| 64 | + </div> |
| 65 | + </CardHeader> |
| 66 | + <CardContent className={compact ? "pt-0" : undefined}> |
| 67 | + <div className="flex justify-center"> |
| 68 | + <Button asChild variant="secondary" size={compact ? "sm" : "default"}> |
| 69 | + <Link href={ctaHref}>{ctaLabel}</Link> |
| 70 | + </Button> |
| 71 | + </div> |
| 72 | + </CardContent> |
| 73 | + </Card> |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +interface EnterpriseFeatureGateProps { |
| 78 | + children: React.ReactNode; |
| 79 | + /** Props for the locked state when license is invalid */ |
| 80 | + lockedProps?: Omit<EnterpriseFeatureLockedProps, "compact">; |
| 81 | + /** Show loading spinner while checking license */ |
| 82 | + fallback?: React.ReactNode; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Renders children only when the instance has a valid enterprise license. |
| 87 | + * Otherwise shows EnterpriseFeatureLocked. |
| 88 | + */ |
| 89 | +export function EnterpriseFeatureGate({ |
| 90 | + children, |
| 91 | + lockedProps, |
| 92 | + fallback, |
| 93 | +}: EnterpriseFeatureGateProps) { |
| 94 | + const { data: haveValidLicense, isLoading } = |
| 95 | + api.licenseKey.haveValidLicenseKey.useQuery(); |
| 96 | + |
| 97 | + if (isLoading) { |
| 98 | + if (fallback) return <>{fallback}</>; |
| 99 | + return ( |
| 100 | + <div className="flex items-center gap-2 justify-center min-h-[25vh]"> |
| 101 | + <Loader2 className="size-6 text-muted-foreground animate-spin" /> |
| 102 | + <span className="text-sm text-muted-foreground"> |
| 103 | + Checking license... |
| 104 | + </span> |
| 105 | + </div> |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + if (!haveValidLicense) { |
| 110 | + return <EnterpriseFeatureLocked {...lockedProps} />; |
| 111 | + } |
| 112 | + |
| 113 | + return <>{children}</>; |
| 114 | +} |
0 commit comments