import { Snippet, zSnippetTypes } from '../services/SnippetService'; /** * Format the snippet link based on the snippet type */ const link = (snippet: Snippet, utms?: string[]): string => { const url = (() => { if (snippet.type === zSnippetTypes.Enum.YouTube) { return `${snippet.url}&t=${Math.round(snippet.startSeconds || 1) - 1}s`; } if (snippet.type === zSnippetTypes.Enum.File) { return `${snippet.url}?pageNumber=${snippet.pageNumber}`; } return snippet.url; })(); return addUTMs(url, utms); }; /** * Add UTM parameters to a link */ const addUTMs = (url: string, utms?: string[]): string => { if (!utms || !utms.length) return url; const parsed = new URL(url); const params = utms.map((utm) => { const parts = utm.split('='); return { key: parts[0], value: parts[1], }; }); params.forEach(({ key, value }) => { if (!key || !value) return; parsed.searchParams.set(key, value); }); return String(parsed); }; const SnippetEdgeUtil = { link, addUTMs, }; export default SnippetEdgeUtil;