"use client"; import * as React from "react"; import type { ColumnDef } from "@tanstack/react-table"; import { DataTableServer, type DataTableServerProps, } from "./data-table"; import { StateEmpty } from "./state-empty"; import { StateError } from "./state-error"; import { StateLoading } from "./state-loading"; import { StateUnauthorized } from "./state-unauthorized"; export type TablePageStatus = "ready" | "loading" | "empty" | "error" | "unauthorized"; export interface TablePageStateLabels { loadingTitle: string; loadingDescription: string; emptyTitle: string; emptyDescription: string; errorTitle: string; errorDescription: string; unauthorizedTitle: string; unauthorizedDescription: string; } const DEFAULT_STATE_LABELS: TablePageStateLabels = { loadingTitle: "Loading", loadingDescription: "Fetching the latest data.", emptyTitle: "No results", emptyDescription: "Nothing matches the current view.", errorTitle: "Something went wrong", errorDescription: "The request could not be completed.", unauthorizedTitle: "Access required", unauthorizedDescription: "Sign in with an account that has permission to view this page.", }; export interface TablePageProps extends DataTableServerProps { title: string; description?: string; actions?: React.ReactNode; status?: TablePageStatus; stateLabels?: Partial; onRetry?: () => void; unauthorizedAction?: React.ReactNode; emptyAction?: React.ReactNode; } export function TablePage({ title, description, actions, status, stateLabels, onRetry, unauthorizedAction, emptyAction, columns, data, loading, ...tableProps }: TablePageProps) { const labels = { ...DEFAULT_STATE_LABELS, ...stateLabels }; const resolvedStatus = status ?? (loading ? "loading" : data.length === 0 ? "empty" : "ready"); return (

{title}

{description ? (

{description}

) : null}
{actions ?
{actions}
: null}
{resolvedStatus === "loading" ? ( ) : null} {resolvedStatus === "error" ? ( ) : null} {resolvedStatus === "unauthorized" ? ( ) : null} {resolvedStatus === "empty" ? ( ) : null} {resolvedStatus === "ready" ? ( columns={columns as ColumnDef[]} data={data} loading={loading} {...tableProps} /> ) : null}
); }