104 lines
4.1 KiB
TypeScript
104 lines
4.1 KiB
TypeScript
import React, { CSSProperties } from 'react';
|
|
import { useNode, UserComponent } from '@craftjs/core';
|
|
import { cssPropsToString } from '../../utils/style-helpers';
|
|
import { escapeHtml, escapeAttr, cssValue, safeUrl } from '../../utils/escape';
|
|
import { tokenRef } from '../../utils/tokens';
|
|
|
|
interface BookingEmbedProps {
|
|
provider?: 'calendly' | 'square' | 'custom';
|
|
url?: string;
|
|
height?: string;
|
|
title?: string;
|
|
style?: CSSProperties;
|
|
animation?: string;
|
|
animationDelay?: string;
|
|
hideOnDesktop?: boolean;
|
|
hideOnTablet?: boolean;
|
|
hideOnMobile?: boolean;
|
|
}
|
|
|
|
// Exact hosts, plus subdomains of the *.suffix entries. Everything else is
|
|
// refused, whatever the `provider` says ('custom' is NOT an escape hatch).
|
|
const EXACT_HOSTS = ['calendly.com', 'squareup.com', 'app.squareup.com'];
|
|
const SUFFIX_HOSTS = ['.calendly.com', '.square.site'];
|
|
|
|
/** Returns the normalised https URL when its host is allow-listed, else ''. */
|
|
export function bookingEmbedUrl(raw: unknown): string {
|
|
const s = safeUrl(typeof raw === 'string' ? raw : '');
|
|
if (!s) return '';
|
|
let u: URL;
|
|
try { u = new URL(s); } catch { return ''; }
|
|
if (u.protocol !== 'https:') return '';
|
|
if (u.username || u.password) return '';
|
|
const host = u.hostname.toLowerCase();
|
|
const ok = EXACT_HOSTS.includes(host) || SUFFIX_HOSTS.some((suf) => host.endsWith(suf) && host.length > suf.length);
|
|
return ok ? u.toString() : '';
|
|
}
|
|
|
|
const PROVIDER_LABEL: Record<string, string> = { calendly: 'Calendly', square: 'Square Appointments', custom: 'Booking' };
|
|
|
|
export const BookingEmbed: UserComponent<BookingEmbedProps> = ({
|
|
provider = 'calendly',
|
|
url = '',
|
|
height = '700px',
|
|
title = 'Book an appointment',
|
|
style = {},
|
|
}) => {
|
|
const { connectors: { connect, drag }, selected } = useNode((node) => ({ selected: node.events.selected }));
|
|
const ok = !!bookingEmbedUrl(url);
|
|
// No live iframe in the canvas: a placeholder card keeps the editor offline-safe.
|
|
return (
|
|
<div
|
|
ref={(ref: HTMLDivElement | null): void => { if (ref) connect(drag(ref)); }}
|
|
style={{ padding: 24, outline: selected ? '2px solid #3b82f6' : 'none', ...style }}
|
|
>
|
|
<div style={{ border: `2px dashed ${tokenRef('border')}`, borderRadius: 12, padding: 32, textAlign: 'center', color: tokenRef('muted'), minHeight: 160 }}>
|
|
<div style={{ fontWeight: 600, color: tokenRef('text') }}>{title}</div>
|
|
<div style={{ marginTop: 8, fontSize: 14 }}>
|
|
{PROVIDER_LABEL[provider] || 'Booking'} embed{ok ? `: ${url}` : url ? ': this address is not an allowed booking host' : ': paste your booking URL in the panel'}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
BookingEmbed.craft = {
|
|
displayName: 'Booking',
|
|
props: {
|
|
provider: 'calendly',
|
|
url: '',
|
|
height: '700px',
|
|
title: 'Book an appointment',
|
|
style: {},
|
|
animation: '',
|
|
animationDelay: '',
|
|
hideOnDesktop: false,
|
|
hideOnTablet: false,
|
|
hideOnMobile: false,
|
|
},
|
|
rules: { canDrag: () => true, canMoveIn: () => false, canMoveOut: () => true },
|
|
};
|
|
|
|
(BookingEmbed as any).toHtml = (props: BookingEmbedProps, _childrenHtml: string) => {
|
|
const title = typeof props.title === 'string' && props.title ? props.title : 'Book an appointment';
|
|
const wrapStyle = cssPropsToString({ width: '100%', ...(props.style || {}) });
|
|
const wrap = (inner: string) => `<div${wrapStyle ? ` style="${wrapStyle}"` : ''}>${inner}</div>`;
|
|
|
|
const src = bookingEmbedUrl(props.url);
|
|
if (src) {
|
|
const height = cssValue(props.height) || '700px';
|
|
return {
|
|
html: wrap(`<iframe src="${escapeAttr(src)}" title="${escapeAttr(title)}" loading="lazy" sandbox="allow-scripts allow-forms allow-same-origin allow-popups" referrerpolicy="strict-origin-when-cross-origin" style="width:100%;height:${height};border:none;display:block"></iframe>`),
|
|
};
|
|
}
|
|
|
|
// Not allow-listed: never an iframe. A plain http(s) link at most.
|
|
const raw = safeUrl(typeof props.url === 'string' ? props.url : '');
|
|
const link = /^https?:\/\//i.test(raw) ? raw : '';
|
|
return {
|
|
html: wrap(link
|
|
? `<p><a href="${escapeAttr(link)}" target="_blank" rel="noopener noreferrer nofollow">${escapeHtml(title)}</a></p>`
|
|
: ''),
|
|
};
|
|
};
|