'use client' import { useRowLabel, useTranslation } from '@payloadcms/ui' import type { AdminText } from '../admin-i18n/define.js' import { ipalT } from '../admin-i18n/t.js' export type RowLabelProps = { /** * Shown with the row number when every path is empty, e.g. "Button" → "Button 02". * A string or `{ en, pl }`; default "Item" in the admin language. */ fallback?: AdminText | Record | string /** Dot paths inside the row, tried in order — the first non-empty text wins. */ fields?: string[] } const at = (obj: unknown, path: string): unknown => path.split('.').reduce((o, k) => (o && typeof o === 'object' ? (o as Record)[k] : undefined), obj) const toText = (v: unknown): string => { if (typeof v === 'string') {return v.trim()} if (typeof v === 'number') {return String(v)} // populated relationship / upload → its title, name, filename or alt if (v && typeof v === 'object') { for (const k of ['title', 'name', 'label', 'alt', 'filename']) { const t = (v as Record)[k] if (typeof t === 'string' && t.trim()) {return t.trim()} } } return '' } /** * Array row title in the admin panel: shows the row's own content ("Contact us", * "Fast delivery") instead of "Item 01", so an editor finds the right row * without expanding every one. Wire it with `rowLabel()`. */ export function RowLabel({ fallback, fields = ['heading', 'title', 'label', 'link.label', 'name'] }: RowLabelProps) { const { data, rowNumber } = useRowLabel>() const { i18n, t } = useTranslation() for (const f of fields) { const text = toText(at(data, f)) if (text) {return {text.length > 60 ? `${text.slice(0, 57)}…` : text}} } const name = typeof fallback === 'string' ? fallback : fallback ? ((fallback as Record)[i18n.language] ?? fallback.en ?? Object.values(fallback)[0]) : ipalT(t, 'rowLabelItem') return {`${name} ${String((rowNumber ?? 0) + 1).padStart(2, '0')}`} }