diff --git a/apps/admin/src/lib/sessionizeSource.ts b/apps/admin/src/lib/sessionizeSource.ts new file mode 100644 index 0000000..3967a5e --- /dev/null +++ b/apps/admin/src/lib/sessionizeSource.ts @@ -0,0 +1,19 @@ +export function normalizeSessionizeSource(value: string): string | null { + let source = value.trim(); + if (!source) return null; + if (source.startsWith('<')) { + const document = new DOMParser().parseFromString(source, 'text/html'); + const scripts = document.querySelectorAll('script[src]'); + if (scripts.length !== 1) throw new Error('Paste one Sessionize script tag or URL'); + source = scripts[0].getAttribute('src') || ''; + } + let url: URL; + try { url = new URL(source); } catch { throw new Error('Enter a valid Sessionize URL or script tag'); } + if (url.protocol !== 'https:' || url.hostname !== 'sessionize.com' || url.port + || url.username || url.password || !/^\/api\/v2\/[a-z0-9]+\/view\/(All|GridSmart)$/i.test(url.pathname)) { + throw new Error('Use a Sessionize HTTPS All or GridSmart endpoint'); + } + url.search = ''; + url.hash = ''; + return url.href; +} \ No newline at end of file diff --git a/apps/admin/src/views/Events.tsx b/apps/admin/src/views/Events.tsx index 1075d93..4c8768b 100644 --- a/apps/admin/src/views/Events.tsx +++ b/apps/admin/src/views/Events.tsx @@ -11,6 +11,7 @@ import { import type { Event, PretixOptions, PretixSyncResult, SyncResult } from '../lib/types'; import { useToast } from '../context/ToastContext'; import { Empty, Modal, Spinner, Toggle } from '../components/ui'; +import { normalizeSessionizeSource } from '../lib/sessionizeSource'; export function Events() { const { push } = useToast(); @@ -286,6 +287,7 @@ function EventModal({ const [endDate, setEndDate] = useState(event ? event.end_date.slice(0, 10) : ''); const [location, setLocation] = useState(event?.location ?? ''); const [endpoint, setEndpoint] = useState(event?.sessionize_endpoint ?? ''); + const [endpointError, setEndpointError] = useState(null); const [pretixApiKey, setPretixApiKey] = useState(event?.pretix_api_key ?? ''); const [pretixOrganizer, setPretixOrganizer] = useState(event?.pretix_organizer ?? ''); const [pretixEventSlug, setPretixEventSlug] = useState(event?.pretix_event_slug ?? ''); @@ -328,6 +330,14 @@ function EventModal({ async function save() { if (!name.trim() || !startDate || !endDate) return; + let source: string | null; + try { + source = normalizeSessionizeSource(endpoint); + setEndpointError(null); + } catch (error) { + setEndpointError(error instanceof Error ? error.message : 'Invalid Sessionize source'); + return; + } setBusy(true); try { await upsertEvent(event?.id ?? null, { @@ -335,7 +345,7 @@ function EventModal({ start_date: startDate, end_date: endDate, location: location.trim() || null, - sessionize_endpoint: endpoint.trim() || null, + sessionize_endpoint: source, pretix_organizer: pretixOrganizer.trim() || null, pretix_event_slug: pretixEventSlug.trim() || null, pretix_api_key: pretixApiKey.trim() || null, @@ -391,16 +401,20 @@ function EventModal({ {endpoint.trim() ? configured : not set}
- - Sessionize endpoint +