|
| 1 | +import type {Id, Store} from '../@types/index.js'; |
| 2 | +import type { |
| 3 | + HtmlTableProps, |
| 4 | + RelationshipInHtmlTableProps, |
| 5 | +} from '../@types/ui-react-dom/index.js'; |
| 6 | +import type {RowProps} from '../@types/ui-react/index.d.ts'; |
| 7 | +import {arrayMap} from '../common/array.ts'; |
| 8 | +import {objToArray} from '../common/obj.ts'; |
| 9 | +import {isUndefined} from '../common/other.ts'; |
| 10 | +import {getProps, getRelationshipsStoreTableIds} from '../common/react.ts'; |
| 11 | +import {DOT, strSplit} from '../common/strings.ts'; |
| 12 | +import {CellView} from '../ui-react/components.tsx'; |
| 13 | +import { |
| 14 | + useRelationshipsOrRelationshipsById, |
| 15 | + useRemoteRowId, |
| 16 | + useRowIds, |
| 17 | + useTableCellIds, |
| 18 | +} from '../ui-react/hooks.ts'; |
| 19 | +import {EditableCellView} from './EditableCellView.tsx'; |
| 20 | +import { |
| 21 | + extraHeaders, |
| 22 | + extraRowCells, |
| 23 | + RelationshipInHtmlRowParams, |
| 24 | + useCells, |
| 25 | + useParams, |
| 26 | +} from './common.tsx'; |
| 27 | + |
| 28 | +const useDottedCellIds = (tableId: Id | undefined, store: Store | undefined) => |
| 29 | + arrayMap( |
| 30 | + useTableCellIds(tableId as Id, store), |
| 31 | + (cellId) => tableId + DOT + cellId, |
| 32 | + ); |
| 33 | + |
| 34 | +export const RelationshipInHtmlRow = ({ |
| 35 | + localRowId, |
| 36 | + params: [ |
| 37 | + idColumn, |
| 38 | + cells, |
| 39 | + localTableId, |
| 40 | + remoteTableId, |
| 41 | + relationshipId, |
| 42 | + relationships, |
| 43 | + store, |
| 44 | + extraCellsBefore, |
| 45 | + extraCellsAfter, |
| 46 | + ], |
| 47 | +}: { |
| 48 | + readonly localRowId: Id; |
| 49 | + readonly params: RelationshipInHtmlRowParams; |
| 50 | +}) => { |
| 51 | + const remoteRowId = useRemoteRowId( |
| 52 | + relationshipId, |
| 53 | + localRowId, |
| 54 | + relationships, |
| 55 | + ) as Id; |
| 56 | + const rowProps: RowProps = { |
| 57 | + tableId: localTableId ?? '', |
| 58 | + rowId: localRowId, |
| 59 | + store, |
| 60 | + }; |
| 61 | + return ( |
| 62 | + <tr> |
| 63 | + {extraRowCells(extraCellsBefore, rowProps)} |
| 64 | + {idColumn === false ? null : ( |
| 65 | + <> |
| 66 | + <th>{localRowId}</th> |
| 67 | + <th>{remoteRowId}</th> |
| 68 | + </> |
| 69 | + )} |
| 70 | + {objToArray( |
| 71 | + cells, |
| 72 | + ({component: CellView, getComponentProps}, compoundCellId) => { |
| 73 | + const [tableId, cellId] = strSplit(compoundCellId, DOT, 2); |
| 74 | + const rowId = |
| 75 | + tableId === localTableId |
| 76 | + ? localRowId |
| 77 | + : tableId === remoteTableId |
| 78 | + ? remoteRowId |
| 79 | + : null; |
| 80 | + return isUndefined(rowId) ? null : ( |
| 81 | + <td key={compoundCellId}> |
| 82 | + <CellView |
| 83 | + {...getProps(getComponentProps, rowId, cellId)} |
| 84 | + store={store} |
| 85 | + tableId={tableId} |
| 86 | + rowId={rowId} |
| 87 | + cellId={cellId} |
| 88 | + /> |
| 89 | + </td> |
| 90 | + ); |
| 91 | + }, |
| 92 | + )} |
| 93 | + {extraRowCells(extraCellsAfter, rowProps, 1)} |
| 94 | + </tr> |
| 95 | + ); |
| 96 | +}; |
| 97 | + |
| 98 | +export const RelationshipInHtmlTable = ({ |
| 99 | + relationshipId, |
| 100 | + relationships, |
| 101 | + editable, |
| 102 | + customCells, |
| 103 | + extraCellsBefore, |
| 104 | + extraCellsAfter, |
| 105 | + className, |
| 106 | + headerRow, |
| 107 | + idColumn = true, |
| 108 | +}: RelationshipInHtmlTableProps & HtmlTableProps): any => { |
| 109 | + const [resolvedRelationships, store, localTableId, remoteTableId] = |
| 110 | + getRelationshipsStoreTableIds( |
| 111 | + useRelationshipsOrRelationshipsById(relationships), |
| 112 | + relationshipId, |
| 113 | + ); |
| 114 | + const cells = useCells( |
| 115 | + [ |
| 116 | + ...useDottedCellIds(localTableId, store), |
| 117 | + ...useDottedCellIds(remoteTableId, store), |
| 118 | + ], |
| 119 | + customCells, |
| 120 | + editable ? EditableCellView : CellView, |
| 121 | + ); |
| 122 | + const params = useParams( |
| 123 | + idColumn, |
| 124 | + cells, |
| 125 | + localTableId, |
| 126 | + remoteTableId, |
| 127 | + relationshipId, |
| 128 | + resolvedRelationships, |
| 129 | + store, |
| 130 | + extraCellsBefore, |
| 131 | + extraCellsAfter, |
| 132 | + ); |
| 133 | + return ( |
| 134 | + <table className={className}> |
| 135 | + {headerRow === false ? null : ( |
| 136 | + <thead> |
| 137 | + <tr> |
| 138 | + {extraHeaders(extraCellsBefore)} |
| 139 | + {idColumn === false ? null : ( |
| 140 | + <> |
| 141 | + <th>{localTableId}.Id</th> |
| 142 | + <th>{remoteTableId}.Id</th> |
| 143 | + </> |
| 144 | + )} |
| 145 | + {objToArray(cells, ({label}, cellId) => ( |
| 146 | + <th key={cellId}>{label}</th> |
| 147 | + ))} |
| 148 | + {extraHeaders(extraCellsAfter, 1)} |
| 149 | + </tr> |
| 150 | + </thead> |
| 151 | + )} |
| 152 | + <tbody> |
| 153 | + {arrayMap(useRowIds(localTableId as Id, store), (localRowId) => ( |
| 154 | + <RelationshipInHtmlRow |
| 155 | + key={localRowId} |
| 156 | + localRowId={localRowId} |
| 157 | + params={params} |
| 158 | + /> |
| 159 | + ))} |
| 160 | + </tbody> |
| 161 | + </table> |
| 162 | + ); |
| 163 | +}; |
0 commit comments