import LocalisedText from '@/components/LocalisedText' import PrefetchLink from '@/components/PrefetchLink' import CountsSortHeader from '@/components/counts/CountsSortHeader' import CountsSelectableTable from '@/components/counts/CountsSelectableTable' import { changeCountSort, readCountSort, selectedCountChildren, writeCountSort, orderedCountFields } from '@/lib/counts' import { countsLogicalName, countsNestedClipboardTable, countsNestedExport, countsNestedOrder, countsNestedText, countsNestedIsScalar, countsNestedScalarText } from '@/lib/countsDisplay' import { useLocale } from '@/lib/useLocale' import { useUITranslation } from '@/lib/useUITranslation' import type { CountField, CountRecord, CountsResource } from '@/lib/counts' const CountsNestedTable = ( { rows, fields, path, resource, title, params, update }: { rows: CountRecord[] fields: CountField[] path: string resource: CountsResource title: { text: string; language: string } params: URLSearchParams update: (key: string, value: string) => void }, ) => { const { message } = useUITranslation ('counts') const locale = useLocale () const selected = selectedCountChildren (params) const columns = orderedCountFields (fields, params, path) .filter (field => selected.has (`${path}.${field.key}`)) const allSorts = readCountSort (params.get ('child_sort')) const sorts = allSorts.filter (sort => sort.key.startsWith (`${path}/`)) .map (sort => ({ ...sort, key: sort.key.slice (path.length + 1) })) const ordered = countsNestedOrder (rows, path, params, locale) const scalar = (row: CountRecord, field: string) => { const value = row[field] if (value == null) return null if (field === 'tagId' || field === 'tagGroupId') return {String (value)} if (field === 'id') { if (row.kind === 'external') return String (value) const target = path === 'parents' ? resource : 'tags' return {String (value)} } const display = countsLogicalName (row, field, locale) if (typeof value === 'boolean') return {value ? '✓' : '✗'} if (display.language != null) return {display.text} if (field === 'name' && typeof row.languageCode === 'string') return {String (value)} return display.text } if (countsNestedIsScalar (fields, path, params)) return {countsNestedScalarText (rows, fields, path, params, locale)} return (
{params.get ('nested_json') === '1' ? (
	      {JSON.stringify (countsNestedExport (rows, fields, path, params, locale))}
	    
) : (columns.length > 0 && rows.length > 0 && (
field.key)} identity={`${path}:${params}`} name={title} copyTable={() => countsNestedClipboardTable (rows, fields, path, params, locale)} className="text-xs"> {columns.map (field => ( { const next = changeCountSort (sorts, key, additive) .map (sort => ({ ...sort, key: `${path}/${sort.key}` })) update ('child_sort', writeCountSort ([ ...allSorts.filter (sort => !sort.key.startsWith (`${path}/`)), ...next])) }}/>))} {ordered.map ((row, index) => ( {columns.map (field => ( {field.children ? : scalar (row, field.key)} ))} ))}
))}
) } export default CountsNestedTable