Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171/** * What an event's place fields should do when tapped. * * Meeting invitations rarely fill `virtualLocations`: a Teams invite arrives * with LOCATION set to "Microsoft Teams Meeting" and the join URL buried in * the description, among dial-in numbers and help links. `findMeetingLink` * digs it out so the sheet can offer one "Join" button without touching the * event on the server (it is someone else's invitation). * * A physical address is handed to the platform's maps app instead. */
import { isSafeExternalUrl } from './open-url';
/** The place fields read here, loose enough for any event shape. */export interface EventPlaces { locations?: Record<string, { name?: string | null; description?: string | null } | null> | null; virtualLocations?: Record<string, { uri?: string | null } | null> | null; description?: string | null;}
export interface MeetingLink { uri: string; /** Display name of the conferencing service, when recognised. */ provider?: string; /** True when the link was found in the description, not a structured field. */ derived: boolean;}
// Hosts whose URLs are joinable meetings. The path test keeps help pages,// "learn more" and dial-in links on the same host out of the result.const PROVIDERS: Array<{ name: string; host: RegExp; path?: RegExp }> = [ { name: 'Teams', host: /^teams\.(microsoft|live)\.com$/, path: /^\/(l\/meetup-join|meet)\//i }, { name: 'Zoom', host: /(^|\.)zoom\.us$/, path: /^\/(j|w|wc|my|s)\//i }, { name: 'Google Meet', host: /^meet\.google\.com$/, path: /^\/[a-z]{3}-[a-z]{4}-[a-z]{3}/i }, { name: 'Webex', host: /(^|\.)webex\.com$/, path: /^\/(meet|join|[^/]+\/j\.php)/i }, { name: 'Jitsi', host: /^meet\.jit\.si$/, path: /^\/./ }, { name: 'Whereby', host: /^whereby\.com$/, path: /^\/./ }, { name: 'GoTo', host: /^(meet\.goto\.com|global\.gotomeeting\.com|app\.gotomeeting\.com)$/, path: /^\/./ }, { name: 'Visio', host: /^(visio|webconf)\.numerique\.gouv\.fr$/, path: /^\/./ },];
const URL_RE = /https?:\/\/[^\s<>"']+/g;
const isHttpUrl = (value: string | undefined | null): value is string => !!value && /^https?:\/\/\S+$/i.test(value.trim());
/** * Outlook's Safe Links and similar gateways wrap the real URL in a query * parameter; unwrap one level so the provider can be recognised. */export function unwrapSafeLink(uri: string): string { try { const u = new URL(uri); if (/(^|\.)safelinks\.protection\.outlook\.com$/i.test(u.hostname)) { const inner = u.searchParams.get('url'); if (inner && isHttpUrl(inner)) return inner; } } catch { // not a parseable URL - keep as is } return uri;}
/** The conferencing service a URL joins, or null for any other link. */export function meetingProviderOf(uri: string): string | null { let u: URL; try { u = new URL(unwrapSafeLink(uri)); } catch { return null; } const host = u.hostname.toLowerCase(); const provider = PROVIDERS.find((p) => p.host.test(host) && (!p.path || p.path.test(u.pathname))); return provider ? provider.name : null;}
function firstMeetingUrl(text: string | undefined | null): MeetingLink | null { if (!text) return null; for (const raw of text.match(URL_RE) ?? []) { // Trailing punctuation belongs to the sentence, not the URL. const candidate = unwrapSafeLink(raw.replace(/[).,;:!?\]]+$/, '')); const provider = meetingProviderOf(candidate); if (provider) return { uri: candidate, provider, derived: true }; } return null;}
/** * The link that joins the meeting: the event's own virtual location first, * then a URL used as the location, then a known conferencing URL found in the * location or the description. */export function findMeetingLink(event: EventPlaces): MeetingLink | null { for (const vl of Object.values(event.virtualLocations ?? {})) { const uri = vl?.uri; if (isHttpUrl(uri)) { return { uri: uri.trim(), provider: meetingProviderOf(uri) ?? undefined, derived: false }; } } const locations = Object.values(event.locations ?? {}); for (const loc of locations) { const found = firstMeetingUrl(loc?.name) ?? firstMeetingUrl(loc?.description); if (found) return found; } const inDescription = firstMeetingUrl(event.description); if (inDescription) return inDescription; // Last resort: a virtual location in another scheme the app may open (a // tel: dial-in, say). App schemes like msteams:, zoommtg: or sip: are left // out because openExternalUrl refuses them, and a Join button that does // nothing is worse than none. for (const vl of Object.values(event.virtualLocations ?? {})) { const uri = vl?.uri?.trim(); if (isSafeExternalUrl(uri)) return { uri, derived: false }; } return null;}
/** The first location's display name, if any. */export function primaryLocationName(event: Pick<EventPlaces, 'locations'>): string | undefined { const name = Object.values(event.locations ?? {})[0]?.name?.trim(); return name || undefined;}
// "Microsoft Teams Meeting", "Réunion Microsoft Teams", "Zoom", "Google Meet"…// A location like this names the service, not a place: tapping it should join// the meeting, never search the maps for it. The whole label must be a service// name, so "Salle Teams" or "Studio Zoom" stay places.const MEETING_LABELS = new Set([ 'microsoft teams', 'teams', 'zoom', 'google meet', 'meet', 'webex', 'jitsi', 'whereby', 'gotomeeting', 'goto', 'visio', 'visioconference', 'online', 'en ligne',]);
export function isMeetingLabel(location: string): boolean { const label = location .normalize('NFD').replace(/[\u0300-\u036f]/g, '') .toLowerCase() .replace(/[^a-z ]+/g, ' ') .replace(/\s+/g, ' ') .trim() .replace(/^(reunion|meeting|online meeting|appel|call) /, '') .replace(/ (meeting|reunion|call)$/, ''); return MEETING_LABELS.has(label);}
export type LocationAction = | { kind: 'url'; uri: string } | { kind: 'maps'; query: string };
/** * What tapping the location does: open it when it is a URL, join the meeting * when it only names the service, search the maps otherwise. */export function locationAction(location: string, meeting: MeetingLink | null): LocationAction { const value = location.trim(); if (isHttpUrl(value)) return { kind: 'url', uri: value }; if (meeting && isMeetingLabel(value)) return { kind: 'url', uri: meeting.uri }; return { kind: 'maps', query: value };}
/** * A maps URL for a free-text address. Android's `geo:` intent opens Google * Maps (or the user's chosen maps app) straight on the search; elsewhere the * Google Maps universal link opens the app when installed, the site otherwise. */export function mapsUrl(query: string, platform: string): string { const q = encodeURIComponent(query.replace(/\s+/g, ' ').trim()); if (platform === 'android') return `geo:0,0?q=${q}`; return `https://www.google.com/maps/search/?api=1&query=${q}`;}