All files / src/components/team InviteMemberModal.tsx

91.17% Statements 31/34
85.45% Branches 47/55
100% Functions 8/8
96.42% Lines 27/28

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                7x                                         242x 242x 212x   66x 66x       130x 130x 50x   80x     80x                                                       355x 355x     355x 114x     355x   146x 146x 355x 355x 355x   355x       355x                                                                                       95x 7x                                                                     4x         438x                                                      
import { useState, useEffect } from "react";
import { X, Mail, Phone, Loader2 } from "lucide-react";
import { Button } from "../ui/button";
import { Input } from "../ui/input";
import { useDialogAccessibility } from "../../hooks";
import { isValidEmail, isValidPhone } from "../../lib/validation";
import type { ContactCheckResult } from "../../lib/api";
 
const ROLES = [
	{
		value: "owner",
		label: "Owner",
		description: "Full access. Can manage team, projects, and settings",
	},
	{
		value: "manager",
		label: "Manager",
		description: "Can manage projects and inquiries",
	},
	{
		value: "staff",
		label: "Staff",
		description: "Can view projects and inquiries",
	},
];
 
type ContactType = "email" | "phone" | "unknown";
 
function detectContactType(value: string): ContactType {
	const trimmed = value.trim();
	if (!trimmed) return "unknown";
	if (trimmed.includes("@")) return "email";
	// Starts with digit or +, treat as phone
	Iif (/^[+\d]/.test(trimmed)) return "phone";
	return "unknown";
}
 
function getFormatError(value: string, type: ContactType): string | null {
	Iif (!value.trim()) return null;
	if (type === "email" && !isValidEmail(value.trim())) {
		return "Enter a valid email address";
	}
	Iif (type === "phone" && !isValidPhone(value.trim().replace(/\D/g, "").replace(/^91/, ""))) {
		return "Enter a valid 10-digit Indian mobile number";
	}
	return null;
}
 
type InviteMemberModalProps = {
	isOpen: boolean;
	contact: string;
	role: "owner" | "manager" | "staff";
	isInviting: boolean;
	contactCheck: ContactCheckResult | null;
	isChecking: boolean;
	onClose: () => void;
	onContactChange: (contact: string) => void;
	onRoleChange: (role: "owner" | "manager" | "staff") => void;
	onSubmit: () => void;
};
 
export function InviteMemberModal({
	isOpen,
	contact,
	role,
	isInviting,
	contactCheck,
	isChecking,
	onClose,
	onContactChange,
	onRoleChange,
	onSubmit,
}: InviteMemberModalProps) {
	const { dialogRef, handleFocusTrap } = useDialogAccessibility(onClose);
	const [touched, setTouched] = useState(false);
 
	// Reset touched state when modal opens/closes
	useEffect(() => {
		if (!isOpen) setTouched(false);
	}, [isOpen]);
 
	if (!isOpen) return null;
 
	const contactType = detectContactType(contact);
	const formatError = touched ? getFormatError(contact, contactType) : null;
	const isFormatValid = contact.trim() !== "" && !getFormatError(contact, contactType);
	const hasBlockingIssue = contactCheck && !contactCheck.available;
	const canSubmit = isFormatValid && !hasBlockingIssue && !isChecking;
 
	const contactIcon = contactType === "phone"
		? <Phone className="h-4 w-4 text-foreground-subtle" />
		: <Mail className="h-4 w-4 text-foreground-subtle" />;
 
	return (
		<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50" tabIndex={-1}>
			<div
				ref={dialogRef}
				role="dialog"
				aria-modal="true"
				aria-labelledby="invite-member-dialog-title"
				onKeyDown={handleFocusTrap}
				className="bg-background-elevated rounded-lg shadow-xl w-full max-w-md mx-4"
			>
				<div className="flex items-center justify-between p-4 border-b">
					<h3 id="invite-member-dialog-title" className="text-lg font-semibold">Invite Team Member</h3>
					<button
						type="button"
						onClick={onClose}
						aria-label="Close"
						className="p-1 hover:bg-background-subtle rounded"
					>
						<X className="h-5 w-5" />
					</button>
				</div>
				<div className="p-4 space-y-4">
					<p className="text-sm text-foreground-muted">
						Enter an email address or phone number to invite someone to your team.
					</p>
					<div>
						<label
							htmlFor="invite-contact"
							className="block text-sm font-medium text-foreground-default mb-1"
						>
							Email or Phone *
						</label>
						<div className="relative">
							<div className="absolute left-3 top-1/2 -translate-y-1/2">
								{isChecking ? (
									<Loader2 className="h-4 w-4 text-foreground-subtle animate-spin" />
								) : (
									contactIcon
								)}
							</div>
							<Input
								id="invite-contact"
								type="text"
								value={contact}
								onChange={(e) => onContactChange(e.target.value)}
								onBlur={() => setTouched(true)}
								placeholder="team@example.com or 9876543210"
								className="pl-10"
								aria-describedby={formatError || hasBlockingIssue ? "contact-feedback" : undefined}
								aria-invalid={!!(formatError || hasBlockingIssue)}
							/>
						</div>
						{/* Validation feedback */}
						{formatError && (
							<p id="contact-feedback" className="mt-1 text-sm text-error" role="alert">
								{formatError}
							</p>
						)}
						{!formatError && hasBlockingIssue && contactCheck?.message && (
							<p id="contact-feedback" className="mt-1 text-sm text-warning" role="alert">
								{contactCheck.message}
							</p>
						)}
						{!formatError && !hasBlockingIssue && isFormatValid && !isChecking && contactCheck?.available && (
							<p className="mt-1 text-sm text-success">
								{contactType === "email" ? "Email" : "Phone number"} is available
							</p>
						)}
					</div>
					<div>
						<label
							htmlFor="invite-role"
							className="block text-sm font-medium text-foreground-default mb-1"
						>
							Role
						</label>
						<select
							id="invite-role"
							value={role}
							onChange={(e) =>
								onRoleChange(e.target.value as "owner" | "manager" | "staff")
							}
							className="h-10 w-full rounded-md border border-default bg-background-elevated px-3 text-sm focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
						>
							{ROLES.map((roleOption) => (
								<option key={roleOption.value} value={roleOption.value}>
									{roleOption.label} - {roleOption.description}
								</option>
							))}
						</select>
					</div>
				</div>
				<div className="flex justify-end gap-2 p-4 border-t">
					<Button variant="outline" onClick={onClose}>
						Cancel
					</Button>
					<Button onClick={onSubmit} isLoading={isInviting} disabled={!canSubmit}>
						{contactType === "phone" ? (
							<Phone className="h-4 w-4 mr-2" />
						) : (
							<Mail className="h-4 w-4 mr-2" />
						)}
						Send Invitation
					</Button>
				</div>
			</div>
		</div>
	);
}
 
// Re-export for use by parent components
export { detectContactType };