import { useState } from "react";
import { Text, Button, Group, Skeleton, Stack, Alert } from "@mantine/core";
import { IconAlertCircle, IconChevronLeft, IconChevronRight } from "@tabler/icons-react";
import { useTranslation } from "react-i18next";
import { useViewData } from "../hooks/use-view-data";
import { useUpdateRow } from "../hooks/use-update-row";
import { useDatabaseRealtimeUpdates } from "../hooks/use-database-realtime-updates";
import { usePermissions } from "../hooks/use-permissions";
import { InlineEditor } from "../components/inline-editor";
import type { BridgeField, BridgeRow } from "../types/database-view.types";
import styles from "./table-renderer.module.css";
/**
* NOTE: This renderer uses plain HTML table elements.
*
* TanStack Table v8 (@tanstack/react-table) is NOT yet installed in
* apps/client/package.json. The headless table logic here is a faithful
* placeholder that mirrors the TanStack Table v8 mental model (columns derived
* from BridgeField[], rows from BridgeRow[]) so migration is a drop-in.
*
* To migrate: install @tanstack/react-table@^8, then replace the manual
* column/row loops with useReactTable() + flexRender(). The data shape
* (fields + rows) is already correct.
*
* DEPENDENCY NEEDED: @tanstack/react-table@^8
*/
const PAGE_SIZE = 50;
interface TableRendererProps {
tableId: string;
viewId: string;
bridgeUrl?: string | null;
}
interface EditingCell {
rowId: string;
fieldId: string;
}
/** Display format for a raw cell value — keeps the table readable without edit. */
function formatCellValue(value: unknown): string {
if (value === null || value === undefined) return "";
if (typeof value === "boolean") return value ? "true" : "false";
if (typeof value === "object") {
// Arrays of strings/objects (select, link fields in Baserow)
if (Array.isArray(value)) {
return value
.map((v) => (typeof v === "object" && v !== null ? (v as { value?: string }).value ?? JSON.stringify(v) : String(v)))
.join(", ");
}
// Objects (file fields, etc.)
return (value as { value?: string }).value ?? JSON.stringify(value);
}
return String(value);
}
/** Loading skeleton mimicking the table layout. */
function TableSkeleton() {
return (