"use client"; import type { RefObject } from "react"; import { useEffect, useId, useRef, useState } from "react"; import { useFormStatus } from "react-dom"; export function UserGroupSelect({ action, effectiveGroupId, groups, returnTo, userId, userLabel, }: { action: (formData: FormData) => Promise; effectiveGroupId: string; groups: Array<{ id: string; name: string }>; returnTo: string; userId: string; userLabel: string; }) { const dialogRef = useRef(null); const titleId = useId(); const descriptionId = useId(); const helpId = useId(); const [selectedGroupId, setSelectedGroupId] = useState(effectiveGroupId); const [proposedGroupId, setProposedGroupId] = useState(null); const [submitting, setSubmitting] = useState(false); const currentGroup = groups.find((group) => group.id === effectiveGroupId); const proposedGroup = groups.find((group) => group.id === proposedGroupId); function resetSelection() { setSelectedGroupId(effectiveGroupId); setProposedGroupId(null); } return ( <> Changing this selection opens a confirmation dialog. { if (submitting) event.preventDefault(); }} onClose={() => { if (!submitting) resetSelection(); }} ref={dialogRef} >
setSubmitting(true)}>

Confirm membership

Move {userLabel}?

Change the effective group from {currentGroup?.name ?? "unknown"} to {proposedGroup?.name ?? "unknown"}. Their access policy changes immediately.

); } function AssignmentActions({ dialogRef, onPendingChange }: { dialogRef: RefObject; onPendingChange: (pending: boolean) => void }) { const { pending } = useFormStatus(); const observedPending = useRef(false); useEffect(() => { if (pending) { observedPending.current = true; onPendingChange(true); } else if (observedPending.current) { observedPending.current = false; onPendingChange(false); } }, [onPendingChange, pending]); return (
{pending ? "Group change in progress." : ""}
); }