-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathObjectView.tsx
More file actions
235 lines (218 loc) · 10.1 KB
/
ObjectView.tsx
File metadata and controls
235 lines (218 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { useState, useMemo } from 'react';
import { useParams, useSearchParams } from 'react-router-dom';
import { ObjectGrid } from '@object-ui/plugin-grid';
import { ObjectKanban } from '@object-ui/plugin-kanban';
import { ObjectCalendar } from '@object-ui/plugin-calendar';
import { ObjectGantt } from '@object-ui/plugin-gantt';
import { Button } from '@object-ui/components';
import { Plus, Calendar as CalendarIcon, Kanban as KanbanIcon, Table as TableIcon, AlignLeft } from 'lucide-react';
export function ObjectView({ dataSource, objects, onEdit }: any) {
const { objectName } = useParams();
const [searchParams, setSearchParams] = useSearchParams();
const [refreshKey, setRefreshKey] = useState(0);
// Get Object Definition
const objectDef = objects.find((o: any) => o.name === objectName);
if (!objectDef) {
return (
<div className="flex flex-col items-center justify-center h-full text-muted-foreground bg-muted/30 rounded-lg border border-dashed p-8 m-4">
<h3 className="font-semibold text-lg">Object Not Found</h3>
<p>The object "{objectName}" does not exist in the current configuration.</p>
</div>
);
}
// Resolve Views
const views = useMemo(() => {
const definedViews = objectDef.list_views || {};
const viewList = Object.entries(definedViews).map(([key, value]: [string, any]) => ({
id: key,
...value,
type: value.type || 'grid'
}));
// Ensure at least one default view exists
if (viewList.length === 0) {
viewList.push({
id: 'all',
label: 'All Records',
type: 'grid',
columns: objectDef.fields ? Object.keys(objectDef.fields).slice(0, 5) : []
});
}
return viewList;
}, [objectDef]);
// Active View State
const activeViewId = searchParams.get('view') || views[0]?.id;
const activeView = views.find((v: any) => v.id === activeViewId) || views[0];
// Helper: Normalize Columns for Grid
const getGridColumns = (view: any) => {
if (!view.columns) return [];
return view.columns.map((colName: string) => {
// Find field definition
const fieldDef = Array.isArray(objectDef.fields)
? objectDef.fields.find((f: any) => f.name === colName)
: objectDef.fields?.[colName];
return {
field: colName,
label: fieldDef?.label || colName,
width: 150
};
});
};
const handleViewChange = (viewId: string) => {
setSearchParams({ view: viewId });
};
const renderCurrentView = () => {
const key = `${objectName}-${activeView.id}-${refreshKey}`;
const commonProps = {
dataSource,
className: "h-full border-none"
};
// Define onRowClick/Edit handlers
const interactionProps = {
onEdit,
onRowClick: (record: any) => onEdit(record), // Default to edit on click
};
switch (activeView.type) {
case 'kanban':
return (
<ObjectKanban
key={key}
{...commonProps}
schema={{
type: 'kanban',
objectName: objectDef.name,
groupBy: activeView.groupBy || 'status',
columns: activeView.columns,
cardTitle: objectDef.titleField || 'name', // Default title field
cardFields: activeView.columns
}}
{...interactionProps}
/>
);
case 'calendar':
return (
<ObjectCalendar
key={key}
{...commonProps}
schema={{
type: 'calendar',
objectName: objectDef.name,
dateField: activeView.dateField || 'due_date',
endField: activeView.endField,
titleField: activeView.titleField || 'name',
colorField: activeView.colorField,
}}
{...interactionProps}
/>
);
case 'gantt':
return (
<ObjectGantt
key={key}
{...commonProps}
schema={{
type: 'object-grid',
objectName: objectDef.name,
// Gantt config is read by ObjectGantt via getGanttConfig helper
// TypeScript workaround: gantt property not in ObjectGridSchema but supported by implementation
gantt: {
startDateField: activeView.startDateField || 'start_date',
endDateField: activeView.endDateField || 'end_date',
titleField: activeView.titleField || 'name',
progressField: activeView.progressField || 'progress',
dependenciesField: activeView.dependenciesField,
colorField: activeView.colorField,
}
} as any}
{...interactionProps}
/>
);
case 'grid':
default:
return (
<ObjectGrid
key={key}
{...commonProps}
schema={{
type: 'object-grid',
objectName: objectDef.name,
filterable: true,
columns: getGridColumns(activeView),
filter: activeView.filter,
sort: activeView.sort
}}
{...interactionProps}
onDelete={async (record: any) => {
if (confirm(`Delete record?`)) {
await dataSource.delete(objectName, record.id || record._id);
setRefreshKey(k => k + 1);
}
}}
/>
);
}
};
return (
<div className="h-full flex flex-col bg-background">
{/* 1. Main Header */}
<div className="flex justify-between items-center py-3 px-4 border-b shrink-0 bg-background z-10">
<div className="flex items-center gap-3">
<div className="bg-primary/10 p-2 rounded-md shrink-0">
<TableIcon className="h-4 w-4 text-primary" />
</div>
<div>
<h1 className="text-lg font-semibold tracking-tight text-foreground">{objectDef.label}</h1>
</div>
</div>
<div className="flex items-center gap-2">
<Button size="sm" onClick={() => onEdit(null)} className="shadow-none gap-2">
<Plus className="h-4 w-4" />
<span className="hidden sm:inline">New</span>
</Button>
</div>
</div>
{/* 2. View Toolbar (Tabs & Controls) */}
<div className="flex justify-between items-center py-2 px-4 border-b shrink-0 bg-muted/20">
{/* Left: View Tabs */}
<div className="flex items-center gap-1 overflow-x-auto no-scrollbar">
{views.map((v: any) => {
const isActive = activeView.id === v.id;
return (
<button
key={v.id}
onClick={() => handleViewChange(v.id)}
className={`
flex items-center gap-2 px-3 py-1.5 rounded-md text-sm font-medium transition-all whitespace-nowrap
${isActive
? 'bg-background shadow-sm text-foreground ring-1 ring-border/50'
: 'text-muted-foreground hover:bg-background/50 hover:text-foreground'
}
`}
>
{v.type === 'kanban' && <KanbanIcon className="h-3.5 w-3.5 opacity-70" />}
{v.type === 'calendar' && <CalendarIcon className="h-3.5 w-3.5 opacity-70" />}
{v.type === 'grid' && <TableIcon className="h-3.5 w-3.5 opacity-70" />}
{v.type === 'gantt' && <AlignLeft className="h-3.5 w-3.5 opacity-70" />}
<span>{v.label}</span>
</button>
);
})}
</div>
{/* Right: View Options (Placeholder for now) */}
<div className="flex items-center gap-2 hidden md:flex">
<Button variant="ghost" size="sm" className="h-8 text-muted-foreground">
<span className="text-xs">Filter</span>
</Button>
<Button variant="ghost" size="sm" className="h-8 text-muted-foreground">
<span className="text-xs">Sort</span>
</Button>
</div>
</div>
{/* 3. Content Area (Edge-to-Edge) */}
<div className="flex-1 overflow-hidden relative">
<div className="absolute inset-0">
{renderCurrentView()}
</div>
</div>
</div>
);
}