Skip to content

Commit d68091d

Browse files
committed
simplify table content
1 parent 77de141 commit d68091d

File tree

1 file changed

+31
-39
lines changed

1 file changed

+31
-39
lines changed

apps/dashboard/components/table/table-content.tsx

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,27 @@ export function TableContent<TData extends { name: string | number }>({
177177

178178
const displayData = table.getRowModel().rows;
179179
const headerGroups = table.getHeaderGroups();
180+
const activeTabConfig = tabs?.find((tab) => tab.id === activeTab);
181+
const isInteractive = !!(onRowClick || onAddFilter || onRowAction);
182+
183+
const handleRowClick = (row: TData, hasSubRows: boolean, rowId: string) => {
184+
if (hasSubRows) {
185+
toggleRowExpansion(rowId);
186+
return;
187+
}
188+
if (onRowAction) {
189+
onRowAction(row);
190+
return;
191+
}
192+
if (onAddFilter && row.name && activeTabConfig?.getFilter) {
193+
const { field, value } = activeTabConfig.getFilter(row);
194+
onAddFilter(field, value, title);
195+
return;
196+
}
197+
if (onRowClick) {
198+
onRowClick('name', row.name);
199+
}
200+
};
180201

181202

182203
if (!displayData.length) {
@@ -246,10 +267,8 @@ export function TableContent<TData extends { name: string | number }>({
246267
</TableHeader>
247268
<TableBody className="overflow-hidden">
248269
{displayData.map((row, rowIndex) => {
249-
const subRows =
250-
expandable && getSubRows ? getSubRows(row.original) : undefined;
251-
const hasSubRows = subRows && subRows.length > 0;
252-
const isExpanded = expandedRow === row.id;
270+
const subRows = expandable && getSubRows ? getSubRows(row.original) : undefined;
271+
const hasSubRows = !!subRows?.length;
253272
const percentage = getRowPercentage(row.original as PercentageRow);
254273
const gradient = percentage > 0 ? getPercentageGradient(percentage) : null;
255274

@@ -258,47 +277,22 @@ export function TableContent<TData extends { name: string | number }>({
258277
<TableRow
259278
className={cn(
260279
'relative h-11 border-border/20 pl-3 transition-all duration-300 ease-in-out',
261-
((onRowClick && !hasSubRows) || hasSubRows || onAddFilter || onRowAction) &&
262-
'cursor-pointer',
263-
!gradient &&
264-
(rowIndex % 2 === 0 ? 'bg-background/50' : 'bg-muted/10')
280+
(isInteractive || hasSubRows) && 'cursor-pointer',
281+
!gradient && (rowIndex % 2 === 0 ? 'bg-background/50' : 'bg-muted/10')
265282
)}
266-
onClick={() => {
267-
if (hasSubRows) {
268-
toggleRowExpansion(row.id);
269-
} else if (onRowAction) {
270-
onRowAction(row.original);
271-
} else if (onAddFilter && row.original.name) {
272-
const activeTabConfig = tabs?.find(
273-
(tab) => tab.id === activeTab
274-
);
275-
const filterFunc = activeTabConfig?.getFilter;
276-
if (!filterFunc) {
277-
return;
278-
}
279-
280-
const { field, value } = filterFunc(row.original);
281-
onAddFilter(field, value, title);
282-
} else if (onRowClick) {
283-
onRowClick('name', row.original.name);
284-
}
285-
}}
283+
onClick={() => handleRowClick(row.original, hasSubRows, row.id)}
286284
onKeyDown={(e) => {
287285
if (e.key === 'Enter' || e.key === ' ') {
288286
e.preventDefault();
289287
e.currentTarget.click();
290288
}
291289
}}
292-
role={
293-
((onRowClick && !hasSubRows) || hasSubRows || onAddFilter || onRowAction) ? 'button' : undefined
294-
}
290+
role={(isInteractive || hasSubRows) ? 'button' : undefined}
295291
style={{
296292
background: gradient?.background,
297293
boxShadow: gradient ? `inset 3px 0 0 0 ${gradient.accentColor}` : undefined,
298294
}}
299-
tabIndex={
300-
((onRowClick && !hasSubRows) || hasSubRows || onAddFilter || onRowAction) ? 0 : -1
301-
}
295+
tabIndex={(isInteractive || hasSubRows) ? 0 : -1}
302296
>
303297
{row.getVisibleCells().map((cell, cellIndex) => (
304298
<TableCell
@@ -317,17 +311,15 @@ export function TableContent<TData extends { name: string | number }>({
317311
<div className="flex items-center gap-2">
318312
{cellIndex === 0 && hasSubRows && (
319313
<button
320-
aria-label={
321-
isExpanded ? 'Collapse row' : 'Expand row'
322-
}
314+
aria-label={expandedRow === row.id ? 'Collapse row' : 'Expand row'}
323315
className="flex-shrink-0 rounded p-0.5 transition-colors hover:bg-sidebar-accent/60"
324316
onClick={(e) => {
325317
e.stopPropagation();
326318
toggleRowExpansion(row.id);
327319
}}
328320
type="button"
329321
>
330-
{isExpanded ? (
322+
{expandedRow === row.id ? (
331323
<ArrowDownIcon className="h-3.5 w-3.5 text-sidebar-foreground/70" />
332324
) : (
333325
<ArrowUpIcon className="h-3.5 w-3.5 text-sidebar-foreground/70" />
@@ -348,7 +340,7 @@ export function TableContent<TData extends { name: string | number }>({
348340
</TableRow>
349341

350342
{hasSubRows &&
351-
isExpanded &&
343+
expandedRow === row.id &&
352344
subRows.map((subRow, subIndex) => (
353345
<TableRow
354346
className="border-sidebar-border/10 bg-sidebar-accent/5 transition-colors hover:bg-sidebar-accent/10"

0 commit comments

Comments
 (0)