1
0
Fork 0
polaris-task-force/src/components/frontend/transfers/TransferApprovalCard.tsx
Z8MB1E 76e948fd59 feat: Add remaining transfer components and utilities
Add transfer workflow components and utilities:

1. Transfer types and core functions (src/lib/transfers/index.ts):
   - Type-safe transfer request and decision creation
   - Appeal, cancel, and resolve core functions
   - Server action conventions with error handling

2. Transfer UI components (src/components/frontend/transfers/):
   - TransferActionDialog.tsx: Handle approval/rejection appeals
   - TransferApprovalCard.tsx: Display approval decisions
   - TransferNoteDialog.tsx: Add notes for transfers/appeals
   - TransferRequestCard.tsx: Show transfer request details
   - TransferResolutionCard.tsx: Display superuser resolutions
   - TransfersView.tsx: Main transfers list page
   - transferTypes.ts: Shared TypeScript types

3. Notification types (src/lib/transfers/notificationTypes.ts):
   - Define notification type constants for transfer events
   - Include TRANSFER_NOTIFICATION_TYPES for bridge filtering

4. Integration tests (tests/int/):
   - transfer-requests.int.spec.ts: Test bot request polling
   - transfer-bot-delivery.int.spec.ts: Test Discord notification delivery

These components and utilities complete the transfer workflow
implementation including approvals, appeals, and delivery.
2026-08-31 23:41:29 -04:00

161 lines
5.4 KiB
TypeScript

"use client";
import { useState } from "react";
import { toast } from "sonner";
import { CheckIcon, XIcon } from "lucide-react";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { approveTransfer, rejectTransfer } from "@/app/(frontend)/transfers/actions";
import { TransferNoteDialog } from "@/components/frontend/transfers/TransferNoteDialog";
import {
TRANSFER_DECISION_LABELS,
TRANSFER_STATUS_LABELS,
TRANSFER_STATUS_VARIANTS,
assignmentLabel,
formatTransferDate,
personLabel,
type TransferRecord,
type TransferSide,
} from "@/components/frontend/transfers/transferTypes";
interface TransferApprovalCardProps {
readonly transfer: TransferRecord;
readonly side: TransferSide;
readonly onUpdated: () => Promise<void>;
}
export function TransferApprovalCard({ transfer, side, onUpdated }: TransferApprovalCardProps) {
const [rejectOpen, setRejectOpen] = useState(false);
const [busy, setBusy] = useState<"approve" | null>(null);
const [error, setError] = useState<string | null>(null);
const sideLabel = side === "from" ? "source" : "destination";
const otherSide = side === "from" ? "to" : "from";
const otherDecision =
otherSide === "from" ? transfer.fromLeaderDecision : transfer.toLeaderDecision;
async function handleApprove() {
setBusy("approve");
setError(null);
try {
const result = await approveTransfer(transfer.id, side);
if (!result.success) {
setError(result.error ?? "Could not approve the transfer request.");
return;
}
toast.success("Transfer request approved");
await onUpdated();
} catch (caughtError) {
setError(
caughtError instanceof Error
? caughtError.message
: "Could not approve the transfer request.",
);
} finally {
setBusy(null);
}
}
async function handleReject(reason: string) {
const result = await rejectTransfer(transfer.id, side, reason);
if (result.success) void onUpdated();
return result;
}
return (
<Card>
<CardHeader className="flex-row items-start">
<div className="flex min-w-0 flex-col gap-1">
<CardTitle className="text-base">Transfer #{transfer.id}</CardTitle>
<CardDescription>
{personLabel(transfer.requester)} · requested {formatTransferDate(transfer.createdAt)}
</CardDescription>
</div>
<Badge variant={TRANSFER_STATUS_VARIANTS[transfer.status]}>
{TRANSFER_STATUS_LABELS[transfer.status]}
</Badge>
</CardHeader>
<CardContent className="flex flex-col gap-3">
<div className="grid min-w-0 grid-cols-1 gap-3 sm:grid-cols-2">
<div className="flex min-w-0 flex-col gap-1">
<span className="text-xs font-semibold uppercase tracking-wide text-muted-foreground">
From
</span>
<span className="truncate text-sm">{assignmentLabel(transfer.fromAssignment)}</span>
</div>
<div className="flex min-w-0 flex-col gap-1">
<span className="text-xs font-semibold uppercase tracking-wide text-muted-foreground">
To
</span>
<span className="truncate text-sm">{assignmentLabel(transfer.toAssignment)}</span>
</div>
</div>
<div className="flex flex-wrap items-center gap-2 text-xs text-muted-foreground">
<span>Your {sideLabel} approval is required.</span>
<span aria-hidden="true">·</span>
<span>
Other side: {TRANSFER_DECISION_LABELS[otherDecision]}
</span>
</div>
{transfer.requestNote && (
<div className="flex flex-col gap-1">
<span className="text-xs font-semibold uppercase tracking-wide text-muted-foreground">
Request note
</span>
<p className="text-sm text-muted-foreground">{transfer.requestNote}</p>
</div>
)}
{error && (
<Alert variant="destructive">
<AlertTitle>Action failed</AlertTitle>
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
</CardContent>
<CardFooter className="flex flex-wrap gap-2">
<Button size="sm" onClick={() => void handleApprove()} disabled={busy !== null}>
{busy === "approve" ? "Approving..." : <CheckIcon data-icon="inline-start" />}
{busy === "approve" ? null : "Approve"}
</Button>
<Button
size="sm"
variant="destructive"
onClick={() => setRejectOpen(true)}
disabled={busy !== null}
>
<XIcon data-icon="inline-start" />
Reject
</Button>
</CardFooter>
<TransferNoteDialog
open={rejectOpen}
onOpenChange={setRejectOpen}
title="Reject transfer request"
description={`Give ${personLabel(transfer.requester)} a reason for rejecting this transfer.`}
fieldId={`transfer-rejection-${transfer.id}-${side}`}
fieldLabel="Rejection reason"
placeholder="Explain the decision for the requester."
submitLabel="Reject request"
successMessage="Transfer request rejected"
required
submitVariant="destructive"
onSubmit={handleReject}
/>
</Card>
);
}