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

useJourneySettings holds the name, cover, archive, the switches saved on the
spot, delete, unlinking a trip and removing a contributor for both the settings
dialog and the settings sheet. useJourneyTripLinking loads the linkable trips
and links one for the add trip dialog and the sheet, and useJourneyShareLink
manages the public share link for the share section and the sheet. The dialog
still closes after a new cover or an unlinked trip, the sheet still stays open.
This commit is contained in:
Maurice
2026-10-09 10:54:55 +02:00
parent e40bc98a33
commit 78ddb1b0b8
8 changed files with 785 additions and 450 deletions
@@ -1,5 +1,4 @@
import { useEffect, useRef, useState } from 'react'
import { useNavigate } from 'react-router'
import { useRef, useState } from 'react'
import {
X, Image, Plus, Trash2, UserPlus, Link as LinkIcon,
List, Grid3x3, MapPin, Route, Archive, ArchiveRestore, Undo2,
@@ -9,29 +8,12 @@ import MIconBtn from '../../components/MIconBtn'
import MToggle from '../../components/MToggle'
import ConfirmDialog from '../../../components/shared/ConfirmDialog'
import { useTranslation } from '../../../i18n'
import { useToast } from '../../../components/shared/Toast'
import { journeyApi } from '../../../api/client'
import { useJourneyStore } from '../../../store/journeyStore'
import type { JourneyDetail } from '../../../store/journeyStore'
import { normalizeImageFile } from '../../../utils/convertHeic'
import { copyText } from '../../../utils/clipboard'
import { useJourneySettings, useJourneyTripLinking } from '../../../components/Journey/useJourneySettings'
import { useJourneyShareLink } from '../../../components/Journey/useJourneyShareLink'
import { pickGradient } from '../../../pages/journeyDetail/JourneyDetailPage.helpers'
import { journeyCoverSrc } from './mobileJourneyMeta'
interface ShareLink {
token: string
share_timeline: boolean
share_gallery: boolean
share_map: boolean
}
interface AvailableTrip {
id: number
title: string
destination?: string
start_date?: string
}
interface MJourneySettingsSheetProps {
journey: JourneyDetail
onClose: () => void
@@ -50,181 +32,27 @@ export default function MJourneySettingsSheet({
journey, onClose, onSaved, onOpenInvite, onRefresh, onRestoreSuggestions,
}: MJourneySettingsSheetProps) {
const { t } = useTranslation()
const toast = useToast()
const navigate = useNavigate()
const { updateJourney, deleteJourney } = useJourneyStore()
const [title, setTitle] = useState(journey.title)
const [subtitle, setSubtitle] = useState(journey.subtitle || '')
const [saving, setSaving] = useState(false)
const [archiving, setArchiving] = useState(false)
const [unlinkTarget, setUnlinkTarget] = useState<{ trip_id: number; title: string } | null>(null)
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false)
const {
title, setTitle, subtitle, setSubtitle, saving, archiving, unlinkTarget, setUnlinkTarget, showDeleteConfirm,
setShowDeleteConfirm, savingTracks, savingField, handleSave, handleCoverUpload, handleArchiveToggle,
handleTracksToggle, handleFieldToggle, handleDelete, handleRemoveContributor, confirmUnlink,
} = useJourneySettings({ journey, onSaved, onRefresh, onContentChanged: onRefresh })
const [addTripOpen, setAddTripOpen] = useState(false)
const [availableTrips, setAvailableTrips] = useState<AvailableTrip[]>([])
const [linkingTripId, setLinkingTripId] = useState<number | null>(null)
const [shareLink, setShareLink] = useState<ShareLink | null>(null)
const [savingTracks, setSavingTracks] = useState(false)
const [copied, setCopied] = useState(false)
const { availableTrips, linkingTripId, loadAvailableTrips, linkTrip } = useJourneyTripLinking({
journeyId: journey.id,
onLinked: () => { setAddTripOpen(false); onRefresh() },
})
const {
link: shareLink, copied, shareUrl, createLink: createShareLink, togglePerm: toggleSharePerm,
deleteLink: deleteShareLink, copyLink: copyShareUrl,
} = useJourneyShareLink(journey.id)
const coverRef = useRef<HTMLInputElement>(null)
useEffect(() => {
journeyApi.getShareLink(journey.id).then(d => setShareLink(d.link || null)).catch(() => {})
}, [journey.id])
const coverSrc = journeyCoverSrc(journey.cover_image)
const shareUrl = shareLink ? `${window.location.origin}/public/journey/${shareLink.token}` : ''
const handleSave = async () => {
setSaving(true)
try {
await updateJourney(journey.id, { title, subtitle: subtitle || null })
onSaved()
} catch {
toast.error(t('journey.settings.saveFailed'))
} finally {
setSaving(false)
}
}
const handleCoverUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0]
if (!file) return
const formData = new FormData()
formData.append('cover', await normalizeImageFile(file))
try {
await journeyApi.uploadCover(journey.id, formData)
toast.success(t('journey.settings.coverUpdated'))
onRefresh()
} catch {
toast.error(t('journey.settings.coverFailed'))
}
}
// Written on the spot rather than on Save, like the archive button: it is a view
// setting and the point of it is watching the map change (#2194). onRefresh, not
// onSaved, because onSaved closes the sheet the instant the switch is flipped and
// would drop a title the owner has typed but not saved yet.
const handleTracksToggle = async (next: boolean) => {
setSavingTracks(true)
try {
await updateJourney(journey.id, { show_trip_tracks: next })
onRefresh()
} catch {
toast.error(t('journey.settings.saveFailed'))
} finally {
setSavingTracks(false)
}
}
/**
* Turn one of the optional entry fields off for this journey (discussion #2299).
*
* Written on the spot and through onRefresh, for the same two reasons the tracks
* switch above is. Nothing stored is erased: the form stops asking, the values
* stay, and switching back on brings them into view.
*/
const [savingField, setSavingField] = useState<string | null>(null)
const handleFieldToggle = async (field: 'show_verdict' | 'show_mood' | 'show_weather', next: boolean) => {
setSavingField(field)
try {
await updateJourney(journey.id, { [field]: next })
onRefresh()
} catch {
toast.error(t('journey.settings.saveFailed'))
} finally {
setSavingField(null)
}
}
const openAddTrip = async () => {
setAddTripOpen(v => !v)
if (availableTrips.length === 0) {
try {
const data = await journeyApi.availableTrips()
setAvailableTrips(data.trips || [])
} catch { /* row stays empty */ }
}
}
const linkTrip = async (tripId: number) => {
setLinkingTripId(tripId)
try {
await journeyApi.addTrip(journey.id, tripId)
toast.success(t('journey.trips.tripLinked'))
setAddTripOpen(false)
onRefresh()
} catch {
toast.error(t('journey.trips.linkFailed'))
} finally {
setLinkingTripId(null)
}
}
const createShareLink = async () => {
try {
const res = await journeyApi.createShareLink(journey.id, { share_timeline: true, share_gallery: true, share_map: true })
setShareLink({ token: res.token, share_timeline: true, share_gallery: true, share_map: true })
toast.success(t('journey.share.linkCreated'))
} catch {
toast.error(t('journey.share.createFailed'))
}
}
const copyShareUrl = async () => {
if (!(await copyText(shareUrl))) return
setCopied(true)
setTimeout(() => setCopied(false), 2000)
}
const toggleSharePerm = async (key: 'share_timeline' | 'share_gallery' | 'share_map') => {
if (!shareLink) return
const previous = shareLink
const updated = { ...previous, [key]: !previous[key] }
setShareLink(updated)
try {
await journeyApi.createShareLink(journey.id, {
share_timeline: updated.share_timeline,
share_gallery: updated.share_gallery,
share_map: updated.share_map,
})
} catch {
setShareLink(previous)
toast.error(t('journey.share.updateFailed'))
}
}
const deleteShareLink = async () => {
try {
await journeyApi.deleteShareLink(journey.id)
setShareLink(null)
toast.success(t('journey.share.linkDeleted'))
} catch {
toast.error(t('journey.share.deleteFailed'))
}
}
const handleArchiveToggle = async () => {
setArchiving(true)
try {
const newStatus = journey.status === 'archived' ? 'active' : 'archived'
await updateJourney(journey.id, { status: newStatus })
toast.success(newStatus === 'archived' ? t('journey.settings.archived') : t('journey.settings.reopened'))
onSaved()
} catch {
toast.error(t('journey.settings.saveFailed'))
} finally {
setArchiving(false)
}
}
const handleDelete = async () => {
try {
await deleteJourney(journey.id)
navigate('/journey')
} catch {
toast.error(t('journey.settings.failedToDelete'))
}
if (availableTrips.length === 0) await loadAvailableTrips()
}
const eyebrow = 'font-geist text-[0.625rem] font-bold uppercase tracking-[.09em] text-m-faint'
@@ -395,16 +223,7 @@ export default function MJourneySettingsSheet({
{c.role !== 'owner' && (
<button
type="button"
onClick={async () => {
if (!window.confirm(t('journey.contributors.removeConfirm', { username: c.username }))) return
try {
await journeyApi.removeContributor(journey.id, c.user_id)
toast.success(t('journey.contributors.removed'))
onRefresh()
} catch {
toast.error(t('journey.contributors.removeFailed'))
}
}}
onClick={() => handleRemoveContributor(c)}
aria-label={t('journey.contributors.remove')}
className="flex h-7 w-7 flex-none items-center justify-center rounded-full text-m-faint"
>
@@ -495,17 +314,7 @@ export default function MJourneySettingsSheet({
<ConfirmDialog
isOpen={!!unlinkTarget}
onClose={() => setUnlinkTarget(null)}
onConfirm={async () => {
if (!unlinkTarget) return
try {
await journeyApi.removeTrip(journey.id, unlinkTarget.trip_id)
toast.success(t('journey.trips.tripUnlinked'))
setUnlinkTarget(null)
onRefresh()
} catch {
toast.error(t('journey.trips.unlinkFailed'))
}
}}
onConfirm={confirmUnlink}
title={t('journey.trips.unlinkTrip')}
message={t('journey.trips.unlinkMessage', { title: unlinkTarget?.title })}
confirmLabel={t('journey.trips.unlink')}