From 94f10fb7c2417a72be55271093ad8687dd0b0481 Mon Sep 17 00:00:00 2001 From: Norbert Csaba Herczeg Date: Thu, 14 Nov 2024 13:09:18 +0100 Subject: [PATCH] JNG-6014 image display component (#489) --- .../components/widgets/ImageDisplay.tsx.hbs | 52 +++++++++++++++++++ .../src/main/resources/ui-react.yaml | 4 ++ 2 files changed, 56 insertions(+) create mode 100644 judo-ui-react/src/main/resources/actor/src/components/widgets/ImageDisplay.tsx.hbs diff --git a/judo-ui-react/src/main/resources/actor/src/components/widgets/ImageDisplay.tsx.hbs b/judo-ui-react/src/main/resources/actor/src/components/widgets/ImageDisplay.tsx.hbs new file mode 100644 index 00000000..9a655ef4 --- /dev/null +++ b/judo-ui-react/src/main/resources/actor/src/components/widgets/ImageDisplay.tsx.hbs @@ -0,0 +1,52 @@ +import type { SxProps } from '@mui/material/styles'; +import Box from '@mui/system/Box'; +import { type CSSProperties, useEffect, useMemo, useState } from 'react'; +import { AccessServiceImpl } from '~/services/data-axios/AccessServiceImpl'; +import { judoAxiosProvider } from '~/services/data-axios/JudoAxiosProvider'; +import { fileHandling } from '~/utilities/file-handling'; + +export interface ImageDisplayProps { + token: string; + imageCSS?: CSSProperties; + sx?: SxProps; + fallbackUrl?: string; +} + +export function ImageDisplay({ token, imageCSS, sx, fallbackUrl }: ImageDisplayProps) { + const { extractFileNameFromToken } = fileHandling(); + + const [imageUrl, setImageUrl] = useState(null); + const [altText, setAltText] = useState(null); + + const accessServiceImpl = useMemo(() => new AccessServiceImpl(judoAxiosProvider), []); + + useEffect(() => { + (async () => { + try { + const response = await accessServiceImpl.downloadFile(token, 'inline'); + + if (response && response.status === 200) { + const fileType = response.headers['content-type']; + const url = window.URL.createObjectURL(new Blob([response.data], { type: fileType })); + setImageUrl(url); + setAltText(extractFileNameFromToken(token)); + } else { + setImageUrl(null); + setAltText(null); + } + } catch (error) { + console.error(error); + setImageUrl(null); + setAltText(null); + } + })(); + }, [token]); + + const effectiveImageUrl: string | undefined = useMemo(() => imageUrl || fallbackUrl, [imageUrl, fallbackUrl]); + + return ( + + {effectiveImageUrl ? {altText : null} + + ); +} diff --git a/judo-ui-react/src/main/resources/ui-react.yaml b/judo-ui-react/src/main/resources/ui-react.yaml index db8873ce..27e4c57c 100644 --- a/judo-ui-react/src/main/resources/ui-react.yaml +++ b/judo-ui-react/src/main/resources/ui-react.yaml @@ -509,6 +509,10 @@ templates: pathExpression: "'src/components/widgets/DefaultToolbar.tsx'" templateName: actor/src/components/widgets/DefaultToolbar.tsx.hbs + - name: actor/src/components/widgets/ImageDisplay.tsx + pathExpression: "'src/components/widgets/ImageDisplay.tsx'" + templateName: actor/src/components/widgets/ImageDisplay.tsx.hbs + # Actor - src - components-api - name: actor/src/components-api/components/Action.ts