From b7c2b3d4cb6ebab51da9790477f78704e3e3a15f Mon Sep 17 00:00:00 2001 From: Enrico Ros Date: Tue, 11 Jun 2024 12:53:55 -0700 Subject: [PATCH] RenderImageURL: improve greatly --- src/modules/blocks/BlocksRenderer.tsx | 2 +- src/modules/blocks/RenderImageURL.tsx | 120 +++++++++++++++++++------- 2 files changed, 92 insertions(+), 30 deletions(-) diff --git a/src/modules/blocks/BlocksRenderer.tsx b/src/modules/blocks/BlocksRenderer.tsx index 4fa99a8a8..dfe7c0ee1 100644 --- a/src/modules/blocks/BlocksRenderer.tsx +++ b/src/modules/blocks/BlocksRenderer.tsx @@ -194,7 +194,7 @@ export const BlocksRenderer = React.forwardRef : block.type === 'imageb' - ? + ? : block.type === 'diffb' ? : (props.renderTextAsMarkdown && !fromSystem && !isUserCommand) diff --git a/src/modules/blocks/RenderImageURL.tsx b/src/modules/blocks/RenderImageURL.tsx index 860824393..6a1c44817 100644 --- a/src/modules/blocks/RenderImageURL.tsx +++ b/src/modules/blocks/RenderImageURL.tsx @@ -71,23 +71,32 @@ export function heuristicLegacyImageBlocks(fullText: string): ImageBlock[] | nul export const RenderImageURL = (props: { - imageBlock: ImageBlock, - noTooltip?: boolean, - onRunAgain?: (e: React.MouseEvent) => void, sx?: SxProps, + imageURL: string | null, // remote URL, or data URL + description?: React.ReactNode, + infoText?: string, + onOpenInNewTab?: (e: React.MouseEvent) => void, + onRunAgain?: (e: React.MouseEvent) => void, + sx?: SxProps, }) => { // state + const [loadingTimeout, setLoadingTimeout] = React.useState(false); const [infoOpen, setInfoOpen] = React.useState(false); - const [showAlert, setShowAlert] = React.useState(true); + const [showDalleAlert, setShowDalleAlert] = React.useState(true); + + // Effect + React.useEffect(() => { + const timeout = setTimeout(() => setLoadingTimeout(true), 2000); + return () => clearTimeout(timeout); + }, []); // derived state - const { url, alt } = props.imageBlock; - const isTempDalleUrl = url.startsWith('https://oaidalle'); + const isTempDalleUrl = props.imageURL?.startsWith('https://oaidalle') || false; return ( - + .overlay-buttons': { opacity: 1 }, - ...props.sx, }} > - {/* External Image */} - - {alt - + {/* Main */} + + + {/* Image / Loading Indicator */} + {props.imageURL ? ( + + {/* eslint-disable-next-line @next/next/no-img-element */} + {props.infoText + + ) : ( + + {loadingTimeout ? 'Error: Could not load image' : 'Loading...'} + + )} + + {/* Description Overlay */} + {!!props.description && ( + + {props.description} + + )} + {/* Information */} - {!!alt && infoOpen && ( - - {alt} + {!!props.infoText && infoOpen && ( + + {props.infoText} )} @@ -134,7 +187,7 @@ export const RenderImageURL = (props: { )} - {!!alt && ( + {(!!props.infoText || !!props.description) && ( setInfoOpen(open => !open)}> @@ -142,21 +195,30 @@ export const RenderImageURL = (props: { )} - - - - - + {!!props.imageURL && ( + + {props.onOpenInNewTab ? ( + + + + ) : props.imageURL.startsWith('http') ? ( + + + + ) : } + + )} - {/* Dalle Warning notice */} - {isTempDalleUrl && showAlert && ( + + {/* (Remove in 2025) Dalle Warning notice */} + {isTempDalleUrl && showDalleAlert && ( } endDecorator={ - setShowAlert(on => !on)} sx={{ my: -0.5 }}> + setShowDalleAlert(on => !on)} sx={{ my: -0.5 }}> } @@ -173,4 +235,4 @@ export const RenderImageURL = (props: { ); -}; \ No newline at end of file +};