snort/src/Element/Text.tsx

221 lines
7.8 KiB
TypeScript
Raw Normal View History

2023-01-20 11:11:50 +00:00
import './Text.css'
import { useMemo } from "react";
2023-01-06 11:05:27 +00:00
import { Link } from "react-router-dom";
2023-01-14 01:39:20 +00:00
import ReactMarkdown from "react-markdown";
2023-01-11 20:23:19 +00:00
import { TwitterTweetEmbed } from "react-twitter-embed";
2023-01-06 11:05:27 +00:00
2023-01-25 01:01:16 +00:00
import { UrlRegex, FileExtensionRegex, MentionRegex, InvoiceRegex, YoutubeUrlRegex, TweetUrlRegex, HashtagRegex, TidalRegex, SoundCloudRegex, MixCloudRegex } from "Const";
2023-01-20 11:11:50 +00:00
import { eventLink, hexToBech32 } from "Util";
import Invoice from "Element/Invoice";
import Hashtag from "Element/Hashtag";
2023-01-14 14:37:31 +00:00
2023-01-20 11:11:50 +00:00
import Tag from "Nostr/Tag";
import { MetadataCache } from "Db/User";
import Mention from "Element/Mention";
2023-01-19 19:51:36 +00:00
import TidalEmbed from "Element/TidalEmbed";
2023-01-20 17:07:14 +00:00
import { useSelector } from 'react-redux';
import { RootState } from 'State/Store';
import { UserPreferences } from 'State/Login';
2023-01-21 15:15:18 +00:00
import SoundCloudEmbed from 'Element/SoundCloudEmded'
2023-01-25 01:01:16 +00:00
import MixCloudEmbed from './MixCloudEmbed';
2023-01-06 11:05:27 +00:00
2023-01-20 17:07:14 +00:00
function transformHttpLink(a: string, pref: UserPreferences) {
try {
2023-01-20 17:07:14 +00:00
if (!pref.autoLoadMedia) {
return <a href={a} onClick={(e) => e.stopPropagation()} target="_blank" rel="noreferrer" className="ext">{a}</a>
}
const url = new URL(a);
const youtubeId = YoutubeUrlRegex.test(a) && RegExp.$1;
const tweetId = TweetUrlRegex.test(a) && RegExp.$2;
2023-01-19 19:51:36 +00:00
const tidalId = TidalRegex.test(a) && RegExp.$1;
2023-01-21 15:15:18 +00:00
const soundcloundId = SoundCloudRegex.test(a) && RegExp.$1;
2023-01-25 01:01:16 +00:00
const mixcloudId = MixCloudRegex.test(a) && RegExp.$1;
const extension = FileExtensionRegex.test(url.pathname.toLowerCase()) && RegExp.$1;
if (extension) {
switch (extension) {
case "gif":
case "jpg":
case "jpeg":
case "png":
case "bmp":
case "webp": {
2023-01-17 14:00:59 +00:00
return <img key={url.toString()} src={url.toString()} />;
}
2023-01-26 18:30:07 +00:00
case "wav":
case "mp3":
case "ogg": {
return <audio key={url.toString()} src={url.toString()} controls />
}
case "mp4":
case "mov":
case "mkv":
case "avi":
case "m4v": {
2023-01-16 17:48:25 +00:00
return <video key={url.toString()} src={url.toString()} controls />
}
default:
2023-01-19 15:37:47 +00:00
return <a key={url.toString()} href={url.toString()} onClick={(e) => e.stopPropagation()} target="_blank" rel="noreferrer" className="ext">{url.toString()}</a>
2023-01-14 01:39:20 +00:00
}
} else if (tweetId) {
return (
<div className="tweet" key={tweetId}>
<TwitterTweetEmbed tweetId={tweetId} />
</div>
)
} else if (youtubeId) {
return (
<>
<br />
<iframe
className="w-max"
src={`https://www.youtube.com/embed/${youtubeId}`}
title="YouTube video player"
key={youtubeId}
frameBorder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
2023-01-16 17:48:25 +00:00
allowFullScreen={true}
/>
<br />
</>
)
2023-01-19 19:51:36 +00:00
} else if (tidalId) {
return <TidalEmbed link={a} />
2023-01-21 15:15:18 +00:00
} else if (soundcloundId){
return <SoundCloudEmbed link={a} />
2023-01-25 01:01:16 +00:00
} else if (mixcloudId){
return <MixCloudEmbed link={a} />
} else {
2023-01-19 15:37:47 +00:00
return <a href={a} onClick={(e) => e.stopPropagation()} target="_blank" rel="noreferrer" className="ext">{a}</a>
2023-01-06 20:38:51 +00:00
}
} catch (error) {
2023-01-06 20:38:51 +00:00
}
2023-01-19 15:37:47 +00:00
return <a href={a} onClick={(e) => e.stopPropagation()} target="_blank" rel="noreferrer" className="ext">{a}</a>
2023-01-06 20:38:51 +00:00
}
2023-01-20 17:07:14 +00:00
function extractLinks(fragments: Fragment[], pref: UserPreferences) {
2023-01-06 11:05:27 +00:00
return fragments.map(f => {
if (typeof f === "string") {
return f.split(UrlRegex).map(a => {
if (a.startsWith("http")) {
2023-01-20 17:07:14 +00:00
return transformHttpLink(a, pref)
2023-01-06 11:05:27 +00:00
}
return a;
});
}
return f;
}).flat();
}
2023-01-20 17:07:14 +00:00
function extractMentions(frag: TextFragment) {
return frag.body.map(f => {
2023-01-06 11:05:27 +00:00
if (typeof f === "string") {
return f.split(MentionRegex).map((match) => {
let matchTag = match.match(/#\[(\d+)\]/);
if (matchTag && matchTag.length === 2) {
let idx = parseInt(matchTag[1]);
2023-01-20 17:07:14 +00:00
let ref = frag.tags?.find(a => a.Index === idx);
2023-01-06 11:05:27 +00:00
if (ref) {
switch (ref.Key) {
case "p": {
2023-01-16 22:22:21 +00:00
return <Mention pubkey={ref.PubKey!} />
2023-01-06 11:05:27 +00:00
}
case "e": {
2023-01-16 17:48:25 +00:00
let eText = hexToBech32("note", ref.Event!).substring(0, 12);
return <Link key={ref.Event} to={eventLink(ref.Event!)} onClick={(e) => e.stopPropagation()}>#{eText}</Link>;
2023-01-06 11:05:27 +00:00
}
case "t": {
return <Hashtag tag={ref.Hashtag!} />
}
2023-01-06 11:05:27 +00:00
}
}
return <b style={{ color: "var(--error)" }}>{matchTag[0]}?</b>;
2023-01-06 11:05:27 +00:00
} else {
return match;
}
});
}
return f;
}).flat();
}
2023-01-16 17:48:25 +00:00
function extractInvoices(fragments: Fragment[]) {
2023-01-06 11:05:27 +00:00
return fragments.map(f => {
if (typeof f === "string") {
return f.split(InvoiceRegex).map(i => {
if (i.toLowerCase().startsWith("lnbc")) {
return <Invoice key={i} invoice={i} />
} else {
return i;
}
});
}
return f;
}).flat();
}
2023-01-10 21:31:08 +00:00
2023-01-16 17:48:25 +00:00
function extractHashtags(fragments: Fragment[]) {
2023-01-10 21:31:08 +00:00
return fragments.map(f => {
if (typeof f === "string") {
return f.split(HashtagRegex).map(i => {
if (i.toLowerCase().startsWith("#")) {
return <Hashtag tag={i.substring(1)} />
2023-01-10 21:31:08 +00:00
} else {
return i;
}
});
}
return f;
}).flat();
}
2023-01-12 09:48:39 +00:00
2023-01-20 17:07:14 +00:00
function transformLi(frag: TextFragment) {
let fragments = transformText(frag)
2023-01-14 18:50:24 +00:00
return <li>{fragments}</li>
2023-01-14 01:39:20 +00:00
}
2023-01-12 09:48:39 +00:00
2023-01-20 17:07:14 +00:00
function transformParagraph(frag: TextFragment) {
const fragments = transformText(frag)
2023-01-14 18:50:24 +00:00
if (fragments.every(f => typeof f === 'string')) {
return <p>{fragments}</p>
}
return <>{fragments}</>
2023-01-14 01:39:20 +00:00
}
2023-01-20 17:07:14 +00:00
function transformText(frag: TextFragment) {
if (frag.body === undefined) {
debugger;
}
2023-01-20 17:07:14 +00:00
let fragments = extractMentions(frag);
fragments = extractLinks(fragments, frag.pref);
2023-01-14 01:39:20 +00:00
fragments = extractInvoices(fragments);
fragments = extractHashtags(fragments);
return fragments;
}
2023-01-16 17:48:25 +00:00
export type Fragment = string | JSX.Element;
export interface TextFragment {
body: Fragment[],
tags: Tag[],
2023-01-20 17:07:14 +00:00
users: Map<string, MetadataCache>,
pref: UserPreferences
2023-01-16 17:48:25 +00:00
}
export interface TextProps {
content: string,
tags: Tag[],
users: Map<string, MetadataCache>
}
export default function Text({ content, tags, users }: TextProps) {
2023-01-20 17:07:14 +00:00
const pref = useSelector<RootState, UserPreferences>(s => s.login.preferences);
2023-01-14 18:50:24 +00:00
const components = useMemo(() => {
return {
2023-01-20 17:07:14 +00:00
p: (x: any) => transformParagraph({ body: x.children ?? [], tags, users, pref }),
a: (x: any) => transformHttpLink(x.href, pref),
li: (x: any) => transformLi({ body: x.children ?? [], tags, users, pref }),
2023-01-14 18:50:24 +00:00
};
}, [content]);
2023-01-14 14:37:31 +00:00
return <ReactMarkdown className="text" components={components}>{content}</ReactMarkdown>
2023-01-14 01:39:20 +00:00
}