Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JNG-6014 image display component #489

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';

noherczeg marked this conversation as resolved.
Show resolved Hide resolved
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]);
noherczeg marked this conversation as resolved.
Show resolved Hide resolved

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>
);
}
noherczeg marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions judo-ui-react/src/main/resources/ui-react.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading