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

fix: situation when screenshot loading fails #2876

Merged
merged 1 commit into from
Jan 28, 2025
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
5 changes: 5 additions & 0 deletions webapp/src/fixtures/useImagePreload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export const useImagePreload = ({ src, enabled = true }: Props) => {
setIsLoading(false);
}
};
img.onerror = () => {
if (mounted) {
setIsLoading(false);
}
};
return () => {
mounted = false;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ import { useWindowSize } from 'usehooks-ts';
import { scaleImage, useImagePreload } from 'tg.fixtures/useImagePreload';
import { BoxLoading } from 'tg.component/common/BoxLoading';

const SCREENSHOT_DETAIL_SIZE = 0.8;

const StyledContainer = styled('div')`
display: grid;
align-content: center;
justify-content: center;
overflow: hidden;
position: relative;

&::after {
content: '';
Expand Down Expand Up @@ -77,21 +80,29 @@ export const ScreenshotDetail: React.FC<ScreenshotDetailProps> = ({
const itemsCount = screenshots.length;
const screenshot = screenshots[index];
const multiple = screenshots.length > 1;
const viewPort = useWindowSize();
const maxDialogSize = {
width: viewPort.width * SCREENSHOT_DETAIL_SIZE,
height: viewPort.width * SCREENSHOT_DETAIL_SIZE,
};

const { size, isLoading } = useImagePreload({
const { size: loadedSize, isLoading } = useImagePreload({
src: screenshot.src,
});

const viewPort = useWindowSize();
const screenshotSize = {
width: screenshot.width || loadedSize.width || 0,
height: screenshot.height || loadedSize.height || 0,
};

const scaledSize = scaleImage(size, {
width: viewPort.width * 0.8,
height: viewPort.height * 0.8,
});
const scaledSize = scaleImage(screenshotSize, maxDialogSize);

const width = scaledSize.width || maxDialogSize.width;
const height = scaledSize.height || maxDialogSize.height;

let scaleMarkers = 1;
if (viewPort.width < screenshot.width!) {
scaleMarkers = screenshot.width! / viewPort.width;
if (screenshot.width && width < screenshot.width) {
scaleMarkers = screenshot.width / width;
}

function moveLeft() {
Expand Down Expand Up @@ -121,8 +132,8 @@ export const ScreenshotDetail: React.FC<ScreenshotDetailProps> = ({
>
<StyledContainer
style={{
width: Math.max(scaledSize.width, 100),
height: Math.max(scaledSize.height, 100),
width: width,
height: height,
}}
className={clsx({ loading: isLoading })}
>
Expand All @@ -146,13 +157,13 @@ export const ScreenshotDetail: React.FC<ScreenshotDetailProps> = ({
showTooltips
screenshot={{
...screenshot,
width: size.width,
height: size.height,
width: screenshotSize.width,
height: screenshotSize.height,
}}
showSecondaryHighlights
style={{
width: scaledSize.width,
height: scaledSize.height,
width,
height,
maxWidth: 'unset',
maxHeight: 'unset',
}}
Expand Down
Loading