diff --git a/projects/packages/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels b/projects/packages/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels new file mode 100644 index 000000000000..93492cf7ed3a --- /dev/null +++ b/projects/packages/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Leaderboards: Match the Most viewed and Videos row labels to the other leaderboard widgets. diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/README.md b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/README.md index 4094c0b2151b..49be6a854378 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/README.md +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/README.md @@ -142,15 +142,26 @@ const row = { A `postLink` row carries no media, and never becomes a chart button: a chart row that is a button cannot nest an anchor. -Inside a widget, use `LeaderboardPostLabel` instead of building the action by hand. It reads the -report window from `WidgetRootContext` and passes it as `search`, so the detail page opens on +Video rows use `videoLink`, which delegates to `VideoTitleLink` so the row reaches the video +detail route instead of the post one. Same constraints: no media, never a chart button. + +```tsx +action: { kind: 'videoLink', id: 9, search: { from: '2026-03-01', to: '2026-03-10' } }, +``` + +Inside a widget, use `LeaderboardPostLabel` instead of building a post action by hand. It reads +the report window from `WidgetRootContext` and passes it as `search`, so the detail page opens on the range the row was read against: ```tsx ``` -Pass `section` to open a named tab on the detail page, such as `email-opens`. +Pass `section` to `LeaderboardPostLabel` to open a named tab on the detail page, such as +`email-opens`. + +There is no `LeaderboardVideoLabel`, so a widget building a `videoLink` action takes that same +window from `useWidgetNavigationSearch()` and passes it as `search`. `LeaderboardRowMedia` provides five semantic media variants. The variant owns its size, fallback, and default alt-text policy: diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/__tests__/leaderboard-row.test.tsx b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/__tests__/leaderboard-row.test.tsx index b7f1d04a851f..aa1dc4ac9ac5 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/__tests__/leaderboard-row.test.tsx +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/__tests__/leaderboard-row.test.tsx @@ -7,6 +7,43 @@ import { fireEvent, render, screen } from '@testing-library/react'; */ import { LeaderboardLabel } from '../leaderboard-label'; import { buildLeaderboardRow, resolveLeaderboardRowAction } from '../leaderboard-row'; +import type { AnchorHTMLAttributes, ReactNode } from 'react'; + +type MockRouteLinkProps = { + to: string; + params?: Record< string, unknown >; + search?: Record< string, unknown >; + children: ReactNode; +} & Omit< AnchorHTMLAttributes< HTMLAnchorElement >, 'href' >; + +// `forwardRef`, because the design system link that renders this forwards a ref. +jest.mock( '@wordpress/route', () => { + const { forwardRef } = jest.requireActual( 'react' ) as typeof import('react'); + + return { + Link: forwardRef< HTMLAnchorElement, MockRouteLinkProps >( + ( { to, params, search, children, ...props }, ref ) => { + const path = Object.entries( params ?? {} ).reduce( + ( result, [ key, value ] ) => result.replace( `$${ key }`, String( value ) ), + to + ); + const query = new URLSearchParams(); + Object.entries( search ?? {} ).forEach( ( [ key, value ] ) => { + if ( value !== undefined && value !== null ) { + query.set( key, String( value ) ); + } + } ); + const queryString = query.toString(); + + return ( + + { children } + + ); + } + ), + }; +} ); describe( 'LeaderboardLabel', () => { it( 'renders media and text without adding row actions', () => { @@ -94,7 +131,7 @@ describe( 'buildLeaderboardRow', () => { const row = buildLeaderboardRow( { label: 'Pricing', media: { kind: 'none' }, - action: { kind: 'postLink', href: 'https://example.com/pricing/' }, + action: { kind: 'postLink', href: 'https://example.com/pricing/', search: {} }, } ); render( row.label ); @@ -106,6 +143,27 @@ describe( 'buildLeaderboardRow', () => { expect( row ).not.toHaveProperty( 'onClick' ); } ); + it( 'routes a video link to the video detail page with the report window', () => { + const row = buildLeaderboardRow( { + label: 'Launch teaser', + media: { kind: 'none' }, + action: { + kind: 'videoLink', + id: 9, + href: 'https://example.com/launch-teaser/', + search: { date_start: '2026-08-01', date_end: '2026-08-26' }, + }, + } ); + + render( row.label ); + + expect( screen.getByRole( 'link', { name: /Launch teaser/ } ) ).toHaveAttribute( + 'href', + '/video/9?date_start=2026-08-01&date_end=2026-08-26' + ); + expect( row ).not.toHaveProperty( 'onClick' ); + } ); + it( 'returns chart button props for a drill-down without nesting an action', () => { const onClick = jest.fn(); const row = buildLeaderboardRow( { diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-label.module.scss b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-label.module.scss index ca09fea316f8..f9fa7aa9fcb3 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-label.module.scss +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-label.module.scss @@ -52,6 +52,8 @@ --jpa-leaderboard-image-fit: cover; } +/* The chart insets its own `.label` in overlay mode, but an element label + * bypasses that rule — and the overlay bar takes its height from this row. */ .row, .rowLink { display: flex; diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx index 59f237556128..8440ba158ffa 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx @@ -6,6 +6,7 @@ import { Link } from '@jetpack-premium-analytics/externals'; * Internal dependencies */ import { PostTitleLink } from '../post-title-link'; +import { VideoTitleLink } from '../video-title-link'; import { LeaderboardLabel, type LeaderboardRowMedia } from './leaderboard-label'; import styles from './leaderboard-label.module.scss'; import type { MouseEvent, ReactElement } from 'react'; @@ -19,8 +20,24 @@ export type LeaderboardRowAction = id?: number | string; /** Public URL. It becomes the link itself when there is no post ID. */ href?: string | null; - /** Search parameters for the detail route, such as the report window. */ - search?: Record< string, unknown >; + /** + * Required, so a row cannot silently open the detail page on its own + * default range. Pass `{}` to navigate without a window on purpose. + */ + search: Record< string, unknown >; + } + | { + /** A video with a detail page inside the dashboard. */ + kind: 'videoLink'; + /** Video ID. Rows carrying one link to the internal detail route. */ + id?: number | string; + /** Public URL. It becomes the link itself when there is no video ID. */ + href?: string | null; + /** + * Required, so a row cannot silently open the detail page on its own + * default range. Pass `{}` to navigate without a window on purpose. + */ + search: Record< string, unknown >; } | { kind: 'drillDown'; @@ -85,19 +102,19 @@ export function resolveLeaderboardRowAction( /** * Render the shared leaderboard row chrome around a label. * - * Link actions own the anchor and its new-tab affordance. Post links delegate - * to `PostTitleLink`, which picks the internal detail route, the public URL, or - * plain text for the row. Drill-down actions stay non-interactive here because - * `LeaderboardChart` turns the whole row into a button; `buildLeaderboardRow` - * passes that action to the chart. + * Drill-down actions stay non-interactive here because `LeaderboardChart` turns + * the whole row into a button; `buildLeaderboardRow` passes that action to the + * chart. * * @return A single label element accepted by `LeaderboardEntry.label`. */ export function LeaderboardRow( { label, media, action }: LeaderboardRowProps ): ReactElement { - // Post rows carry no media, so the row chrome goes on the anchor itself. - if ( action.kind === 'postLink' ) { + // Title-link rows carry no media, so the row chrome goes on the title element. + if ( action.kind === 'postLink' || action.kind === 'videoLink' ) { + const TitleLink = action.kind === 'postLink' ? PostTitleLink : VideoTitleLink; + return ( - { const titleLink = await screen.findByRole( 'link', { name: /^Hello World Post$/ } ); expect( titleLink ).toHaveAttribute( 'href', expect.stringContaining( '/post/1' ) ); expect( titleLink ).not.toHaveAttribute( 'target' ); + expect( titleLink ).toHaveAttribute( 'title', 'Hello World Post' ); // A row with a detail page carries no link out to the live post: the // external-link icon marks destinations outside the app, and the detail @@ -529,6 +530,7 @@ describe( 'TopPostsWidget', () => { expect( screen.getByText( 'About Page' ) ).toBeInTheDocument(); // The homepage entry has no URL — it must not render as a link. expect( screen.queryByRole( 'link', { name: /Homepage/ } ) ).not.toBeInTheDocument(); + expect( screen.getByTitle( 'Homepage (Latest posts)' ) ).toBeInTheDocument(); } ); it( 'gates archive comparison UI on overlapping archive types', async () => { diff --git a/projects/packages/premium-analytics/widgets/top-posts/render.tsx b/projects/packages/premium-analytics/widgets/top-posts/render.tsx index 41f3157c460c..f5e709e4d9c3 100644 --- a/projects/packages/premium-analytics/widgets/top-posts/render.tsx +++ b/projects/packages/premium-analytics/widgets/top-posts/render.tsx @@ -8,11 +8,9 @@ import { type StatsTopPostsComparisonItem, } from '@jetpack-premium-analytics/data'; import { reports } from '@jetpack-premium-analytics/icons'; -import { pickReportDateParams } from '@jetpack-premium-analytics/routing'; import { LeaderboardChart, LeaderboardSkeleton, - PostTitleLink, ReportLink, RowsCsvDownloadButton, WIDGET_ROW_LIMIT, @@ -20,19 +18,21 @@ import { WidgetFooter, WidgetRoot, WidgetState, + buildLeaderboardRow, calculateDelta, getCombinedPeriodMax, safeHttpUrl, sharePercentage, useReportCsvExport, useWidgetDrillDown, + useWidgetNavigationSearch, useWidgetRootContext, type CsvColumn, type LeaderboardChartData, + type LeaderboardRowAction, type ReportParamsFieldAttributes, } from '@jetpack-premium-analytics/widgets-toolkit'; import { __, sprintf } from '@wordpress/i18n'; -import { Text } from '@jetpack-premium-analytics/externals'; import { useCallback, useEffect, useMemo } from 'react'; /** * Internal dependencies @@ -87,19 +87,35 @@ type TopPostsWidgetProps = WidgetRenderProps< TopPostsRenderAttributes >; const DATA_FORMAT = { type: 'number' as const, options: { useMultipliers: true, decimals: 0 } }; +/** Pick the one action a top-posts row exposes. */ +function resolveRowAction( + row: TopPostRow, + detailSearch: Record< string, unknown >, + onDrillDown?: ( row: TopPostRow ) => void +): LeaderboardRowAction { + if ( ! row.children?.length ) { + return { kind: 'postLink', id: row.postId, href: row.href, search: detailSearch }; + } + + if ( ! onDrillDown ) { + return { kind: 'static' }; + } + + return { + kind: 'drillDown', + onClick: () => onDrillDown( row ), + ariaLabel: sprintf( + /* translators: %s is an archive category label, e.g. "Searches". */ + __( 'View %s archive pages', 'jetpack-premium-analytics-pkg' ), + row.label + ), + }; +} + /** * Maps normalized top-posts rows onto the shape `LeaderboardChart` expects. - * Current shares are computed relative to the most-viewed row so the overlay - * bars are proportional. When `withComparison` is set, previous-period shares - * and per-row deltas are derived from each row's `previousValue`; otherwise - * the comparison fields are zeroed. - * - * Titles route through `PostTitleLink`: post/page rows navigate to the - * internal detail page through the router, and rows without a post ID (the - * Archives view) fall back to the public URL and take the external-link icon. - * Rows with children instead become drill-down rows (per the widget - * drill-down convention they carry no anchors). The label fills its row so - * the leaderboard overlay bar gets its height from it. + * Shares use the largest value across both periods as one denominator, so + * equal-width bars represent equal values. */ function buildLeaderboardData( rows: TopPostRow[], @@ -114,34 +130,14 @@ function buildLeaderboardData( return rows.map( ( row, index ) => { const previousValue = row.previousValue; - const hasChildren = !! row.children?.length; return { id: `${ index }-${ row.href ?? row.label }`, - label: ( - - { /* Rows inside a drill-down button cannot carry anchors. */ } - { hasChildren ? ( - - { row.label } - - ) : ( - - ) } - - ), + ...buildLeaderboardRow( { + label: row.label, + media: { kind: 'none' }, + action: resolveRowAction( row, detailSearch, onDrillDown ), + } ), currentValue: row.value, currentShare: sharePercentage( row.value, maxViews ), // Rows without a comparison-period match keep `undefined` so the chart @@ -155,15 +151,6 @@ function buildLeaderboardData( withComparison && previousValue !== undefined ? calculateDelta( row.value, previousValue ) : undefined, - ...( hasChildren && - onDrillDown && { - onClick: () => onDrillDown( row ), - ariaLabel: sprintf( - /* translators: %s is an archive category label, e.g. "Searches". */ - __( 'View %s archive pages', 'jetpack-premium-analytics-pkg' ), - row.label - ), - } ), }; } ); } @@ -264,7 +251,7 @@ function TopPostsReport() { useStatsTopPosts( statsParams, { maxRows: WIDGET_ROW_LIMIT } ); const rows = useMemo( () => toTopPostRows( comparisonRows?.rows ?? [] ), [ comparisonRows ] ); - const detailSearch = useMemo( () => pickReportDateParams( reportParams ), [ reportParams ] ); + const detailSearch = useWidgetNavigationSearch(); const withComparison = hasComparison; // Serialize whatever the leaderboard has loaded, mirroring the Jetpack Stats diff --git a/projects/packages/premium-analytics/widgets/top-posts/style.module.css b/projects/packages/premium-analytics/widgets/top-posts/style.module.css index d724290d4fdd..210d65ca1d7c 100644 --- a/projects/packages/premium-analytics/widgets/top-posts/style.module.css +++ b/projects/packages/premium-analytics/widgets/top-posts/style.module.css @@ -14,55 +14,3 @@ flex: 1 1 0; min-height: 0; } - -/* Vertical padding sets the overlay bar height — the bar fills the row, - whose height is driven by this label (there is no image to size it). - The inline-start inset keeps the text off the bar's rounded left edge: - the chart applies it to its default `.label` in overlay mode, but a - custom label element bypasses that rule, so we mirror it here. */ -.labelRow { - display: flex; - align-items: center; - gap: var(--wpds-dimension-gap-xs); - min-width: 0; - padding-block: var(--wpds-dimension-padding-md); - padding-inline-start: var(--wpds-dimension-padding-sm); -} - -/* `min-width: 0` lets the flex item shrink below its content width so the - ellipsis applies; without it a long title pushes the trailing icon out. The - gap matches the leaderboard rows' `.rowLink`, so the outbound marker sits - at the same distance from the title in every widget. */ -.labelTitle, -.labelTitleLink, -.labelExternalLink { - min-width: 0; - display: inline-flex; - align-items: center; - gap: var(--wpds-dimension-gap-xs); -} - -/* The label text carries the ellipsis in every branch: `text-overflow` needs - an inline formatting context, which the flex containers above do not give - it. */ -.labelText { - min-width: 0; - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; -} - -.labelTitleLink, -.labelExternalLink { - color: inherit; - text-decoration: none; -} - -/* Underline only the title text, matching the leaderboard rows' hover rule — - the outbound marker stays clean instead of taking the underline with it. */ -.labelTitleLink:hover .labelText, -.labelTitleLink:focus .labelText, -.labelExternalLink:hover .labelText, -.labelExternalLink:focus .labelText { - text-decoration: underline; -} diff --git a/projects/packages/premium-analytics/widgets/videopress/render.tsx b/projects/packages/premium-analytics/widgets/videopress/render.tsx index 29ab9902f2cf..e7fbe02723a9 100644 --- a/projects/packages/premium-analytics/widgets/videopress/render.tsx +++ b/projects/packages/premium-analytics/widgets/videopress/render.tsx @@ -2,19 +2,19 @@ * External dependencies */ import { useStatsVideoPlays } from '@jetpack-premium-analytics/data'; -import { pickReportDateParams } from '@jetpack-premium-analytics/routing'; import { LeaderboardChart, LeaderboardSkeleton, ReportLink, - VideoTitleLink, WIDGET_ROW_LIMIT, WidgetFooter, WidgetRoot, WidgetState, + buildLeaderboardRow, calculateDelta, getCombinedPeriodMax, sharePercentage, + useWidgetNavigationSearch, useWidgetRootContext, type LeaderboardChartData, type ReportParamsFieldAttributes, @@ -42,28 +42,6 @@ type VideoPressWidgetProps = WidgetRenderProps< VideoPressRenderAttributes > & { setError?: ComponentProps< typeof WidgetRoot >[ 'setError' ]; }; -/** - * Build a video row's title. Attachment rows navigate to the internal detail - * route; rows without an ID retain the original external-link fallback. - */ -function buildVideoTitle( row: VideoPlaysRow, search: Record< string, unknown > ): JSX.Element { - return ( - - ); -} - /** * Maps normalized video rows onto the shape `LeaderboardChart` expects. Shares * are computed against the largest value of either period so the overlay bars @@ -72,7 +50,7 @@ function buildVideoTitle( row: VideoPlaysRow, search: Record< string, unknown > */ function buildLeaderboardData( rows: VideoPlaysRow[], - search: Record< string, unknown > + detailSearch: Record< string, unknown > ): LeaderboardChartData { const maxPlays = getCombinedPeriodMax( rows.map( row => row.plays ), @@ -81,7 +59,11 @@ function buildLeaderboardData( return rows.map( row => ( { id: row.key, - label: buildVideoTitle( row, search ), + ...buildLeaderboardRow( { + label: row.label, + media: { kind: 'none' }, + action: { kind: 'videoLink', id: row.id, href: row.link, search: detailSearch }, + } ), currentValue: row.plays, currentShare: sharePercentage( row.plays, maxPlays ), previousValue: row.previousPlays, @@ -98,6 +80,7 @@ function buildLeaderboardData( */ function VideoPressReport() { const { reportParams } = useWidgetRootContext(); + const detailSearch = useWidgetNavigationSearch(); const statsParams = useMemo( () => ( { ...reportParams, max: WIDGET_ROW_LIMIT } ), [ reportParams ] @@ -114,8 +97,6 @@ function VideoPressReport() { const isInitialLoading = isLoading || primary.isPending; const rows = useMemo( () => toVideoPlaysRows( comparisonRows?.rows ?? [] ), [ comparisonRows ] ); - const detailSearch = useMemo( () => pickReportDateParams( reportParams ), [ reportParams ] ); - const chartData = useMemo( () => buildLeaderboardData( rows, detailSearch ), [ rows, detailSearch ] diff --git a/projects/packages/premium-analytics/widgets/videopress/style.module.css b/projects/packages/premium-analytics/widgets/videopress/style.module.css index 252e70468a4c..5a943c435006 100644 --- a/projects/packages/premium-analytics/widgets/videopress/style.module.css +++ b/projects/packages/premium-analytics/widgets/videopress/style.module.css @@ -1,50 +1,3 @@ -.labelLink, -.labelText, -.internalLink { - - /* `inline-flex` keeps the outbound marker outside the clipped text: the - inner text span carries the ellipsis, so a long title truncates while - the marker stays visible. `min-width: 0` lets the box shrink below its - content width; `max-width: 100%` keeps it inside the label slot. The gap - matches the leaderboard rows' `.rowLink`, so the marker sits at the same - distance from the title in every widget. */ - display: inline-flex; - align-items: center; - min-width: 0; - max-width: 100%; - gap: var(--wpds-dimension-gap-xs); - - /* Vertical padding sets the overlay bar height — the bar fills the row, - whose height is driven by this label (there is no image to size it). */ - padding-block: var(--wpds-dimension-padding-md); - - /* Inset the text from the bar's rounded left edge. The chart applies this to - its default `.label` in overlay mode (`.is-overlay .label`), but a custom - label element bypasses that rule, so we mirror it here. */ - padding-inline-start: var(--wpds-dimension-padding-sm); - color: inherit; - text-decoration: none; -} - -/* The label text carries the ellipsis in every branch: `text-overflow` needs - an inline formatting context, which the flex containers above do not give - it. */ -.titleText { - min-width: 0; - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; -} - -/* Underline only the title text, matching the leaderboard rows' hover rule — - the outbound marker stays clean instead of taking the underline with it. */ -.labelLink:hover .titleText, -.labelLink:focus .titleText, -.internalLink:hover .titleText, -.internalLink:focus .titleText { - text-decoration: underline; -} - .root { container-type: inline-size; display: flex; diff --git a/projects/plugins/jetpack/changelog/wooa7s-1989-migrate-custom-title-labels b/projects/plugins/jetpack/changelog/wooa7s-1989-migrate-custom-title-labels new file mode 100644 index 000000000000..b9f497569f31 --- /dev/null +++ b/projects/plugins/jetpack/changelog/wooa7s-1989-migrate-custom-title-labels @@ -0,0 +1,4 @@ +Significance: patch +Type: enhancement + +Premium Analytics: Match the Most viewed and Videos leaderboard row labels to the other widgets. diff --git a/projects/plugins/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels b/projects/plugins/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels new file mode 100644 index 000000000000..93492cf7ed3a --- /dev/null +++ b/projects/plugins/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Leaderboards: Match the Most viewed and Videos row labels to the other leaderboard widgets.