2026-08-04 09:53:48 -03:00
|
|
|
import { type KeyboardEvent, type MouseEvent } from "react";
|
2026-08-06 22:23:30 -03:00
|
|
|
import SearchOffOutlinedIcon from "@mui/icons-material/SearchOffOutlined";
|
2026-08-04 09:53:48 -03:00
|
|
|
import EditOutlinedIcon from "@mui/icons-material/EditOutlined";
|
|
|
|
|
import VisibilityOutlinedIcon from "@mui/icons-material/VisibilityOutlined";
|
|
|
|
|
import {
|
|
|
|
|
Avatar,
|
2026-08-06 23:30:23 -03:00
|
|
|
Box,
|
2026-08-04 09:53:48 -03:00
|
|
|
CircularProgress,
|
|
|
|
|
IconButton,
|
|
|
|
|
Link,
|
|
|
|
|
Paper,
|
|
|
|
|
Stack,
|
|
|
|
|
Table,
|
|
|
|
|
TableBody,
|
|
|
|
|
TableCell,
|
|
|
|
|
TableContainer,
|
|
|
|
|
TableHead,
|
|
|
|
|
TableRow,
|
|
|
|
|
Tooltip,
|
|
|
|
|
} from "@mui/material";
|
2026-08-06 22:23:30 -03:00
|
|
|
import { Text } from "@/components/ui/text";
|
2026-08-04 09:53:48 -03:00
|
|
|
import type { VendorListItem } from "@/domain/vendors/types/vendor";
|
|
|
|
|
|
|
|
|
|
const TABLE_COLUMNS = [
|
|
|
|
|
"Company",
|
|
|
|
|
"Technician",
|
|
|
|
|
"Location",
|
|
|
|
|
"Trade",
|
|
|
|
|
"Phone",
|
|
|
|
|
"Email",
|
|
|
|
|
"Total Jobs",
|
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
|
|
function getInitials(name: string): string {
|
|
|
|
|
const parts = name.trim().split(/\s+/).filter(Boolean);
|
|
|
|
|
if (parts.length === 0) return "?";
|
|
|
|
|
const first = parts[0]?.[0] ?? "";
|
|
|
|
|
const last = parts.length > 1 ? (parts[parts.length - 1]?.[0] ?? "") : "";
|
|
|
|
|
return (first + last).toUpperCase();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function splitTrades(value: string | undefined): string[] {
|
|
|
|
|
return (value ?? "")
|
|
|
|
|
.split(",")
|
|
|
|
|
.map((trade) => trade.trim())
|
|
|
|
|
.filter(Boolean);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stopPropagation(event: MouseEvent): void {
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-06 23:30:23 -03:00
|
|
|
function VendorStatus({ isActive }: { isActive: boolean }) {
|
|
|
|
|
return (
|
|
|
|
|
<Stack direction="row" spacing={1} sx={{ alignItems: "center", pl: 1 }}>
|
|
|
|
|
<Box
|
|
|
|
|
component="span"
|
|
|
|
|
sx={{
|
|
|
|
|
width: 6,
|
|
|
|
|
height: 6,
|
|
|
|
|
borderRadius: "9999px",
|
|
|
|
|
backgroundColor: isActive ? "success.main" : "text.secondary",
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<Text variant="description">{isActive ? "Active" : "Inactive"}</Text>
|
|
|
|
|
</Stack>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-04 09:53:48 -03:00
|
|
|
interface VendorTableRowProps {
|
|
|
|
|
row: VendorListItem;
|
|
|
|
|
onOpenDetail: (row: VendorListItem) => void;
|
2026-08-06 22:23:30 -03:00
|
|
|
onOpenEdit: (row: VendorListItem) => void;
|
2026-08-04 09:53:48 -03:00
|
|
|
}
|
|
|
|
|
|
2026-08-06 22:23:30 -03:00
|
|
|
function VendorTableRow({ row, onOpenDetail, onOpenEdit }: VendorTableRowProps) {
|
2026-08-04 09:53:48 -03:00
|
|
|
const trades = splitTrades(row.tradeSpecialties);
|
|
|
|
|
const primaryTrade = trades[0] ?? "—";
|
|
|
|
|
const extraTrades = trades.length > 1 ? trades.length - 1 : 0;
|
|
|
|
|
const locationText = [row.city, row.state].filter(Boolean).join(", ") || "—";
|
|
|
|
|
const rowKey = String(row.id ?? `company-${row.companyId ?? ""}`);
|
|
|
|
|
|
|
|
|
|
const handleKeyDown = (event: KeyboardEvent) => {
|
|
|
|
|
if (event.key !== "Enter" && event.key !== " ") return;
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
onOpenDetail(row);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<TableRow
|
|
|
|
|
key={rowKey}
|
|
|
|
|
hover
|
|
|
|
|
tabIndex={0}
|
|
|
|
|
role="button"
|
|
|
|
|
aria-label={`Open vendor details for ${row.companyName}`}
|
|
|
|
|
sx={{ cursor: "pointer" }}
|
|
|
|
|
onClick={() => onOpenDetail(row)}
|
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
|
>
|
|
|
|
|
<TableCell>
|
2026-08-06 23:30:23 -03:00
|
|
|
<Text variant="description" sx={{ color: "var(--color-foreground)" }}>
|
2026-08-06 22:23:30 -03:00
|
|
|
{row.companyName || "—"}
|
|
|
|
|
</Text>
|
2026-08-04 09:53:48 -03:00
|
|
|
</TableCell>
|
|
|
|
|
<TableCell>
|
2026-08-06 23:30:23 -03:00
|
|
|
<Stack direction="row" spacing={1.25} sx={{ alignItems: "center" }}>
|
2026-08-04 09:53:48 -03:00
|
|
|
<Avatar
|
|
|
|
|
sx={{
|
|
|
|
|
width: 28,
|
|
|
|
|
height: 28,
|
2026-08-06 22:23:30 -03:00
|
|
|
backgroundColor: "var(--color-bg-muted)",
|
|
|
|
|
color: "var(--color-primary)",
|
|
|
|
|
fontSize: 10.5,
|
|
|
|
|
fontWeight: 600,
|
2026-08-04 09:53:48 -03:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{getInitials(row.contactName)}
|
|
|
|
|
</Avatar>
|
2026-08-06 23:30:23 -03:00
|
|
|
<Text variant="description" sx={{ color: "var(--color-foreground)" }}>
|
|
|
|
|
{row.contactName || "—"}
|
|
|
|
|
</Text>
|
2026-08-04 09:53:48 -03:00
|
|
|
</Stack>
|
|
|
|
|
</TableCell>
|
2026-08-06 23:30:23 -03:00
|
|
|
<TableCell sx={{ color: "var(--color-muted-foreground)" }}>{locationText}</TableCell>
|
|
|
|
|
<TableCell sx={{ color: "var(--color-muted-foreground)" }}>
|
2026-08-04 09:53:48 -03:00
|
|
|
{primaryTrade === "—" ? (
|
|
|
|
|
"—"
|
|
|
|
|
) : (
|
|
|
|
|
<Stack direction="row" spacing={0.5} sx={{ alignItems: "center" }}>
|
|
|
|
|
<span>{primaryTrade}</span>
|
2026-08-06 23:30:23 -03:00
|
|
|
{extraTrades > 0 && (
|
|
|
|
|
<Box
|
|
|
|
|
component="span"
|
|
|
|
|
sx={{
|
|
|
|
|
borderRadius: "9999px",
|
|
|
|
|
backgroundColor: "var(--color-bg-muted)",
|
|
|
|
|
px: 0.75,
|
|
|
|
|
py: 0.25,
|
|
|
|
|
fontSize: 10.5,
|
|
|
|
|
lineHeight: 1.2,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
+{extraTrades}
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
2026-08-04 09:53:48 -03:00
|
|
|
</Stack>
|
|
|
|
|
)}
|
|
|
|
|
</TableCell>
|
2026-08-06 23:30:23 -03:00
|
|
|
<TableCell sx={{ color: "var(--color-muted-foreground)" }}>
|
2026-08-04 09:53:48 -03:00
|
|
|
{row.phone ? (
|
2026-08-06 23:30:23 -03:00
|
|
|
<Link
|
|
|
|
|
href={`tel:${row.phone}`}
|
|
|
|
|
underline="hover"
|
|
|
|
|
color="inherit"
|
|
|
|
|
onClick={stopPropagation}
|
|
|
|
|
>
|
2026-08-04 09:53:48 -03:00
|
|
|
{row.phone}
|
|
|
|
|
</Link>
|
|
|
|
|
) : (
|
|
|
|
|
"—"
|
|
|
|
|
)}
|
|
|
|
|
</TableCell>
|
|
|
|
|
<TableCell>
|
|
|
|
|
{row.email ? (
|
2026-08-06 23:30:23 -03:00
|
|
|
<Link
|
|
|
|
|
href={`mailto:${row.email}`}
|
|
|
|
|
underline="hover"
|
|
|
|
|
sx={{ color: "#6b7a96" }}
|
|
|
|
|
onClick={stopPropagation}
|
|
|
|
|
>
|
2026-08-04 09:53:48 -03:00
|
|
|
{row.email}
|
|
|
|
|
</Link>
|
|
|
|
|
) : (
|
|
|
|
|
"—"
|
|
|
|
|
)}
|
|
|
|
|
</TableCell>
|
2026-08-06 22:23:30 -03:00
|
|
|
<TableCell align="center">{row.totalJobs ?? 0}</TableCell>
|
2026-08-04 09:53:48 -03:00
|
|
|
<TableCell>
|
2026-08-06 23:30:23 -03:00
|
|
|
<VendorStatus isActive={row.isActive} />
|
2026-08-04 09:53:48 -03:00
|
|
|
</TableCell>
|
2026-08-06 22:23:30 -03:00
|
|
|
<TableCell align="right">
|
2026-08-04 09:53:48 -03:00
|
|
|
<Stack
|
|
|
|
|
direction="row"
|
|
|
|
|
spacing={0.5}
|
2026-08-06 22:23:30 -03:00
|
|
|
className="vendor-row-actions"
|
|
|
|
|
sx={{ alignItems: "center", justifyContent: "flex-end" }}
|
2026-08-04 09:53:48 -03:00
|
|
|
>
|
2026-08-06 22:23:30 -03:00
|
|
|
<Tooltip title="View details">
|
|
|
|
|
<IconButton
|
|
|
|
|
size="small"
|
|
|
|
|
aria-label={`View vendor ${row.companyName}`}
|
|
|
|
|
onClick={(event) => {
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
onOpenDetail(row);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<VisibilityOutlinedIcon fontSize="small" />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
2026-08-04 09:53:48 -03:00
|
|
|
<Tooltip title="Edit">
|
|
|
|
|
<IconButton
|
|
|
|
|
size="small"
|
|
|
|
|
aria-label={`Edit vendor ${row.companyName}`}
|
|
|
|
|
onClick={(event) => {
|
|
|
|
|
event.stopPropagation();
|
2026-08-06 22:23:30 -03:00
|
|
|
onOpenEdit(row);
|
2026-08-04 09:53:48 -03:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<EditOutlinedIcon fontSize="small" />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
</Stack>
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface VendorsTableProps {
|
|
|
|
|
rows: VendorListItem[];
|
2026-08-06 22:23:30 -03:00
|
|
|
isActive: boolean;
|
|
|
|
|
isFiltered: boolean;
|
2026-08-04 09:53:48 -03:00
|
|
|
isLoading: boolean;
|
|
|
|
|
showError: boolean;
|
|
|
|
|
onOpenDetail: (row: VendorListItem) => void;
|
2026-08-06 22:23:30 -03:00
|
|
|
onOpenEdit: (row: VendorListItem) => void;
|
2026-08-04 09:53:48 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function VendorsTable({
|
|
|
|
|
rows,
|
2026-08-06 22:23:30 -03:00
|
|
|
isActive,
|
|
|
|
|
isFiltered,
|
2026-08-04 09:53:48 -03:00
|
|
|
isLoading,
|
|
|
|
|
showError,
|
|
|
|
|
onOpenDetail,
|
2026-08-06 22:23:30 -03:00
|
|
|
onOpenEdit,
|
2026-08-04 09:53:48 -03:00
|
|
|
}: VendorsTableProps) {
|
|
|
|
|
const showEmptyRow = !isLoading && !showError && rows.length === 0;
|
2026-08-06 22:23:30 -03:00
|
|
|
const columnCount = TABLE_COLUMNS.length + 2;
|
2026-08-04 09:53:48 -03:00
|
|
|
|
|
|
|
|
return (
|
2026-08-06 22:23:30 -03:00
|
|
|
<TableContainer
|
|
|
|
|
component={Paper}
|
|
|
|
|
variant="outlined"
|
2026-08-06 23:30:23 -03:00
|
|
|
sx={{
|
|
|
|
|
mt: 2,
|
|
|
|
|
mb: 2,
|
|
|
|
|
flex: "1 1 auto",
|
|
|
|
|
overflow: "auto",
|
|
|
|
|
borderRadius: 1.5,
|
|
|
|
|
fontSize: 13,
|
|
|
|
|
}}
|
2026-08-06 22:23:30 -03:00
|
|
|
>
|
|
|
|
|
<Table
|
|
|
|
|
size="small"
|
|
|
|
|
stickyHeader
|
|
|
|
|
sx={{
|
2026-08-06 23:30:23 -03:00
|
|
|
minWidth: 1550,
|
|
|
|
|
tableLayout: "fixed",
|
2026-08-06 22:23:30 -03:00
|
|
|
"& .MuiTableCell-head": {
|
|
|
|
|
backgroundColor: "var(--color-bg-muted)",
|
|
|
|
|
color: "var(--color-foreground)",
|
|
|
|
|
fontSize: 10.5,
|
|
|
|
|
fontWeight: 700,
|
|
|
|
|
letterSpacing: "0.06em",
|
2026-08-06 23:30:23 -03:00
|
|
|
lineHeight: "24px",
|
2026-08-06 22:23:30 -03:00
|
|
|
px: 1.75,
|
|
|
|
|
py: 1.375,
|
|
|
|
|
textTransform: "uppercase",
|
|
|
|
|
whiteSpace: "nowrap",
|
|
|
|
|
},
|
|
|
|
|
"& .MuiTableCell-body": {
|
|
|
|
|
fontSize: 13,
|
|
|
|
|
px: 1.5,
|
|
|
|
|
py: 1,
|
2026-08-06 23:30:23 -03:00
|
|
|
height: 52,
|
|
|
|
|
lineHeight: 1.43,
|
2026-08-06 22:23:30 -03:00
|
|
|
whiteSpace: "nowrap",
|
|
|
|
|
},
|
|
|
|
|
"& .vendor-row-actions": {
|
|
|
|
|
opacity: { xs: 1, md: 0 },
|
|
|
|
|
transition: "opacity 120ms ease",
|
|
|
|
|
},
|
|
|
|
|
"& .MuiTableRow-root:hover .vendor-row-actions, & .MuiTableRow-root:focus-within .vendor-row-actions":
|
|
|
|
|
{
|
|
|
|
|
opacity: 1,
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
>
|
2026-08-06 23:30:23 -03:00
|
|
|
<colgroup>
|
|
|
|
|
<col style={{ width: 196.34375 }} />
|
|
|
|
|
<col style={{ width: 207.25 }} />
|
|
|
|
|
<col style={{ width: 152.703125 }} />
|
|
|
|
|
<col style={{ width: 218.15625 }} />
|
|
|
|
|
<col style={{ width: 152.703125 }} />
|
|
|
|
|
<col style={{ width: 251.90625 }} />
|
|
|
|
|
<col style={{ width: 109.078125 }} />
|
|
|
|
|
<col style={{ width: 119.984375 }} />
|
|
|
|
|
<col style={{ width: 141.875 }} />
|
|
|
|
|
</colgroup>
|
2026-08-04 09:53:48 -03:00
|
|
|
<TableHead>
|
|
|
|
|
<TableRow>
|
|
|
|
|
{TABLE_COLUMNS.map((column) => (
|
2026-08-06 22:23:30 -03:00
|
|
|
<TableCell key={column} align={column === "Total Jobs" ? "center" : "left"}>
|
2026-08-04 09:53:48 -03:00
|
|
|
{column}
|
|
|
|
|
</TableCell>
|
|
|
|
|
))}
|
2026-08-06 22:23:30 -03:00
|
|
|
<TableCell>Status</TableCell>
|
|
|
|
|
<TableCell align="right" aria-label="Vendor actions" />
|
2026-08-04 09:53:48 -03:00
|
|
|
</TableRow>
|
|
|
|
|
</TableHead>
|
|
|
|
|
<TableBody>
|
|
|
|
|
{isLoading ? (
|
|
|
|
|
<TableRow>
|
2026-08-06 22:23:30 -03:00
|
|
|
<TableCell colSpan={columnCount} align="center" className="py-10">
|
2026-08-04 09:53:48 -03:00
|
|
|
<CircularProgress size={28} />
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
) : showEmptyRow ? (
|
|
|
|
|
<TableRow>
|
2026-08-06 22:23:30 -03:00
|
|
|
<TableCell colSpan={columnCount} align="center" className="py-10">
|
|
|
|
|
<Stack spacing={1} sx={{ alignItems: "center" }}>
|
|
|
|
|
<SearchOffOutlinedIcon sx={{ color: "text.secondary", fontSize: 32 }} />
|
|
|
|
|
<Text variant="description" className="font-semibold">
|
|
|
|
|
{`No ${isActive ? "active" : "inactive"} technicians found`}
|
|
|
|
|
</Text>
|
|
|
|
|
<Text variant="caption" tone="muted">
|
|
|
|
|
{isFiltered
|
|
|
|
|
? "Try a different search, or add a new vendor."
|
|
|
|
|
: "Add a vendor to get started."}
|
|
|
|
|
</Text>
|
|
|
|
|
</Stack>
|
2026-08-04 09:53:48 -03:00
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
) : (
|
|
|
|
|
rows.map((row) => (
|
|
|
|
|
<VendorTableRow
|
|
|
|
|
key={String(row.id ?? `company-${row.companyId ?? ""}`)}
|
|
|
|
|
row={row}
|
|
|
|
|
onOpenDetail={onOpenDetail}
|
2026-08-06 22:23:30 -03:00
|
|
|
onOpenEdit={onOpenEdit}
|
2026-08-04 09:53:48 -03:00
|
|
|
/>
|
|
|
|
|
))
|
|
|
|
|
)}
|
|
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
|
|
|
|
</TableContainer>
|
|
|
|
|
);
|
|
|
|
|
}
|