ProductMenuItem.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import Link from 'next/link'
  2. import { Badge, Button, Menu } from 'ui'
  3. import { ProductMenuGroupItem } from './ProductMenu.types'
  4. import { ShortcutTooltip } from '@/components/ui/ShortcutTooltip'
  5. interface ProductMenuItemProps {
  6. item: ProductMenuGroupItem
  7. isActive: boolean
  8. target?: '_blank' | '_self'
  9. hoverText?: string
  10. onClick?: () => void
  11. }
  12. export const ProductMenuItem = ({
  13. item,
  14. isActive,
  15. target = '_self',
  16. hoverText = '',
  17. onClick,
  18. }: ProductMenuItemProps) => {
  19. const { name = '', url = '', icon, rightIcon, isExternal, label, disabled, shortcutId } = item
  20. const menuItem = (
  21. <Menu.Item icon={icon} active={isActive} onClick={onClick}>
  22. <div className="flex w-full items-center justify-between gap-1">
  23. <div
  24. className="flex items-center gap-1 min-w-0 flex-1"
  25. title={
  26. shortcutId ? undefined : hoverText ? hoverText : typeof name === 'string' ? name : ''
  27. }
  28. >
  29. <span className="truncate flex-1 min-w-0">{name}</span>
  30. {label !== undefined && (
  31. <Badge
  32. className="shrink-0"
  33. variant={label.toLowerCase() === 'new' ? 'success' : 'warning'}
  34. >
  35. {label}
  36. </Badge>
  37. )}
  38. </div>
  39. {rightIcon && <div>{rightIcon}</div>}
  40. </div>
  41. </Menu.Item>
  42. )
  43. if (disabled) {
  44. return <div className="opacity-50 pointer-events-none">{menuItem}</div>
  45. }
  46. if (url) {
  47. if (isExternal) {
  48. const externalLink = (
  49. <Button asChild block className="justify-start!" type="text" size="small" icon={icon}>
  50. <Link href={url} target="_blank" rel="noreferrer">
  51. {name}
  52. </Link>
  53. </Button>
  54. )
  55. return shortcutId ? (
  56. <ShortcutTooltip shortcutId={shortcutId} side="right" delayDuration={1000}>
  57. {externalLink}
  58. </ShortcutTooltip>
  59. ) : (
  60. externalLink
  61. )
  62. }
  63. const link = (
  64. <Link href={url} className="block" target={target} onClick={onClick}>
  65. {menuItem}
  66. </Link>
  67. )
  68. return shortcutId ? (
  69. <ShortcutTooltip shortcutId={shortcutId} side="right" delayDuration={1000}>
  70. {link}
  71. </ShortcutTooltip>
  72. ) : (
  73. link
  74. )
  75. }
  76. return menuItem
  77. }