refactor(journey): share the entry editor logic between desktop and phone

useJourneyEntryForm holds the fields, the photo queue that only goes out on
save, the gallery links, the location search and "use my location", the
verdict rows, quick capture and the save for both the editor dialog and the
entry sheet. journeyWeatherCategory moves to components/Journey/journeyWeather
so the dialog no longer reaches into mobile/. Where the two differed they still
do: the dialog starts with a blank verdict row, counts a moved point as a
change, toasts a failed save or location and fills the weather from the
forecast; the sheet edits tags and shows a refused location under the field.
This commit is contained in:
Maurice
2026-10-09 11:10:53 +02:00
parent 78ddb1b0b8
commit 8e95b4c7b6
10 changed files with 1071 additions and 674 deletions
@@ -1,41 +1,24 @@
import { useEffect, useMemo, useRef, useState } from 'react'
import { localIsoDate } from '../../../utils/localDate'
import { Briefcase, Camera, Plus, Image, Images, X, MapPin, Locate, Trash2, CheckCircle2, MinusCircle, ChevronUp, ChevronDown, EyeOff, Play } from 'lucide-react'
import MSheet from '../../components/MSheet'
import MIconBtn from '../../components/MIconBtn'
import MToggle from '../../components/MToggle'
import { useTranslation } from '../../../i18n'
import { useToast } from '../../../components/shared/Toast'
import CustomTimePicker from '../../../components/shared/CustomTimePicker'
import { CustomDatePicker } from '../../../components/shared/CustomDateTimePicker'
import { journeyApi, mapsApi, weatherApi } from '../../../api/client'
import { getApiErrorMessage } from '../../../types'
import { normalizeImageFiles } from '../../../utils/convertHeic'
import { isVideoFile } from '../../../utils/videoPoster'
import { getCurrentPositionOnce } from '../../../hooks/useGeolocation'
import type { ResilientResult, UploadProgress } from '../../../utils/uploadQueue'
import type { JourneyEntry, JourneyPhoto, GalleryPhoto, JourneyTrip } from '../../../store/journeyStore'
import { useAddonStore } from '../../../store/addonStore'
import { photoUrl, posterlessVideo, geoOnceErrorKey, isValidGeoPoint } from '../../../pages/journeyDetail/JourneyDetailPage.helpers'
import { photoUrl, posterlessVideo } from '../../../pages/journeyDetail/JourneyDetailPage.helpers'
import JournalBody from '../../../components/Journey/JournalBody'
import { ProviderPicker, type ProviderPhotoGroup } from '../../../components/Journey/JourneyDetailPageProviderPicker'
import { journeyWeatherCategory, MOBILE_MOODS, MOBILE_WEATHERS } from './mobileJourneyMeta'
import { useJourneyTripSuggestion } from '../../../components/Journey/useJourneyTripSuggestion'
import { useEntryPhotoOrder } from '../../../components/Journey/useEntryPhotoOrder'
import { usePlaceLanguage } from '../../../hooks/usePlaceLanguage'
import { ProviderPicker } from '../../../components/Journey/JourneyDetailPageProviderPicker'
import { MOBILE_MOODS, MOBILE_WEATHERS } from './mobileJourneyMeta'
import { useJourneyEntryForm, type PendingProviderGroup } from '../../../components/Journey/useJourneyEntryForm'
const PRO_COLOR = '#2FA37A'
const CON_COLOR = '#D6273B'
interface LocationResult {
name: string
address?: string
lat: number
lng: number
}
type PendingProviderGroup = ProviderPhotoGroup & { provider: string }
// A clip with no poster to show: the tinted tile and play mark the gallery grid
// gives it, since an <img> asked for its thumbnail draws the broken glyph (#2341).
function ClipTile() {
@@ -89,54 +72,26 @@ export default function MJourneyEntrySheet({
showVerdict = true, showMood = true, showWeather = true, onMoveEarlier, onMoveLater, onDismiss,
onClose, onSave, onUploadPhotos, onAddProviderPhotos, onDelete, onDone,
}: MJourneyEntrySheetProps) {
const { t, language } = useTranslation()
const placeLang = usePlaceLanguage()
const toast = useToast()
// Which verdict row to hand the caret after the next render — see addVerdictRow.
const verdictFocusRef = useRef<string | null>(null)
const [title, setTitle] = useState(entry.title || '')
const [story, setStory] = useState(entry.story || '')
const [entryDate, setEntryDate] = useState(entry.entry_date || localIsoDate())
const [entryTime, setEntryTime] = useState(entry.entry_time?.slice(0, 5) || '')
const [locationName, setLocationName] = useState(entry.location_name || '')
const [locationLat, setLocationLat] = useState<number | null>(entry.location_lat ?? null)
const [locationLng, setLocationLng] = useState<number | null>(entry.location_lng ?? null)
const [locationQuery, setLocationQuery] = useState('')
const [locationResults, setLocationResults] = useState<LocationResult[]>([])
const [showLocationResults, setShowLocationResults] = useState(false)
const locationTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
const [mood, setMood] = useState(entry.mood || '')
const [weather, setWeather] = useState(entry.weather || '')
const [statsExcluded, setStatsExcluded] = useState(entry.stats_excluded ?? false)
// The trip this day belongs to, when the journey does not follow it yet (#2265).
const tripSuggestion = useJourneyTripSuggestion(entry.journey_id, trips.map(tr => tr.trip_id), entryDate, !readOnly)
const [isDraft, setIsDraft] = useState(entry.is_draft ?? false)
const [pros, setPros] = useState<string[]>(entry.pros_cons?.pros ?? [])
const [cons, setCons] = useState<string[]>(entry.pros_cons?.cons ?? [])
const [tags, setTags] = useState<string[]>(entry.tags ?? [])
const [tagInput, setTagInput] = useState('')
const [photos, setPhotos] = useState<(JourneyPhoto | GalleryPhoto)[]>(entry.photos || [])
// Sends a photo to the front in one request, the same way the desktop editor does (#824).
const photoOrder = useEntryPhotoOrder(entry.id, photos, setPhotos)
const [pendingFiles, setPendingFiles] = useState<File[]>([])
const [pendingLinkIds, setPendingLinkIds] = useState<number[]>([])
const [pendingProviderGroups, setPendingProviderGroups] = useState<PendingProviderGroup[]>([])
const [showGalleryPick, setShowGalleryPick] = useState(false)
const { t } = useTranslation()
const {
title, setTitle, story, setStory, entryDate, setEntryDate, entryTime, setEntryTime,
locationName, locationLat, locationQuery, locationResults, showLocationResults, setShowLocationResults,
locating, locationError, mood, setMood, weather, setWeather, statsExcluded, setStatsExcluded, tripSuggestion,
isDraft, setIsDraft, pros, setPros, cons, setCons, tags, setTags, tagInput, setTagInput, saving, uploadProgress,
photos, photoOrder, pendingFiles, setPendingFiles, pendingPreviews, setPendingProviderGroups,
showGalleryPick, setShowGalleryPick, isDirty, availableGalleryPhotos, queuedProviderPhotos, providerAssetIds,
contextLocation, offersStatsToggle, addVerdictRow, verdictRowRef, addTag, handleSave, handleFileChange,
pickGalleryPhoto, removePhoto, searchLocation, pickLocation, handleUseCurrentLocation,
} = useJourneyEntryForm({
entry, journeyId: entry.journey_id, trips, galleryPhotos, onSave, onUploadPhotos, onAddProviderPhotos, onDone,
readOnly, quickCapture, withTags: true, inlineLocateError: true,
})
const [showExternal, setShowExternal] = useState(false)
const [externalProvider, setExternalProvider] = useState<string | null>(null)
const [providers, setProviders] = useState<{ id: string; name: string }[]>([])
const [saving, setSaving] = useState(false)
const [captureOnly, setCaptureOnly] = useState(quickCapture)
const [locating, setLocating] = useState(false)
const [locationError, setLocationError] = useState('')
const [uploadProgress, setUploadProgress] = useState<{ done: number; total: number } | null>(null)
const fileRef = useRef<HTMLInputElement>(null)
const cameraRef = useRef<HTMLInputElement>(null)
// A save that creates the entry and then fails on the provider photos keeps
// the sheet open; without the id of what was just created, the retry would
// create a second entry (#1808).
const persistedEntryIdRef = useRef<number | null>(entry.id > 0 ? entry.id : null)
// The addon list is already in the store — this only probes which of the
// photo providers is actually connected for this user.
@@ -147,11 +102,6 @@ export default function MJourneyEntrySheet({
[addons],
)
// Minting the preview inside the tile markup would hand out a fresh blob URL on
// every keystroke in the story field, and nothing would ever release them.
const pendingPreviews = useMemo(() => pendingFiles.map(f => URL.createObjectURL(f)), [pendingFiles])
useEffect(() => () => { pendingPreviews.forEach(url => URL.revokeObjectURL(url)) }, [pendingPreviews])
useEffect(() => {
if (readOnly) return
// App.tsx loads the addon list on boot, so this only reacts to it. Kicking off
@@ -174,236 +124,13 @@ export default function MJourneyEntrySheet({
}, [readOnly, addonsLoaded, photoProviders])
const activeProvider = externalProvider || providers[0]?.id || null
const queuedProviderPhotos = pendingProviderGroups.reduce((sum, group) => sum + group.assetIds.length, 0)
const providerExistingAssetIds = new Set<string>()
if (activeProvider) {
photos.forEach(photo => {
if (photo.provider === activeProvider && photo.asset_id) providerExistingAssetIds.add(photo.asset_id)
})
pendingProviderGroups.forEach(group => {
if (group.provider === activeProvider) group.assetIds.forEach(assetId => providerExistingAssetIds.add(assetId))
})
}
const contextLocation = isValidGeoPoint({ lat: locationLat ?? Number.NaN, lng: locationLng ?? Number.NaN })
? { lat: locationLat!, lng: locationLng!, name: locationName || undefined }
: null
// The route switch belongs to an entry that is a stop, or was one: an entry
// without a point was never on the route, and a new one is not on it yet.
const offersStatsToggle = entry.id > 0 && (contextLocation != null || !!entry.stats_excluded)
useEffect(() => {
if (!quickCapture || readOnly || entry.location_lat != null || entry.location_lng != null) return
let active = true
setLocating(true)
getCurrentPositionOnce({ enableHighAccuracy: true, maximumAge: 60_000, timeout: 10_000 })
.then(async pos => {
if (!active) return
setLocationLat(pos.lat)
setLocationLng(pos.lng)
const [placeResult, weatherResult] = await Promise.allSettled([
mapsApi.reverse(pos.lat, pos.lng, placeLang),
weatherApi.getCurrent(pos.lat, pos.lng, language),
])
if (!active) return
if (placeResult.status === 'fulfilled') {
setLocationName(placeResult.value.name || placeResult.value.address || '')
}
if (weatherResult.status === 'fulfilled' && !weatherResult.value.error) {
setWeather(current => current || journeyWeatherCategory(weatherResult.value.main, weatherResult.value.description))
}
setLocating(false)
}, err => {
if (!active) return
setLocationError(t(geoOnceErrorKey(err)))
setLocating(false)
})
return () => { active = false }
}, [quickCapture, readOnly, entry.location_lat, entry.location_lng, entry.entry_date, t, language, placeLang])
const isDirty =
title !== (entry.title || '') ||
story !== (entry.story || '') ||
entryDate !== (entry.entry_date || localIsoDate()) ||
entryTime !== (entry.entry_time?.slice(0, 5) || '') ||
locationName !== (entry.location_name || '') ||
mood !== (entry.mood || '') ||
weather !== (entry.weather || '') ||
statsExcluded !== (entry.stats_excluded ?? false) ||
isDraft !== (entry.is_draft ?? false) ||
pros.filter(p => p.trim()).join('\n') !== (entry.pros_cons?.pros ?? []).join('\n') ||
cons.filter(c => c.trim()).join('\n') !== (entry.pros_cons?.cons ?? []).join('\n') ||
tags.join('\n') !== (entry.tags ?? []).join('\n') ||
pendingFiles.length > 0 ||
pendingLinkIds.length > 0 ||
pendingProviderGroups.length > 0
/**
* Enter opens the next pro or con, directly below the one you are in.
*
* Same behaviour and same reason as the desktop editor: a list of short things
* should not need a button press between every item (discussion #2299). On a
* phone keyboard the return key is right there, which is more of a gain than
* it is with a mouse.
*/
const addVerdictRow = (list: 'pros' | 'cons', index: number) => {
const [values, setValues] = list === 'pros' ? [pros, setPros] as const : [cons, setCons] as const
const next = [...values]
next.splice(index + 1, 0, '')
setValues(next)
verdictFocusRef.current = `${list}-${index + 1}`
}
const verdictRowRef = (key: string) => (el: HTMLInputElement | null) => {
if (el && verdictFocusRef.current === key) {
verdictFocusRef.current = null
el.focus()
}
}
const providerExistingAssetIds = providerAssetIds(activeProvider)
const handleClose = () => {
if (!captureOnly && !readOnly && isDirty && !window.confirm(t('journey.editor.discardChangesConfirm'))) return
onClose()
}
const handleSave = async () => {
setSaving(true)
try {
const entryId = await onSave({
title: title || null,
story: story || null,
entry_date: entryDate,
entry_time: entryTime || null,
location_name: locationName || null,
location_lat: locationLat,
location_lng: locationLng,
stats_excluded: offersStatsToggle ? statsExcluded : undefined,
is_draft: isDraft,
mood: mood || null,
weather: weather || null,
tags: tags.filter(tag => tag.trim()),
pros_cons: { pros: pros.filter(p => p.trim()), cons: cons.filter(c => c.trim()) },
// An explicit Save is the user saying this suggestion is now their entry —
// it does not need a story to earn that (#2008).
type: entry.type === 'skeleton' ? 'entry' : undefined,
}, persistedEntryIdRef.current ?? undefined)
if (entryId > 0) persistedEntryIdRef.current = entryId
if (pendingFiles.length > 0 && entryId) {
const toUpload = pendingFiles
setUploadProgress({ done: 0, total: toUpload.length })
try {
const { failed } = await onUploadPhotos(entryId, toUpload, {
onProgress: p => setUploadProgress({ done: p.done, total: p.total }),
})
setPendingFiles(failed)
if (failed.length > 0) {
toast.error(t('journey.editor.uploadPartialFailed', { failed: String(failed.length), total: String(toUpload.length) }))
}
} catch (err) {
toast.error(getApiErrorMessage(err, t('journey.editor.uploadFailed')))
} finally {
setUploadProgress(null)
}
}
if (pendingLinkIds.length > 0 && entryId) {
for (const photoId of pendingLinkIds) {
try { await journeyApi.linkPhoto(entryId, photoId) } catch { /* linked photo stays in gallery */ }
}
}
if (pendingProviderGroups.length > 0 && entryId && onAddProviderPhotos) {
const failed: PendingProviderGroup[] = []
for (const group of pendingProviderGroups) {
try { await onAddProviderPhotos(entryId, group) } catch { failed.push(group) }
}
if (failed.length > 0) {
// Keep the sheet open with the failed groups queued so the next save
// retries them instead of losing the selection.
setPendingProviderGroups(failed)
toast.error(t('journey.editor.externalPhotosPartialFailed', { failed: String(failed.length), total: String(pendingProviderGroups.length) }))
return
}
setPendingProviderGroups([])
}
onDone()
} finally {
setSaving(false)
}
}
const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
const files = e.target.files
if (!files?.length) return
const normalized = await normalizeImageFiles(files)
setPendingFiles(prev => [...prev, ...normalized])
}
const searchLocation = (query: string) => {
setLocationQuery(query)
setShowLocationResults(true)
if (locationTimerRef.current) clearTimeout(locationTimerRef.current)
if (query.trim().length < 2) {
setLocationResults([])
return
}
locationTimerRef.current = setTimeout(async () => {
try {
const res = await mapsApi.search(query, placeLang)
setLocationResults((res.places || []).slice(0, 6).map((p: { name: string; address?: string; lat: number | string; lng: number | string }) => ({
name: p.name, address: p.address, lat: Number(p.lat), lng: Number(p.lng),
})))
} catch {
setLocationResults([])
}
}, 400)
}
const handleUseCurrentLocation = async () => {
if (locating) return
setLocating(true)
setLocationError('')
try {
const pos = await getCurrentPositionOnce()
// Fill coordinates right away; the name is refined below once the
// reverse geocode comes back.
const fallbackName = `${pos.lat.toFixed(5)}, ${pos.lng.toFixed(5)}`
if (locationTimerRef.current) clearTimeout(locationTimerRef.current)
setLocationLat(pos.lat)
setLocationLng(pos.lng)
setLocationName(fallbackName)
setLocationQuery('')
setLocationResults([])
setShowLocationResults(false)
try {
const data = await mapsApi.reverse(pos.lat, pos.lng, placeLang)
const name = data.name || data.address
// Only replace the coordinate fallback — don't clobber a search
// result the user may have picked while the reverse call was in flight.
if (name) setLocationName(prev => (prev === fallbackName ? name : prev))
} catch { /* best effort — keep the coordinate fallback */ }
} catch (err) {
setLocationError(t(geoOnceErrorKey(err)))
} finally {
setLocating(false)
}
}
const addTag = () => {
// Trailing commas dropped by a scan, not /,+$/: an unanchored ,+ before $ has to
// retry from every comma in the run, so a pasted string of them freezes the tab.
const trimmed = tagInput.trim()
let end = trimmed.length
while (end > 0 && trimmed[end - 1] === ',') end--
const value = trimmed.slice(0, end)
if (!value) return
if (!tags.includes(value)) setTags(prev => [...prev, value])
setTagInput('')
}
const availableGalleryPhotos = galleryPhotos.filter(gp => !photos.some(p => p.id === gp.id))
const eyebrow = 'font-geist text-[0.625rem] font-bold uppercase tracking-[.09em] text-m-faint'
const fieldShell = 'rounded-xl border border-[color:var(--m-rowbr)] bg-[color:var(--m-ic)]'
@@ -603,17 +330,7 @@ export default function MJourneyEntrySheet({
type="button"
className="relative w-full overflow-hidden rounded-lg"
style={{ paddingTop: '100%' }}
onClick={async () => {
if (entry.id > 0) {
try {
const linked = await journeyApi.linkPhoto(entry.id, gp.id)
if (linked) setPhotos(prev => [...prev, linked])
} catch { /* keep picker open on failure */ }
} else {
setPendingLinkIds(prev => [...prev, gp.id])
setPhotos(prev => [...prev, gp])
}
}}
onClick={() => pickGalleryPhoto(gp)}
>
{posterlessVideo(gp) ? (
<ClipTile />
@@ -654,14 +371,7 @@ export default function MJourneyEntrySheet({
{!readOnly && (
<button
type="button"
onClick={async () => {
setPhotos(prev => prev.filter(x => x.id !== p.id))
if (entry.id > 0) {
try { await journeyApi.unlinkPhoto(entry.id, p.id) } catch { /* refreshed on next load */ }
} else {
setPendingLinkIds(prev => prev.filter(id => id !== p.id))
}
}}
onClick={() => removePhoto(p)}
aria-label={t('common.delete')}
className="absolute end-[3px] top-[3px] flex h-5 w-5 items-center justify-center rounded-full bg-black/60 text-white"
>
@@ -868,14 +578,7 @@ export default function MJourneyEntrySheet({
<button
key={i}
type="button"
onClick={() => {
setLocationName(r.name)
setLocationLat(r.lat)
setLocationLng(r.lng)
setLocationQuery('')
setShowLocationResults(false)
setLocationResults([])
}}
onClick={() => pickLocation(r)}
className="flex w-full items-start gap-2 border-b border-[color:var(--m-rowbr)] px-3 py-[10px] text-start last:border-0"
>
<MapPin size={13} className="mt-[2px] flex-none text-m-faint" />
@@ -45,17 +45,6 @@ export function weatherMeta(id: string | null | undefined): WeatherMeta | undefi
return id ? MOBILE_WEATHERS.find(w => w.id === id) : undefined
}
export function journeyWeatherCategory(main: string, description: string): string {
const normalizedMain = main.toLowerCase()
const normalizedDescription = description.toLowerCase()
if (normalizedMain === 'clear') return 'sunny'
if (normalizedMain === 'thunderstorm') return 'stormy'
if (normalizedMain === 'snow') return 'cold'
if (normalizedMain === 'rain' || normalizedMain === 'drizzle') return 'rainy'
if (normalizedDescription.includes('partly') || normalizedDescription.includes('teilweise')) return 'partly'
return 'cloudy'
}
/** Journey cover_image is stored relative — prefix /uploads/ unless it already is. */
export function journeyCoverSrc(coverImage: string | null | undefined): string | null {
if (!coverImage) return null