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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | 4x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 34x 231x 39x 18x 39x 39x 6x 3x 3x 39x 39x 39x 7x 7x 2x 4x 2x 2x 2x 2x 5x 39x 273x 39x 1x 2x 2x 6x 2x 2x 1x 1x 1x | import { useState, useMemo, useRef, type FormEvent } from "react";
import { X } from "lucide-react";
import { validateUrlField } from "@interioring/utils/validation/url";
import { Button } from "../../ui/button";
import { Input } from "../../ui/input";
import { ConfirmDialog } from "../../ui/confirm-dialog";
import { useUnsavedChanges, useDialogAccessibility } from "../../../hooks";
import type { Pro } from "../../../lib/api";
interface SocialMediaModalProps {
pro: Pro;
onSave: (data: {
instagramHandle: string | null;
websiteUrl: string | null;
googleBusinessUrl: string | null;
facebookUrl: string | null;
youtubeUrl: string | null;
linkedinUrl: string | null;
twitterUrl: string | null;
pinterestUrl: string | null;
}) => void;
onClose: () => void;
}
// Field ids match the existing input ids so labels and aria-describedby line up.
const URL_FIELD_IDS = {
websiteUrl: "sm-website",
googleBusinessUrl: "sm-google-business",
facebookUrl: "sm-facebook",
youtubeUrl: "sm-youtube",
linkedinUrl: "sm-linkedin",
twitterUrl: "sm-twitter",
pinterestUrl: "sm-pinterest",
} as const;
export function SocialMediaModal({ pro, onSave, onClose }: SocialMediaModalProps) {
const [instagramHandle, setInstagramHandle] = useState(pro.instagramHandle || "");
const [websiteUrl, setWebsiteUrl] = useState(pro.websiteUrl || "");
const [googleBusinessUrl, setGoogleBusinessUrl] = useState(pro.googleBusinessUrl || "");
const [facebookUrl, setFacebookUrl] = useState(pro.facebookUrl || "");
const [youtubeUrl, setYoutubeUrl] = useState(pro.youtubeUrl || "");
const [linkedinUrl, setLinkedinUrl] = useState(pro.linkedinUrl || "");
const [twitterUrl, setTwitterUrl] = useState(pro.twitterUrl || "");
const [pinterestUrl, setPinterestUrl] = useState(pro.pinterestUrl || "");
const [showDiscardConfirm, setShowDiscardConfirm] = useState(false);
// Issue #642: validate each URL field against its platform domain so we
// reject `https://FGJF`, bare `https://`, and cross-platform links before
// the API rejects them with a 400.
const errors = useMemo(
() => ({
websiteUrl: validateUrlField(websiteUrl),
googleBusinessUrl: validateUrlField(googleBusinessUrl, "googleBusiness"),
facebookUrl: validateUrlField(facebookUrl, "facebook"),
youtubeUrl: validateUrlField(youtubeUrl, "youtube"),
linkedinUrl: validateUrlField(linkedinUrl, "linkedin"),
twitterUrl: validateUrlField(twitterUrl, "twitter"),
pinterestUrl: validateUrlField(pinterestUrl, "pinterest"),
}),
[
websiteUrl,
googleBusinessUrl,
facebookUrl,
youtubeUrl,
linkedinUrl,
twitterUrl,
pinterestUrl,
],
);
const hasErrors = Object.values(errors).some((msg) => msg !== null);
const initialValues = useMemo(
() => ({
instagramHandle: pro.instagramHandle || "",
websiteUrl: pro.websiteUrl || "",
googleBusinessUrl: pro.googleBusinessUrl || "",
facebookUrl: pro.facebookUrl || "",
youtubeUrl: pro.youtubeUrl || "",
linkedinUrl: pro.linkedinUrl || "",
twitterUrl: pro.twitterUrl || "",
pinterestUrl: pro.pinterestUrl || "",
}),
[pro],
);
const isDirty = useUnsavedChanges(initialValues, {
instagramHandle,
websiteUrl,
googleBusinessUrl,
facebookUrl,
youtubeUrl,
linkedinUrl,
twitterUrl,
pinterestUrl,
});
const handleClose = () => {
if (isDirty) {
setShowDiscardConfirm(true);
} else {
onClose();
}
};
const { dialogRef, handleFocusTrap } = useDialogAccessibility(handleClose);
const formRef = useRef<HTMLFormElement | null>(null);
const handleSubmit = (e: FormEvent) => {
e.preventDefault();
if (hasErrors) {
const firstBadField = (
Object.entries(errors) as [keyof typeof errors, string | null][]
).find(([, msg]) => msg !== null);
Eif (firstBadField && formRef.current) {
const inputId = URL_FIELD_IDS[firstBadField[0]];
formRef.current.querySelector<HTMLInputElement>(`#${inputId}`)?.focus();
}
return;
}
onSave({
instagramHandle: instagramHandle || null,
websiteUrl: websiteUrl || null,
googleBusinessUrl: googleBusinessUrl || null,
facebookUrl: facebookUrl || null,
youtubeUrl: youtubeUrl || null,
linkedinUrl: linkedinUrl || null,
twitterUrl: twitterUrl || null,
pinterestUrl: pinterestUrl || null,
});
};
const renderError = (id: string, message: string | null) =>
message ? (
<p
id={`${id}-error`}
role="alert"
className="text-sm text-red-600 dark:text-red-400"
>
{message}
</p>
) : null;
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-black/50 cursor-default"
onClick={handleClose}
tabIndex={-1}
/>
<div
ref={dialogRef}
role="dialog"
aria-modal="true"
aria-labelledby="social-media-modal-title"
tabIndex={-1}
className="relative bg-background-elevated rounded-lg shadow-xl w-full max-w-lg mx-4 max-h-[90vh] overflow-y-auto focus:outline-none"
>
<div className="flex items-center justify-between p-4 border-b">
<h3 id="social-media-modal-title" className="font-semibold">
Edit Social Media & Web
</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 ref={formRef} onSubmit={handleSubmit} noValidate className="p-4 space-y-4">
<div className="space-y-2">
<label
htmlFor="sm-instagram"
className="text-sm font-medium text-foreground-default"
>
Instagram Handle
</label>
<div className="flex items-center">
<span className="inline-flex items-center px-3 h-10 border border-r-0 border-border-default rounded-l-md bg-background-muted text-sm text-foreground-subtle">
@
</span>
<Input
id="sm-instagram"
value={instagramHandle}
onChange={(e) => setInstagramHandle(e.target.value)}
placeholder="yourhandle"
className="rounded-l-none"
/>
</div>
</div>
<div className="space-y-2">
<label
htmlFor="sm-website"
className="text-sm font-medium text-foreground-default"
>
Website URL
</label>
<Input
id="sm-website"
value={websiteUrl}
onChange={(e) => setWebsiteUrl(e.target.value)}
placeholder="https://yourwebsite.com"
type="url"
aria-invalid={errors.websiteUrl ? true : undefined}
aria-describedby={errors.websiteUrl ? "sm-website-error" : undefined}
/>
{renderError("sm-website", errors.websiteUrl)}
</div>
<div className="space-y-2">
<label
htmlFor="sm-google-business"
className="text-sm font-medium text-foreground-default"
>
Google Business URL
</label>
<Input
id="sm-google-business"
value={googleBusinessUrl}
onChange={(e) => setGoogleBusinessUrl(e.target.value)}
placeholder="https://g.page/yourbusiness"
type="url"
aria-invalid={errors.googleBusinessUrl ? true : undefined}
aria-describedby={
errors.googleBusinessUrl ? "sm-google-business-error" : undefined
}
/>
{renderError("sm-google-business", errors.googleBusinessUrl)}
</div>
<div className="space-y-2">
<label
htmlFor="sm-facebook"
className="text-sm font-medium text-foreground-default"
>
Facebook URL
</label>
<Input
id="sm-facebook"
value={facebookUrl}
onChange={(e) => setFacebookUrl(e.target.value)}
placeholder="https://facebook.com/yourpage"
type="url"
aria-invalid={errors.facebookUrl ? true : undefined}
aria-describedby={errors.facebookUrl ? "sm-facebook-error" : undefined}
/>
{renderError("sm-facebook", errors.facebookUrl)}
</div>
<div className="space-y-2">
<label
htmlFor="sm-youtube"
className="text-sm font-medium text-foreground-default"
>
YouTube URL
</label>
<Input
id="sm-youtube"
value={youtubeUrl}
onChange={(e) => setYoutubeUrl(e.target.value)}
placeholder="https://youtube.com/@yourchannel"
type="url"
aria-invalid={errors.youtubeUrl ? true : undefined}
aria-describedby={errors.youtubeUrl ? "sm-youtube-error" : undefined}
/>
{renderError("sm-youtube", errors.youtubeUrl)}
</div>
<div className="space-y-2">
<label
htmlFor="sm-linkedin"
className="text-sm font-medium text-foreground-default"
>
LinkedIn URL
</label>
<Input
id="sm-linkedin"
value={linkedinUrl}
onChange={(e) => setLinkedinUrl(e.target.value)}
placeholder="https://linkedin.com/company/yourcompany"
type="url"
aria-invalid={errors.linkedinUrl ? true : undefined}
aria-describedby={errors.linkedinUrl ? "sm-linkedin-error" : undefined}
/>
{renderError("sm-linkedin", errors.linkedinUrl)}
</div>
<div className="space-y-2">
<label
htmlFor="sm-twitter"
className="text-sm font-medium text-foreground-default"
>
Twitter / X URL
</label>
<Input
id="sm-twitter"
value={twitterUrl}
onChange={(e) => setTwitterUrl(e.target.value)}
placeholder="https://twitter.com/yourhandle"
type="url"
aria-invalid={errors.twitterUrl ? true : undefined}
aria-describedby={errors.twitterUrl ? "sm-twitter-error" : undefined}
/>
{renderError("sm-twitter", errors.twitterUrl)}
</div>
<div className="space-y-2">
<label
htmlFor="sm-pinterest"
className="text-sm font-medium text-foreground-default"
>
Pinterest URL
</label>
<Input
id="sm-pinterest"
value={pinterestUrl}
onChange={(e) => setPinterestUrl(e.target.value)}
placeholder="https://pinterest.com/yourprofile"
type="url"
aria-invalid={errors.pinterestUrl ? true : undefined}
aria-describedby={
errors.pinterestUrl ? "sm-pinterest-error" : undefined
}
/>
{renderError("sm-pinterest", errors.pinterestUrl)}
</div>
{hasErrors ? (
<p role="alert" className="text-sm text-red-600 dark:text-red-400">
Please fix the highlighted fields before saving.
</p>
) : null}
<div className="flex justify-end gap-2 pt-4">
<Button type="button" variant="outline" onClick={handleClose}>
Cancel
</Button>
<Button type="submit" disabled={hasErrors}>
Save
</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>
);
}
|