|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useCallback, useState } from "react"; |
| 4 | +import { useRouter } from "next/navigation"; |
| 5 | +import { Info } from "lucide-react"; |
| 6 | +import { useToast } from "@/components/hooks/use-toast"; |
| 7 | +import { createPortalSession } from "@/ee/features/lighthouse/actions"; |
| 8 | +import { isServiceError } from "@/lib/utils"; |
| 9 | + |
| 10 | +// @note take care to keep this in sync with |
| 11 | +// the value in constants.ts in lighthouse. |
| 12 | +const YEARLY_CANCELLATION_NOTICE_DAYS = 30; |
| 13 | +const DAY_MS = 24 * 60 * 60 * 1000; |
| 14 | + |
| 15 | +interface UpcomingRenewalBannerProps { |
| 16 | + renewalAt: Date; |
| 17 | + seats: number; |
| 18 | +} |
| 19 | + |
| 20 | +export function UpcomingYearlyRenewalBanner({ renewalAt, seats }: UpcomingRenewalBannerProps) { |
| 21 | + const router = useRouter(); |
| 22 | + const { toast } = useToast(); |
| 23 | + const [isOpeningPortal, setIsOpeningPortal] = useState(false); |
| 24 | + const [nowMs] = useState(() => Date.now()); |
| 25 | + |
| 26 | + const handleCancelClick = useCallback(() => { |
| 27 | + setIsOpeningPortal(true); |
| 28 | + createPortalSession().then((response) => { |
| 29 | + if (isServiceError(response)) { |
| 30 | + toast({ |
| 31 | + description: `Failed to open subscription portal: ${response.message}`, |
| 32 | + variant: "destructive", |
| 33 | + }); |
| 34 | + setIsOpeningPortal(false); |
| 35 | + return; |
| 36 | + } |
| 37 | + router.push(response.url); |
| 38 | + }); |
| 39 | + }, [router, toast]); |
| 40 | + |
| 41 | + const daysUntilRenewal = (renewalAt.getTime() - nowMs) / DAY_MS; |
| 42 | + const noticeDeadlineHasPassed = daysUntilRenewal < YEARLY_CANCELLATION_NOTICE_DAYS; |
| 43 | + const noticeDeadline = new Date(renewalAt.getTime() - YEARLY_CANCELLATION_NOTICE_DAYS * DAY_MS); |
| 44 | + |
| 45 | + return ( |
| 46 | + <div className="flex items-start gap-3 border-t bg-blue-500/10 px-4 py-2.5"> |
| 47 | + <Info className="h-4 w-4 mt-0.5 flex-shrink-0 text-blue-400" /> |
| 48 | + <div className="text-sm"> |
| 49 | + <p> |
| 50 | + <span className="font-medium"> |
| 51 | + Auto-renewing on {formatDate(renewalAt)} |
| 52 | + </span>{" "} |
| 53 | + <span className="text-muted-foreground"> |
| 54 | + with {seats} {seats === 1 ? 'seat' : 'seats'}. |
| 55 | + </span> |
| 56 | + </p> |
| 57 | + {!noticeDeadlineHasPassed && ( |
| 58 | + <p className="text-muted-foreground mt-1"> |
| 59 | + You have until {formatDate(noticeDeadline)} to{" "} |
| 60 | + <button |
| 61 | + type="button" |
| 62 | + onClick={handleCancelClick} |
| 63 | + disabled={isOpeningPortal} |
| 64 | + className="text-link hover:underline disabled:opacity-50" |
| 65 | + > |
| 66 | + cancel |
| 67 | + </button>{" "} |
| 68 | + your subscription or{" "} |
| 69 | + <a |
| 70 | + href="mailto:ar@sourcebot.dev" |
| 71 | + className="text-link hover:underline" |
| 72 | + > |
| 73 | + request |
| 74 | + </a>{" "} |
| 75 | + a different seat count for renewal. |
| 76 | + </p> |
| 77 | + )} |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +function formatDate(date: Date): string { |
| 84 | + return new Date(date).toLocaleDateString('en-US', { |
| 85 | + month: 'short', |
| 86 | + day: 'numeric', |
| 87 | + year: 'numeric', |
| 88 | + }); |
| 89 | +} |
0 commit comments