@acrsolutions/chat-components
Version:
Un set di componenti per la creazione di un'applicazione di messaggistica
54 lines (53 loc) • 2.33 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { useMemo } from 'react';
import { StyledLink } from '../ui/StyledLink';
import { HighlightResearch } from './HighlightResearch';
/**
* Questo componente renderizza il testo del messaggio.
* Utilizza il componente HighlightResearch per evidenziare le parole in base alla stringa di ricerca.
* Supporta anche i link, che vengono evidenziati e resi cliccabili.
*/
export function MessageTextRender({
/**
* Il testo del messaggio
*/
text,
/**
* La stringa di ricerca per evidenziare le parole
*/
searchString,
/**
* La funzione che riceve i riferimenti per l'evidenziazione
*/
onSearchRefs,
/**
* L'indice del riferimento attualmente focalizzato
*/
focusedRefIndex,
/**
* La chiave del riferimento attualmente focalizzato
*/
focusedKey, }) {
const cleanedWordsList = useMemo(() => {
const wordsList = text.includes(' ') ? text.split(' ') : [text];
return wordsList.map((word) => {
if (word.includes('www') ||
(word.includes('http') && !word.startsWith('<'))) {
return `<${word}>`;
}
return `${word} `;
});
}, [text]);
return (_jsx(_Fragment, { children: cleanedWordsList.map((word, i) => {
if (!word || word == ' ')
return;
const focusedKeySection = focusedKey?.split('_')[3];
if (word.startsWith('<') && word.includes('>')) {
const href = word.replace(/<|>/g, '');
return (_jsx(StyledLink, { "$inlineElement": true, href: href, target: "_blank", children: onSearchRefs && focusedRefIndex != null ? (_jsx(HighlightResearch, { onSearchRefs: onSearchRefs, searchString: searchString, text: href + ' ', focusedRefIndex: focusedRefIndex, focusedKeyParentIndexSection: focusedKeySection, parentIndex: i })) : (href + ' ') }, word + i));
}
else {
return (_jsx("span", { style: { whiteSpace: 'pre-wrap' }, children: onSearchRefs && focusedRefIndex != null ? (_jsx(HighlightResearch, { onSearchRefs: onSearchRefs, searchString: searchString, text: word, focusedRefIndex: focusedRefIndex, focusedKeyParentIndexSection: focusedKeySection, parentIndex: i })) : (word) }, word + i));
}
}) }));
}