fix: render single images without gallery

This commit is contained in:
Kieran 2023-10-05 14:36:29 +01:00
parent 4d12de302d
commit 77016cfbc7
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941

View File

@ -1,5 +1,5 @@
import "./Text.css"; import "./Text.css";
import { useState } from "react"; import { ReactNode, useState } from "react";
import { HexKey, ParsedFragment } from "@snort/system"; import { HexKey, ParsedFragment } from "@snort/system";
import Invoice from "Element/Embed/Invoice"; import Invoice from "Element/Embed/Invoice";
@ -155,16 +155,15 @@ export default function Text({
const renderContent = () => { const renderContent = () => {
let lenCtr = 0; let lenCtr = 0;
let chunks: (JSX.Element | null)[] = []; const chunks: Array<ReactNode> = [];
for (let i = 0; i < elements.length; i++) { for (let i = 0; i < elements.length; i++) {
const element = elements[i]; const element = elements[i];
if (truncate) { if (truncate) {
if (lenCtr > truncate) { if (lenCtr + element.content.length > truncate) {
continue;
} else if (lenCtr + element.content.length > truncate) {
lenCtr += element.content.length; lenCtr += element.content.length;
chunks = chunks.concat(<div className="text-frag">{element.content.slice(0, truncate - lenCtr)}...</div>); chunks.push(<div className="text-frag">{element.content.slice(0, truncate - lenCtr)}...</div>);
return chunks;
} else { } else {
lenCtr += element.content.length; lenCtr += element.content.length;
} }
@ -172,9 +171,9 @@ export default function Text({
if (element.type === "media" && element.mimeType?.startsWith("image")) { if (element.type === "media" && element.mimeType?.startsWith("image")) {
if (disableMedia ?? false) { if (disableMedia ?? false) {
chunks = chunks.concat(<DisableMedia content={element.content} />); chunks.push(<DisableMedia content={element.content} />);
} else { } else {
let galleryImages: ParsedFragment[] = [element]; const galleryImages: ParsedFragment[] = [element];
// If the current element is of type media and mimeType starts with image, // If the current element is of type media and mimeType starts with image,
// we verify if the next elements are of the same type and mimeType and push to the galleryImages // we verify if the next elements are of the same type and mimeType and push to the galleryImages
// Whenever one of the next elements is not longer of the type we are looking for, we break the loop // Whenever one of the next elements is not longer of the type we are looking for, we break the loop
@ -182,13 +181,15 @@ export default function Text({
const nextElement = elements[j + 1]; const nextElement = elements[j + 1];
if (nextElement && nextElement.type === "media" && nextElement.mimeType?.startsWith("image")) { if (nextElement && nextElement.type === "media" && nextElement.mimeType?.startsWith("image")) {
galleryImages = galleryImages.concat(nextElement); galleryImages.push(nextElement);
i++; i++;
} else { } else {
break; break;
} }
} }
if (galleryImages.length === 1) {
chunks.push(<RevealMediaInstance content={galleryImages[0].content} />);
} else {
// We build a grid layout to render the grouped images // We build a grid layout to render the grouped images
const imagesWithGridConfig = galleryImages.map((gi, index) => { const imagesWithGridConfig = galleryImages.map((gi, index) => {
const config = gridConfigMap.get(galleryImages.length); const config = gridConfigMap.get(galleryImages.length);
@ -221,7 +222,8 @@ export default function Text({
))} ))}
</div> </div>
); );
chunks = chunks.concat(gallery); chunks.push(gallery);
}
} }
} }
@ -230,30 +232,30 @@ export default function Text({
(element.mimeType?.startsWith("audio") || element.mimeType?.startsWith("video")) (element.mimeType?.startsWith("audio") || element.mimeType?.startsWith("video"))
) { ) {
if (disableMedia ?? false) { if (disableMedia ?? false) {
chunks = chunks.concat(<DisableMedia content={element.content} />); chunks.push(<DisableMedia content={element.content} />);
} else { } else {
chunks = chunks.concat(<RevealMediaInstance content={element.content} />); chunks.push(<RevealMediaInstance content={element.content} />);
} }
} }
if (element.type === "invoice") { if (element.type === "invoice") {
chunks = chunks.concat(<Invoice invoice={element.content} />); chunks.push(<Invoice invoice={element.content} />);
} }
if (element.type === "hashtag") { if (element.type === "hashtag") {
chunks = chunks.concat(<Hashtag tag={element.content} />); chunks.push(<Hashtag tag={element.content} />);
} }
if (element.type === "cashu") { if (element.type === "cashu") {
chunks = chunks.concat(<CashuNuts token={element.content} />); chunks.push(<CashuNuts token={element.content} />);
} }
if (element.type === "link" || (element.type === "media" && element.mimeType?.startsWith("unknown"))) { if (element.type === "link" || (element.type === "media" && element.mimeType?.startsWith("unknown"))) {
chunks = chunks.concat( chunks.push(
<HyperText link={element.content} depth={depth} showLinkPreview={!(disableLinkPreview ?? false)} />, <HyperText link={element.content} depth={depth} showLinkPreview={!(disableLinkPreview ?? false)} />,
); );
} }
if (element.type === "custom_emoji") { if (element.type === "custom_emoji") {
chunks = chunks.concat(<ProxyImg src={element.content} size={15} className="custom-emoji" />); chunks.push(<ProxyImg src={element.content} size={15} className="custom-emoji" />);
} }
if (element.type === "text") { if (element.type === "text") {
chunks = chunks.concat( chunks.push(
<div className="text-frag"> <div className="text-frag">
{highlighText ? renderContentWithHighlightedText(element.content, highlighText) : element.content} {highlighText ? renderContentWithHighlightedText(element.content, highlighText) : element.content}
</div>, </div>,