TheCodedProf | da197f2 | 2023-03-01 18:15:51 -0500 | [diff] [blame] | 1 | import Styles from "../../styles/Components/transcripts.module.css" |
| 2 | import Image from "next/image"; |
TheCodedProf | 2a69bd9 | 2023-03-02 16:36:32 -0500 | [diff] [blame^] | 3 | import Showdown from "showdown"; |
| 4 | import Axios from "axios"; |
| 5 | import { useEffect, useState } from "react"; |
TheCodedProf | c86e44b | 2023-02-28 17:30:57 -0500 | [diff] [blame] | 6 | |
TheCodedProf | 2a69bd9 | 2023-03-02 16:36:32 -0500 | [diff] [blame^] | 7 | const bold = /(\*\*.+\*\*)/g; |
| 8 | const italic = /(\*.+\*|\_.+\_)/g; |
| 9 | const emoji = /(<a?:.+:\D+>)/g; |
| 10 | const user = /(<@!?\d+>)/g; |
| 11 | const underline = /(\_\_.+\_\_)/g; |
| 12 | const strikethrough = /(\~\~.+\~\~)/g; |
| 13 | const code = /(\`.+\`)/g; |
| 14 | const codeBlock = /(\`\`\`.+\`\`\`)/g; |
| 15 | const spoiler = /(\`\`\`.+\`\`\`)/g; |
| 16 | |
| 17 | const regex = /(\*\*\*.+\*\*\*|\*\*.+\*\*|\*.+\*|\_.+\_|\_\_.+\_\_|\~\~.+\~\~|\`.+\`|\`\`\`.+\`\`\`|\`\`\`.+\`\`\`|<a?:.+:\D+>|<@!?\d+>)/g; |
| 18 | |
| 19 | const converter = new Showdown.Converter(); |
| 20 | |
| 21 | async function parse(text) { |
| 22 | const splitText = text.split(regex); |
| 23 | console.log(splitText) |
| 24 | return await splitText.map( async (item, index) => { |
| 25 | if (item.match(bold)) { |
| 26 | return <b key={index}>{item.replaceAll(/\*\*/g, '')}</b> |
| 27 | } |
| 28 | if (item.match(underline)) { |
| 29 | return <u key={index}>{item.replaceAll(/\_\_/g, '')}</u> |
| 30 | } |
| 31 | if (item.match(italic)) { |
| 32 | return <i key={index}>{item.replaceAll(/\*/g, '')}</i> |
| 33 | } |
| 34 | if (item.match(code)) { |
| 35 | return <code key={index}>{item.replaceAll(/\`/g, '')}</code> |
| 36 | } |
| 37 | if (item.match(emoji)) { |
| 38 | return <Image key={index} src={`https://cdn.discord.com/emojis/${item.replaceAll(/\D/g, '')}`} width={20} height={20} alt="" /> |
| 39 | } |
| 40 | if (item.match(user)) { |
| 41 | const username = (await Axios.get(`http://localhost:10000/users/${item.replaceAll(/\D/g, '')}`)).data; |
| 42 | console.log(username) |
| 43 | return <>{username}</> |
| 44 | |
| 45 | } |
| 46 | return item |
| 47 | }) |
| 48 | |
| 49 | |
| 50 | } |
| 51 | |
| 52 | function Embed(props) { |
TheCodedProf | c86e44b | 2023-02-28 17:30:57 -0500 | [diff] [blame] | 53 | |
TheCodedProf | da197f2 | 2023-03-01 18:15:51 -0500 | [diff] [blame] | 54 | let description = props.embed.description; |
| 55 | if (description) { |
| 56 | description = description.split("\n"); |
| 57 | } |
| 58 | |
TheCodedProf | 2a69bd9 | 2023-03-02 16:36:32 -0500 | [diff] [blame^] | 59 | const [newDesc, setNewDesc] = useState([]); |
| 60 | |
| 61 | useEffect(() => { |
| 62 | async function stuff() { |
| 63 | const parsed = await parse(props.embed.description); |
| 64 | setNewDesc(newDesc => [...newDesc, parsed]); |
| 65 | } |
| 66 | stuff(); |
| 67 | }) |
| 68 | |
| 69 | return ( |
TheCodedProf | da197f2 | 2023-03-01 18:15:51 -0500 | [diff] [blame] | 70 | <div className={Styles.embed} style={{borderColor: props.color ?? "#F27878"}}> |
| 71 | { |
| 72 | props.embed.author ? |
| 73 | <div className={Styles.embedAuthor}> |
| 74 | { |
| 75 | props.embed.author.iconURL ? |
| 76 | <Image src={props.embed.author.iconURL} alt=""/> : |
| 77 | null |
| 78 | } |
| 79 | { |
| 80 | props.embed.author.url ? |
| 81 | <a href={props.embed.author.url}> |
| 82 | {props.embed.author?.name ?? ""} |
| 83 | </a> : |
| 84 | <p>{props.embed.author?.name ?? ""}</p> |
| 85 | } |
| 86 | </div> : |
| 87 | null |
| 88 | } |
| 89 | { |
| 90 | props.embed.title ? |
| 91 | <div className={Styles.embedTitle}>{props.embed.title}</div> : |
| 92 | null |
| 93 | } |
| 94 | { |
| 95 | props.embed.description ? |
| 96 | <div className={Styles.embedDescription}> |
| 97 | { |
TheCodedProf | 2a69bd9 | 2023-03-02 16:36:32 -0500 | [diff] [blame^] | 98 | newDesc.map((item) => { |
| 99 | return item; |
TheCodedProf | da197f2 | 2023-03-01 18:15:51 -0500 | [diff] [blame] | 100 | }) |
| 101 | } |
| 102 | </div> |
| 103 | : null |
| 104 | } |
| 105 | </div> |
| 106 | ) |
TheCodedProf | c86e44b | 2023-02-28 17:30:57 -0500 | [diff] [blame] | 107 | } |
| 108 | |
TheCodedProf | 2a69bd9 | 2023-03-02 16:36:32 -0500 | [diff] [blame^] | 109 | export default Embed; |