Skip to content

Commit 3924d34

Browse files
brandonkachenjahoomagreptile-apps[bot]
authored
[codex] Show ad title when ad URL is missing (#546)
Co-authored-by: James Grugett <jahooma@gmail.com> Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent bf6e29c commit 3924d34

2 files changed

Lines changed: 54 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: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ 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+
return text.slice(0, width - 1) + '…'
32+
}
33+
34+
export const extractDomain = (url: string): string => {
2935
try {
3036
const parsed = new URL(url)
3137
return parsed.hostname.replace(/^www\./, '')
@@ -34,6 +40,17 @@ const extractDomain = (url: string): string => {
3440
}
3541
}
3642

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

95114
return (
96115
<Button
@@ -130,8 +149,16 @@ export const ChoiceAdBanner: React.FC<ChoiceAdBannerProps> = ({ ads, onImpressio
130149
>
131150
{` ${ctaText} `}
132151
</text>
133-
<text style={{ fg: theme.muted, attributes: TextAttributes.UNDERLINE }}>
134-
{domain}
152+
<text
153+
style={{
154+
fg: theme.muted,
155+
attributes:
156+
label.variant === 'domain'
157+
? TextAttributes.UNDERLINE
158+
: TextAttributes.DIM,
159+
}}
160+
>
161+
{labelText}
135162
</text>
136163

137164
</box>

0 commit comments

Comments
 (0)