|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useTransition } from "react"; |
| 4 | + |
| 5 | +import { |
| 6 | + acceptInvitationAction, |
| 7 | + declineInvitationAction, |
| 8 | +} from "@/app/invitations/actions"; |
| 9 | +import { Button } from "@/components/core/Button"; |
| 10 | +import { StatusPill } from "@/components/core/StatusPill"; |
| 11 | +import { Toast } from "@/components/core/Toast"; |
| 12 | +import type { ProjectStatus, UserTeamMembershipDto } from "@/lib/api/types"; |
| 13 | +import { cn } from "@/lib/utils"; |
| 14 | + |
| 15 | +function toStatusPill(projectStatus: ProjectStatus) { |
| 16 | + switch (projectStatus) { |
| 17 | + case "Open": |
| 18 | + return <StatusPill status="open" />; |
| 19 | + case "TeamConfirmed": |
| 20 | + return <StatusPill status="accepted" label="Team confirmed" />; |
| 21 | + case "Closed": |
| 22 | + return <StatusPill status="closed" />; |
| 23 | + default: |
| 24 | + return null; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +interface InvitationCardProps { |
| 29 | + membership: UserTeamMembershipDto; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Card displaying a single team invitation or self-request. |
| 34 | + * Invited entries show Accept/Decline buttons; Requested entries show an awaiting-response indicator. |
| 35 | + * @param membership - The membership record to display |
| 36 | + */ |
| 37 | +export function InvitationCard({ membership }: InvitationCardProps) { |
| 38 | + const [isPendingAccept, startAccept] = useTransition(); |
| 39 | + const [isPendingDecline, startDecline] = useTransition(); |
| 40 | + const [toast, setToast] = useState<string | null>(null); |
| 41 | + |
| 42 | + const busy = isPendingAccept || isPendingDecline; |
| 43 | + |
| 44 | + function handleAccept() { |
| 45 | + setToast("Invitation accepted"); |
| 46 | + startAccept(async () => { |
| 47 | + await acceptInvitationAction(membership.teamId); |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + function handleDecline() { |
| 52 | + setToast("Invitation declined"); |
| 53 | + startDecline(async () => { |
| 54 | + await declineInvitationAction(membership.teamId); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + return ( |
| 59 | + <> |
| 60 | + <article |
| 61 | + className={cn( |
| 62 | + "bg-paper border border-[var(--border)] rounded-md p-[22px]", |
| 63 | + "flex flex-col gap-3", |
| 64 | + busy && "opacity-60", |
| 65 | + )} |
| 66 | + > |
| 67 | + <div className="flex items-start justify-between gap-4"> |
| 68 | + <div className="min-w-0"> |
| 69 | + <h3 className="text-[18px] font-medium tracking-[-0.015em] leading-[1.25] text-ink m-0"> |
| 70 | + {membership.projectTitle} |
| 71 | + </h3> |
| 72 | + <p className="text-[13px] text-ink-soft mt-1 m-0"> |
| 73 | + Role: <span className="text-ink">{membership.projectRole}</span> |
| 74 | + </p> |
| 75 | + </div> |
| 76 | + <div className="shrink-0 mt-0.5"> |
| 77 | + {toStatusPill(membership.projectStatus)} |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + |
| 81 | + {membership.membershipStatus === "Invited" && ( |
| 82 | + <div className="flex gap-2 pt-1"> |
| 83 | + <Button size="sm" onClick={handleAccept} disabled={busy}> |
| 84 | + Accept |
| 85 | + </Button> |
| 86 | + <Button |
| 87 | + size="sm" |
| 88 | + variant="destructive" |
| 89 | + onClick={handleDecline} |
| 90 | + disabled={busy} |
| 91 | + > |
| 92 | + Decline |
| 93 | + </Button> |
| 94 | + </div> |
| 95 | + )} |
| 96 | + |
| 97 | + {membership.membershipStatus === "Requested" && ( |
| 98 | + <div className="pt-1"> |
| 99 | + <StatusPill status="pending" label="Awaiting response" /> |
| 100 | + </div> |
| 101 | + )} |
| 102 | + </article> |
| 103 | + |
| 104 | + {toast && <Toast message={toast} onDismiss={() => setToast(null)} />} |
| 105 | + </> |
| 106 | + ); |
| 107 | +} |
0 commit comments