'use client'; import { useState } from 'react'; import { Loader2, Pencil } from 'lucide-react'; import { PhoneInput, type PhoneInputValue } from '@/components/shared/phone-input'; import { toastError } from '@/lib/api/toast-error'; import { parsePhone } from '@/lib/i18n/phone'; import type { CountryCode } from '@/lib/i18n/countries'; import { cn } from '@/lib/utils'; interface InlinePhoneFieldProps { /** E.164 form ('+442079460958'), null when unset. */ e164: string | null | undefined; /** ISO-3166-1 alpha-2 the number was parsed against. */ country: string | null | undefined; /** Falls back to this country if `country` isn't set. */ defaultCountry?: CountryCode; onSave: (next: { e164: string | null; country: CountryCode }) => Promise; /** * Notifies the parent when the field enters/exits edit mode. Lets the row * dim or hide noise (tag chips, action buttons) while the user is focused * on the editor. */ onEditingChange?: (editing: boolean) => void; emptyText?: string; disabled?: boolean; className?: string; 'data-testid'?: string; } export function InlinePhoneField({ e164, country, defaultCountry, onSave, onEditingChange, emptyText = '-', disabled, className, 'data-testid': testId, }: InlinePhoneFieldProps) { const [editing, setEditingRaw] = useState(false); const [draft, setDraft] = useState(() => { if (!e164 && !country) return null; return { e164: e164 ?? null, country: (country as CountryCode | null) ?? defaultCountry ?? 'US', }; }); const [saving, setSaving] = useState(false); function setEditing(next: boolean) { setEditingRaw(next); onEditingChange?.(next); } async function commit() { const next = draft ?? { e164: null, country: defaultCountry ?? 'US' }; if (next.e164 === (e164 ?? null) && next.country === (country ?? null)) { setEditing(false); return; } setSaving(true); try { await onSave(next); setEditing(false); } catch (err) { toastError(err); } finally { setSaving(false); } } if (editing) { return ( // Two clean lines: country picker + number on top, action pair below.
setDraft(v)} defaultCountry={defaultCountry} data-testid={testId} />
); } // Display: prefer the parsed national format (more readable than raw E.164). let display: string | null = null; if (e164) { const parsed = parsePhone(e164, (country as CountryCode | undefined) ?? defaultCountry); display = parsed.national ?? e164; } return ( ); }