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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 5x 5x 4x 4x 4x 4x 3x 1x 1x 4x 88x 33x 88x 88x 5x 3x 2x 88x 88x 9x 9x 5x 88x 1x 1x 1x 25x 12x 3x 1x 2x 1x | import { useState, useMemo, useRef, type FormEvent } from "react";
import { X, Linkedin, Instagram, Upload, Loader2, Camera } from "lucide-react";
import { Button } from "../ui/button";
import { Input } from "../ui/input";
import { ConfirmDialog } from "../ui/confirm-dialog";
import { useUnsavedChanges, useDialogAccessibility } from "../../hooks";
import { type ProLeadership, getImageUrl } from "../../lib/api";
import { uploadImage } from "../../lib/upload";
interface LeadershipModalProps {
member: ProLeadership | null;
proId: string;
onSave: (data: {
name: string;
role: string;
bio?: string;
photoUrl?: string | null;
linkedinUrl?: string;
instagramHandle?: string;
}) => void;
onClose: () => void;
}
export function LeadershipModal({
member,
proId,
onSave,
onClose,
}: LeadershipModalProps) {
const [name, setName] = useState(member?.name || "");
const [role, setRole] = useState(member?.role || "");
const [bio, setBio] = useState(member?.bio || "");
const [photoUrl, setPhotoUrl] = useState(member?.photoUrl || "");
const [uploadingPhoto, setUploadingPhoto] = useState(false);
const [linkedinUrl, setLinkedinUrl] = useState(member?.linkedinUrl || "");
const [instagramHandle, setInstagramHandle] = useState(
member?.instagramHandle || "",
);
const [showDiscardConfirm, setShowDiscardConfirm] = useState(false);
const [uploadError, setUploadError] = useState<string | null>(null);
const fileInputRef = useRef<HTMLInputElement>(null);
const handlePhotoUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (!file) return;
setUploadingPhoto(true);
setUploadError(null);
try {
const result = await uploadImage(proId, file, { type: "leadership" });
setPhotoUrl(result.storageKey);
} catch (err) {
console.error("Photo upload failed:", err);
setUploadError("Failed to upload photo. Please try a smaller image (max 10MB) in JPG, PNG, or WebP format.");
} finally {
setUploadingPhoto(false);
}
};
const initialValues = useMemo(
() => ({
name: member?.name || "",
role: member?.role || "",
bio: member?.bio || "",
photoUrl: member?.photoUrl || "",
linkedinUrl: member?.linkedinUrl || "",
instagramHandle: member?.instagramHandle || "",
}),
[member],
);
const isDirty = useUnsavedChanges(initialValues, {
name,
role,
bio,
photoUrl,
linkedinUrl,
instagramHandle,
});
const handleClose = () => {
if (isDirty) {
setShowDiscardConfirm(true);
} else {
onClose();
}
};
const { dialogRef, handleFocusTrap } = useDialogAccessibility(handleClose);
const handleSubmit = (e: FormEvent) => {
e.preventDefault();
if (!name || !role) return;
onSave({
name,
role,
bio: bio || undefined,
photoUrl: photoUrl || null,
linkedinUrl: linkedinUrl || undefined,
instagramHandle: instagramHandle || undefined,
});
};
return (
<div role="dialog" className="fixed inset-0 flex items-center justify-center z-50" onKeyDown={handleFocusTrap}>
<button
type="button"
aria-label="Close modal"
className="fixed inset-0 bg-overlay-dark cursor-default"
onClick={handleClose}
tabIndex={-1}
/>
<div
ref={dialogRef}
role="dialog"
aria-modal="true"
aria-labelledby="leadership-modal-title"
tabIndex={-1}
className="relative bg-background-elevated rounded-lg shadow-xl w-full max-w-md mx-4 focus:outline-none"
>
<div className="flex items-center justify-between p-4 border-b">
<h3 id="leadership-modal-title" className="font-semibold">
{member ? "Edit Team Member" : "Add Team Member"}
</h3>
<button
type="button"
onClick={handleClose}
aria-label="Close"
className="text-foreground-subtle hover:text-foreground-muted"
>
<X className="h-5 w-5" />
</button>
</div>
<form onSubmit={handleSubmit} className="p-4 space-y-4">
{/* Photo Upload */}
<input
ref={fileInputRef}
type="file"
accept="image/jpeg,image/png,image/webp,image/avif"
onChange={handlePhotoUpload}
className="hidden"
disabled={uploadingPhoto}
/>
<div className="flex items-center gap-4">
{photoUrl ? (
<div className="relative group">
<img
src={getImageUrl(photoUrl)}
alt="Member avatar"
className="h-20 w-20 rounded-full object-cover border-2 border-border-default"
/>
<button
type="button"
onClick={() => fileInputRef.current?.click()}
className="absolute inset-0 flex items-center justify-center bg-overlay-dark rounded-full opacity-0 group-hover:opacity-100 cursor-pointer transition-opacity"
>
<Camera className="h-5 w-5 text-foreground-inverse" />
</button>
</div>
) : (
<button
type="button"
onClick={() => fileInputRef.current?.click()}
className="flex items-center justify-center h-20 w-20 rounded-full border-2 border-dashed border-border-default cursor-pointer hover:border-primary-300 transition-colors bg-background-muted"
>
{uploadingPhoto ? (
<Loader2 className="h-5 w-5 animate-spin text-primary-600" />
) : (
<Upload className="h-5 w-5 text-foreground-muted" />
)}
</button>
)}
<div className="flex-1">
<p className="text-sm font-medium text-foreground-default">Photo</p>
<p className="text-xs text-foreground-subtle">JPG, PNG or WebP (max 10MB)</p>
{photoUrl && (
<button
type="button"
onClick={() => setPhotoUrl("")}
className="text-xs text-error hover:underline mt-1"
>
Remove photo
</button>
)}
</div>
</div>
{uploadError && <p className="text-sm text-error">{uploadError}</p>}
<div className="space-y-2">
<label
htmlFor="leader-name"
className="text-sm font-medium text-foreground-default"
>
Name *
</label>
<Input
id="leader-name"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Full name"
required
/>
</div>
<div className="space-y-2">
<label
htmlFor="leader-role"
className="text-sm font-medium text-foreground-default"
>
Role *
</label>
<Input
id="leader-role"
value={role}
onChange={(e) => setRole(e.target.value)}
placeholder="e.g., Founder, Lead Designer"
required
/>
</div>
<div className="space-y-2">
<label
htmlFor="leader-bio"
className="text-sm font-medium text-foreground-default"
>
Bio
</label>
<textarea
id="leader-bio"
value={bio}
onChange={(e) => setBio(e.target.value)}
placeholder="Brief description..."
className="flex min-h-[80px] w-full rounded-md border border-default bg-background-elevated px-3 py-2 text-sm"
maxLength={300}
/>
</div>
<div className="space-y-2">
<label
htmlFor="leader-linkedin"
className="flex items-center gap-2 text-sm font-medium text-foreground-default"
>
<Linkedin className="h-4 w-4 text-social-linkedin" />
LinkedIn URL
</label>
<Input
id="leader-linkedin"
type="url"
value={linkedinUrl}
onChange={(e) => setLinkedinUrl(e.target.value)}
placeholder="https://linkedin.com/in/..."
/>
</div>
<div className="space-y-2">
<label
htmlFor="leader-instagram"
className="flex items-center gap-2 text-sm font-medium text-foreground-default"
>
<Instagram className="h-4 w-4 text-social-instagram" />
Instagram
</label>
<div className="flex">
<span className="inline-flex items-center px-3 rounded-l-md border border-r-0 border-border-default bg-background-muted text-foreground-subtle text-sm">
@
</span>
<Input
id="leader-instagram"
value={instagramHandle}
onChange={(e) =>
setInstagramHandle(e.target.value.replace(/^@/, ""))
}
placeholder="username"
className="rounded-l-none"
/>
</div>
</div>
<div className="flex justify-end gap-2 pt-4">
<Button type="button" variant="outline" onClick={handleClose}>
Cancel
</Button>
<Button type="submit">{member ? "Update" : "Add"}</Button>
</div>
</form>
</div>
<ConfirmDialog
open={showDiscardConfirm}
title="Discard unsaved changes?"
description="You have unsaved changes that will be lost if you close this form."
confirmLabel="Discard"
variant="destructive"
onConfirm={onClose}
onCancel={() => setShowDiscardConfirm(false)}
/>
</div>
);
}
|