All files / components/transcript named-tables.tsx

100% Statements 41/41
90% Branches 18/20
100% Functions 12/12
100% Lines 37/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228          5x 5x                   5x           5x                       6x           6x   6x               6x               6x 6x   6x                     25x               25x 5x     20x                                                                     20x 20x 23x                           23x 9x     23x     20x 18x   20x 18x     20x                     12x             12x 2x     10x                                                       10x 15x                   10x 8x   10x 8x     10x                    
import type {
  ToolOutputTableAction,
  ToolOutputTableColumn,
  ToolOutputTableRow,
} from "@/components/chat/tool-output-table";
import { formatHumanDateRange } from "@/utils/date";
import { leaveTypeLabel } from "@/utils/leave";
import {
  asString,
  asNumber,
  asRecordArray,
  compactLeaveTypeLabel,
  getRequestRowSummary,
  getBalanceRowSummary,
  getMemberRowSummary,
  type UnknownRecord,
} from "./utils";
import {
  renderLeaveTypeChip,
  renderStatusChip,
  renderEmployeeCell,
  getMemberRowActions,
} from "./cells";
 
export type ToolOutputTableModel = {
  id: string;
  title: string;
  columns: ToolOutputTableColumn[];
  rows: ToolOutputTableRow[];
  rowActions?: ToolOutputTableAction[][];
  rowActionSummaries?: string[];
  emptyLabel: string;
};
 
export function buildMembersTableModel(params: {
  id: string;
  title: string;
  memberRows: UnknownRecord[];
  emptyLabel: string;
}): ToolOutputTableModel | null {
  const { memberRows } = params;
 
  const columns: ToolOutputTableColumn[] = [
    { key: "employee", label: "Employee", className: "sm:pr-4" },
    { key: "pendingCount", label: "Pending", align: "center", className: "font-semibold tabular-nums" },
    { key: "approvedCount", label: "Approved", align: "center", className: "font-semibold tabular-nums" },
    { key: "cancelledCount", label: "Cancelled", align: "center", className: "font-semibold tabular-nums" },
    { key: "totalCount", label: "Total", align: "center", className: "font-semibold tabular-nums" },
  ];
 
  const rows = memberRows.map((member) => ({
    employee: renderEmployeeCell(member),
    pendingCount: asNumber(member.pendingCount, "0"),
    approvedCount: asNumber(member.approvedCount, "0"),
    cancelledCount: asNumber(member.cancelledCount, "0"),
    totalCount: asNumber(member.totalCount, "0"),
  }));
 
  const rowActions = memberRows.map((member) => getMemberRowActions(member));
  const rowActionSummaries = memberRows.map((member) => getMemberRowSummary(member));
 
  return {
    id: params.id,
    title: params.title,
    columns,
    rows,
    rowActions,
    rowActionSummaries,
    emptyLabel: params.emptyLabel,
  };
}
 
export function getRequestTableModel(params: {
  id: string;
  title: string;
  payload: UnknownRecord;
  showEmployee: boolean;
  emptyLabel: string;
  getRowActions?: (request: UnknownRecord) => ToolOutputTableAction[];
}): ToolOutputTableModel | null {
  if (!Array.isArray(params.payload.requests)) {
    return null;
  }
 
  const columns: ToolOutputTableColumn[] = params.showEmployee
    ? [
        { key: "employee", label: "Employee", className: "sm:pr-4" },
        { key: "leaveType", label: "Leave type", align: "center" },
        {
          key: "dateRange",
          label: "Date range",
          align: "center",
          className: "whitespace-nowrap",
        },
        {
          key: "days",
          label: "Days",
          align: "center",
          className: "font-semibold tabular-nums",
        },
        { key: "status", label: "Status", align: "center" },
      ]
    : [
        { key: "leaveType", label: "Leave type", align: "center" },
        {
          key: "dateRange",
          label: "Date range",
          align: "center",
          className: "whitespace-nowrap",
        },
        {
          key: "days",
          label: "Days",
          align: "center",
          className: "font-semibold tabular-nums",
        },
        { key: "status", label: "Status", align: "center" },
      ];
 
  const requestRows = asRecordArray(params.payload.requests);
  const rows = requestRows.map((request) => {
    const row: ToolOutputTableRow = {
      leaveType: renderLeaveTypeChip(
        compactLeaveTypeLabel(
          asString(request.leaveTypeLabel, asString(request.leaveType)),
        ),
      ),
      dateRange: formatHumanDateRange(
        asString(request.startDate, ""),
        asString(request.endDate, ""),
      ),
      days: asNumber(request.days),
      status: renderStatusChip(asString(request.status, "")),
    };
 
    if (params.showEmployee) {
      row.employee = renderEmployeeCell(request);
    }
 
    return row;
  });
 
  const rowActions = params.getRowActions
    ? requestRows.map((request) => params.getRowActions?.(request) ?? [])
    : undefined;
  const rowActionSummaries = rowActions
    ? requestRows.map((request) => getRequestRowSummary(request))
    : undefined;
 
  return {
    id: params.id,
    title: params.title,
    columns,
    rows,
    rowActions,
    rowActionSummaries,
    emptyLabel: params.emptyLabel,
  };
}
 
export function getBalanceTableModel(params: {
  id: string;
  title: string;
  payload: UnknownRecord;
  emptyLabel: string;
  getRowActions?: (balance: UnknownRecord) => ToolOutputTableAction[];
}): ToolOutputTableModel | null {
  if (!Array.isArray(params.payload.balances)) {
    return null;
  }
 
  const columns: ToolOutputTableColumn[] = [
    { key: "leaveType", label: "Leave type", align: "center" },
    {
      key: "allowance",
      label: "Allowance",
      align: "center",
      className: "font-semibold tabular-nums",
    },
    {
      key: "used",
      label: "Used",
      align: "center",
      className: "font-semibold tabular-nums",
    },
    {
      key: "pending",
      label: "Pending",
      align: "center",
      className: "font-semibold tabular-nums",
    },
    {
      key: "remaining",
      label: "Remaining",
      align: "center",
      className: "font-semibold tabular-nums",
    },
  ];
 
  const balanceRows = asRecordArray(params.payload.balances);
  const rows = balanceRows.map((balance) => ({
    leaveType: renderLeaveTypeChip(
      compactLeaveTypeLabel(leaveTypeLabel(asString(balance.leaveType, "annual"))),
    ),
    allowance: asNumber(balance.allowance),
    used: asNumber(balance.used),
    pending: asNumber(balance.pending),
    remaining: asNumber(balance.remaining),
  }));
 
  const rowActions = params.getRowActions
    ? balanceRows.map((balance) => params.getRowActions?.(balance) ?? [])
    : undefined;
  const rowActionSummaries = rowActions
    ? balanceRows.map((balance) => getBalanceRowSummary(balance))
    : undefined;
 
  return {
    id: params.id,
    title: params.title,
    columns,
    rows,
    rowActions,
    rowActionSummaries,
    emptyLabel: params.emptyLabel,
  };
}