Skip to content

Commit 4936b9b

Browse files
committed
Show ad title without URL
1 parent bf6e29c commit 4936b9b

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { describe, expect, test } from 'bun:test'
2+
3+
import { getAdDisplayLabel } from '../choice-ad-banner'
4+
5+
describe('choice ad banner display label', () => {
6+
test('uses the display domain when the ad has a URL', () => {
7+
expect(
8+
getAdDisplayLabel({
9+
title: 'Example Sponsor',
10+
url: 'https://www.example.com/path',
11+
}),
12+
).toEqual({ text: 'example.com', variant: 'domain' })
13+
})
14+
15+
test('uses the ad title when the ad has no URL', () => {
16+
expect(
17+
getAdDisplayLabel({
18+
title: 'Example Sponsor',
19+
url: '',
20+
}),
21+
).toEqual({ text: 'Example Sponsor', variant: 'title' })
22+
})
23+
})

cli/src/components/choice-ad-banner.tsx

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ function truncateToLines(text: string, lineWidth: number, maxLines: number): str
2525
return text.slice(0, maxChars - 1) + '…'
2626
}
2727

28-
const extractDomain = (url: string): string => {
28+
function truncateToWidth(text: string, width: number): string {
29+
if (width <= 0) return ''
30+
if (text.length <= width) return text
31+
if (width === 1) return '…'
32+
return text.slice(0, width - 1) + '…'
33+
}
34+
35+
export const extractDomain = (url: string): string => {
2936
try {
3037
const parsed = new URL(url)
3138
return parsed.hostname.replace(/^www\./, '')
@@ -34,6 +41,17 @@ const extractDomain = (url: string): string => {
3441
}
3542
}
3643

44+
export function getAdDisplayLabel(
45+
ad: Pick<AdResponse, 'title' | 'url'>,
46+
): { text: string; variant: 'domain' | 'title' } {
47+
const url = ad.url.trim()
48+
if (url) {
49+
return { text: extractDomain(url), variant: 'domain' }
50+
}
51+
52+
return { text: ad.title.trim() || 'Sponsored', variant: 'title' }
53+
}
54+
3755
/**
3856
* Calculate evenly distributed column widths that sum exactly to availableWidth.
3957
* Distributes remainder pixels across the first N columns so there's no gap.
@@ -89,8 +107,10 @@ export const ChoiceAdBanner: React.FC<ChoiceAdBannerProps> = ({ ads, onImpressio
89107
>
90108
{visibleAds.map((ad, i) => {
91109
const isHovered = hoveredIndex === i
92-
const domain = extractDomain(ad.url)
93110
const ctaText = ad.cta || ad.title || 'Learn more'
111+
const label = getAdDisplayLabel(ad)
112+
const labelMaxWidth = Math.max(0, widths[i] - ctaText.length - 5)
113+
const labelText = truncateToWidth(label.text, labelMaxWidth)
94114

95115
return (
96116
<Button
@@ -130,8 +150,16 @@ export const ChoiceAdBanner: React.FC<ChoiceAdBannerProps> = ({ ads, onImpressio
130150
>
131151
{` ${ctaText} `}
132152
</text>
133-
<text style={{ fg: theme.muted, attributes: TextAttributes.UNDERLINE }}>
134-
{domain}
153+
<text
154+
style={{
155+
fg: theme.muted,
156+
attributes:
157+
label.variant === 'domain'
158+
? TextAttributes.UNDERLINE
159+
: TextAttributes.DIM,
160+
}}
161+
>
162+
{labelText}
135163
</text>
136164

137165
</box>

0 commit comments

Comments
 (0)