|
| 1 | +import { TextAttributes } from '@opentui/core' |
| 2 | +import { safeOpen } from '../utils/open-url' |
| 3 | +import React, { useState, useMemo, useEffect } from 'react' |
| 4 | + |
| 5 | +import { Button } from './button' |
| 6 | +import { useTerminalDimensions } from '../hooks/use-terminal-dimensions' |
| 7 | +import { useTheme } from '../hooks/use-theme' |
| 8 | +import { BORDER_CHARS } from '../utils/ui-constants' |
| 9 | + |
| 10 | +import type { AdResponse } from '../hooks/use-gravity-ad' |
| 11 | + |
| 12 | +interface ChoiceAdBannerProps { |
| 13 | + ads: AdResponse[] |
| 14 | + onImpression?: (impUrl: string) => void |
| 15 | +} |
| 16 | + |
| 17 | +const CARD_HEIGHT = 5 // border-top + 2 lines description + spacer + cta row + border-bottom |
| 18 | +const MAX_DESC_LINES = 2 |
| 19 | +const MIN_CARD_WIDTH = 60 // Minimum width per ad card to remain readable |
| 20 | + |
| 21 | +function truncateToLines(text: string, lineWidth: number, maxLines: number): string { |
| 22 | + if (lineWidth <= 0) return text |
| 23 | + const maxChars = lineWidth * maxLines |
| 24 | + if (text.length <= maxChars) return text |
| 25 | + return text.slice(0, maxChars - 1) + '…' |
| 26 | +} |
| 27 | + |
| 28 | +const extractDomain = (url: string): string => { |
| 29 | + try { |
| 30 | + const parsed = new URL(url) |
| 31 | + return parsed.hostname.replace(/^www\./, '') |
| 32 | + } catch { |
| 33 | + return url |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Calculate evenly distributed column widths that sum exactly to availableWidth. |
| 39 | + * Distributes remainder pixels across the first N columns so there's no gap. |
| 40 | + */ |
| 41 | +function columnWidths(count: number, availableWidth: number): number[] { |
| 42 | + const base = Math.floor(availableWidth / count) |
| 43 | + const remainder = availableWidth - base * count |
| 44 | + return Array.from({ length: count }, (_, i) => base + (i < remainder ? 1 : 0)) |
| 45 | +} |
| 46 | + |
| 47 | +export const ChoiceAdBanner: React.FC<ChoiceAdBannerProps> = ({ ads, onImpression }) => { |
| 48 | + const theme = useTheme() |
| 49 | + const { terminalWidth } = useTerminalDimensions() |
| 50 | + const [hoveredIndex, setHoveredIndex] = useState<number | null>(null) |
| 51 | + |
| 52 | + // Available width for cards (terminal minus left/right margin of 1 each) |
| 53 | + const colAvail = terminalWidth - 2 |
| 54 | + |
| 55 | + // Only show as many ads as fit with a healthy minimum width; hide the rest |
| 56 | + const maxVisible = Math.max(1, Math.floor(colAvail / MIN_CARD_WIDTH)) |
| 57 | + const visibleAds = useMemo( |
| 58 | + () => (ads.length > maxVisible ? ads.slice(0, maxVisible) : ads), |
| 59 | + [ads, maxVisible], |
| 60 | + ) |
| 61 | + |
| 62 | + const widths = useMemo(() => columnWidths(visibleAds.length, colAvail), [visibleAds.length, colAvail]) |
| 63 | + |
| 64 | + // Fire impressions only for visible ads |
| 65 | + useEffect(() => { |
| 66 | + if (onImpression) { |
| 67 | + for (const ad of visibleAds) { |
| 68 | + onImpression(ad.impUrl) |
| 69 | + } |
| 70 | + } |
| 71 | + }, [visibleAds, onImpression]) |
| 72 | + |
| 73 | + const hoverBorderColor = theme.link |
| 74 | + |
| 75 | + return ( |
| 76 | + <box |
| 77 | + style={{ |
| 78 | + width: '100%', |
| 79 | + flexDirection: 'column', |
| 80 | + }} |
| 81 | + > |
| 82 | + {/* Card columns */} |
| 83 | + <box |
| 84 | + style={{ |
| 85 | + marginLeft: 1, |
| 86 | + marginRight: 1, |
| 87 | + flexDirection: 'row', |
| 88 | + }} |
| 89 | + > |
| 90 | + {visibleAds.map((ad, i) => { |
| 91 | + const isHovered = hoveredIndex === i |
| 92 | + const domain = extractDomain(ad.url) |
| 93 | + const ctaText = ad.cta || ad.title || 'Learn more' |
| 94 | + |
| 95 | + return ( |
| 96 | + <Button |
| 97 | + key={ad.impUrl} |
| 98 | + onClick={() => { |
| 99 | + if (ad.clickUrl) safeOpen(ad.clickUrl) |
| 100 | + }} |
| 101 | + onMouseOver={() => setHoveredIndex(i)} |
| 102 | + onMouseOut={() => setHoveredIndex(null)} |
| 103 | + style={{ |
| 104 | + width: widths[i], |
| 105 | + height: CARD_HEIGHT, |
| 106 | + borderStyle: 'single', |
| 107 | + borderColor: isHovered ? hoverBorderColor : theme.muted, |
| 108 | + customBorderChars: BORDER_CHARS, |
| 109 | + paddingLeft: 1, |
| 110 | + paddingRight: 1, |
| 111 | + flexDirection: 'column', |
| 112 | + |
| 113 | + }} |
| 114 | + > |
| 115 | + <box style={{ flexDirection: 'row', justifyContent: 'space-between', alignItems: 'flex-start', height: MAX_DESC_LINES, overflow: 'hidden' }}> |
| 116 | + <text style={{ fg: theme.muted, flexShrink: 1 }}> |
| 117 | + {truncateToLines(ad.adText, widths[i] - 8, MAX_DESC_LINES)} |
| 118 | + </text> |
| 119 | + <text style={{ fg: theme.muted, flexShrink: 0 }}>{' Ad'}</text> |
| 120 | + </box> |
| 121 | + <box style={{ flexGrow: 1 }} /> |
| 122 | + {/* Bottom: CTA + domain */} |
| 123 | + <box style={{ flexDirection: 'row', columnGap: 1, alignItems: 'center' }}> |
| 124 | + <text |
| 125 | + style={{ |
| 126 | + fg: theme.name === 'light' ? '#ffffff' : theme.background, |
| 127 | + bg: isHovered ? theme.link : theme.muted, |
| 128 | + attributes: TextAttributes.BOLD, |
| 129 | + }} |
| 130 | + > |
| 131 | + {` ${ctaText} `} |
| 132 | + </text> |
| 133 | + <text style={{ fg: theme.muted, attributes: TextAttributes.UNDERLINE }}> |
| 134 | + {domain} |
| 135 | + </text> |
| 136 | + |
| 137 | + </box> |
| 138 | + </Button> |
| 139 | + ) |
| 140 | + })} |
| 141 | + |
| 142 | + </box> |
| 143 | + |
| 144 | + </box> |
| 145 | + ) |
| 146 | +} |
0 commit comments