import React from 'react'; import { defineMessages, FormattedMessage } from 'react-intl'; import { parsePhoneNumber } from 'libphonenumber-js/mobile'; import SendMessage from './send-message.jsx'; import LetterTile from './letter-tile.jsx'; import { TheCard } from 'tinode-sdk'; import { importVCard } from '../lib/blob-helpers.js'; const messages = defineMessages({ unknown_name: { id: 'unknown_name', defaultMessage: 'Unknown', description: 'Name to show when the name is missing' } }); export default class TheCardPreview extends React.PureComponent { constructor(props) { super(props); this.state = { content: props.content.object }; if (!props.content.object) { importVCard(props.content.file) .then(card => { this.setState({ content: card }); }); } this.handleSendCard = this.handleSendCard.bind(this); this.handleKeyDown = this.handleKeyDown.bind(this); } componentDidMount() { document.addEventListener('keydown', this.handleKeyDown); } componentWillUnmount() { document.removeEventListener('keydown', this.handleKeyDown); } handleKeyDown(e) { e.preventDefault(); if (e.key === 'Escape') { this.props.onClose(); } } handleSendCard() { this.props.onClose(); this.props.onSendMessage(this.props.content); } render() { const card = this.state.content; const fullName = TheCard.getFn(card); const photoUrl = TheCard.getPhotoUrl(card) || null; const note = card?.note; const org = TheCard.getOrg(card); // Get communication methods const emails = TheCard.getComm(card, 'email') || []; const phones = TheCard.getComm(card, 'tel') || []; const tinodeIds = TheCard.getComm(card, 'tinode') || []; const urls = TheCard.getComm(card, 'http') || []; const contacts = []; // Add emails emails.forEach((email, idx) => { const types = Array.isArray(email.des) ? email.des.join(', ') : ''; contacts.push(
{email.value} {types ? `(${types})` : ''}
); }); // Add phones phones.forEach((phone, idx) => { let val = phone.value; try { const number = parsePhoneNumber(phone.value); val = number ? number.formatInternational() : phone.value; } catch (err) { // Use original value if parsing fails } const types = Array.isArray(phone.des) ? phone.des.join(', ') : ''; contacts.push(
{val} {types ? `(${types})` : ''}
); }); // Add Tinode IDs tinodeIds.forEach((tid, idx) => { const types = Array.isArray(tid.des) ? tid.des.join(', ') : ''; contacts.push(
{tid.value} {types ? `(${types})` : ''}
); }); // Add URLs urls.forEach((url, idx) => { const types = Array.isArray(url.des) ? url.des.join(', ') : ''; contacts.push(
{url.value} {types ? `(${types})` : ''}
); }); return (
{e.preventDefault(); this.props.onClose();}}> close
{fullName || }
{org ?
{org}
: null} {note ?
{note}
: null} {contacts.length > 0 ?
{contacts}
: null}
); } }