-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JNG-6014 image display component (#489)
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
judo-ui-react/src/main/resources/actor/src/components/widgets/ImageDisplay.tsx.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string | null>(null); | ||
const [altText, setAltText] = useState<string | null>(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 ( | ||
<Box sx={sx}> | ||
{effectiveImageUrl ? <img src={effectiveImageUrl} alt={altText as any} style={imageCSS} /> : null} | ||
</Box> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters