split-test-badge.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { useSplitTestContext } from '../context/split-test-context'
  2. import BetaBadge from './beta-badge'
  3. type TooltipProps = {
  4. id?: string
  5. className?: string
  6. }
  7. type SplitTestBadgeProps = {
  8. splitTestName: string
  9. displayOnVariants: string[]
  10. tooltip?: TooltipProps
  11. }
  12. export default function SplitTestBadge({
  13. splitTestName,
  14. displayOnVariants,
  15. tooltip = {},
  16. }: SplitTestBadgeProps) {
  17. const { splitTestVariants, splitTestInfo } = useSplitTestContext()
  18. const testInfo = splitTestInfo[splitTestName]
  19. if (!testInfo) {
  20. return null
  21. }
  22. const variant = splitTestVariants[splitTestName]
  23. if (!variant || !displayOnVariants.includes(variant)) {
  24. return null
  25. }
  26. return (
  27. <BetaBadge
  28. tooltip={{
  29. id: tooltip.id || `${splitTestName}-badge-tooltip`,
  30. className: `split-test-badge-tooltip ${tooltip.className}`,
  31. text: testInfo.badgeInfo?.tooltipText || (
  32. <>
  33. We are testing this new feature.
  34. <br />
  35. Click to give feedback
  36. </>
  37. ),
  38. }}
  39. phase={testInfo.phase}
  40. link={{
  41. href: testInfo.badgeInfo?.url?.length
  42. ? testInfo.badgeInfo?.url
  43. : undefined,
  44. }}
  45. />
  46. )
  47. }