Skip to content

Commit cb9b403

Browse files
committed
Add image proxy capabilities
1 parent 7deef2e commit cb9b403

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

src/components/result/GenericResultView.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { SearchFieldConfiguration } from "@elastic/search-ui"
1313
import { GenericResultViewTag, GenericResultViewTagProps } from "@/components/result/GenericResultViewTag"
1414
import { z } from "zod"
1515
import { GenericResultViewImage } from "@/components/result/GenericResultViewImage"
16+
import { FairDOConfig } from "@/config/FairDOConfig"
1617

1718
const HTTP_REGEX = /https?:\/\/.*/
1819

@@ -96,6 +97,11 @@ export interface GenericResultViewProps {
9697
* Whether to show the open in FairDOScope button in the dropdown
9798
*/
9899
showOpenInFairDoScope?: boolean
100+
101+
/**
102+
* Pass the same config here as passed to FairDOElasticSearch
103+
*/
104+
config: FairDOConfig
99105
}
100106

101107
/**
@@ -118,7 +124,8 @@ export function GenericResultView({
118124
additionalIdentifierField = "identifier",
119125
relatedItemsPrefetch = { searchFields: { pid: {} } },
120126
tags = [],
121-
showOpenInFairDoScope = true
127+
showOpenInFairDoScope = true,
128+
config
122129
}: GenericResultViewProps) {
123130
const { openRelationGraph } = useContext(RFS_GlobalModalContext)
124131
const { searchTerm, elasticConnector, searchFor } = useContext(FairDOSearchContext)
@@ -216,8 +223,11 @@ export function GenericResultView({
216223

217224
const previewImage = useMemo(() => {
218225
const images = getArrayOrSingleField(imageField ?? "imageURL")
219-
return Array.isArray(images) ? (images.length === 1 ? images[0] : images) : images
220-
}, [getArrayOrSingleField, imageField])
226+
const normalized = Array.isArray(images) ? (images.length === 1 ? images[0] : images) : images
227+
if (config.imageProxy && normalized) {
228+
return Array.isArray(normalized) ? normalized.map(config.imageProxy) : config.imageProxy(normalized)
229+
} else return normalized
230+
}, [config, getArrayOrSingleField, imageField])
221231

222232
const identifier = useMemo(() => {
223233
const maybeArray = getArrayOrSingleField(additionalIdentifierField ?? "identifier")

src/config/FairDOConfig.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ export interface FairDOConfig {
108108
headers?: Record<string, string>
109109
}
110110

111+
/**
112+
* Rewrite image source URLs to use proxies for the images. You might want to consider using an image proxy
113+
* if the images in you dataset are large.
114+
* @example
115+
* imageProxy: (src) => `https://myproxy.org/${src}?format=webp`
116+
*/
117+
imageProxy?: (src: string) => string
118+
111119
/**
112120
* Initialize the internal state. This can be useful to specify the default sorting of the results.
113121
* @example

src/stories/0 Getting Started.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export default function Home() {
8888
return (
8989
<GenericResultView
9090
result={props.result}
91+
config={config}
9192
invertImageInDarkMode // Not recommended for colorful images
9293
tags={[
9394
{

src/stories/FairDOElasticSearch.stories.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ const demoConfig: FairDOConfig = {
135135
"Pulse_Sequence_Name.keyword",
136136
"Acquisition_Nucleus.keyword",
137137
"licenseURL.keyword"
138-
]
138+
],
139+
imageProxy: (src) => `https://wsrv.nl/?url=${src}&h=1000&output=webp&ll`
139140
}
140141

141142
// const demoConfigWithCompound: FairDOConfig = {
@@ -191,6 +192,7 @@ export const GenericResultRenderer: Story = {
191192
<GenericResultView
192193
result={props.result}
193194
invertImageInDarkMode
195+
config={demoConfig}
194196
tags={[
195197
{
196198
icon: <UserIcon className="rfs-shrink-0 rfs-size-4 rfs-mr-2" />,

0 commit comments

Comments
 (0)