/** * Shared HTML builder for contact links (phone, Telegram) used by day detail, * current duty, and duty list. */ import { t } from "./i18n.js"; import { escapeHtml } from "./utils.js"; /** * Build HTML for contact links (phone, Telegram username). * Validates phone/username, builds tel: and t.me hrefs, wraps in spans/links. * * @param {'ru'|'en'} lang - UI language for labels (when showLabels is true) * @param {string|null|undefined} phone - Phone number * @param {string|null|undefined} username - Telegram username with or without leading @ * @param {object} options - Rendering options * @param {string} options.classPrefix - CSS class prefix (e.g. "day-detail-contact", "duty-contact") * @param {boolean} [options.showLabels=true] - Whether to show "Phone:" / "Telegram:" labels * @param {string} [options.separator=' '] - Separator between contact parts (e.g. " ", " ยท ") * @returns {string} HTML string or "" if no valid contact */ export function buildContactLinksHtml(lang, phone, username, options) { const { classPrefix, showLabels = true, separator = " " } = options || {}; const parts = []; if (phone && String(phone).trim()) { const p = String(phone).trim(); const safeHref = "tel:" + p.replace(/&/g, "&").replace(/"/g, """).replace(/' + escapeHtml(p) + ""; if (showLabels) { const label = t(lang, "contact.phone"); parts.push( '' + escapeHtml(label) + ": " + linkHtml + "" ); } else { parts.push(linkHtml); } } if (username && String(username).trim()) { const u = String(username).trim().replace(/^@+/, ""); if (u) { const display = "@" + u; const href = "https://t.me/" + encodeURIComponent(u); const linkHtml = '' + escapeHtml(display) + ""; if (showLabels) { const label = t(lang, "contact.telegram"); parts.push( '' + escapeHtml(label) + ": " + linkHtml + "" ); } else { parts.push(linkHtml); } } } if (parts.length === 0) return ""; const rowClass = classPrefix + "-row"; return '
' + parts.join(separator) + "
"; }