index.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React from 'react'
  4. import UpgradeBtn from '../upgrade-btn'
  5. import { Plan } from '../type'
  6. import cn from '@/utils/classnames'
  7. import { useProviderContext } from '@/context/provider-context'
  8. type Props = {
  9. onClick?: () => void
  10. isDisplayOnly?: boolean
  11. }
  12. const HeaderBillingBtn: FC<Props> = ({
  13. onClick,
  14. isDisplayOnly = false,
  15. }) => {
  16. const { plan, enableBilling, isFetchedPlan } = useProviderContext()
  17. const {
  18. type,
  19. } = plan
  20. const name = (() => {
  21. if (type === Plan.professional)
  22. return 'pro'
  23. return type
  24. })()
  25. const classNames = (() => {
  26. if (type === Plan.professional)
  27. return `border-[#E0F2FE] ${!isDisplayOnly ? 'hover:border-[#B9E6FE]' : ''} bg-[#E0F2FE] text-[#026AA2]`
  28. if (type === Plan.team)
  29. return `border-[#E0EAFF] ${!isDisplayOnly ? 'hover:border-[#C7D7FE]' : ''} bg-[#E0EAFF] text-[#3538CD]`
  30. return ''
  31. })()
  32. if (!enableBilling || !isFetchedPlan)
  33. return null
  34. if (type === Plan.sandbox)
  35. return <UpgradeBtn onClick={isDisplayOnly ? undefined : onClick} isShort />
  36. const handleClick = () => {
  37. if (!isDisplayOnly && onClick)
  38. onClick()
  39. }
  40. return (
  41. <div
  42. onClick={handleClick}
  43. className={cn(
  44. classNames,
  45. 'flex items-center h-[22px] px-2 rounded-md border text-xs font-semibold uppercase',
  46. isDisplayOnly ? 'cursor-default' : 'cursor-pointer',
  47. )}
  48. >
  49. {name}
  50. </div>
  51. )
  52. }
  53. export default React.memo(HeaderBillingBtn)