import { NOSTR_MENTIONS } from "@lume/utils"; import { nanoid } from "nanoid"; import { ReactNode, useMemo } from "react"; import { useTranslation } from "react-i18next"; import reactStringReplace from "react-string-replace"; import { User } from "../user"; import { Hashtag } from "./mentions/hashtag"; import { MentionUser } from "./mentions/user"; import { useEvent } from "@lume/ark"; export function NoteChild({ eventId, isRoot, }: { eventId: string; isRoot?: boolean; }) { const { t } = useTranslation(); const { isLoading, isError, data } = useEvent(eventId); const richContent = useMemo(() => { if (!data) return ""; let parsedContent: string | ReactNode[] = data.content.replace( /\n+/g, "\n", ); const text = parsedContent as string; const words = text.split(/( |\n)/); const hashtags = words.filter((word) => word.startsWith("#")); const mentions = words.filter((word) => NOSTR_MENTIONS.some((el) => word.startsWith(el)), ); try { if (hashtags.length) { for (const hashtag of hashtags) { const regex = new RegExp(`(|^)${hashtag}\\b`, "g"); parsedContent = reactStringReplace(parsedContent, regex, () => { return ; }); } } if (mentions.length) { for (const mention of mentions) { parsedContent = reactStringReplace( parsedContent, mention, (match, i) => , ); } } parsedContent = reactStringReplace( parsedContent, /(https?:\/\/\S+)/g, (match, i) => { const url = new URL(match); return ( {url.toString()} ); }, ); return parsedContent; } catch (e) { console.log(e); return parsedContent; } }, [data]); if (isLoading) { return (
); } if (isError || !data) { return (
{t("note.error")}
); } return (
{richContent}
{isRoot ? t("note.posted") : t("note.replied")}:
); }