Skip to content

Commit 9c69269

Browse files
committed
Distribute config through internal context
1 parent 853e292 commit 9c69269

8 files changed

+87
-42
lines changed

src/components/FairDOElasticSearch.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export function FairDOElasticSearch({
138138
)}
139139
/>
140140
)}
141-
{wasSearched && <DefaultSorting config={rawConfig} />}
141+
{wasSearched && <DefaultSorting />}
142142
</div>
143143
}
144144
bodyFooter={

src/components/FairDOSearchContext.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import type { ResponseState } from "@elastic/search-ui"
44
import type ElasticsearchAPIConnector from "@elastic/search-ui-elasticsearch-connector"
55
import { createContext } from "react"
6+
import { FairDOConfig } from "@/config/FairDOConfig"
67

78
/**
89
* Extends the elasticsearch SearchContext with additional utilities
@@ -29,6 +30,7 @@ export interface FairDOSearchContext {
2930
searchTerm: string
3031

3132
elasticConnector?: ElasticsearchAPIConnector
33+
config: FairDOConfig
3234
}
3335

3436
/**
@@ -45,5 +47,8 @@ export const FairDOSearchContext = createContext<FairDOSearchContext>({
4547
async searchForBackground(): Promise<ResponseState | undefined> {
4648
console.error(`FairDOSearchContext not mounted, but searchFor was executed`)
4749
return undefined
50+
},
51+
get config(): FairDOConfig {
52+
throw "FairDOSearchContext not mounted, but tried to read config"
4853
}
4954
})

src/components/FairDOSearchProvider.tsx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@ import { useCallback, useMemo } from "react"
1515
*/
1616
export function FairDOSearchProvider(props: PropsWithChildren & { config: FairDOConfig }) {
1717
const connector = useMemo(() => {
18-
return new FairDOConfigBuilder(props.config).buildConnector()
18+
try {
19+
return new FairDOConfigBuilder(props.config).buildConnector()
20+
} catch (e) {
21+
console.error("Failed to build connector in FairDOSearchProvider", e)
22+
return undefined
23+
}
1924
}, [props.config])
2025

2126
const searchForBackground = useCallback(
22-
(query: string) => {
27+
async (query: string) => {
28+
if (!connector) return
29+
2330
// Hacky but works
2431
return connector.onSearch(
2532
{ searchTerm: query, resultsPerPage: 20 },
@@ -34,6 +41,25 @@ export function FairDOSearchProvider(props: PropsWithChildren & { config: FairDO
3441
[connector, props.config.indices]
3542
)
3643

44+
// Fallback for testing without elastic context
45+
if (!connector) {
46+
console.warn("Using fallback context for FairDOSearchProvider as elastic config is invalid. Elastic-related features will not work.")
47+
return (
48+
<FairDOSearchContext.Provider
49+
value={{
50+
searchTerm: "",
51+
searchFor: () => {},
52+
searchForBackground: async () => {
53+
return undefined
54+
},
55+
config: props.config
56+
}}
57+
>
58+
{props.children}
59+
</FairDOSearchContext.Provider>
60+
)
61+
}
62+
3763
return (
3864
<WithSearch
3965
mapContextToProps={({ searchTerm, setSearchTerm, clearFilters, setSort }: SearchContextState) => ({
@@ -59,7 +85,8 @@ export function FairDOSearchProvider(props: PropsWithChildren & { config: FairDO
5985
})
6086
},
6187
elasticConnector: connector,
62-
searchForBackground
88+
searchForBackground,
89+
config: props.config
6390
}}
6491
>
6592
{props.children}

src/components/graph/RelationsGraphModal.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ export function RelationsGraphModal({
4242
searchFor: localSearchFor,
4343
searchTerm: searchContext.searchTerm,
4444
searchForBackground: searchContext.searchForBackground,
45-
elasticConnector: searchContext.elasticConnector
45+
elasticConnector: searchContext.elasticConnector,
46+
config: searchContext.config
4647
}}
4748
>
4849
<RelationsGraph referencedBy={referencedBy} references={references} base={base} resultView={resultView} />

src/components/result/GenericResultView.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ 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"
1716

1817
const HTTP_REGEX = /https?:\/\/.*/
1918

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

107101
/**
@@ -124,11 +118,11 @@ export function GenericResultView({
124118
additionalIdentifierField = "identifier",
125119
relatedItemsPrefetch = { searchFields: { pid: {} } },
126120
tags = [],
127-
showOpenInFairDoScope = true,
128-
config
121+
showOpenInFairDoScope = true
129122
}: GenericResultViewProps) {
130123
const { openRelationGraph } = useContext(RFS_GlobalModalContext)
131-
const { searchTerm, elasticConnector, searchFor } = useContext(FairDOSearchContext)
124+
const { searchTerm, elasticConnector, searchFor, config } = useContext(FairDOSearchContext)
125+
console.log(config)
132126
const addToResultCache = useStore(resultCache, (s) => s.set)
133127
const [loadingRelatedItems, setLoadingRelatedItems] = useState(false)
134128

src/components/search/DefaultSorting.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
22
import { useCallback, useContext } from "react"
33
import { SearchContext } from "@elastic/react-search-ui"
4-
import { FairDOConfig } from "@/config/FairDOConfig"
54
import { ArrowUpDown } from "lucide-react"
5+
import { FairDOSearchContext } from "@/components/FairDOSearchContext"
66

7-
export function DefaultSorting({ config }: { config: FairDOConfig }) {
7+
export function DefaultSorting() {
88
const search = useContext(SearchContext)
9+
const { config } = useContext(FairDOSearchContext)
910

1011
const handleChange = useCallback(
1112
(value: string) => {

src/stories/GenericResultView.stories.tsx

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { AtomIcon, GlobeIcon, GraduationCap, ScaleIcon } from "lucide-react"
55
import { tryURLPrettyPrint } from "@/lib/utils"
66
import { GlobalModalProvider } from "@/components/GlobalModalProvider"
77
import { TooltipProvider } from "@/components/ui/tooltip"
8+
import { FairDOSearchProvider } from "@/components/FairDOSearchProvider"
89

910
const meta = {
1011
component: GenericResultView,
@@ -21,11 +22,11 @@ export const Simple: Story = {
2122
decorators: [
2223
(Story) => (
2324
<TooltipProvider>
24-
<GlobalModalProvider resultView={(props) => <GenericResultView {...props} {...Simple.args} config={emptyConfig} />}>
25-
<div>
25+
<FairDOSearchProvider config={emptyConfig}>
26+
<GlobalModalProvider resultView={(props) => <GenericResultView {...props} {...Simple.args} />}>
2627
<Story />
27-
</div>
28-
</GlobalModalProvider>
28+
</GlobalModalProvider>
29+
</FairDOSearchProvider>
2930
</TooltipProvider>
3031
)
3132
],
@@ -37,20 +38,19 @@ export const Simple: Story = {
3738
titleField: "title",
3839
descriptionField: "description",
3940
imageField: undefined,
40-
invertImageInDarkMode: true,
41-
config: emptyConfig
41+
invertImageInDarkMode: true
4242
}
4343
}
4444

4545
export const MultipleImages: Story = {
4646
decorators: [
4747
(Story) => (
4848
<TooltipProvider>
49-
<GlobalModalProvider resultView={(props) => <GenericResultView {...props} {...MultipleImages.args} config={emptyConfig} />}>
50-
<div>
49+
<FairDOSearchProvider config={emptyConfig}>
50+
<GlobalModalProvider resultView={(props) => <GenericResultView {...props} {...MultipleImages.args} />}>
5151
<Story />
52-
</div>
53-
</GlobalModalProvider>
52+
</GlobalModalProvider>
53+
</FairDOSearchProvider>
5454
</TooltipProvider>
5555
)
5656
],
@@ -69,20 +69,19 @@ export const MultipleImages: Story = {
6969
titleField: "title",
7070
descriptionField: "description",
7171
invertImageInDarkMode: true,
72-
imageField: "images",
73-
config: emptyConfig
72+
imageField: "images"
7473
}
7574
}
7675

7776
export const Full: Story = {
7877
decorators: [
7978
(Story) => (
8079
<TooltipProvider>
81-
<GlobalModalProvider resultView={(props) => <GenericResultView {...props} {...Full.args} config={{ host: "", indices: [] }} />}>
82-
<div>
80+
<FairDOSearchProvider config={emptyConfig}>
81+
<GlobalModalProvider resultView={(props) => <GenericResultView {...props} {...Full.args} />}>
8382
<Story />
84-
</div>
85-
</GlobalModalProvider>
83+
</GlobalModalProvider>
84+
</FairDOSearchProvider>
8685
</TooltipProvider>
8786
)
8887
],
@@ -164,7 +163,6 @@ export const Full: Story = {
164163
field: "stringArrayTest",
165164
singleValueMapper: (v) => v.toUpperCase()
166165
}
167-
],
168-
config: emptyConfig
166+
]
169167
}
170168
}

src/stories/RelationsGraph.stories.tsx

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import type { Meta, StoryObj } from "@storybook/react"
22
import { ReactFlowProvider } from "@xyflow/react"
33
import { RelationsGraph } from "@/components/graph/RelationsGraph"
44
import { GenericResultView } from "@/components/result"
5+
import { resultCache } from "@/lib/ResultCache"
6+
import { FairDOSearchProvider } from "@/components/FairDOSearchProvider"
7+
import { FairDOConfig } from "@/config/FairDOConfig"
58

69
const meta = {
710
component: RelationsGraph,
@@ -12,20 +15,36 @@ export default meta
1215

1316
type Story = StoryObj<typeof meta>
1417

18+
const emptyConfig: FairDOConfig = { host: "", indices: [] }
19+
1520
export const Default: Story = {
1621
decorators: [
17-
(Story) => (
18-
<ReactFlowProvider>
19-
<div style={{ width: "100%", height: "min(70vh, 700px)" }}>
20-
<Story />
21-
</div>
22-
</ReactFlowProvider>
23-
)
22+
(Story) => {
23+
resultCache.getState().set("a", { name: "Left side A" })
24+
resultCache.getState().set("b", { name: "Left side B" })
25+
resultCache.getState().set("c", { name: "Left side C" })
26+
resultCache.getState().set("self", { name: "Center entry" })
27+
resultCache.getState().set("1", { name: "Right side A" })
28+
resultCache.getState().set("2", { name: "Right side B" })
29+
resultCache.getState().set("3", { name: "Right side C" })
30+
resultCache.getState().set("4", { name: "Right side D" })
31+
resultCache.getState().set("5", { name: "Right side E" })
32+
33+
return (
34+
<ReactFlowProvider>
35+
<FairDOSearchProvider config={emptyConfig}>
36+
<div style={{ width: "100%", height: "min(70vh, 700px)" }}>
37+
<Story />
38+
</div>
39+
</FairDOSearchProvider>
40+
</ReactFlowProvider>
41+
)
42+
}
2443
],
2544
args: {
2645
referencedBy: ["a", "b", "c"],
2746
base: "self",
2847
references: ["1", "2", "3", "4", "5"],
29-
resultView: (props) => <GenericResultView {...props} config={{ host: "", indices: [] }} />
48+
resultView: (props) => <GenericResultView {...props} />
3049
}
3150
}

0 commit comments

Comments
 (0)