"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { UserCheckIcon, UserPlusIcon, UsersIcon, XIcon } from "lucide-react"; import { Item, ItemContent, ItemHeader, ItemTitle } from "@/components/ui/item"; import { Button } from "@/components/ui/button"; import { cn } from "@/lib/utils"; import { RoleDescription, type RoleDescriptionData, } from "@/components/frontend/intelligence/RoleDescription"; import { removeMissionRoleSignup, signupForMissionRole, withdrawMissionRoleSignup, type ActionResult, } from "@/app/(frontend)/intelligence/missions/actions"; interface SignupPersonView { signupId: number; userId: number; name: string; holding: boolean; position: number; } interface SignupRoleView { roleIndex: number; roleName: string; description?: RoleDescriptionData; missionCritical: boolean; slots: number; filled: number; entries: SignupPersonView[]; eligibility: { eligible: boolean; reasons: string[] } | null; } interface MissionRoleSignupsProps { missionId: number; currentUserId: number | null; closed: boolean; closedReason?: string | null; roles: SignupRoleView[]; mySignup: { roleIndex: number; holding: boolean; position: number } | null; canManage: boolean; } export function MissionRoleSignups({ missionId, currentUserId, closed, closedReason, roles, mySignup, canManage, }: MissionRoleSignupsProps) { const router = useRouter(); const [pending, setPending] = useState(null); const [error, setError] = useState(null); if (roles.length === 0) return null; async function run(key: string, action: () => Promise) { setPending(key); setError(null); const res = await action(); if (!res.success) { setError(res.error ?? "Something went wrong."); } setPending(null); router.refresh(); } const signup = (role: SignupRoleView) => run(`signup:${role.roleIndex}`, () => signupForMissionRole({ missionId, roleIndex: role.roleIndex }), ); const withdraw = () => run("withdraw", () => withdrawMissionRoleSignup({ missionId })); const remove = (signupId: number) => run(`remove:${signupId}`, () => removeMissionRoleSignup({ missionId, signupId })); return ( First come, first served {closed && closedReason &&

{closedReason}

} {!currentUserId && (

Log in to sign up for a role.

)} {error &&

{error}

} {roles.map((role) => { const holders = role.entries.filter((e) => e.holding); const waitlist = role.entries.filter((e) => !e.holding); const isMine = mySignup?.roleIndex === role.roleIndex; const openSlots = Math.max(0, role.slots - role.filled); const eligible = role.eligibility?.eligible ?? true; const reasons = role.eligibility?.reasons ?? []; return (
{role.roleName} {role.missionCritical && ( Critical )} {role.filled}/{role.slots}
{role.description && }
Slots {openSlots > 0 && ( ({openSlots} open) )}
{holders.length > 0 ? (
    {holders.map((person) => (
  • {person.name} {canManage && person.userId !== currentUserId && ( )}
  • ))}
) : (

Unfilled.

)}
{waitlist.length > 0 && (
Waitlist ({waitlist.length})
    {waitlist.map((person) => (
  • {person.position}. {person.name} {canManage && ( )}
  • ))}
)} {isMine && (

{mySignup?.holding ? "You hold a slot for this role." : `You are waitlisted (#${mySignup?.position}).`}

)} {currentUserId !== null && !closed && !eligible && !isMine && reasons.length > 0 && (

{reasons.join(" ")}

)} {currentUserId !== null && !closed && (
{isMine ? ( ) : ( )}
)}
); })}
); }