|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { |
| 4 | + Popover, |
| 5 | + PopoverAnchor, |
| 6 | + PopoverContent, |
| 7 | + PopoverDivider, |
| 8 | + PopoverItem, |
| 9 | +} from '@/components/emcn' |
| 10 | + |
| 11 | +interface UsageIndicatorContextMenuProps { |
| 12 | + /** |
| 13 | + * Whether the context menu is open |
| 14 | + */ |
| 15 | + isOpen: boolean |
| 16 | + /** |
| 17 | + * Position of the context menu |
| 18 | + */ |
| 19 | + position: { x: number; y: number } |
| 20 | + /** |
| 21 | + * Ref for the menu element |
| 22 | + */ |
| 23 | + menuRef: React.RefObject<HTMLDivElement | null> |
| 24 | + /** |
| 25 | + * Callback when menu should close |
| 26 | + */ |
| 27 | + onClose: () => void |
| 28 | + /** |
| 29 | + * Menu items configuration based on plan and permissions |
| 30 | + */ |
| 31 | + menuItems: UsageMenuItems |
| 32 | +} |
| 33 | + |
| 34 | +interface UsageMenuItems { |
| 35 | + /** |
| 36 | + * Show "Set usage limit" option |
| 37 | + */ |
| 38 | + showSetLimit: boolean |
| 39 | + /** |
| 40 | + * Show "Upgrade to Pro" option (free users) |
| 41 | + */ |
| 42 | + showUpgradeToPro: boolean |
| 43 | + /** |
| 44 | + * Show "Upgrade to Team" option (free or pro users) |
| 45 | + */ |
| 46 | + showUpgradeToTeam: boolean |
| 47 | + /** |
| 48 | + * Show "Manage seats" option (team admins) |
| 49 | + */ |
| 50 | + showManageSeats: boolean |
| 51 | + /** |
| 52 | + * Show "Upgrade to Enterprise" option |
| 53 | + */ |
| 54 | + showUpgradeToEnterprise: boolean |
| 55 | + /** |
| 56 | + * Show "Contact support" option (enterprise users) |
| 57 | + */ |
| 58 | + showContactSupport: boolean |
| 59 | + /** |
| 60 | + * Callbacks |
| 61 | + */ |
| 62 | + onSetLimit?: () => void |
| 63 | + onUpgradeToPro?: () => void |
| 64 | + onUpgradeToTeam?: () => void |
| 65 | + onManageSeats?: () => void |
| 66 | + onUpgradeToEnterprise?: () => void |
| 67 | + onContactSupport?: () => void |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Context menu component for usage indicator. |
| 72 | + * Displays plan-appropriate options in a popover at the right-click position. |
| 73 | + */ |
| 74 | +export function UsageIndicatorContextMenu({ |
| 75 | + isOpen, |
| 76 | + position, |
| 77 | + menuRef, |
| 78 | + onClose, |
| 79 | + menuItems, |
| 80 | +}: UsageIndicatorContextMenuProps) { |
| 81 | + const { |
| 82 | + showSetLimit, |
| 83 | + showUpgradeToPro, |
| 84 | + showUpgradeToTeam, |
| 85 | + showManageSeats, |
| 86 | + showUpgradeToEnterprise, |
| 87 | + showContactSupport, |
| 88 | + onSetLimit, |
| 89 | + onUpgradeToPro, |
| 90 | + onUpgradeToTeam, |
| 91 | + onManageSeats, |
| 92 | + onUpgradeToEnterprise, |
| 93 | + onContactSupport, |
| 94 | + } = menuItems |
| 95 | + |
| 96 | + const hasLimitSection = showSetLimit |
| 97 | + const hasUpgradeSection = |
| 98 | + showUpgradeToPro || showUpgradeToTeam || showUpgradeToEnterprise || showContactSupport |
| 99 | + const hasTeamSection = showManageSeats |
| 100 | + |
| 101 | + return ( |
| 102 | + <Popover |
| 103 | + open={isOpen} |
| 104 | + onOpenChange={(open) => !open && onClose()} |
| 105 | + variant='secondary' |
| 106 | + size='sm' |
| 107 | + colorScheme='inverted' |
| 108 | + > |
| 109 | + <PopoverAnchor |
| 110 | + style={{ |
| 111 | + position: 'fixed', |
| 112 | + left: `${position.x}px`, |
| 113 | + top: `${position.y}px`, |
| 114 | + width: '1px', |
| 115 | + height: '1px', |
| 116 | + }} |
| 117 | + /> |
| 118 | + <PopoverContent ref={menuRef} align='start' side='top' sideOffset={4}> |
| 119 | + {/* Limit management section */} |
| 120 | + {showSetLimit && onSetLimit && ( |
| 121 | + <PopoverItem |
| 122 | + onClick={() => { |
| 123 | + onSetLimit() |
| 124 | + onClose() |
| 125 | + }} |
| 126 | + > |
| 127 | + Set usage limit |
| 128 | + </PopoverItem> |
| 129 | + )} |
| 130 | + |
| 131 | + {/* Team management section */} |
| 132 | + {hasLimitSection && hasTeamSection && <PopoverDivider />} |
| 133 | + {showManageSeats && onManageSeats && ( |
| 134 | + <PopoverItem |
| 135 | + onClick={() => { |
| 136 | + onManageSeats() |
| 137 | + onClose() |
| 138 | + }} |
| 139 | + > |
| 140 | + Manage seats |
| 141 | + </PopoverItem> |
| 142 | + )} |
| 143 | + |
| 144 | + {/* Upgrade section */} |
| 145 | + {(hasLimitSection || hasTeamSection) && hasUpgradeSection && <PopoverDivider />} |
| 146 | + {showUpgradeToPro && onUpgradeToPro && ( |
| 147 | + <PopoverItem |
| 148 | + onClick={() => { |
| 149 | + onUpgradeToPro() |
| 150 | + onClose() |
| 151 | + }} |
| 152 | + > |
| 153 | + Upgrade to Pro |
| 154 | + </PopoverItem> |
| 155 | + )} |
| 156 | + {showUpgradeToTeam && onUpgradeToTeam && ( |
| 157 | + <PopoverItem |
| 158 | + onClick={() => { |
| 159 | + onUpgradeToTeam() |
| 160 | + onClose() |
| 161 | + }} |
| 162 | + > |
| 163 | + Upgrade to Team |
| 164 | + </PopoverItem> |
| 165 | + )} |
| 166 | + {showUpgradeToEnterprise && onUpgradeToEnterprise && ( |
| 167 | + <PopoverItem |
| 168 | + onClick={() => { |
| 169 | + onUpgradeToEnterprise() |
| 170 | + onClose() |
| 171 | + }} |
| 172 | + > |
| 173 | + Upgrade to Enterprise |
| 174 | + </PopoverItem> |
| 175 | + )} |
| 176 | + {showContactSupport && onContactSupport && ( |
| 177 | + <PopoverItem |
| 178 | + onClick={() => { |
| 179 | + onContactSupport() |
| 180 | + onClose() |
| 181 | + }} |
| 182 | + > |
| 183 | + Contact support |
| 184 | + </PopoverItem> |
| 185 | + )} |
| 186 | + </PopoverContent> |
| 187 | + </Popover> |
| 188 | + ) |
| 189 | +} |
0 commit comments