Files
TREK/client/src/components/Journey/JourneyShareSection.tsx
T
Maurice 0c08a6dcb9 fix(ci): repair Journey route registration and shared client behavior
Register the public view route, align generated entities and test fixtures, and share the scoped reads and access switches. Keep gallery and notification writes in order, and submit the email subscription from the dialog footer.
2026-11-30 17:32:50 +01:00

266 lines
12 KiB
TypeScript

import { Link, List, Grid, MapPin, Check, CalendarRange, Route, MessageCircle, BellPlus, Lock, CalendarClock, Eye, X } from 'lucide-react'
import { useTranslation } from '../../i18n'
import { fs } from '../shared/DialogShell'
import { Tooltip } from '../shared/Tooltip'
import {
SHARE_MAP_EXTRAS, useJourneyShareLink, type JourneyShareLink, type JourneyShareMapExtra, type JourneyShareInteraction,
} from './useJourneyShareLink'
import { useJourneyShareAccess } from './useJourneyShareAccess'
import JourneySharePasswordDialog from './JourneySharePasswordDialog'
type ShareAccess = ReturnType<typeof useJourneyShareAccess>
const READER_SWITCHES: { key: JourneyShareInteraction; icon: typeof MessageCircle; labelKey: string; hintKey: string }[] = [
{ key: 'allow_comments', icon: MessageCircle, labelKey: 'journey.share.allowComments', hintKey: 'journey.share.allowCommentsHint' },
{ key: 'allow_subscriptions', icon: BellPlus, labelKey: 'journey.share.allowFollowers', hintKey: 'journey.share.allowFollowersHint' },
]
/** The addresses following the link, folded into two counts until the owner opens them. */
function ShareFollowers({ access }: { access: ShareAccess }) {
const { t } = useTranslation()
const { followers } = access
if (followers.status !== 'ready' || (followers.count === 0 && followers.pending === 0)) return null
return (
<div className="ms-5 border-s border-edge ps-3">
<button
type="button"
aria-expanded={access.followersOpen}
onClick={() => access.setFollowersOpen(!access.followersOpen)}
className="flex flex-wrap items-center gap-1.5 py-1 text-caption text-content-muted hover:text-content"
>
<span className="rounded-full bg-surface-secondary px-2 py-0.5 font-semibold">{t('journey.share.followers', { count: followers.count })}</span>
{followers.pending > 0 && (
<span className="rounded-full bg-surface-secondary px-2 py-0.5 text-content-faint">{t('journey.share.followersPending', { count: followers.pending })}</span>
)}
</button>
{access.followersOpen && (
<ul className="mt-1 flex flex-col">
{followers.subscribers.map(sub => (
<li key={sub.id} className="group flex items-center gap-2 py-1">
<span className={`min-w-0 flex-1 truncate text-caption ${sub.confirmed ? 'text-content' : 'text-content-faint'}`}>{sub.email}</span>
<Tooltip label={t('journey.share.removeFollower')} placement="top">
<button
type="button"
aria-label={t('journey.share.removeFollower')}
onClick={() => void access.removeFollower(sub.id)}
className="flex h-6 w-6 flex-none items-center justify-center rounded-full text-content-faint opacity-40 transition-opacity hover:bg-danger-soft hover:text-danger group-hover:opacity-100"
>
<X size={12} />
</button>
</Tooltip>
</li>
))}
</ul>
)}
</div>
)
}
function ShareDetailToggle({ icon: Icon, label, hint, on, disabledReason, toggle }: {
icon: typeof MessageCircle
label: string
hint: string
on: boolean
disabledReason: string
toggle: () => void
}) {
return (
<Tooltip label={disabledReason} placement="top">
<button
type="button"
aria-pressed={on}
disabled={!!disabledReason}
onClick={toggle}
className={`flex w-full items-start gap-2.5 rounded-xl border px-3 py-2 text-start transition-colors disabled:cursor-not-allowed disabled:opacity-50 ${
on ? 'border-accent bg-accent text-accent-text' : 'border-edge text-content-muted hover:border-content-faint'
}`}
>
<Icon size={13} className="mt-0.5 flex-none" />
<span className="flex min-w-0 flex-1 flex-col gap-0.5">
<span className="font-medium" style={fs(12, 'body')}>{label}</span>
<span className={on ? 'text-accent-text opacity-80' : 'text-content-faint'} style={fs(11)}>{hint}</span>
</span>
{on && <Check size={12} className="mt-0.5 flex-none" />}
</button>
</Tooltip>
)
}
/** What readers of the link may do besides reading (#1702, #1669): each off until switched on. */
function ShareReaders({ link, access, onToggle }: { link: JourneyShareLink; access: ShareAccess; onToggle: (key: JourneyShareInteraction) => void }) {
const { t } = useTranslation()
return (
<div className="flex flex-col gap-1.5">
{READER_SWITCHES.map(({ key, icon: Icon, labelKey, hintKey }) => {
// Comments sit under the entries, so they need the timeline shared.
const blocked = key === 'allow_comments' && !link.share_timeline
const on = !blocked && !!link[key]
return (
<ShareDetailToggle
key={key}
icon={Icon}
label={t(labelKey)}
hint={t(hintKey)}
on={on}
disabledReason={blocked ? t('journey.share.needsTimeline') : ''}
toggle={() => onToggle(key)}
/>
)
})}
{link.allow_subscriptions && <ShareFollowers access={access} />}
</div>
)
}
/** Who may open the link and until when (#2553): a password and an end date, both optional. */
function ShareAccessCard({ access }: { access: ShareAccess }) {
const { t } = useTranslation()
return (
<div className="flex flex-col rounded-xl border border-edge">
<div className="flex items-center gap-2.5 px-3 py-2">
<Lock size={13} className="flex-none text-content-muted" />
<span className="flex-1 font-medium text-content" style={fs(12, 'body')}>{t('common.password')}</span>
{access.hasPassword && (
<button type="button" onClick={() => void access.removePassword()} className="rounded-full px-2.5 py-1 text-caption font-semibold text-danger hover:bg-danger-soft">
{t('common.remove')}
</button>
)}
<button type="button" onClick={() => access.setPasswordOpen(true)} className="rounded-full bg-surface-secondary px-2.5 py-1 text-caption font-semibold text-content hover:bg-surface-tertiary">
{access.hasPassword ? t('common.change') : t('journey.share.passwordSet')}
</button>
</div>
<div className="flex items-center gap-2.5 border-t border-edge px-3 py-2">
<CalendarClock size={13} className="flex-none text-content-muted" />
<span className="flex-1 font-medium text-content" style={fs(12, 'body')}>{t('journey.share.expires')}</span>
{!access.expiry && <span className="text-caption text-content-faint">{t('journey.share.expiresNever')}</span>}
<input
type="date"
value={access.expiry}
min={access.minExpiry}
onChange={e => access.setExpiry(e.target.value)}
aria-label={t('journey.share.expires')}
className="rounded-lg border border-edge bg-surface px-2 py-1 text-caption text-content outline-none focus:border-content-faint"
/>
{access.expiry && (
<Tooltip label={t('journey.share.expiresNever')} placement="top">
<button type="button" aria-label={t('journey.share.expiresNever')} onClick={() => access.setExpiry('')} className="flex h-6 w-6 items-center justify-center rounded-full text-content-faint hover:bg-surface-secondary hover:text-content">
<X size={12} />
</button>
</Tooltip>
)}
</div>
{access.passwordOpen && <JourneySharePasswordDialog onSave={access.savePassword} onClose={() => access.setPasswordOpen(false)} />}
</div>
)
}
const EXTRA_ICONS = { share_plan: CalendarRange, share_recordings: Route } as const
/**
* What the shared map shows besides the entries (JRN-04): the trips' plan and
* the recorded days, each off until the owner switches it on, nested under the
* map and dimmed with the reason while the map is not shared.
*/
function ShareMapExtras({ link, onToggle }: { link: JourneyShareLink; onToggle: (key: JourneyShareMapExtra) => void }) {
const { t } = useTranslation()
const mapShared = !!link.share_map
return (
<div className="ms-5 flex flex-col gap-1.5 border-s border-edge ps-3">
{SHARE_MAP_EXTRAS.map(({ key, labelKey, hintKey }) => {
const Icon = EXTRA_ICONS[key]
const on = mapShared && !!link[key]
return (
<ShareDetailToggle
key={key}
icon={Icon}
label={t(labelKey)}
hint={t(hintKey)}
on={on}
disabledReason={mapShared ? '' : t('journey.share.needsMap')}
toggle={() => onToggle(key)}
/>
)
})}
</div>
)
}
export default function JourneyShareSection({ journeyId }: { journeyId: number }) {
const { t } = useTranslation()
const { link, loading, manageable, copied, shareUrl, createLink, togglePerm, deleteLink, copyLink, setProtection } = useJourneyShareLink(journeyId)
const access = useJourneyShareAccess(journeyId, link, setProtection)
if (loading || !manageable) return null
return (
<div>
<label className="text-caption font-semibold tracking-[0.12em] uppercase text-content-faint block mb-2">{t('journey.share.publicShare')}</label>
{!link ? (
<button type="button"
onClick={createLink}
className="w-full flex items-center justify-center gap-1.5 py-2.5 rounded-xl border border-dashed border-edge text-body font-medium text-content-muted hover:border-content-faint hover:text-content transition-colors"
>
<Link size={14} /> {t('journey.share.createLink')}
</button>
) : (
<div className="flex flex-col gap-3">
{/* URL + Copy */}
<div className="flex items-center gap-2 p-2 rounded-2xl bg-surface-secondary border border-edge-faint">
<Link size={13} className="text-content-faint flex-shrink-0 ms-1.5" />
<span className="flex-1 text-caption text-content-muted truncate">{shareUrl}</span>
<button type="button"
onClick={copyLink}
className="flex-shrink-0 px-3 py-1.5 rounded-full bg-accent text-accent-text text-caption font-semibold hover:bg-accent-hover transition-colors"
>
{copied ? t('journey.share.copied') : t('journey.share.copy')}
</button>
<button type="button"
onClick={deleteLink}
className="flex-shrink-0 px-3 py-1.5 rounded-full text-caption font-semibold text-danger hover:bg-danger-soft transition-colors"
>
{t('share.deleteLink')}
</button>
</div>
{access.views > 0 && (
<div className="-mt-1.5 flex items-center gap-1.5 ps-2 text-caption text-content-faint">
<Eye size={12} />
<span>{t('journey.share.views', { count: access.views })}</span>
{access.lastViewed && <span>{t('journey.share.lastViewed', { date: access.lastViewed })}</span>}
</div>
)}
{/* Permission toggles */}
<div className="flex flex-col gap-1.5">
{[
{ key: 'share_timeline' as const, label: t('journey.share.timeline'), icon: List },
{ key: 'share_gallery' as const, label: t('journey.share.gallery'), icon: Grid },
{ key: 'share_map' as const, label: t('journey.share.map'), icon: MapPin },
].map(({ key, label, icon: Icon }) => (
<button type="button"
key={key}
aria-pressed={!!link[key]}
onClick={() => togglePerm(key)}
className={`flex items-center gap-2.5 px-3 py-2 rounded-xl border text-body font-medium transition-all ${
link[key]
? 'border-accent bg-accent text-accent-text'
: 'border-edge text-content-muted hover:border-content-faint'
}`}
>
<Icon size={13} />
{label}
{link[key] && <Check size={12} className="ms-auto" />}
</button>
))}
<ShareMapExtras link={link} onToggle={key => void togglePerm(key)} />
</div>
<ShareReaders link={link} access={access} onToggle={key => void togglePerm(key)} />
<ShareAccessCard access={access} />
</div>
)}
</div>
)
}