mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +00:00
ede109dce8
Connect the Journey backend to the web and mobile UI, including public access and subscriptions, day recordings, photo drafts and per-map framing. Preserve legacy recording responses and cover the new behavior in every locale.
254 lines
13 KiB
TypeScript
254 lines
13 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>
|
|
)
|
|
}
|
|
|
|
/** 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 (
|
|
<Tooltip key={key} label={blocked ? t('journey.share.needsTimeline') : ''} placement="top">
|
|
<button
|
|
type="button"
|
|
aria-pressed={on}
|
|
disabled={blocked}
|
|
onClick={() => onToggle(key)}
|
|
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')}>{t(labelKey)}</span>
|
|
<span className={on ? 'text-accent-text opacity-80' : 'text-content-faint'} style={fs(11)}>{t(hintKey)}</span>
|
|
</span>
|
|
{on && <Check size={12} className="mt-0.5 flex-none" />}
|
|
</button>
|
|
</Tooltip>
|
|
)
|
|
})}
|
|
{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 (
|
|
<Tooltip key={key} label={mapShared ? '' : t('journey.share.needsMap')} placement="top">
|
|
<button
|
|
type="button"
|
|
aria-pressed={on}
|
|
disabled={!mapShared}
|
|
onClick={() => onToggle(key)}
|
|
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')}>{t(labelKey)}</span>
|
|
<span className={on ? 'text-accent-text opacity-80' : 'text-content-faint'} style={fs(11)}>{t(hintKey)}</span>
|
|
</span>
|
|
{on && <Check size={12} className="mt-0.5 flex-none" />}
|
|
</button>
|
|
</Tooltip>
|
|
)
|
|
})}
|
|
</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>
|
|
)
|
|
}
|