|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import React, { useState, useEffect, useContext, useMemo } from 'react'; |
| 10 | +import { useDataScope, SchemaRendererContext, SchemaRenderer } from '@object-ui/react'; |
| 11 | +import { extractRecords } from '@object-ui/core'; |
| 12 | +import { Skeleton, cn } from '@object-ui/components'; |
| 13 | + |
| 14 | +export interface ObjectDataTableProps { |
| 15 | + schema: { |
| 16 | + type: string; |
| 17 | + objectName?: string; |
| 18 | + dataProvider?: { provider: string; object?: string }; |
| 19 | + bind?: string; |
| 20 | + filter?: any; |
| 21 | + data?: any[]; |
| 22 | + columns?: any[]; |
| 23 | + searchable?: boolean; |
| 24 | + pagination?: boolean; |
| 25 | + className?: string; |
| 26 | + [key: string]: any; |
| 27 | + }; |
| 28 | + dataSource?: any; |
| 29 | + className?: string; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * ObjectDataTable — Async-aware wrapper for data-table. |
| 34 | + * |
| 35 | + * When `objectName` is provided and a `dataSource` is available via context |
| 36 | + * or props, fetches records automatically and passes them to the registered |
| 37 | + * `data-table` component via SchemaRenderer. |
| 38 | + * |
| 39 | + * Also auto-derives columns from fetched data keys when no explicit columns |
| 40 | + * are configured. |
| 41 | + * |
| 42 | + * Lifecycle states: |
| 43 | + * - **Loading** → skeleton placeholder |
| 44 | + * - **Error** → error message |
| 45 | + * - **Empty** → friendly "No data available" message |
| 46 | + * - **Data** → data-table with fetched rows |
| 47 | + */ |
| 48 | +export const ObjectDataTable: React.FC<ObjectDataTableProps> = ({ schema, dataSource: propDataSource, className }) => { |
| 49 | + const context = useContext(SchemaRendererContext); |
| 50 | + const dataSource = propDataSource || context?.dataSource; |
| 51 | + const boundData = useDataScope(schema.bind); |
| 52 | + |
| 53 | + const [fetchedData, setFetchedData] = useState<any[]>([]); |
| 54 | + const [loading, setLoading] = useState(false); |
| 55 | + const [error, setError] = useState<string | null>(null); |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + let isMounted = true; |
| 59 | + |
| 60 | + const fetchData = async () => { |
| 61 | + if (!dataSource || !schema.objectName) return; |
| 62 | + if (isMounted) { |
| 63 | + setLoading(true); |
| 64 | + setError(null); |
| 65 | + } |
| 66 | + try { |
| 67 | + let data: any[]; |
| 68 | + |
| 69 | + if (typeof dataSource.find === 'function') { |
| 70 | + const results = await dataSource.find(schema.objectName, { |
| 71 | + $filter: schema.filter, |
| 72 | + }); |
| 73 | + data = extractRecords(results); |
| 74 | + } else { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (isMounted) { |
| 79 | + setFetchedData(data); |
| 80 | + } |
| 81 | + } catch (e) { |
| 82 | + console.error('[ObjectDataTable] Fetch error:', e); |
| 83 | + if (isMounted) { |
| 84 | + setError(e instanceof Error ? e.message : 'Failed to load data'); |
| 85 | + } |
| 86 | + } finally { |
| 87 | + if (isMounted) setLoading(false); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + if (schema.objectName && !boundData && (!schema.data || schema.data.length === 0)) { |
| 92 | + fetchData(); |
| 93 | + } |
| 94 | + |
| 95 | + return () => { isMounted = false; }; |
| 96 | + }, [schema.objectName, dataSource, boundData, schema.data, schema.filter]); |
| 97 | + |
| 98 | + // Resolve data: bound data > static schema data > fetched data |
| 99 | + const rawData = boundData || schema.data || fetchedData; |
| 100 | + const finalData = Array.isArray(rawData) ? rawData : []; |
| 101 | + |
| 102 | + // Auto-derive columns from data keys when none are provided |
| 103 | + const derivedColumns = useMemo(() => { |
| 104 | + if (schema.columns && schema.columns.length > 0) return schema.columns; |
| 105 | + if (finalData.length === 0) return []; |
| 106 | + // Exclude internal/private fields (prefixed with '_') from auto-derived columns |
| 107 | + const keys = Object.keys(finalData[0]).filter(k => !k.startsWith('_')); |
| 108 | + // Convert camelCase keys to human-readable headers (e.g. firstName → First Name) |
| 109 | + return keys.map(k => ({ |
| 110 | + header: k.charAt(0).toUpperCase() + k.slice(1).replace(/([A-Z])/g, ' $1'), |
| 111 | + accessorKey: k, |
| 112 | + })); |
| 113 | + }, [schema.columns, finalData]); |
| 114 | + |
| 115 | + // Loading skeleton |
| 116 | + if (loading && finalData.length === 0) { |
| 117 | + return ( |
| 118 | + <div className={cn('overflow-auto', className)} data-testid="table-loading"> |
| 119 | + <div className="space-y-2 p-2"> |
| 120 | + <div className="flex gap-2"> |
| 121 | + <Skeleton className="h-6 w-1/4" /> |
| 122 | + <Skeleton className="h-6 w-1/4" /> |
| 123 | + <Skeleton className="h-6 w-1/4" /> |
| 124 | + <Skeleton className="h-6 w-1/4" /> |
| 125 | + </div> |
| 126 | + {[1, 2, 3, 4].map((i) => ( |
| 127 | + <div key={i} className="flex gap-2"> |
| 128 | + <Skeleton className="h-5 w-1/4" /> |
| 129 | + <Skeleton className="h-5 w-1/4" /> |
| 130 | + <Skeleton className="h-5 w-1/4" /> |
| 131 | + <Skeleton className="h-5 w-1/4" /> |
| 132 | + </div> |
| 133 | + ))} |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + // Error state |
| 140 | + if (error) { |
| 141 | + return ( |
| 142 | + <div className={cn('overflow-auto', className)} data-testid="table-error"> |
| 143 | + <div className="flex flex-col items-center justify-center py-8 text-destructive" data-testid="table-error-message"> |
| 144 | + <svg xmlns="http://www.w3.org/2000/svg" className="h-8 w-8 mb-2 opacity-60" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"> |
| 145 | + <circle cx="12" cy="12" r="10" /> |
| 146 | + <line x1="12" y1="8" x2="12" y2="12" /> |
| 147 | + <line x1="12" y1="16" x2="12.01" y2="16" /> |
| 148 | + </svg> |
| 149 | + <p className="text-xs">{error}</p> |
| 150 | + </div> |
| 151 | + </div> |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + // No data source available but objectName configured |
| 156 | + if (!dataSource && schema.objectName && finalData.length === 0) { |
| 157 | + return ( |
| 158 | + <div className={cn('overflow-auto', className)}> |
| 159 | + <div className="flex flex-col items-center justify-center py-8 text-muted-foreground"> |
| 160 | + <p className="text-xs">No data source available for “{schema.objectName}”</p> |
| 161 | + </div> |
| 162 | + </div> |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + // Empty state |
| 167 | + if (finalData.length === 0) { |
| 168 | + return ( |
| 169 | + <div className={cn('overflow-auto', className)} data-testid="table-empty-state"> |
| 170 | + <div className="flex flex-col items-center justify-center py-8 text-muted-foreground"> |
| 171 | + <svg xmlns="http://www.w3.org/2000/svg" className="h-8 w-8 mb-2 opacity-40" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"> |
| 172 | + <rect x="3" y="3" width="18" height="18" rx="2" ry="2" /> |
| 173 | + <line x1="3" y1="9" x2="21" y2="9" /> |
| 174 | + <line x1="9" y1="21" x2="9" y2="9" /> |
| 175 | + </svg> |
| 176 | + <p className="text-xs">No data available</p> |
| 177 | + </div> |
| 178 | + </div> |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + // Delegate to data-table via SchemaRenderer |
| 183 | + const tableSchema = { |
| 184 | + ...schema, |
| 185 | + type: 'data-table', |
| 186 | + data: finalData, |
| 187 | + columns: derivedColumns, |
| 188 | + }; |
| 189 | + |
| 190 | + return <SchemaRenderer schema={tableSchema} className={className} />; |
| 191 | +}; |
0 commit comments