From 190f6a58bdc4af80bb0f0db39202815abec74266 Mon Sep 17 00:00:00 2001 From: Developer Date: Wed, 11 Mar 2026 14:20:20 +0800 Subject: [PATCH] feat: add videoRows utility for big-card list layout --- utils/videoRows.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 utils/videoRows.ts diff --git a/utils/videoRows.ts b/utils/videoRows.ts new file mode 100644 index 0000000..d110e62 --- /dev/null +++ b/utils/videoRows.ts @@ -0,0 +1,38 @@ +import type { VideoItem } from '../services/types'; + +export interface NormalRow { + type: 'pair'; + left: VideoItem; + right: VideoItem | null; +} + +export interface BigRow { + type: 'big'; + item: VideoItem; +} + +export type ListRow = NormalRow | BigRow; + +/** + * Transform a flat VideoItem array into display rows. + * The last item always becomes a full-width BigRow. + * All preceding items are grouped into NormalRow pairs. + */ +export function toListRows(videos: VideoItem[]): ListRow[] { + if (videos.length === 0) return []; + if (videos.length === 1) return [{ type: 'big', item: videos[0] }]; + + const rows: ListRow[] = []; + const body = videos.slice(0, videos.length - 1); + + for (let i = 0; i < body.length; i += 2) { + rows.push({ + type: 'pair', + left: body[i], + right: body[i + 1] ?? null, + }); + } + + rows.push({ type: 'big', item: videos[videos.length - 1] }); + return rows; +}