mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 18:34:09 +00:00
fix(ci): repair mobile client checks
Split mobile view logic at existing UI boundaries and keep shared calendar hooks outside component modules. Register the phone-only files and make the sheet gesture and final budget assertions independent of scheduling.
This commit is contained in:
@@ -2443,7 +2443,7 @@ describe('CostsPanel: a bill entered in the display currency (#2525)', () => {
|
||||
|
||||
const card = (await screen.findByRole('region', { name: 'Final budget' })) as HTMLElement
|
||||
await user.click(await within(card).findByRole('button', { name: /You/ }))
|
||||
expect(within(card).getAllByText('+$791.55')).toHaveLength(2)
|
||||
await waitFor(() => expect(within(card).getAllByText('+$791.55')).toHaveLength(2))
|
||||
expect(within(card).getByText('Aparthotel Silver')).toBeInTheDocument()
|
||||
expect(within(card).getByText('$801.76')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
@@ -87,17 +87,27 @@ function failureOf(err: unknown, t: TranslationFn): { error: string | null; offl
|
||||
}
|
||||
|
||||
/** The filters of "All places". */
|
||||
function useLibraryFilters(initial?: PlacesLibraryFilters) {
|
||||
const [search, setSearch] = useState(initial?.search ?? '');
|
||||
const [query, setQuery] = useState(initial?.search.trim() ?? '');
|
||||
const [kind, setKind] = useState<LibraryKindFilter>(initial?.kind ?? 'all');
|
||||
const [categoryId, setCategoryId] = useState<number | 'all'>(initial?.categoryId ?? 'all');
|
||||
const [sort, setSort] = useState<PlacesLibrarySort>(initial?.sort ?? 'name');
|
||||
const [status, setStatus] = useState<CollectionStatus | 'all'>(initial?.status ?? 'all');
|
||||
const DEFAULT_LIBRARY_FILTERS: PlacesLibraryFilters = {
|
||||
search: '',
|
||||
kind: 'all',
|
||||
categoryId: 'all',
|
||||
sort: 'name',
|
||||
status: 'all',
|
||||
minRating: 'all',
|
||||
tagNames: [],
|
||||
};
|
||||
|
||||
function useLibraryFilters(initial = DEFAULT_LIBRARY_FILTERS) {
|
||||
const [search, setSearch] = useState(initial.search);
|
||||
const [query, setQuery] = useState(initial.search.trim());
|
||||
const [kind, setKind] = useState(initial.kind);
|
||||
const [categoryId, setCategoryId] = useState(initial.categoryId);
|
||||
const [sort, setSort] = useState(initial.sort);
|
||||
const [status, setStatus] = useState(initial.status);
|
||||
// At least this many stars on the average of the votes, as the lists filter; unrated places drop out.
|
||||
const [minRating, setMinRating] = useState<number | 'all'>(initial?.minRating ?? 'all');
|
||||
const [minRating, setMinRating] = useState(initial.minRating);
|
||||
// PLC-21: tag names of the lists and trips, any of which an entry must carry, whatever their case.
|
||||
const [tagNames, setTagNames] = useState<string[]>(initial?.tagNames ?? []);
|
||||
const [tagNames, setTagNames] = useState(initial.tagNames);
|
||||
const toggleTagName = useCallback(
|
||||
(name: string) =>
|
||||
setTagNames((names) => {
|
||||
|
||||
@@ -35,6 +35,27 @@ interface Props {
|
||||
mobile?: boolean;
|
||||
}
|
||||
|
||||
const COMMENT_APPEARANCE = {
|
||||
phone: {
|
||||
toggle:
|
||||
'flex min-h-11 w-full items-center gap-2 rounded-xl bg-m-sheetop px-3 text-start text-control font-medium text-m-ink',
|
||||
controls: 'flex flex-none items-center gap-0.5',
|
||||
controlSize: 'size-11',
|
||||
field: 'min-h-11 border-m-rowbr bg-m-sheetop text-m-ink placeholder:text-m-muted',
|
||||
submit: 'size-11 bg-m-act text-m-actfg',
|
||||
hint: 'font-normal text-m-muted',
|
||||
},
|
||||
desktop: {
|
||||
toggle:
|
||||
'inline-flex items-center gap-1.5 self-start text-caption font-medium text-content-faint transition-colors hover:text-content',
|
||||
controls: 'flex flex-none items-center gap-0.5 opacity-40 transition-opacity group-hover:opacity-100',
|
||||
controlSize: 'size-7',
|
||||
field: 'min-h-[38px] border-edge bg-surface text-content placeholder:text-content-faint focus:border-content-faint',
|
||||
submit: 'size-[38px] bg-accent text-accent-text hover:bg-accent-hover',
|
||||
hint: 'text-content-faint',
|
||||
},
|
||||
};
|
||||
|
||||
const GUEST_NAME_KEY = 'trek.journey.guestName';
|
||||
|
||||
function rememberedName(): string {
|
||||
@@ -80,11 +101,15 @@ export default function JourneyEntryComments({
|
||||
const [message, setMessage] = useState<string | null>(null);
|
||||
|
||||
if (!canWrite && comments.length === 0) return null;
|
||||
const appearance = COMMENT_APPEARANCE[mobile ? 'phone' : 'desktop'];
|
||||
const open = expanded ?? Boolean(defaultOpen || (mobile && comments.length));
|
||||
|
||||
const needsGuestName = guest && !name.trim();
|
||||
const cannotSubmit = busy || !body.trim() || needsGuestName;
|
||||
|
||||
const submit = async (e: FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (busy || !body.trim() || (guest && !name.trim())) return;
|
||||
if (cannotSubmit) return;
|
||||
// Only a bot fills the field people never see: it is told all went well.
|
||||
if (trap) {
|
||||
setBody('');
|
||||
@@ -105,7 +130,10 @@ export default function JourneyEntryComments({
|
||||
const dateOf = (iso: string | null | undefined) =>
|
||||
iso ? new Date(iso).toLocaleDateString(locale || undefined, { day: 'numeric', month: 'short' }) : '';
|
||||
|
||||
const toggle = (
|
||||
const renderPublicHint = () =>
|
||||
publicHint && !guest && <p className={`text-caption ${appearance.hint}`}>{t('journey.comments.publicHint')}</p>;
|
||||
|
||||
const renderCollapsed = () => (
|
||||
<button
|
||||
type="button"
|
||||
aria-expanded={open}
|
||||
@@ -113,22 +141,18 @@ export default function JourneyEntryComments({
|
||||
e.stopPropagation();
|
||||
setExpanded(!open);
|
||||
}}
|
||||
className={
|
||||
mobile
|
||||
? 'flex min-h-11 w-full items-center gap-2 rounded-xl bg-m-sheetop px-3 text-start text-control font-medium text-m-ink'
|
||||
: 'inline-flex items-center gap-1.5 self-start text-caption font-medium text-content-faint transition-colors hover:text-content'
|
||||
}
|
||||
className={appearance.toggle}
|
||||
>
|
||||
<MessageCircle size={13} />
|
||||
{comments.length > 0 ? t('journey.comments.count', { count: comments.length }) : t('journey.comments.write')}
|
||||
</button>
|
||||
);
|
||||
if (!open) return toggle;
|
||||
if (!open) return renderCollapsed();
|
||||
|
||||
return (
|
||||
// The thread sits inside clickable cards: a click in it must not open the entry.
|
||||
<div className="flex flex-col gap-3" onClick={(e) => e.stopPropagation()} role="presentation">
|
||||
{!defaultOpen && !mobile && toggle}
|
||||
{!defaultOpen && !mobile && renderCollapsed()}
|
||||
{comments.length > 0 && (
|
||||
<ul className="flex flex-col gap-2.5">
|
||||
{comments.map((c) => {
|
||||
@@ -156,13 +180,7 @@ export default function JourneyEntryComments({
|
||||
<p className="mt-0.5 whitespace-pre-wrap break-words text-body text-content-muted">{c.body}</p>
|
||||
</div>
|
||||
{(mayHide || mayDelete) && (
|
||||
<div
|
||||
className={
|
||||
mobile
|
||||
? 'flex flex-none items-center gap-0.5'
|
||||
: 'flex flex-none items-center gap-0.5 opacity-40 transition-opacity group-hover:opacity-100'
|
||||
}
|
||||
>
|
||||
<div className={appearance.controls}>
|
||||
{mayHide && (
|
||||
<Tooltip
|
||||
label={c.hidden ? t('journey.comments.show') : t('journey.comments.hide')}
|
||||
@@ -172,7 +190,7 @@ export default function JourneyEntryComments({
|
||||
type="button"
|
||||
aria-label={c.hidden ? t('journey.comments.show') : t('journey.comments.hide')}
|
||||
onClick={() => void onHide?.(c.id, !c.hidden)}
|
||||
className={`flex ${mobile ? 'size-11' : 'size-7'} items-center justify-center rounded-full text-content-faint hover:bg-surface-secondary hover:text-content`}
|
||||
className={`flex ${appearance.controlSize} items-center justify-center rounded-full text-content-faint hover:bg-surface-secondary hover:text-content`}
|
||||
>
|
||||
{c.hidden ? <Eye size={13} /> : <EyeOff size={13} />}
|
||||
</button>
|
||||
@@ -184,7 +202,7 @@ export default function JourneyEntryComments({
|
||||
type="button"
|
||||
aria-label={t('common.delete')}
|
||||
onClick={() => void onDelete?.(c.id)}
|
||||
className={`flex ${mobile ? 'size-11' : 'size-7'} items-center justify-center rounded-full text-content-faint hover:bg-danger-soft hover:text-danger`}
|
||||
className={`flex ${appearance.controlSize} items-center justify-center rounded-full text-content-faint hover:bg-danger-soft hover:text-danger`}
|
||||
>
|
||||
<Trash2 size={13} />
|
||||
</button>
|
||||
@@ -230,20 +248,16 @@ export default function JourneyEntryComments({
|
||||
placeholder={t('journey.comments.write')}
|
||||
aria-label={t('journey.comments.write')}
|
||||
className={`max-h-40 min-w-0 flex-1 resize-none rounded-xl border px-3 py-2 text-body outline-none [field-sizing:content] ${
|
||||
mobile
|
||||
? 'min-h-11 border-m-rowbr bg-m-sheetop text-m-ink placeholder:text-m-muted'
|
||||
: 'min-h-[38px] border-edge bg-surface text-content placeholder:text-content-faint focus:border-content-faint'
|
||||
appearance.field
|
||||
}`}
|
||||
/>
|
||||
<Tooltip label={t('journey.comments.send')} placement="top">
|
||||
<button
|
||||
type="submit"
|
||||
aria-label={t('journey.comments.send')}
|
||||
disabled={busy || !body.trim() || (guest && !name.trim())}
|
||||
disabled={cannotSubmit}
|
||||
className={`flex flex-none items-center justify-center rounded-full transition-opacity disabled:opacity-40 ${
|
||||
mobile
|
||||
? 'size-11 bg-m-act text-m-actfg'
|
||||
: 'size-[38px] bg-accent text-accent-text hover:bg-accent-hover'
|
||||
appearance.submit
|
||||
}`}
|
||||
>
|
||||
<SendHorizontal size={15} />
|
||||
@@ -251,11 +265,7 @@ export default function JourneyEntryComments({
|
||||
</Tooltip>
|
||||
</div>
|
||||
{message && <p className="text-caption text-danger">{message}</p>}
|
||||
{publicHint && !guest && (
|
||||
<p className={`text-caption ${mobile ? 'font-normal text-m-muted' : 'text-content-faint'}`}>
|
||||
{t('journey.comments.publicHint')}
|
||||
</p>
|
||||
)}
|
||||
{renderPublicHint()}
|
||||
</form>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useState, useEffect, useId, type ReactNode } from 'react'
|
||||
import { useId, type ReactNode } from 'react'
|
||||
import { useIcsSubscription } from './useIcsSubscription'
|
||||
import { RefreshCw, Calendar, Power } from 'lucide-react'
|
||||
import { SubscribeLinks } from './SubscribeLinks'
|
||||
import ToggleSwitch from '../Settings/ToggleSwitch'
|
||||
@@ -13,24 +14,6 @@ export interface IcsSubscribeModalProps {
|
||||
onClose: () => void
|
||||
}
|
||||
|
||||
// A server that has no APP_URL configured hands back a host-relative path; the
|
||||
// webcal:// handoff and Google deep link need an absolute URL, so resolve it
|
||||
// against the current origin as a fallback.
|
||||
function absolutize(url: string): string {
|
||||
if (!url) return ''
|
||||
if (/^https?:\/\//i.test(url)) return url
|
||||
if (url.startsWith('/')) return window.location.origin + url
|
||||
return url
|
||||
}
|
||||
|
||||
// The feeds take days=0 to leave out the all-day summary every day with
|
||||
// untimed places or notes gets (#2602). The choice lives in the link itself,
|
||||
// so a calendar keeps what it subscribed to and the token stays one per trip.
|
||||
function withDayEvents(url: string, dayEvents: boolean): string {
|
||||
if (!url || dayEvents) return url
|
||||
return `${url}${url.includes('?') ? '&' : '?'}days=0`
|
||||
}
|
||||
|
||||
/** The switch for those summaries, above the links it changes, on both shells. */
|
||||
export function DayEventsSwitch({ on, onToggle }: { on: boolean; onToggle: () => void }) {
|
||||
const { t } = useTranslation()
|
||||
@@ -48,46 +31,6 @@ export function DayEventsSwitch({ on, onToggle }: { on: boolean; onToggle: () =>
|
||||
)
|
||||
}
|
||||
|
||||
/** Opening reads the token. Creating or changing it always takes an explicit action. */
|
||||
export function useIcsSubscription(endpoint: string) {
|
||||
const tokenUrl = `${endpoint}/token`
|
||||
const [feedUrl, setFeedUrl] = useState<string | null>(null)
|
||||
const [loadedTokenUrl, setLoadedTokenUrl] = useState<string | null>(null)
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [dayEvents, setDayEvents] = useState(true)
|
||||
const toggleDayEvents = () => setDayEvents(on => !on)
|
||||
|
||||
const httpsUrl = feedUrl ? withDayEvents(absolutize(feedUrl), dayEvents) : ''
|
||||
const webcalUrl = httpsUrl ? httpsUrl.replace(/^https?:\/\//, 'webcal://') : ''
|
||||
|
||||
useEffect(() => {
|
||||
let active = true
|
||||
void fetch(tokenUrl, { credentials: 'include' })
|
||||
.then(async res => res.ok ? (await res.json() as { feed_url: string | null }).feed_url : null)
|
||||
.catch(() => null)
|
||||
.then(url => {
|
||||
if (!active) return
|
||||
setFeedUrl(url)
|
||||
setLoadedTokenUrl(tokenUrl)
|
||||
})
|
||||
return () => { active = false }
|
||||
}, [tokenUrl])
|
||||
|
||||
const mutate = async (method: 'POST' | 'PUT' | 'DELETE') => {
|
||||
setBusy(true)
|
||||
try {
|
||||
const res = await fetch(tokenUrl, { method, credentials: 'include' })
|
||||
if (res.ok) {
|
||||
const data = await res.json() as { feed_url: string | null }
|
||||
setFeedUrl(data.feed_url)
|
||||
}
|
||||
} catch { /* ignore */ }
|
||||
setBusy(false)
|
||||
}
|
||||
|
||||
return { loading: loadedTokenUrl !== tokenUrl, busy, feedUrl, httpsUrl, webcalUrl, mutate, dayEvents, toggleDayEvents }
|
||||
}
|
||||
|
||||
export function IcsSubscribeModal({ endpoint, title, description, onClose }: IcsSubscribeModalProps) {
|
||||
const titleId = useId()
|
||||
const { loading, busy, feedUrl, httpsUrl, webcalUrl, mutate, dayEvents, toggleDayEvents } = useIcsSubscription(endpoint)
|
||||
|
||||
@@ -89,51 +89,59 @@ export default function PlaceImagePicker({
|
||||
/>
|
||||
);
|
||||
const showUploadRow = mobile && !own?.pendingPreview;
|
||||
const showTiles = Boolean(current || attached.length || photos.length || (upload && !showUploadRow));
|
||||
|
||||
const renderGallery = () => {
|
||||
const showTiles = Boolean(current || attached.length || photos.length || (upload && !showUploadRow));
|
||||
return (
|
||||
<>
|
||||
{showTiles && (
|
||||
<div className={`grid grid-cols-3 gap-1.5 ${showUploadRow && upload ? 'mt-2' : ''}`}>
|
||||
{!showUploadRow && upload}
|
||||
{current && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => (chosen.kind === 'current' ? own?.onRemove() : onPickUrl(current))}
|
||||
aria-pressed={chosen.kind === 'current'}
|
||||
aria-label={t('places.image.current')}
|
||||
onMouseEnter={() => setHovered({ kind: 'own', label: t('places.image.current') })}
|
||||
onMouseLeave={() => setHovered(null)}
|
||||
className={`${TILE} ${ring(chosen.kind === 'current', dialog)}`}
|
||||
>
|
||||
<img src={current} alt="" className="h-full w-full object-cover" />
|
||||
{chosen.kind === 'current' && <Picked />}
|
||||
</button>
|
||||
)}
|
||||
{attached.map((file) => (
|
||||
<AttachedTile
|
||||
key={file.id}
|
||||
file={file}
|
||||
selected={chosen.kind === 'attached' && chosen.fileId === file.id}
|
||||
onPick={() => own?.onPickAttached(file)}
|
||||
onHover={(on) => setHovered(on ? { kind: 'own', label: file.original_name } : null)}
|
||||
dialog={dialog}
|
||||
/>
|
||||
))}
|
||||
{photos.map((photo) => (
|
||||
<PhotoTile
|
||||
key={photo.key}
|
||||
photo={photo}
|
||||
selected={chosen.kind === 'url' && chosen.url === photo.url}
|
||||
onPick={onPickUrl}
|
||||
onHover={(on) => setHovered(on ? { kind: 'photo', photo } : null)}
|
||||
dialog={dialog}
|
||||
t={t}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
{showUploadRow && upload}
|
||||
{showTiles && (
|
||||
<div className={`grid grid-cols-3 gap-1.5 ${showUploadRow && upload ? 'mt-2' : ''}`}>
|
||||
{!showUploadRow && upload}
|
||||
{current && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => (chosen.kind === 'current' ? own?.onRemove() : onPickUrl(current))}
|
||||
aria-pressed={chosen.kind === 'current'}
|
||||
aria-label={t('places.image.current')}
|
||||
onMouseEnter={() => setHovered({ kind: 'own', label: t('places.image.current') })}
|
||||
onMouseLeave={() => setHovered(null)}
|
||||
className={`${TILE} ${ring(chosen.kind === 'current', dialog)}`}
|
||||
>
|
||||
<img src={current} alt="" className="h-full w-full object-cover" />
|
||||
{chosen.kind === 'current' && <Picked />}
|
||||
</button>
|
||||
)}
|
||||
{attached.map((file) => (
|
||||
<AttachedTile
|
||||
key={file.id}
|
||||
file={file}
|
||||
selected={chosen.kind === 'attached' && chosen.fileId === file.id}
|
||||
onPick={() => own?.onPickAttached(file)}
|
||||
onHover={(on) => setHovered(on ? { kind: 'own', label: file.original_name } : null)}
|
||||
dialog={dialog}
|
||||
/>
|
||||
))}
|
||||
{photos.map((photo) => (
|
||||
<PhotoTile
|
||||
key={photo.key}
|
||||
photo={photo}
|
||||
selected={chosen.kind === 'url' && chosen.url === photo.url}
|
||||
onPick={onPickUrl}
|
||||
onHover={(on) => setHovered(on ? { kind: 'photo', photo } : null)}
|
||||
dialog={dialog}
|
||||
t={t}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
{renderGallery()}
|
||||
{(shown || removable) && (
|
||||
<CreditLine shown={shown} onRemove={removable ? own?.onRemove : undefined} dialog={dialog} t={t} />
|
||||
)}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState } from 'react'
|
||||
import { useSubscribeLinks } from './useSubscribeLinks'
|
||||
import { Copy, Check, CalendarPlus, Calendar } from 'lucide-react'
|
||||
import { useTranslation } from '../../i18n'
|
||||
|
||||
@@ -7,45 +7,6 @@ interface SubscribeLinksProps {
|
||||
webcalUrl: string
|
||||
}
|
||||
|
||||
/**
|
||||
* The provider links and clipboard behavior shared by both subscription dialogs.
|
||||
*/
|
||||
export function useSubscribeLinks(httpsUrl: string, webcalUrl: string) {
|
||||
const { t } = useTranslation()
|
||||
const [copied, setCopied] = useState<'https' | 'webcal' | null>(null)
|
||||
|
||||
// Google Calendar's add-by-URL deep link. The cid must carry the webcal://
|
||||
// scheme (not https), URL-encoded, and the feed must be served over HTTPS.
|
||||
const googleUrl = `https://www.google.com/calendar/render?cid=${encodeURIComponent(webcalUrl)}`
|
||||
|
||||
const copy = async (url: string, which: 'https' | 'webcal') => {
|
||||
try {
|
||||
if (navigator.clipboard && window.isSecureContext) {
|
||||
await navigator.clipboard.writeText(url)
|
||||
} else {
|
||||
// Fallback for non-secure contexts (plain HTTP) where navigator.clipboard is unavailable
|
||||
const ta = document.createElement('textarea')
|
||||
ta.value = url
|
||||
ta.style.position = 'fixed'
|
||||
ta.style.left = '-9999px'
|
||||
document.body.appendChild(ta)
|
||||
ta.focus()
|
||||
ta.select()
|
||||
document.execCommand('copy')
|
||||
document.body.removeChild(ta)
|
||||
}
|
||||
setCopied(which)
|
||||
setTimeout(() => setCopied(null), 2000)
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
|
||||
const manualLinks = [
|
||||
{ label: 'Google Calendar', hint: t('dashboard.ics.pasteFromUrl'), url: httpsUrl, copied: copied === 'https', onCopy: () => void copy(httpsUrl, 'https') },
|
||||
{ label: 'Apple Calendar / Outlook', hint: 'webcal://', url: webcalUrl, copied: copied === 'webcal', onCopy: () => void copy(webcalUrl, 'webcal') },
|
||||
]
|
||||
return { googleUrl, manualLinks }
|
||||
}
|
||||
|
||||
export function SubscribeLinks({ httpsUrl, webcalUrl }: SubscribeLinksProps) {
|
||||
const { t } = useTranslation()
|
||||
const { googleUrl, manualLinks } = useSubscribeLinks(httpsUrl, webcalUrl)
|
||||
|
||||
@@ -483,6 +483,36 @@ function ItineraryCard({ it, tzFrom, tzTo, is12h, expanded, onToggle, onAdd, add
|
||||
// the TransportModal (and could embed anywhere else). The host owns the modal
|
||||
// chrome and closes itself once onAdd resolves.
|
||||
|
||||
function TransitStopFields({ mobile, isMobile, from, to, setFrom, setTo, quickPicks, near }: {
|
||||
mobile: boolean
|
||||
isMobile: boolean
|
||||
from: PickedPlace | null
|
||||
to: PickedPlace | null
|
||||
setFrom: (place: PickedPlace | null) => void
|
||||
setTo: (place: PickedPlace | null) => void
|
||||
quickPicks: PickedPlace[]
|
||||
near: string | null
|
||||
}) {
|
||||
const { t } = useTranslation()
|
||||
return (
|
||||
<div className={mobile ? 'flex flex-col gap-2.5 rounded-[14px] bg-m-ic p-3' : `flex gap-2 ${isMobile ? 'flex-col' : 'items-end'}`}>
|
||||
<StopPicker mobile={mobile} label={t('transit.from')} value={from} onPick={setFrom} quickPicks={quickPicks} near={near} placeholder={t('transit.searchStop')} />
|
||||
{!mobile && !isMobile && (
|
||||
<button type="button"
|
||||
onClick={() => { const f = from; setFrom(to); setTo(f) }}
|
||||
aria-label={t('transit.swap')}
|
||||
title={t('transit.swap')}
|
||||
className="bg-surface-secondary text-content-muted"
|
||||
style={{ border: 'none', borderRadius: 10, width: 38, height: 38, display: 'grid', placeItems: 'center', cursor: 'pointer', flexShrink: 0 }}
|
||||
>
|
||||
<ArrowLeftRight size={15} />
|
||||
</button>
|
||||
)}
|
||||
<StopPicker mobile={mobile} label={t('transit.to')} value={to} onPick={setTo} quickPicks={quickPicks} near={near} placeholder={t('transit.searchStop')} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
interface TransitSearchPanelProps {
|
||||
day: Day
|
||||
days: Day[]
|
||||
@@ -688,22 +718,8 @@ export default function TransitSearchPanel({ day, days, places, accommodations =
|
||||
return (
|
||||
<div style={{ display: 'flex', flexDirection: 'column', gap: 16, fontFamily: 'var(--font-system)' }}>
|
||||
<WalkTransitNote walk={walkHere} />
|
||||
{/* from / to — stacked tight on mobile, swap button on desktop only */}
|
||||
<div className={mobile ? 'flex flex-col gap-2.5 rounded-[14px] bg-m-ic p-3' : `flex gap-2 ${isMobile ? 'flex-col' : 'items-end'}`}>
|
||||
<StopPicker mobile={mobile} label={t('transit.from')} value={from} onPick={setFrom} quickPicks={quickPicks} near={near} placeholder={t('transit.searchStop')} />
|
||||
{!mobile && !isMobile && (
|
||||
<button type="button"
|
||||
onClick={() => { const f = from; setFrom(to); setTo(f) }}
|
||||
aria-label={t('transit.swap')}
|
||||
title={t('transit.swap')}
|
||||
className="bg-surface-secondary text-content-muted"
|
||||
style={{ border: 'none', borderRadius: 10, width: 38, height: 38, display: 'grid', placeItems: 'center', cursor: 'pointer', flexShrink: 0 }}
|
||||
>
|
||||
<ArrowLeftRight size={15} />
|
||||
</button>
|
||||
)}
|
||||
<StopPicker mobile={mobile} label={t('transit.to')} value={to} onPick={setTo} quickPicks={quickPicks} near={near} placeholder={t('transit.searchStop')} />
|
||||
</div>
|
||||
<TransitStopFields mobile={mobile} isMobile={isMobile} from={from} to={to}
|
||||
setFrom={setFrom} setTo={setTo} quickPicks={quickPicks} near={near} />
|
||||
|
||||
{mobile ? (
|
||||
<>
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
// A server that has no APP_URL configured hands back a host-relative path; the
|
||||
// webcal:// handoff and Google deep link need an absolute URL, so resolve it
|
||||
// against the current origin as a fallback.
|
||||
function absolutize(url: string): string {
|
||||
if (!url) return '';
|
||||
if (/^https?:\/\//i.test(url)) return url;
|
||||
if (url.startsWith('/')) return window.location.origin + url;
|
||||
return url;
|
||||
}
|
||||
|
||||
// The feeds take days=0 to leave out the all-day summary every day with
|
||||
// untimed places or notes gets (#2602). The choice lives in the link itself,
|
||||
// so a calendar keeps what it subscribed to and the token stays one per trip.
|
||||
function withDayEvents(url: string, dayEvents: boolean): string {
|
||||
if (!url || dayEvents) return url;
|
||||
return `${url}${url.includes('?') ? '&' : '?'}days=0`;
|
||||
}
|
||||
|
||||
/** Opening reads the token. Creating or changing it always takes an explicit action. */
|
||||
export function useIcsSubscription(endpoint: string) {
|
||||
const tokenUrl = `${endpoint}/token`;
|
||||
const [feedUrl, setFeedUrl] = useState<string | null>(null);
|
||||
const [loadedTokenUrl, setLoadedTokenUrl] = useState<string | null>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [dayEvents, setDayEvents] = useState(true);
|
||||
const toggleDayEvents = () => setDayEvents((on) => !on);
|
||||
|
||||
const httpsUrl = feedUrl ? withDayEvents(absolutize(feedUrl), dayEvents) : '';
|
||||
const webcalUrl = httpsUrl ? httpsUrl.replace(/^https?:\/\//, 'webcal://') : '';
|
||||
|
||||
useEffect(() => {
|
||||
let active = true;
|
||||
void fetch(tokenUrl, { credentials: 'include' })
|
||||
.then(async (res) => (res.ok ? ((await res.json()) as { feed_url: string | null }).feed_url : null))
|
||||
.catch(() => null)
|
||||
.then((url) => {
|
||||
if (!active) return;
|
||||
setFeedUrl(url);
|
||||
setLoadedTokenUrl(tokenUrl);
|
||||
});
|
||||
return () => {
|
||||
active = false;
|
||||
};
|
||||
}, [tokenUrl]);
|
||||
|
||||
const mutate = async (method: 'POST' | 'PUT' | 'DELETE') => {
|
||||
setBusy(true);
|
||||
try {
|
||||
const res = await fetch(tokenUrl, { method, credentials: 'include' });
|
||||
if (res.ok) {
|
||||
const data = (await res.json()) as { feed_url: string | null };
|
||||
setFeedUrl(data.feed_url);
|
||||
}
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
setBusy(false);
|
||||
};
|
||||
|
||||
return {
|
||||
loading: loadedTokenUrl !== tokenUrl,
|
||||
busy,
|
||||
feedUrl,
|
||||
httpsUrl,
|
||||
webcalUrl,
|
||||
mutate,
|
||||
dayEvents,
|
||||
toggleDayEvents,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { useState } from 'react';
|
||||
import { useTranslation } from '../../i18n';
|
||||
|
||||
/**
|
||||
* The provider links and clipboard behavior shared by both subscription dialogs.
|
||||
*/
|
||||
export function useSubscribeLinks(httpsUrl: string, webcalUrl: string) {
|
||||
const { t } = useTranslation();
|
||||
const [copied, setCopied] = useState<'https' | 'webcal' | null>(null);
|
||||
|
||||
// Google Calendar's add-by-URL deep link. The cid must carry the webcal://
|
||||
// scheme (not https), URL-encoded, and the feed must be served over HTTPS.
|
||||
const googleUrl = `https://www.google.com/calendar/render?cid=${encodeURIComponent(webcalUrl)}`;
|
||||
|
||||
const copy = async (url: string, which: 'https' | 'webcal') => {
|
||||
try {
|
||||
if (navigator.clipboard && window.isSecureContext) {
|
||||
await navigator.clipboard.writeText(url);
|
||||
} else {
|
||||
// Fallback for non-secure contexts (plain HTTP) where navigator.clipboard is unavailable
|
||||
const ta = document.createElement('textarea');
|
||||
ta.value = url;
|
||||
ta.style.position = 'fixed';
|
||||
ta.style.left = '-9999px';
|
||||
document.body.appendChild(ta);
|
||||
ta.focus();
|
||||
ta.select();
|
||||
document.execCommand('copy');
|
||||
document.body.removeChild(ta);
|
||||
}
|
||||
setCopied(which);
|
||||
setTimeout(() => setCopied(null), 2000);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
};
|
||||
|
||||
const manualLinks = [
|
||||
{
|
||||
label: 'Google Calendar',
|
||||
hint: t('dashboard.ics.pasteFromUrl'),
|
||||
url: httpsUrl,
|
||||
copied: copied === 'https',
|
||||
onCopy: () => void copy(httpsUrl, 'https'),
|
||||
},
|
||||
{
|
||||
label: 'Apple Calendar / Outlook',
|
||||
hint: 'webcal://',
|
||||
url: webcalUrl,
|
||||
copied: copied === 'webcal',
|
||||
onCopy: () => void copy(webcalUrl, 'webcal'),
|
||||
},
|
||||
];
|
||||
return { googleUrl, manualLinks };
|
||||
}
|
||||
Reference in New Issue
Block a user