import { cn } from "@lume/utils"; import { useRouteContext } from "@tanstack/react-router"; import { nanoid } from "nanoid"; import { type ReactNode, useMemo } from "react"; import reactStringReplace from "react-string-replace"; import { Hashtag } from "./mentions/hashtag"; import { MentionNote } from "./mentions/note"; import { MentionUser } from "./mentions/user"; import { Images } from "./preview/images"; import { Videos } from "./preview/videos"; import { useNoteContext } from "./provider"; export function NoteContent({ quote = true, mention = true, clean, className, }: { quote?: boolean; mention?: boolean; clean?: boolean; className?: string; }) { const { settings } = useRouteContext({ strict: false }); const event = useNoteContext(); const content = useMemo(() => { try { // Get parsed meta const { content, hashtags, events, mentions } = event.meta; // Define rich content let richContent: ReactNode[] | string = settings.display_media ? content : event.content; for (const hashtag of hashtags) { const regex = new RegExp(`(|^)${hashtag}\\b`, "g"); richContent = reactStringReplace(richContent, regex, (_, index) => { return ; }); } for (const event of events) { if (quote) { richContent = reactStringReplace(richContent, event, (_, index) => ( )); } if (!quote && clean) { richContent = reactStringReplace(richContent, event, () => null); } } for (const user of mentions) { if (mention) { richContent = reactStringReplace(richContent, user, (_, index) => ( )); } if (!mention && clean) { richContent = reactStringReplace(richContent, user, () => null); } } richContent = reactStringReplace( richContent, /(https?:\/\/\S+)/gi, (match, index) => ( {match} ), ); richContent = reactStringReplace(richContent, /(\r\n|\r|\n)+/g, () => (
)); return richContent; } catch (e) { console.log("[parser]: ", e); return event.content; } }, [event.content]); return (
400 ? "max-h-[250px] gradient-mask-b-0" : "", className, )} > {content}
{settings.display_media && event.meta?.images.length ? ( ) : null} {settings.display_media && event.meta?.videos.length ? ( ) : null}
); }