@@ -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 ( / ^ w w w \. / , '' )
@@ -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