All files / components/transcript cells.tsx

98.68% Statements 75/76
97.4% Branches 75/77
100% Functions 11/11
98.61% Lines 71/72

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 229 230 231 232 233 234 2357x 7x 7x 7x                   7x   41x       34x 34x   33x   33x                             20x 20x 20x   20x                                                       2x 11x     11x 11x 11x   11x 9x   2x     3x       23x 23x     23x 23x 23x 23x 16x 7x   9x     11x 13x   13x 3x     10x 1x     9x     9x                 9x 13x   13x   13x 8x                               5x   4x 3x                   1x 1x                         8x 8x 8x 1x     7x                 9x 9x 9x   8x 8x   8x 4x             8x           8x     12x 12x       5x         4x   3x 4x 4x            
import { Avatar } from "@/components/ui/avatar";
import { Badge } from "@/components/ui/badge";
import { getAvatarUrl, getInitialsFromName } from "@/utils/avatar";
import { formatHumanDateRange } from "@/utils/date";
import type { ToolOutputTableAction } from "@/components/chat/tool-output-table";
import {
  asString,
  asOptionalString,
  formatStatus,
  compactLeaveTypeLabel,
  normalizeRequestStatus,
  isFutureOrTodayDate,
  type UnknownRecord,
} from "./utils";
 
export function renderLeaveTypeChip(label: string) {
  return <Badge variant="info" className="px-2.5 py-0.5 text-xs">{label}</Badge>;
}
 
export function renderStatusChip(status: string) {
  if (!status) return "—";
 
  const normalizedStatus = status.toLowerCase();
  const statusVariant =
    normalizedStatus === "approved"
      ? "success"
      : normalizedStatus === "pending"
        ? "warning"
        : normalizedStatus === "rejected"
          ? "danger"
          : "neutral";
 
  return (
    <Badge variant={statusVariant} className="px-2.5 py-0.5 text-xs">
      {formatStatus(status)}
    </Badge>
  );
}
 
export function renderEmployeeCell(request: UnknownRecord) {
  const employeeName = asString(request.employeeName);
  const employeeTeam = asString(request.team, "");
  const employeeAvatar =
    asOptionalString(request.employeeAvatar)?.trim() ||
    getAvatarUrl(employeeName);
 
  return (
    <div className="flex min-w-0 items-center gap-2.5">
      <Avatar
        variant="user"
        src={employeeAvatar}
        alt={`${employeeName} avatar`}
        initials={getInitialsFromName(employeeName)}
        size="sm"
        className="ring-white/15"
      />
 
      <div className="min-w-0">
        <p className="whitespace-nowrap font-dm-sans text-sm font-semibold leading-[1.25] text-white/92 light:text-slate-800">
          {employeeName}
        </p>
        {employeeTeam ? (
          <p className="mt-0.5 whitespace-nowrap font-dm-sans text-xs leading-[1.25] text-white/60 light:text-slate-500">
            {employeeTeam}
          </p>
        ) : null}
      </div>
    </div>
  );
}
 
export function buildSelfCancelPrompt(request: UnknownRecord): string {
  const leaveType = compactLeaveTypeLabel(
    asString(request.leaveTypeLabel, asString(request.leaveType, "")),
  );
  const startDate = asOptionalString(request.startDate)?.trim() ?? "";
  const endDate = asOptionalString(request.endDate)?.trim() ?? "";
  const dateRange = formatHumanDateRange(startDate, endDate);
 
  if (leaveType && leaveType !== "—" && dateRange) {
    return `I'd like to cancel my ${leaveType} leave ${dateRange}.`;
  }
  return "";
}
 
export function buildTeamActionPrompt(
  request: UnknownRecord,
  action: "approve" | "reject",
): string {
  const name = asOptionalString(request.employeeName)?.trim() ?? "";
  const leaveType = compactLeaveTypeLabel(
    asString(request.leaveTypeLabel, asString(request.leaveType, "")),
  );
  const startDate = asOptionalString(request.startDate)?.trim() ?? "";
  const endDate = asOptionalString(request.endDate)?.trim() ?? "";
  const dateRange = formatHumanDateRange(startDate, endDate);
  if (!name || !leaveType || leaveType === "—" || !dateRange) return "";
  if (action === "approve") {
    return `Approve ${name}'s ${leaveType} leave ${dateRange}. Comment: Approved.`;
  }
  return `Reject ${name}'s ${leaveType} leave ${dateRange}. Reason: Not approved.`;
}
 
export function getSelfRequestRowActions(request: UnknownRecord): ToolOutputTableAction[] {
  const status = normalizeRequestStatus(request.status);
 
  if (status !== "approved" && status !== "pending") {
    return [];
  }
 
  if (!isFutureOrTodayDate(request.startDate)) {
    return [];
  }
 
  const prompt = buildSelfCancelPrompt(request)
    || "I want to cancel one of my requests. Could you list my cancellable requests so I can choose one?";
 
  return [
    {
      label: "Cancel request",
      prompt,
      tone: "danger",
    },
  ];
}
 
export function getTeamRequestRowActions(request: UnknownRecord): ToolOutputTableAction[] {
  const status = normalizeRequestStatus(request.status);
 
  const isFuture = isFutureOrTodayDate(request.startDate);
 
  if (status === "pending") {
    return [
      {
        label: "Approve",
        prompt: buildTeamActionPrompt(request, "approve") ||
          "Please approve the selected pending team request.",
        tone: "success",
      },
      {
        label: "Reject",
        prompt: buildTeamActionPrompt(request, "reject") ||
          "Please reject the selected pending team request.",
        tone: "danger",
      },
    ];
  }
 
  if (!isFuture) return [];
 
  if (status === "approved") {
    return [
      {
        label: "Reject",
        prompt: buildTeamActionPrompt(request, "reject") ||
          "Please reject the selected approved team request.",
        tone: "danger",
      },
    ];
  }
 
  Eif (status === "rejected") {
    return [
      {
        label: "Approve",
        prompt: buildTeamActionPrompt(request, "approve") ||
          "Please approve the selected rejected team request.",
        tone: "success",
      },
    ];
  }
 
  return [];
}
 
export function getBalanceRowActions(balance: UnknownRecord): ToolOutputTableAction[] {
  const leaveType = asOptionalString(balance.leaveType)?.trim().toLowerCase();
  if (!leaveType) {
    return [];
  }
 
  return [
    {
      label: "Request this type",
      prompt: `I want to submit a ${leaveType} time-off request.`,
      tone: "success",
    },
  ];
}
 
export function getMemberRowActions(member: UnknownRecord): ToolOutputTableAction[] {
  const employeeName = asOptionalString(member.employeeName)?.trim();
  if (!employeeName) return [];
 
  const pendingCount = typeof member.pendingCount === "number" ? member.pendingCount : 0;
  const actions: ToolOutputTableAction[] = [];
 
  if (pendingCount > 0) {
    actions.push({
      label: "View pending",
      prompt: `Show ${employeeName}'s pending time-off requests.`,
      tone: "neutral",
    });
  }
 
  actions.push({
    label: "View all",
    prompt: `Show all time-off requests for ${employeeName}.`,
    tone: "neutral",
  });
 
  return actions;
}
 
export function getRequestRowActionBuilder(toolName: string | null) {
  switch (toolName) {
    case "list_team_time_off_requests":
    case "approve_team_time_off_request":
    case "reject_team_time_off_request":
      return getTeamRequestRowActions;
    case "list_my_time_off_requests":
    case "get_my_time_off_balance":
    case "submit_my_time_off_request":
    case "cancel_my_time_off_request":
      return getSelfRequestRowActions;
    default:
      return (request: UnknownRecord) => {
        const hasEmployee = Boolean(asOptionalString(request.employeeName)?.trim());
        return hasEmployee
          ? getTeamRequestRowActions(request)
          : getSelfRequestRowActions(request);
      };
  }
}