Skip to content

Commit d337f3b

Browse files
Fix DataTable guardrails
Add additional defensive checks to DataTable to ensure rows and sortedRows are always defined, and safeguard slice usage to prevent runtime TypeError when sorting/paginating. Update logic to consistently default to empty arrays and safely access sortedRows. X-Lovable-Edit-ID: edt-5b7c5edd-bb54-4ab3-bceb-53a47638281f
2 parents 94e9685 + 7b4b03e commit d337f3b

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/components/ui/data-table.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ export function DataTable<T extends Record<string, any>>({
200200

201201
// Sort rows
202202
const sortedRows = React.useMemo(() => {
203+
if (!filteredRows || !Array.isArray(filteredRows)) return [];
203204
if (!orderBy) return filteredRows;
204205

205206
return [...filteredRows].sort((a, b) => {
@@ -214,10 +215,9 @@ export function DataTable<T extends Record<string, any>>({
214215
}, [filteredRows, order, orderBy]);
215216

216217
// Paginate rows - with safety check
217-
const paginatedRows = sortedRows?.slice(
218-
page * rowsPerPage,
219-
page * rowsPerPage + rowsPerPage
220-
) || [];
218+
const paginatedRows = Array.isArray(sortedRows)
219+
? sortedRows.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
220+
: [];
221221

222222
// Show action column if any action handler is provided
223223
const showActions = !!(onEdit || onDelete || onView);

0 commit comments

Comments
 (0)