feat: excel exports, bulk asset import, and responsive layout improvements
This commit is contained in:
@@ -0,0 +1,373 @@
|
||||
import { useRef, useState } from "react";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import {
|
||||
Download,
|
||||
FileSpreadsheet,
|
||||
Upload,
|
||||
CheckCircle2,
|
||||
AlertCircle,
|
||||
X,
|
||||
} from "lucide-react";
|
||||
import { assetsApi, type BulkImportResult } from "@/api/assets";
|
||||
import { ApiError } from "@/api/client";
|
||||
import { Modal } from "@/components/ui/dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { toast } from "@/components/ToastProvider";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
interface BulkAssetImportModalProps {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
const ACCEPTED_EXTS = [".xlsx", ".xls", ".csv"];
|
||||
|
||||
export function BulkAssetImportModal({ open, onClose }: BulkAssetImportModalProps) {
|
||||
const qc = useQueryClient();
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const [selectedFile, setSelectedFile] = useState<File | null>(null);
|
||||
const [dragActive, setDragActive] = useState(false);
|
||||
const [downloadingTemplate, setDownloadingTemplate] = useState(false);
|
||||
const [validating, setValidating] = useState(false);
|
||||
const [importing, setImporting] = useState(false);
|
||||
const [skipInvalid, setSkipInvalid] = useState(false);
|
||||
const [preview, setPreview] = useState<BulkImportResult | null>(null);
|
||||
const [errorMsg, setErrorMsg] = useState<string | null>(null);
|
||||
|
||||
function resetState() {
|
||||
setSelectedFile(null);
|
||||
setPreview(null);
|
||||
setErrorMsg(null);
|
||||
setSkipInvalid(false);
|
||||
setValidating(false);
|
||||
setImporting(false);
|
||||
if (fileInputRef.current) fileInputRef.current.value = "";
|
||||
}
|
||||
|
||||
function handleClose() {
|
||||
if (importing) return;
|
||||
resetState();
|
||||
onClose();
|
||||
}
|
||||
|
||||
async function handleDownloadTemplate() {
|
||||
setDownloadingTemplate(true);
|
||||
setErrorMsg(null);
|
||||
try {
|
||||
await assetsApi.downloadImportTemplate();
|
||||
toast.success("Downloaded asset_import_template.xlsx");
|
||||
} catch (err) {
|
||||
const msg =
|
||||
err instanceof ApiError
|
||||
? err.detail
|
||||
: "Failed to download import template.";
|
||||
setErrorMsg(msg);
|
||||
toast.error(msg);
|
||||
} finally {
|
||||
setDownloadingTemplate(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleFileSelect(files: FileList | null) {
|
||||
if (!files || files.length === 0) return;
|
||||
const file = files[0];
|
||||
const lower = file.name.toLowerCase();
|
||||
if (!ACCEPTED_EXTS.some((ext) => lower.endsWith(ext))) {
|
||||
setErrorMsg("Unsupported file type. Please upload an .xlsx, .xls, or .csv file.");
|
||||
setSelectedFile(null);
|
||||
setPreview(null);
|
||||
return;
|
||||
}
|
||||
|
||||
setSelectedFile(file);
|
||||
setErrorMsg(null);
|
||||
setPreview(null);
|
||||
setSkipInvalid(false);
|
||||
setValidating(true);
|
||||
|
||||
try {
|
||||
const result = await assetsApi.bulkImport(file, true, false);
|
||||
setPreview(result);
|
||||
} catch (err) {
|
||||
const msg =
|
||||
err instanceof ApiError
|
||||
? err.detail
|
||||
: "Failed to validate import file.";
|
||||
setErrorMsg(msg);
|
||||
} finally {
|
||||
setValidating(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleConfirmImport() {
|
||||
if (!selectedFile || !preview) return;
|
||||
setImporting(true);
|
||||
setErrorMsg(null);
|
||||
try {
|
||||
const result = await assetsApi.bulkImport(selectedFile, false, skipInvalid);
|
||||
await Promise.all([
|
||||
qc.invalidateQueries({ queryKey: ["assets"] }),
|
||||
qc.invalidateQueries({ queryKey: ["dashboard"] }),
|
||||
]);
|
||||
toast.success(
|
||||
`Imported ${result.created_count} asset${result.created_count === 1 ? "" : "s"}`
|
||||
);
|
||||
resetState();
|
||||
onClose();
|
||||
} catch (err) {
|
||||
const msg =
|
||||
err instanceof ApiError ? err.detail : "Bulk asset import failed.";
|
||||
setErrorMsg(msg);
|
||||
toast.error(msg);
|
||||
} finally {
|
||||
setImporting(false);
|
||||
}
|
||||
}
|
||||
|
||||
const canImport =
|
||||
Boolean(selectedFile && preview) &&
|
||||
!validating &&
|
||||
!importing &&
|
||||
preview!.valid_count > 0 &&
|
||||
(preview!.error_count === 0 || skipInvalid);
|
||||
|
||||
return (
|
||||
<Modal
|
||||
open={open}
|
||||
onOpenChange={(next) => {
|
||||
if (!next) handleClose();
|
||||
}}
|
||||
title="Bulk Import Assets"
|
||||
description="Upload an Excel (.xlsx/.xls) or CSV spreadsheet to register multiple hospital assets at once."
|
||||
className="max-w-4xl"
|
||||
>
|
||||
<div className="space-y-4">
|
||||
{/* Step 1: Template Download */}
|
||||
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 rounded-lg border border-blue-200 bg-blue-50/70 p-3.5">
|
||||
<div className="flex items-start gap-2.5 text-xs text-blue-950">
|
||||
<FileSpreadsheet className="h-5 w-5 text-[#1e3a8a] shrink-0 mt-0.5" />
|
||||
<div>
|
||||
<div className="font-semibold text-sm text-[#1e3a8a]">
|
||||
Step 1: Download the Excel Template
|
||||
</div>
|
||||
<p className="mt-0.5 text-blue-900/90">
|
||||
Fill equipment rows in the <strong>Assets</strong> sheet and check valid
|
||||
departments, locations, categories, and suppliers in the{" "}
|
||||
<strong>Master Reference</strong> sheet.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
loading={downloadingTemplate}
|
||||
onClick={handleDownloadTemplate}
|
||||
className="shrink-0 bg-white"
|
||||
>
|
||||
<Download className="h-4 w-4 mr-1.5" />
|
||||
Download Template (.xlsx)
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Step 2: File Picker / Dropzone */}
|
||||
<div
|
||||
onDragOver={(e) => {
|
||||
e.preventDefault();
|
||||
setDragActive(true);
|
||||
}}
|
||||
onDragLeave={() => setDragActive(false)}
|
||||
onDrop={(e) => {
|
||||
e.preventDefault();
|
||||
setDragActive(false);
|
||||
void handleFileSelect(e.dataTransfer.files);
|
||||
}}
|
||||
className={cn(
|
||||
"border-2 border-dashed rounded-lg p-5 text-center transition-colors",
|
||||
dragActive
|
||||
? "border-[#1e3a8a] bg-blue-50"
|
||||
: "border-gray-300 bg-gray-50/60 hover:bg-gray-50"
|
||||
)}
|
||||
>
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept=".xlsx,.xls,.csv"
|
||||
aria-label="Upload asset spreadsheet"
|
||||
onChange={(e) => void handleFileSelect(e.target.files)}
|
||||
className="hidden"
|
||||
/>
|
||||
<Upload className="h-7 w-7 text-gray-400 mx-auto mb-2" />
|
||||
<p className="text-sm font-medium text-gray-800">
|
||||
{selectedFile ? selectedFile.name : "Drop your spreadsheet here, or browse"}
|
||||
</p>
|
||||
<p className="text-xs text-gray-500 mt-1">
|
||||
Accepts <code>.xlsx</code>, <code>.xls</code>, or <code>.csv</code> — automatically validates rows before importing.
|
||||
</p>
|
||||
<div className="mt-3 flex items-center justify-center gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
disabled={validating || importing}
|
||||
onClick={() => fileInputRef.current?.click()}
|
||||
>
|
||||
{selectedFile ? "Choose Another File" : "Select Spreadsheet"}
|
||||
</Button>
|
||||
{selectedFile && (
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
disabled={validating || importing}
|
||||
onClick={resetState}
|
||||
>
|
||||
<X className="h-3.5 w-3.5 mr-1" />
|
||||
Clear
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{validating && (
|
||||
<div className="rounded-md border border-blue-200 bg-blue-50 px-3.5 py-2.5 text-xs text-blue-900">
|
||||
Validating rows against hospital masters...
|
||||
</div>
|
||||
)}
|
||||
|
||||
{errorMsg && (
|
||||
<div
|
||||
role="alert"
|
||||
className="flex items-start gap-2 rounded-md border border-red-200 bg-red-50 px-3.5 py-2.5 text-xs text-red-800"
|
||||
>
|
||||
<AlertCircle className="h-4 w-4 text-red-600 shrink-0 mt-0.5" />
|
||||
<span>{errorMsg}</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Step 3: Dry-run Validation Preview */}
|
||||
{preview && (
|
||||
<div className="space-y-3">
|
||||
<div className="flex flex-wrap items-center justify-between gap-2">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<span className="inline-flex items-center px-2.5 py-1 rounded-full text-xs font-medium bg-gray-100 text-gray-700 border border-gray-200">
|
||||
Total Rows: {preview.total_rows}
|
||||
</span>
|
||||
<span className="inline-flex items-center gap-1 px-2.5 py-1 rounded-full text-xs font-medium bg-green-100 text-green-800 border border-green-200">
|
||||
<CheckCircle2 className="h-3.5 w-3.5" />
|
||||
Valid: {preview.valid_count}
|
||||
</span>
|
||||
<span
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1 px-2.5 py-1 rounded-full text-xs font-medium border",
|
||||
preview.error_count > 0
|
||||
? "bg-red-100 text-red-800 border-red-200"
|
||||
: "bg-gray-100 text-gray-600 border-gray-200"
|
||||
)}
|
||||
>
|
||||
<AlertCircle className="h-3.5 w-3.5" />
|
||||
Errors: {preview.error_count}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{preview.error_count > 0 && preview.valid_count > 0 && (
|
||||
<label className="inline-flex items-center gap-2 text-xs font-medium text-gray-700 cursor-pointer">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={skipInvalid}
|
||||
onChange={(e) => setSkipInvalid(e.target.checked)}
|
||||
className="rounded border-gray-300 text-[#1e3a8a] focus:ring-blue-500"
|
||||
/>
|
||||
Skip invalid rows and import valid rows only
|
||||
</label>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="border border-gray-200 rounded-lg overflow-hidden">
|
||||
<div className="max-h-64 overflow-auto">
|
||||
<table className="w-full text-xs">
|
||||
<thead className="bg-gray-50 border-b border-gray-200 sticky top-0">
|
||||
<tr>
|
||||
<th className="px-3 py-2 text-left font-medium text-gray-600">Row</th>
|
||||
<th className="px-3 py-2 text-left font-medium text-gray-600">Asset Code</th>
|
||||
<th className="px-3 py-2 text-left font-medium text-gray-600">Name</th>
|
||||
<th className="px-3 py-2 text-left font-medium text-gray-600">Department</th>
|
||||
<th className="px-3 py-2 text-left font-medium text-gray-600">Location</th>
|
||||
<th className="px-3 py-2 text-left font-medium text-gray-600">Validation</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-100 bg-white">
|
||||
{preview.preview_rows.length === 0 ? (
|
||||
<tr>
|
||||
<td colSpan={6} className="px-3 py-6 text-center text-gray-400">
|
||||
No data rows found in spreadsheet.
|
||||
</td>
|
||||
</tr>
|
||||
) : (
|
||||
preview.preview_rows.map((r) => (
|
||||
<tr
|
||||
key={r.row}
|
||||
className={r.valid ? "bg-white" : "bg-red-50/60"}
|
||||
>
|
||||
<td className="px-3 py-2 font-mono text-gray-500">{r.row}</td>
|
||||
<td className="px-3 py-2 font-mono font-medium text-gray-900">
|
||||
{r.asset_code || "—"}
|
||||
</td>
|
||||
<td className="px-3 py-2 text-gray-800">{r.name || "—"}</td>
|
||||
<td className="px-3 py-2 text-gray-700">
|
||||
{r.department_name || "—"}
|
||||
</td>
|
||||
<td className="px-3 py-2 text-gray-700">
|
||||
{r.location_name || "—"}
|
||||
</td>
|
||||
<td className="px-3 py-2">
|
||||
{r.valid ? (
|
||||
<span className="inline-flex items-center gap-1 text-green-700 font-medium">
|
||||
<CheckCircle2 className="h-3.5 w-3.5" />
|
||||
Ready
|
||||
</span>
|
||||
) : (
|
||||
<span className="inline-flex items-start gap-1 text-red-700">
|
||||
<AlertCircle className="h-3.5 w-3.5 shrink-0 mt-0.5" />
|
||||
<span>{r.error || "Invalid row"}</span>
|
||||
</span>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
))
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Footer */}
|
||||
<div className="flex flex-col-reverse sm:flex-row sm:items-center justify-end gap-2 pt-2 border-t border-gray-100">
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
disabled={importing}
|
||||
onClick={handleClose}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
loading={importing}
|
||||
disabled={!canImport}
|
||||
onClick={handleConfirmImport}
|
||||
>
|
||||
<Upload className="h-4 w-4 mr-1.5" />
|
||||
Import {preview?.valid_count ?? 0} Asset
|
||||
{(preview?.valid_count ?? 0) === 1 ? "" : "s"}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
@@ -1,8 +1,17 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { render, screen, fireEvent } from "@testing-library/react";
|
||||
import { ColumnDef } from "@tanstack/react-table";
|
||||
import * as XLSX from "xlsx";
|
||||
import { DataTable } from "./DataTable";
|
||||
|
||||
vi.mock("xlsx", async () => {
|
||||
const actual = await vi.importActual<typeof import("xlsx")>("xlsx");
|
||||
return {
|
||||
...actual,
|
||||
writeFile: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
interface RowItem {
|
||||
code: string;
|
||||
name: string;
|
||||
@@ -19,8 +28,19 @@ const data: RowItem[] = [
|
||||
];
|
||||
|
||||
describe("DataTable", () => {
|
||||
it("renders rows and filters globally", () => {
|
||||
render(<DataTable data={data} columns={columns} globalFilterPlaceholder="Search..." />);
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("renders rows, filters globally, and exports visible rows to Excel (.xlsx)", () => {
|
||||
render(
|
||||
<DataTable
|
||||
data={data}
|
||||
columns={columns}
|
||||
globalFilterPlaceholder="Search..."
|
||||
exportFilename="test-assets"
|
||||
/>
|
||||
);
|
||||
expect(screen.getByText("Ventilator")).toBeInTheDocument();
|
||||
expect(screen.getByText("Monitor")).toBeInTheDocument();
|
||||
|
||||
@@ -29,5 +49,17 @@ describe("DataTable", () => {
|
||||
|
||||
expect(screen.getByText("Ventilator")).toBeInTheDocument();
|
||||
expect(screen.queryByText("Monitor")).not.toBeInTheDocument();
|
||||
|
||||
const exportBtn = screen.getByRole("button", {
|
||||
name: /Export current view to Excel/i,
|
||||
});
|
||||
expect(exportBtn).toHaveTextContent("Export Excel (.xlsx)");
|
||||
fireEvent.click(exportBtn);
|
||||
|
||||
expect(XLSX.writeFile).toHaveBeenCalledTimes(1);
|
||||
expect(XLSX.writeFile).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ SheetNames: ["Data"] }),
|
||||
"test-assets.xlsx"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,9 +11,18 @@ import {
|
||||
VisibilityState,
|
||||
PaginationState,
|
||||
FilterFn,
|
||||
type Table as TanStackTable,
|
||||
} from "@tanstack/react-table";
|
||||
import { useState, useMemo } from "react";
|
||||
import { ChevronUp, ChevronDown, ChevronsUpDown, Download, Eye, Search } from "lucide-react";
|
||||
import { useState } from "react";
|
||||
import * as XLSX from "xlsx";
|
||||
import {
|
||||
ChevronUp,
|
||||
ChevronDown,
|
||||
ChevronsUpDown,
|
||||
FileSpreadsheet,
|
||||
Eye,
|
||||
Search,
|
||||
} from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { Button } from "@/components/ui/button";
|
||||
|
||||
@@ -29,20 +38,39 @@ interface DataTableProps<TData> {
|
||||
exportFilename?: string;
|
||||
}
|
||||
|
||||
function exportToCsv<TData>(data: TData[], filename: string) {
|
||||
if (!data.length) return;
|
||||
const headers = Object.keys(data[0] as object);
|
||||
const rows = data.map((row) =>
|
||||
headers.map((h) => JSON.stringify((row as Record<string, unknown>)[h] ?? "")).join(",")
|
||||
);
|
||||
const csv = [headers.join(","), ...rows].join("\n");
|
||||
const blob = new Blob([csv], { type: "text/csv" });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = `${filename}.csv`;
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
function formatExcelCell(val: unknown): string | number {
|
||||
if (val === null || val === undefined) return "";
|
||||
if (typeof val === "boolean") return val ? "Yes" : "No";
|
||||
if (typeof val === "number") return Number.isFinite(val) ? val : "";
|
||||
if (typeof val === "string") return val;
|
||||
return JSON.stringify(val);
|
||||
}
|
||||
|
||||
function exportToExcel<TData>(table: TanStackTable<TData>, filename: string) {
|
||||
const rows = table.getPrePaginationRowModel().rows;
|
||||
if (!rows.length) return;
|
||||
|
||||
const visibleCols = table
|
||||
.getVisibleLeafColumns()
|
||||
.filter((col) => col.id !== "actions");
|
||||
if (!visibleCols.length) return;
|
||||
|
||||
const exportRows = rows.map((row) => {
|
||||
const record: Record<string, string | number> = {};
|
||||
for (const col of visibleCols) {
|
||||
const headerLabel =
|
||||
typeof col.columnDef.header === "string"
|
||||
? col.columnDef.header
|
||||
: col.id;
|
||||
record[headerLabel] = formatExcelCell(row.getValue(col.id));
|
||||
}
|
||||
return record;
|
||||
});
|
||||
|
||||
const ws = XLSX.utils.json_to_sheet(exportRows);
|
||||
const wb = XLSX.utils.book_new();
|
||||
XLSX.utils.book_append_sheet(wb, ws, "Data");
|
||||
XLSX.writeFile(wb, `${filename}.xlsx`);
|
||||
}
|
||||
|
||||
export function DataTable<TData>({
|
||||
@@ -88,47 +116,53 @@ export function DataTable<TData>({
|
||||
|
||||
const visibleRows = table.getRowModel().rows;
|
||||
const filteredRows = table.getFilteredRowModel().rows;
|
||||
const allFilteredData = useMemo(
|
||||
() => filteredRows.map((r) => r.original),
|
||||
[filteredRows]
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
{/* Toolbar */}
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<div className="relative flex-1 min-w-48">
|
||||
<div className="flex flex-col sm:flex-row sm:items-center gap-2">
|
||||
<div className="relative flex-1 min-w-0">
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-gray-400" />
|
||||
<input
|
||||
value={globalFilter}
|
||||
onChange={(e) => setGlobalFilter(e.target.value)}
|
||||
placeholder={globalFilterPlaceholder}
|
||||
aria-label={globalFilterPlaceholder}
|
||||
className="w-full pl-9 pr-4 py-1.5 text-sm border border-gray-200 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||
/>
|
||||
</div>
|
||||
<div className="relative">
|
||||
<Button variant="outline" size="sm" onClick={() => setShowColVis(!showColVis)}>
|
||||
<Eye className="h-4 w-4 mr-1" /> Columns
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<div className="relative">
|
||||
<Button variant="outline" size="sm" onClick={() => setShowColVis(!showColVis)}>
|
||||
<Eye className="h-4 w-4 mr-1" /> Columns
|
||||
</Button>
|
||||
{showColVis && (
|
||||
<div className="absolute right-0 mt-1 w-48 bg-white border border-gray-200 rounded-md shadow-lg z-20 p-2">
|
||||
{table.getAllLeafColumns().map((col) => (
|
||||
<label key={col.id} className="flex items-center gap-2 px-2 py-1 hover:bg-gray-50 rounded cursor-pointer text-sm">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={col.getIsVisible()}
|
||||
onChange={col.getToggleVisibilityHandler()}
|
||||
className="rounded"
|
||||
/>
|
||||
{typeof col.columnDef.header === "string"
|
||||
? col.columnDef.header
|
||||
: col.id}
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
aria-label="Export current view to Excel"
|
||||
onClick={() => exportToExcel(table, exportFilename)}
|
||||
>
|
||||
<FileSpreadsheet className="h-4 w-4 mr-1" /> Export Excel (.xlsx)
|
||||
</Button>
|
||||
{showColVis && (
|
||||
<div className="absolute right-0 mt-1 w-48 bg-white border border-gray-200 rounded-md shadow-lg z-20 p-2">
|
||||
{table.getAllLeafColumns().map((col) => (
|
||||
<label key={col.id} className="flex items-center gap-2 px-2 py-1 hover:bg-gray-50 rounded cursor-pointer text-sm">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={col.getIsVisible()}
|
||||
onChange={col.getToggleVisibilityHandler()}
|
||||
className="rounded"
|
||||
/>
|
||||
{String(col.columnDef.header ?? col.id)}
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<Button variant="outline" size="sm" onClick={() => exportToCsv(allFilteredData, exportFilename)}>
|
||||
<Download className="h-4 w-4 mr-1" /> CSV (current view)
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Table */}
|
||||
@@ -139,7 +173,7 @@ export function DataTable<TData>({
|
||||
{table.getHeaderGroups().map((hg) => (
|
||||
<tr key={hg.id}>
|
||||
{hg.headers.map((header) => (
|
||||
<th key={header.id} className="text-left font-medium text-gray-600 px-4 py-3">
|
||||
<th key={header.id} className="text-left font-medium text-gray-600 px-2.5 py-2 sm:px-4 sm:py-3">
|
||||
{header.isPlaceholder ? null : (
|
||||
<div className="space-y-1">
|
||||
{/* Sortable header */}
|
||||
@@ -184,7 +218,7 @@ export function DataTable<TData>({
|
||||
[...Array(5)].map((_, i) => (
|
||||
<tr key={i}>
|
||||
{columns.map((_, j) => (
|
||||
<td key={j} className="px-4 py-3">
|
||||
<td key={j} className="px-2.5 py-2 sm:px-4 sm:py-3">
|
||||
<div className="h-4 bg-gray-100 rounded animate-pulse" />
|
||||
</td>
|
||||
))}
|
||||
@@ -211,7 +245,7 @@ export function DataTable<TData>({
|
||||
onClick={() => onRowClick?.(row.original)}
|
||||
>
|
||||
{row.getVisibleCells().map((cell) => (
|
||||
<td key={cell.id} className="px-4 py-3 text-gray-700">
|
||||
<td key={cell.id} className="px-2.5 py-2 sm:px-4 sm:py-3 text-gray-700">
|
||||
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
||||
</td>
|
||||
))}
|
||||
@@ -224,7 +258,7 @@ export function DataTable<TData>({
|
||||
|
||||
{/* Pagination */}
|
||||
{!loading && visibleRows.length > 0 && (
|
||||
<div className="flex items-center justify-between px-4 py-3 border-t border-gray-200 bg-gray-50 text-sm text-gray-600">
|
||||
<div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-2 px-3 sm:px-4 py-3 border-t border-gray-200 bg-gray-50 text-sm text-gray-600">
|
||||
<span>
|
||||
{filteredRows.length} record{filteredRows.length !== 1 ? "s" : ""}
|
||||
{globalFilter || columnFilters.length ? " (filtered)" : ""}
|
||||
|
||||
@@ -15,7 +15,7 @@ export function AppShell() {
|
||||
/>
|
||||
<div className="flex-1 flex flex-col min-w-0">
|
||||
<TopBar onMenuClick={() => setMobileNavOpen(true)} />
|
||||
<main className="flex-1 p-6 overflow-auto">
|
||||
<main className="flex-1 p-3 sm:p-4 lg:p-6 overflow-auto">
|
||||
<Outlet />
|
||||
</main>
|
||||
</div>
|
||||
|
||||
@@ -104,7 +104,7 @@ export function Sidebar({ mobileOpen, onClose }: SidebarProps = {}) {
|
||||
type="button"
|
||||
onClick={onClose}
|
||||
aria-label="Close navigation menu"
|
||||
className="md:hidden absolute right-2 top-2 p-1 rounded text-blue-200 hover:text-white hover:bg-blue-700 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white"
|
||||
className="lg:hidden absolute right-2 top-2 p-1 rounded text-blue-200 hover:text-white hover:bg-blue-700 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white"
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</button>
|
||||
@@ -159,9 +159,9 @@ export function Sidebar({ mobileOpen, onClose }: SidebarProps = {}) {
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="hidden md:flex shrink-0">{asideContent}</div>
|
||||
<div className="hidden lg:flex shrink-0">{asideContent}</div>
|
||||
{mobileOpen && (
|
||||
<div className="fixed inset-0 z-40 flex md:hidden">
|
||||
<div className="fixed inset-0 z-40 flex lg:hidden">
|
||||
<div
|
||||
className="fixed inset-0 bg-black/40"
|
||||
onClick={onClose}
|
||||
|
||||
@@ -230,14 +230,14 @@ export function TopBar({ title, onMenuClick }: TopBarProps) {
|
||||
}
|
||||
|
||||
return (
|
||||
<header className="min-h-14 bg-white border-b border-gray-200 flex items-center flex-wrap px-4 py-2 gap-3 sticky top-0 z-10">
|
||||
{/* Mobile nav toggle */}
|
||||
<header className="min-h-14 bg-white border-b border-gray-200 flex items-center flex-wrap px-3 sm:px-4 py-2 gap-2 sm:gap-3 sticky top-0 z-10">
|
||||
{/* Mobile/tablet nav toggle */}
|
||||
{onMenuClick && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={onMenuClick}
|
||||
aria-label="Open navigation menu"
|
||||
className="md:hidden p-1.5 rounded-md text-gray-600 hover:bg-gray-100 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-500"
|
||||
className="lg:hidden p-1.5 rounded-md text-gray-600 hover:bg-gray-100 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-500"
|
||||
>
|
||||
<Menu className="h-5 w-5" />
|
||||
</button>
|
||||
@@ -253,7 +253,10 @@ export function TopBar({ title, onMenuClick }: TopBarProps) {
|
||||
{/* Live Global Quick Search */}
|
||||
<div
|
||||
ref={searchContainerRef}
|
||||
className={cn("relative", title ? "w-64 sm:w-80" : "flex-1 max-w-xs sm:max-w-md")}
|
||||
className={cn(
|
||||
"relative order-last lg:order-none w-full lg:w-auto",
|
||||
title ? "lg:w-72" : "lg:flex-1 lg:max-w-md"
|
||||
)}
|
||||
>
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-gray-400 pointer-events-none" />
|
||||
<input
|
||||
@@ -407,7 +410,7 @@ export function TopBar({ title, onMenuClick }: TopBarProps) {
|
||||
|
||||
{/* Admin Demo Clock Picker + Run Alert Check */}
|
||||
{isAdmin && (
|
||||
<div className="flex items-center gap-1.5 bg-gray-50 border border-gray-200 rounded-md px-2 py-1 text-xs flex-wrap">
|
||||
<div className="w-full xl:w-auto order-last xl:order-none flex items-center gap-1.5 bg-gray-50 border border-gray-200 rounded-md px-2 py-1 text-xs flex-wrap">
|
||||
<span className="text-gray-500 font-medium hidden md:inline">
|
||||
As of Date:
|
||||
</span>
|
||||
@@ -451,8 +454,10 @@ export function TopBar({ title, onMenuClick }: TopBarProps) {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Live Notification Bell & Dropdown */}
|
||||
<div ref={bellContainerRef} className="relative ml-auto">
|
||||
{/* Right utility cluster (Bell + User Menu) */}
|
||||
<div className="ml-auto flex items-center gap-1.5 sm:gap-2">
|
||||
{/* Live Notification Bell & Dropdown */}
|
||||
<div ref={bellContainerRef} className="relative">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setBellOpen((o) => !o)}
|
||||
@@ -600,6 +605,7 @@ export function TopBar({ title, onMenuClick }: TopBarProps) {
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
);
|
||||
|
||||
@@ -25,7 +25,7 @@ export function Modal({
|
||||
<DialogPrimitive.Overlay className="fixed inset-0 z-40 bg-black/40 backdrop-blur-[1px]" />
|
||||
<DialogPrimitive.Content
|
||||
className={cn(
|
||||
"fixed left-1/2 top-1/2 z-50 w-full max-w-lg -translate-x-1/2 -translate-y-1/2 rounded-lg bg-white p-6 shadow-xl border border-gray-200 max-h-[90vh] overflow-y-auto",
|
||||
"fixed left-1/2 top-1/2 z-50 w-[95vw] sm:w-full max-w-lg -translate-x-1/2 -translate-y-1/2 rounded-lg bg-white p-4 sm:p-6 shadow-xl border border-gray-200 max-h-[90vh] overflow-y-auto",
|
||||
className
|
||||
)}
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user