Skip to content
Closed
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
Expand Up @@ -100,6 +100,49 @@ describe("ImageResultsList", () => {
);
});

it("keys tiles by image identity when images share a page URL", async () => {
const gallery = "https://example.com/gallery";
render(
<MantineProvider>
<ImageResultsList
imageResults={[
[
"First gallery image",
gallery,
"https://cdn.example.com/first-thumb.gif",
"https://cdn.example.com/first.gif",
],
[
"Second gallery image",
gallery,
"https://cdn.example.com/second-thumb.gif",
"https://cdn.example.com/second.gif",
],
]}
/>
</MantineProvider>,
);

const [first, second] = await waitFor(() => {
const images = screen.getAllByRole("img");
expect(images).toHaveLength(2);
return images;
});

expect(getThumbnailSrc).toHaveBeenCalledWith(
"https://cdn.example.com/first-thumb.gif",
);
expect(getThumbnailSrc).toHaveBeenCalledWith(
"https://cdn.example.com/second-thumb.gif",
);
expect(first.getAttribute("src")).toBe(
thumbnailSrcFor("https://cdn.example.com/first-thumb.gif"),
);
expect(second.getAttribute("src")).toBe(
thumbnailSrcFor("https://cdn.example.com/second-thumb.gif"),
);
});

it("shows the host name for a result without a thumbnail", async () => {
render(
<MantineProvider>
Expand Down
162 changes: 86 additions & 76 deletions client/components/Search/Results/Graphical/ImageResultsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Captions from "yet-another-react-lightbox/plugins/captions";
import classes from "./ImageResultsList.module.css";
import "yet-another-react-lightbox/styles.css";
import "yet-another-react-lightbox/plugins/captions.css";
import { getImageIdentity } from "@shared/imageIdentity";
import { addLogEntry } from "@/modules/logEntries";
import { getHostname } from "@/modules/stringFormatters";
import { getThumbnailSrc } from "@/modules/thumbnailUrls";
Expand Down Expand Up @@ -90,11 +91,12 @@ export default function ImageResultsList({

void (async () => {
const entries = await Promise.all(
imageResults.map(async ([, url, thumbnailUrl]) => {
imageResults.map(async ([, url, thumbnailUrl, sourceUrl]) => {
const identity = getImageIdentity(sourceUrl, thumbnailUrl, url);
try {
return [url, await getThumbnailSrc(thumbnailUrl)] as const;
return [identity, await getThumbnailSrc(thumbnailUrl)] as const;
} catch {
return [url, null] as const;
return [identity, null] as const;
}
}),
);
Expand All @@ -118,12 +120,12 @@ export default function ImageResultsList({
}));
};

const markThumbnailFailed = (url: string) => {
setFailedThumbnails((prev) => ({ ...prev, [url]: true }));
const markThumbnailFailed = (identity: string) => {
setFailedThumbnails((prev) => ({ ...prev, [identity]: true }));
};

const renderTile = (title: string, url: string) => {
const tileSrc = failedThumbnails[url] ? null : thumbnailSrcs[url];
const renderTile = (title: string, url: string, identity: string) => {
const tileSrc = failedThumbnails[identity] ? null : thumbnailSrcs[identity];

if (tileSrc) {
return (
Expand All @@ -132,15 +134,15 @@ export default function ImageResultsList({
src={tileSrc}
loading="lazy"
style={imageStyle}
onError={() => markThumbnailFailed(url)}
onError={() => markThumbnailFailed(identity)}
/>
);
}

// `null` means the result has no thumbnail, or the load failed: the host
// name is the most that can be shown. `undefined` means the src is still
// being resolved, so the tile keeps a skeleton in the meantime.
if (thumbnailSrcs[url] === undefined) {
if (thumbnailSrcs[identity] === undefined) {
return (
<Box style={tileBoxStyle}>
<Skeleton style={{ height: "100%", width: "100%" }} />
Expand Down Expand Up @@ -176,89 +178,97 @@ export default function ImageResultsList({
loop: true,
}}
>
{imageResults.map(([title, url], index) => (
<Transition
key={url}
mounted={state.canStartTransition}
transition="fade"
timingFunction="ease"
enterDelay={shouldReduceMotion ? 0 : Math.min(index, 5) * 40}
duration={shouldReduceMotion ? 0 : 250}
>
{(styles) => (
<Carousel.Slide style={styles}>
<UnstyledButton
aria-label={`Open image preview: ${title || getHostname(url)}`}
className={classes.thumbnailButton}
onClick={() => handleImageClick(index)}
>
{renderTile(title, url)}
</UnstyledButton>
</Carousel.Slide>
)}
</Transition>
))}
{imageResults.map(([title, url, thumbnailUrl, sourceUrl], index) => {
const identity = getImageIdentity(sourceUrl, thumbnailUrl, url);
return (
<Transition
key={identity}
mounted={state.canStartTransition}
transition="fade"
timingFunction="ease"
enterDelay={shouldReduceMotion ? 0 : Math.min(index, 5) * 40}
duration={shouldReduceMotion ? 0 : 250}
>
{(styles) => (
<Carousel.Slide style={styles}>
<UnstyledButton
aria-label={`Open image preview: ${title || getHostname(url)}`}
className={classes.thumbnailButton}
onClick={() => handleImageClick(index)}
>
{renderTile(title, url, identity)}
</UnstyledButton>
</Carousel.Slide>
)}
</Transition>
);
})}
</Carousel>
<Lightbox
open={state.isLightboxOpen}
close={() => setState((prev) => ({ ...prev, isLightboxOpen: false }))}
plugins={[Captions]}
index={state.lightboxIndex}
slides={imageResults.map(([title, url, , sourceUrl]) => ({
// The grid's thumbnail when it loaded; when it did not, the same
// host-name placeholder the tile shows.
src:
thumbnailSrcs[url] && !failedThumbnails[url]
? thumbnailSrcs[url]
: hostNamePlaceholder(getHostname(url)),
alt: title,
description: (
<Stack align="center" gap="md">
{title && (
<Text component="cite" ta="center">
{title}
</Text>
)}
{sourceUrl && (
<Text size="xs" c="dimmed">
Source: {getHostname(sourceUrl)}
</Text>
)}
<Group align="center" justify="center" gap="xs">
slides={imageResults.map(([title, url, thumbnailUrl, sourceUrl]) => {
const identity = getImageIdentity(sourceUrl, thumbnailUrl, url);
return {
// The grid's thumbnail when it loaded; when it did not, the same
// host-name placeholder the tile shows.
src:
thumbnailSrcs[identity] && !failedThumbnails[identity]
? thumbnailSrcs[identity]
: hostNamePlaceholder(getHostname(url)),
alt: title,
description: (
<Stack align="center" gap="md">
{title && (
<Text component="cite" ta="center">
{title}
</Text>
)}
{sourceUrl && (
<Text size="xs" c="dimmed">
Source: {getHostname(sourceUrl)}
</Text>
)}
<Group align="center" justify="center" gap="xs">
{sourceUrl && (
<Button
variant="subtle"
component="a"
size="xs"
href={sourceUrl}
target="_blank"
title="Click to see the image in full size"
rel="noopener noreferrer"
onClick={() => {
addLogEntry(
"User visited an image result in full size",
);
}}
>
View in full resolution
</Button>
)}
<Button
variant="subtle"
component="a"
size="xs"
href={sourceUrl}
href={url}
target="_blank"
title="Click to see the image in full size"
size="xs"
title="Click to visit the page where the image was found"
rel="noopener noreferrer"
onClick={() => {
addLogEntry("User visited an image result in full size");
addLogEntry("User visited an image result source");
}}
>
View in full resolution
Visit {getHostname(url)}
</Button>
)}
<Button
variant="subtle"
component="a"
href={url}
target="_blank"
size="xs"
title="Click to visit the page where the image was found"
rel="noopener noreferrer"
onClick={() => {
addLogEntry("User visited an image result source");
}}
>
Visit {getHostname(url)}
</Button>
</Group>
</Stack>
),
}))}
</Group>
</Stack>
),
};
})}
/>
</>
);
Expand Down
Loading