SidebarPlan.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use client'
  2. import Link from 'next/link'
  3. import { Zap } from 'lucide-react'
  4. import { useTranslations } from 'next-intl'
  5. import { SidebarMenuButton, SidebarMenuItem, useSidebar } from '@/components/ui/sidebar'
  6. import { track } from '@/lib/analytics'
  7. import { cn } from '@/lib/utils'
  8. import type { SidebarPlan as SidebarPlanData } from '@/features/subscription/Lib/sidebarPlan.server'
  9. /**
  10. * The plan row at the foot of the sidebar: which plan the workshop is on,
  11. * how much of it is used, and the way to the plans.
  12. *
  13. * Until it existed a free workshop only learned it was on a plan at all
  14. * when a limit refused something. Kept quiet on purpose: a muted icon and a
  15. * thin meter, the way a nav row looks, so it reads as information rather
  16. * than an advert. Collapsed to icons it is one icon with the same words in
  17. * its tooltip, like every other row.
  18. */
  19. export function SidebarPlan({ plan }: { plan: SidebarPlanData }) {
  20. const t = useTranslations('navigation.sidebar.plan')
  21. const tSettings = useTranslations('settings.subscription')
  22. const { isMobile, setOpenMobile } = useSidebar()
  23. const label =
  24. plan.kind === 'free'
  25. ? t('free')
  26. : t('demo', {
  27. plan: plan.plan === 'enterprise' ? tSettings('planEnterprise') : tSettings('planPro'),
  28. })
  29. const detail =
  30. plan.kind === 'free'
  31. ? t('customers', { used: plan.customers, limit: plan.customerLimit })
  32. : plan.daysLeft !== null
  33. ? t('endsIn', { days: plan.daysLeft })
  34. : null
  35. // The plans page's meter, the same arithmetic and the same colours.
  36. const over = plan.kind === 'free' && plan.customers > plan.customerLimit
  37. const share =
  38. plan.kind === 'free' && plan.customerLimit > 0
  39. ? Math.min(100, (plan.customers / plan.customerLimit) * 100)
  40. : 0
  41. const handleClick = () => {
  42. track(
  43. 'plan:sidebar_upgrade_click',
  44. plan.kind === 'free'
  45. ? { kind: 'free', customers: plan.customers, customer_limit: plan.customerLimit }
  46. : { kind: 'demo' }
  47. )
  48. if (isMobile) setOpenMobile(false)
  49. }
  50. return (
  51. <SidebarMenuItem>
  52. <SidebarMenuButton
  53. asChild
  54. tooltip={{
  55. children: (
  56. <span className="grid gap-0.5">
  57. <span className="font-medium">{label}</span>
  58. {detail && <span>{detail}</span>}
  59. <span>{t('upgrade')}</span>
  60. </span>
  61. ),
  62. }}
  63. className="h-auto border border-sidebar-border group-data-[collapsible=icon]:border-0"
  64. >
  65. <Link href="/settings/subscription" onClick={handleClick}>
  66. <Zap className="size-4" />
  67. <div className="grid min-w-0 flex-1 gap-1 leading-tight group-data-[collapsible=icon]:hidden">
  68. <div className="flex items-center justify-between gap-2">
  69. <span className="truncate font-medium">{label}</span>
  70. <span className="shrink-0 text-xs font-medium underline-offset-4 group-hover/menu-item:underline">
  71. {t('upgrade')}
  72. </span>
  73. </div>
  74. {plan.kind === 'free' && (
  75. <div aria-hidden className="h-1.5 overflow-hidden rounded-full bg-sidebar-border">
  76. <div
  77. className={cn(
  78. 'h-full rounded-full',
  79. over ? 'bg-destructive' : 'bg-sidebar-primary'
  80. )}
  81. style={{ width: `${share}%` }}
  82. />
  83. </div>
  84. )}
  85. {detail && (
  86. <span
  87. className={cn(
  88. 'truncate text-xs',
  89. over ? 'font-medium text-destructive' : 'text-sidebar-foreground/60'
  90. )}
  91. >
  92. {detail}
  93. </span>
  94. )}
  95. </div>
  96. </Link>
  97. </SidebarMenuButton>
  98. </SidebarMenuItem>
  99. )
  100. }