Skip to content

Commit 455895c

Browse files
committed
Better initial sorting and pid display fix
1 parent e844622 commit 455895c

File tree

6 files changed

+70
-30
lines changed

6 files changed

+70
-30
lines changed

src/components/FairDOElasticSearch.tsx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,40 @@ export function FairDOElasticSearch({
120120
)}
121121
/>
122122
)}
123+
{/*{wasSearched && (
124+
<Sorting
125+
sortOptions={[
126+
{
127+
name: "Relevance",
128+
value: "",
129+
direction: ""
130+
},
131+
{
132+
name: "Title",
133+
value: "name.keyword",
134+
direction: "asc"
135+
},
136+
{
137+
name: "Image",
138+
value: "locationPreview/Sample.keyword",
139+
direction: "asc"
140+
}
141+
]}
142+
/>
143+
)}*/}
144+
</div>
145+
}
146+
bodyFooter={
147+
<div className="flex items-center flex-col gap-2 md:grid grid-cols-[1fr_auto_1fr] w-full p-2">
148+
<div />
149+
<Paging />
123150
{wasSearched && (
124151
<ResultsPerPage
152+
options={[20, 50, 100, 250]}
125153
view={(props) => {
126154
return (
127-
<div className="flex h-full items-center gap-2">
128-
<div>Results per Page</div>
155+
<div className="flex h-full items-center gap-2 justify-self-end">
156+
<div className="text-xs text-muted-foreground">Results per Page</div>
129157
<Select
130158
value={`${props.value}`}
131159
onValueChange={(v) => props.onChange(Number.parseInt(v))}
@@ -150,7 +178,6 @@ export function FairDOElasticSearch({
150178
)}
151179
</div>
152180
}
153-
bodyFooter={<Paging />}
154181
/>
155182
</ErrorBoundary>
156183
)

src/components/result/NMRResultView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export function NMRResultView({ result, debug }: { result: SearchResult; debug?:
167167
return (
168168
<div className={`m-2 rounded-lg border border-border p-4 ${exactPidMatch ? "animate-outline-ping" : ""}`}>
169169
<div className="grid grid-rows-[100px_1fr] gap-4 overflow-x-auto md:max-w-full md:grid-cols-[200px_1fr] md:grid-rows-1">
170-
<div className="flex justify-center rounded dark:bg-white md:items-center md:p-2">
170+
<div className="flex justify-center rounded md:items-center p-2 dark:invert">
171171
{previewImage ? (
172172
<img className="md:size-[200px]" src={previewImage} alt={`Preview for ${title}`} />
173173
) : (
@@ -185,7 +185,7 @@ export function NMRResultView({ result, debug }: { result: SearchResult; debug?:
185185
<div className="font-bold md:text-xl">
186186
{title}
187187
<span className="ml-2 text-sm font-normal text-muted-foreground">
188-
{identifier} -{creationDate}
188+
{identifier} - {creationDate}
189189
</span>
190190
</div>
191191
<a href={`https://hdl.handle.net/${id}`} target="_blank" className="mb-2 block leading-3 hover:underline">

src/components/result/PidDisplay.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { PidResolver, pidResolver } from "@/lib/pidResolver"
22
import { memo, useCallback } from "react"
3-
import useSWR from "swr"
43
import { ontobeeResolver } from "@/lib/OntobeeResolver"
4+
import useSWRImmutable from "swr/immutable"
55

66
/**
77
* Resolves a PID and displays the name of the received record
@@ -20,7 +20,7 @@ export const PidDisplay = memo(function PidDisplay({ pid }: { pid: string }) {
2020
}
2121
}, [])
2222

23-
const { data, error } = useSWR(pid, resolveContent)
23+
const { data, error } = useSWRImmutable(pid, resolveContent)
2424

2525
if (error) {
2626
return <div className="text-red-500">{JSON.stringify(error) || "Unknown error"}</div>

src/config/FairDOConfig.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { RequestState } from "@elastic/search-ui"
2+
13
export interface FairDOCoreFacetConfig {
24
key: string
35
label: string
@@ -86,4 +88,6 @@ export interface FairDOConfig {
8688
*/
8789
headers?: Record<string, string>
8890
}
91+
92+
initialState?: Partial<RequestState>
8993
}

src/config/FairDOConfigBuilder.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FacetConfiguration, FilterValueRange } from "@elastic/search-ui"
1+
import type { FacetConfiguration, FilterValueRange, SearchDriverOptions, SearchFieldConfiguration, SearchQuery } from "@elastic/search-ui"
22
import type { FairDOConfig, FairDODateRangeFacetConfig, FairDOFacetConfig, FairDONumericRangeFacetConfig } from "./FairDOConfig"
33
import ElasticsearchAPIConnector from "@elastic/search-ui-elasticsearch-connector"
44
import moment from "moment"
@@ -24,15 +24,16 @@ export class FairDOConfigBuilder {
2424
})
2525
}
2626

27-
buildElasticSearchConfig() {
27+
buildElasticSearchConfig(): SearchDriverOptions {
2828
return {
2929
searchQuery: {
3030
facets: this.getFacetConfig(),
3131
...this.getSearchOptions()
3232
},
3333
autocompleteQuery: this.getAutocompleteQueryConfig(),
3434
apiConnector: this.buildConnector(),
35-
alwaysSearchOnInitialLoad: true
35+
alwaysSearchOnInitialLoad: true,
36+
initialState: this.getConfig().initialState
3637
}
3738
}
3839

@@ -52,30 +53,26 @@ export class FairDOConfigBuilder {
5253
return facets
5354
}
5455

55-
getSearchOptions() {
56+
getSearchOptions(): SearchQuery & { index_names: string[] } {
5657
const config = this.getConfig()
5758
const index_names: string[] = []
58-
let allSearchFields: Record<string, Record<never, never>> = {}
59+
let allSearchFields: Record<string, SearchFieldConfiguration> = {}
5960
let allResultFields: Record<string, { raw: Record<never, never> }> = {}
6061

6162
for (const index of config.indices) {
62-
// store index name
6363
index_names.push(index.name)
6464

65-
// obtain search fields
66-
allSearchFields = (index.searchFields || []).reduce(
67-
// accumulate from config.searchFields
68-
(acc, n) => {
69-
// initialize accumulator if acc is not available, yet
70-
acc = acc || {}
71-
// set n-element (n is resultFieldKey) in acc
65+
allSearchFields = (index.searchFields || []).reduce((acc, n) => {
66+
if (n === "locationPreview/Sample") {
67+
acc[n] = {
68+
weight: 10
69+
}
70+
} else {
7271
acc[n] = {}
73-
// return current acc to next iteration
74-
return acc
75-
},
76-
// set initial value to already collected fields
77-
allSearchFields
78-
)
72+
}
73+
74+
return acc
75+
}, allSearchFields)
7976

8077
// build result fields for current index
8178
allResultFields = (index.resultFields || []).reduce(

src/stories/FairDOElasticSearch.stories.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ const demoConfig: FairDOConfig = {
5353
]
5454
}
5555

56-
const demoConfig2: FairDOConfig = {
56+
const demoConfig3: FairDOConfig = {
5757
debug: false,
5858
alwaysSearchOnInitialLoad: true,
5959
// host: "https://matwerk.datamanager.kit.edu/search-proxy/api/v1",
6060
host: "https://ddaa9283-f114-4496-b6ed-af12ee34b107.ka.bw-cloud-instance.org:9200",
6161
apiKey: "VG9NNFNwUUJyWWdtamJ6UGExcjY6aXhKUkk1M0xTT1dTS2xzN3daQjA3UQ==",
6262
indices: [
6363
{
64-
name: "fdo-test-2",
64+
name: "fdo-test-3",
6565
facets: [
6666
{
6767
key: "resourceType.keyword",
@@ -92,6 +92,18 @@ const demoConfig2: FairDOConfig = {
9292
searchFields: ["name", "pid", "hasMetadata", "isMetadataFor", "NMR_Method"]
9393
}
9494
],
95+
initialState: {
96+
sortList: [
97+
{
98+
field: "name.keyword",
99+
direction: "asc"
100+
},
101+
{
102+
field: "locationPreview/Sample.keyword",
103+
direction: "asc"
104+
}
105+
]
106+
},
95107
disjunctiveFacets: ["NMR_Method.keyword"]
96108
}
97109

@@ -102,9 +114,9 @@ export const DemoElastic: Story = {
102114
}
103115
}
104116

105-
export const DemoElastic2: Story = {
117+
export const DemoElastic3: Story = {
106118
args: {
107-
config: demoConfig2,
119+
config: demoConfig3,
108120
debug: false
109121
}
110122
}

0 commit comments

Comments
 (0)