import { useEffect, useId, useRef, useState } from 'react' import { Copy, Mail, Phone, RotateCcw, X } from 'lucide-react' import type { PlaceClientOrigin, PlaceOpeningHours } from '@trek/shared' import CustomTimePicker from '../shared/CustomTimePicker' import { Tooltip } from '../shared/Tooltip' import { DialogSection, fs } from '../shared/DialogShell' import { AddRowButton, EditorField, GRID_2, INPUT } from '../shared/dialogParts' import { cleanWeek, readWeek, weekdayNames, writeWeek } from './placeHours' import { useTranslation } from '../../i18n' import { NO_AUTOFILL } from '../../utils/noAutofill' interface Props { /** Anchor for the "add by hand" jump from an empty search. */ id?: string phone: string email: string /** The stored hours text (JSON), empty when the place has none of its own. */ openingHours: string /** `origin` is set when the hours were taken over from the details lookup rather than typed. */ onChange: (field: 'phone' | 'email' | 'opening_hours', value: string, origin?: PlaceClientOrigin) => void /** * The hours the place details column looked up, as a week. A place without * hours of its own takes them over on its own; one that has some gets a * button to take them over instead. */ suggestedHours?: PlaceOpeningHours | null } /** * Phone, e-mail and opening hours, by hand (#2472). The search fills phone and * website when it knows them; everything it does not know, or a place nobody * has listed at all, the traveller can add here. The hours stay folded away * behind one row until somebody wants them, so a place without any does not * carry a seven-row grid. */ export function PlaceContactFields({ id: anchorId, phone, email, openingHours, onChange, suggestedHours }: Props) { const { t, locale } = useTranslation() const id = useId() // The editor's own draft, so a half-typed time ("09:3") stays on screen while // only finished times reach the place. const [week, setDraft] = useState(() => readWeek(openingHours)) const written = useRef(openingHours) const [editing, setEditing] = useState(() => openingHours !== '') const names = weekdayNames(locale, 'short') const longNames = weekdayNames(locale, 'long') // A value from outside (a picked search result, a reopened place) replaces the draft. useEffect(() => { if (openingHours === written.current) return written.current = openingHours setDraft(readWeek(openingHours)) if (openingHours) setEditing(true) }, [openingHours]) const setWeek = (next: PlaceOpeningHours, origin?: PlaceClientOrigin) => { setDraft(next) const text = writeWeek(cleanWeek(next)) written.current = text onChange('opening_hours', text, origin) } const setDay = (i: number, patch: Partial) => setWeek(week.map((day, d) => (d === i ? { ...day, ...patch } : day))) const copyFirstToAll = () => setWeek(week.map(() => ({ ...week[0] }))) const clearHours = () => { setWeek(readWeek('')); setEditing(false) } // Looked-up hours are taken over by a place that has none yet, once per lookup. const adopted = useRef(null) useEffect(() => { if (!suggestedHours || adopted.current === suggestedHours) return adopted.current = suggestedHours if (written.current) return setWeek(suggestedHours, 'enrichment') setEditing(true) // setWeek only reads refs and setters; the lookup is the trigger. }, [suggestedHours]) const canTakeOver = !!suggestedHours && writeWeek(suggestedHours) !== writeWeek(cleanWeek(week)) return (
onChange('phone', e.target.value)} maxLength={50} placeholder="+49 …" className={`${INPUT} ps-8`} {...NO_AUTOFILL} />
onChange('email', e.target.value)} maxLength={254} placeholder="info@…" className={`${INPUT} ps-8`} {...NO_AUTOFILL} />
{canTakeOver && ( )}
) : undefined} > {editing ? (
{week.map((day, i) => (
{names[i]} {day.closed ? ( {t('places.hoursClosed')} ) : (
setDay(i, { open: v || undefined })} aria-label={`${longNames[i]} ${t('places.hoursOpens')}`} />
–
setDay(i, { close: v || undefined })} aria-label={`${longNames[i]} ${t('places.hoursCloses')}`} />
)}
))}
) : ( setEditing(true)}>{t('places.hoursAdd')} )} ) }