Skip to content

Commit 47e0a31

Browse files
committed
DIGG-465: Updating some missing code, seems to work for now
1 parent 4acfc74 commit 47e0a31

File tree

2 files changed

+109
-109
lines changed

2 files changed

+109
-109
lines changed

components/content/Search/SearchResults/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export const SearchResults: FC<SearchResultsProps> = ({
245245
}
246246

247247
const SearchResultSkeleton = () => (
248-
<div className="animate-pulse space-y-lg">
248+
<div className="animate-pulse space-y-lg opacity-50">
249249
<div className="rounded h-lg w-1/4 bg-green-600" />
250250
<div className="flex flex-col gap-sm">
251251
<div className="rounded h-md w-3/4 bg-textPrimary" />
@@ -256,7 +256,7 @@ export const SearchResults: FC<SearchResultsProps> = ({
256256
);
257257

258258
return (
259-
<div id="search-result" className="my-lg pt-xl md:my-xl">
259+
<div id="search-result" className="my-lg py-xl md:my-xl">
260260
<div className="mb-lg flex flex-col-reverse justify-between md:flex-row">
261261
<Heading level={2} size="md" className="search-result-header">
262262
{/* Visual display of the count */}

providers/SearchProvider/index.tsx

Lines changed: 107 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,29 @@ export const SearchContext = createContext<SearchContextData>(
105105
* SearchProvider component
106106
*/
107107
class SearchProvider extends Component<SearchProviderProps, SearchContextData> {
108+
private entryScape: EntryScape;
109+
private getCacheKeys(request: SearchRequest) {
110+
const cacheKeyBase = `${request.language || ""}_${
111+
request.esRdfTypes?.[0] || ""
112+
}`;
113+
return {
114+
data: `${cacheKeyBase}_facets-cache`,
115+
timestamp: `${cacheKeyBase}_facets-cache-ts`,
116+
};
117+
}
118+
108119
constructor(props: SearchProviderProps) {
109120
super(props);
121+
const { t, lang } = props.i18n;
122+
123+
this.entryScape = new EntryScape(
124+
props.entryscapeUrl || "https://admin.dataportal.se/store",
125+
lang,
126+
t,
127+
props.facetSpecification,
128+
props.hitSpecifications,
129+
);
130+
110131
this.state = {
111132
...defaultSearchSettings,
112133
request: {
@@ -115,6 +136,20 @@ class SearchProvider extends Component<SearchProviderProps, SearchContextData> {
115136
};
116137
}
117138

139+
// Update EntryScape instance when language changes
140+
componentDidUpdate(prevProps: SearchProviderProps) {
141+
if (prevProps.i18n.lang !== this.props.i18n.lang) {
142+
const { t, lang } = this.props.i18n;
143+
this.entryScape = new EntryScape(
144+
this.props.entryscapeUrl || "https://admin.dataportal.se/store",
145+
lang,
146+
t,
147+
this.props.facetSpecification,
148+
this.props.hitSpecifications,
149+
);
150+
}
151+
}
152+
118153
/**
119154
* On component mount
120155
*
@@ -173,7 +208,6 @@ class SearchProvider extends Component<SearchProviderProps, SearchContextData> {
173208

174209
if (dcatmeta && dcatmeta.templates && dcatmeta.templates.length > 0) {
175210
this.setState({
176-
...this.state,
177211
dcatmeta: dcatmeta,
178212
});
179213
}
@@ -184,21 +218,11 @@ class SearchProvider extends Component<SearchProviderProps, SearchContextData> {
184218
* This is for accurate facets count
185219
*/
186220
updateFacetStatsGrouped = async (): Promise<void> => {
187-
const { t, lang } = this.props.i18n;
188-
189221
// Only continue if we have allFacets and a SearchResult
190222
if (!this.state.allFacets || !this.state.result?.facets) {
191223
return;
192224
}
193225

194-
const entryScape = new EntryScape(
195-
this.props.entryscapeUrl || "https://admin.dataportal.se/store",
196-
lang,
197-
t,
198-
this.props.facetSpecification,
199-
this.props.hitSpecifications,
200-
);
201-
202226
const facetValues = this.state.request.facetValues as SearchFacetValue[];
203227
if (!facetValues?.length) {
204228
return;
@@ -223,7 +247,7 @@ class SearchProvider extends Component<SearchProviderProps, SearchContextData> {
223247
const searchPromises = Object.keys(groupedFacets).map(async (group) => {
224248
const facetsNotInGroup = facetValues.filter((f) => f.facet !== group);
225249

226-
const res = await entryScape.solrSearch({
250+
const res = await this.entryScape.solrSearch({
227251
...this.state.request,
228252
takeFacets: 100,
229253
fetchFacets: true,
@@ -441,85 +465,74 @@ class SearchProvider extends Component<SearchProviderProps, SearchContextData> {
441465
* Will cache and fetch from localStorage, cache expires in 5 mins
442466
*/
443467
fetchAllFacets = async (): Promise<void> => {
444-
const { t, lang } = this.props.i18n;
445-
const store_cache_key = `${this.state.request.language || ""}_${
446-
this.state.request.esRdfTypes
447-
? this.state.request.esRdfTypes[0].toString()
448-
: ""
449-
}_facets-cache`;
450-
const store_cache_key_stamp = `${this.state.request.language || ""}_${
451-
this.state.request.esRdfTypes
452-
? this.state.request.esRdfTypes[0].toString()
453-
: ""
454-
}_facets-cache-ts`;
468+
const { request, dcatmeta } = this.state;
469+
const CACHE_KEYS = this.getCacheKeys(request);
470+
471+
const CACHE_EXPIRY_MINS = 5;
455472

456473
this.setState({ loadingFacets: true });
457474

458-
// Check cache
459-
if (hasLocalStore && hasWindow) {
460-
const ls_AllFacets = localStorage.getItem(store_cache_key);
461-
const ls_Stamp = localStorage.getItem(store_cache_key_stamp);
475+
try {
476+
// Check cache if browser environment
477+
if (hasWindow && hasLocalStore) {
478+
const cachedData = localStorage.getItem(CACHE_KEYS.data);
479+
const cachedTimestamp = localStorage.getItem(CACHE_KEYS.timestamp);
480+
481+
if (cachedData && cachedTimestamp) {
482+
const allFacets = JSON.parse(cachedData) as {
483+
[facet: string]: SearchFacet;
484+
};
485+
const cacheAge =
486+
(Date.now() - new Date(JSON.parse(cachedTimestamp)).getTime()) /
487+
60000;
488+
489+
if (cacheAge <= CACHE_EXPIRY_MINS) {
490+
this.setState({ allFacets, loadingFacets: false });
491+
return;
492+
}
462493

463-
if (ls_AllFacets && ls_Stamp) {
464-
const allFacets = JSON.parse(ls_AllFacets) as {
465-
[facet: string]: SearchFacet;
466-
};
467-
const stampAllFacets = new Date(JSON.parse(ls_Stamp));
468-
const diff = (new Date().getTime() - stampAllFacets.getTime()) / 60000;
469-
470-
if (diff > 5) {
471-
// Cache expired, remove it
472-
localStorage.removeItem(store_cache_key);
473-
localStorage.removeItem(store_cache_key_stamp);
474-
} else {
475-
// Use cached data
476-
this.setState({ allFacets, loadingFacets: false });
477-
return;
494+
// Clear expired cache
495+
localStorage.removeItem(CACHE_KEYS.data);
496+
localStorage.removeItem(CACHE_KEYS.timestamp);
478497
}
479498
}
480-
}
481499

482-
try {
483-
const entryScape = new EntryScape(
484-
this.props.entryscapeUrl || "https://admin.dataportal.se/store",
485-
lang,
486-
t,
487-
this.props.facetSpecification,
488-
this.props.hitSpecifications,
489-
);
490-
491-
const res = await entryScape.solrSearch({
500+
const searchResult = await this.entryScape.solrSearch({
492501
query: "*",
493502
fetchFacets: true,
494503
take: 1,
495-
takeFacets: this.state.request.takeFacets || 30,
504+
takeFacets: request.takeFacets || 30,
496505
});
497506

498-
if (!res.esFacets || !this.state.dcatmeta) return;
507+
if (!searchResult.esFacets || !dcatmeta) {
508+
throw new Error("Missing required facets or DCAT metadata");
509+
}
499510

500-
const facets = await entryScape.getFacets(
501-
res.esFacets,
502-
this.state.dcatmeta,
511+
const facets = await this.entryScape.getFacets(
512+
searchResult.esFacets,
513+
dcatmeta,
503514
);
504515

505-
if (facets) {
506-
this.setState({ allFacets: facets });
516+
if (!facets) {
517+
throw new Error("Failed to process facets");
518+
}
519+
520+
this.setState({ allFacets: facets });
507521

508-
if (hasLocalStore && hasWindow) {
509-
localStorage.setItem(store_cache_key, JSON.stringify(facets));
522+
// Cache results if browser environment
523+
if (hasWindow && hasLocalStore) {
524+
try {
525+
localStorage.setItem(CACHE_KEYS.data, JSON.stringify(facets));
510526
localStorage.setItem(
511-
store_cache_key_stamp,
527+
CACHE_KEYS.timestamp,
512528
JSON.stringify(new Date()),
513529
);
530+
} catch (cacheError) {
531+
console.warn("Failed to cache facets:", cacheError);
514532
}
515-
} else {
516-
this.setState({
517-
request: this.state.request,
518-
loadingFacets: false,
519-
});
520533
}
521534
} catch (error) {
522-
console.error("Failed to fetch all facets:", error);
535+
console.error("Failed to fetch facets:", error);
523536
} finally {
524537
this.setState({ loadingFacets: false });
525538
}
@@ -533,32 +546,25 @@ class SearchProvider extends Component<SearchProviderProps, SearchContextData> {
533546
*
534547
*/
535548
searchInFacets = async (query: string, facetkey: string): Promise<void> => {
536-
const { t, lang } = this.props.i18n;
537-
const store_cache_key = `${this.state.request.language || ""}_${
538-
this.state.request.esRdfTypes
539-
? this.state.request.esRdfTypes[0].toString()
540-
: ""
541-
}_facets-cache`;
542-
const store_cache_key_stamp = `${store_cache_key}-ts`;
549+
const { request } = this.state;
550+
const CACHE_KEYS = this.getCacheKeys(request);
543551

544552
const facets = { ...this.state.allFacets };
545553

546-
const entryScape = new EntryScape(
547-
this.props.entryscapeUrl || "https://admin.dataportal.se/store",
548-
lang,
549-
t,
550-
undefined, // Optional configuration
551-
{
552-
"http://xmlns.com/foaf/0.1/Agent": {
553-
path: ``,
554-
titleResource: "http://xmlns.com/foaf/0.1/name",
555-
descriptionResource: "",
556-
},
554+
// Store original specifications
555+
const originalSpecs = { ...this.entryScape.hitSpecifications };
556+
557+
// Update specifications for this search
558+
this.entryScape.hitSpecifications = {
559+
"http://xmlns.com/foaf/0.1/Agent": {
560+
path: ``,
561+
titleResource: "http://xmlns.com/foaf/0.1/name",
562+
descriptionResource: "",
557563
},
558-
);
564+
};
559565

560566
try {
561-
const res = await entryScape.solrSearch({
567+
const res = await this.entryScape.solrSearch({
562568
titleQuery: query && query.length > 0 ? query : "*",
563569
fetchFacets: false,
564570
take: 100,
@@ -595,9 +601,9 @@ class SearchProvider extends Component<SearchProviderProps, SearchContextData> {
595601
this.setState({ allFacets: facets });
596602

597603
if (hasLocalStore && hasWindow) {
598-
window.localStorage.setItem(store_cache_key, JSON.stringify(facets));
604+
window.localStorage.setItem(CACHE_KEYS.data, JSON.stringify(facets));
599605
window.localStorage.setItem(
600-
store_cache_key_stamp,
606+
CACHE_KEYS.timestamp,
601607
JSON.stringify(new Date()),
602608
);
603609
}
@@ -607,6 +613,8 @@ class SearchProvider extends Component<SearchProviderProps, SearchContextData> {
607613
console.error("Error searching in facets:", error);
608614
} finally {
609615
this.setState({ loadingFacets: false });
616+
// Restore original specifications
617+
this.entryScape.hitSpecifications = originalSpecs;
610618
}
611619
};
612620

@@ -849,31 +857,21 @@ class SearchProvider extends Component<SearchProviderProps, SearchContextData> {
849857
setStateToLocation: Boolean = true,
850858
reSortOnDone: Boolean = true,
851859
): Promise<void> => {
852-
const { t, lang } = this.props.i18n;
853-
854860
this.setState({ loadingHits: true });
855861

856862
if (setStateToLocation) {
857863
this.setStateToLocation();
858864
}
859865

860866
try {
861-
const entryScape = new EntryScape(
862-
this.props.entryscapeUrl || "https://admin.dataportal.se/store",
863-
lang,
864-
t,
865-
this.props.facetSpecification,
866-
this.props.hitSpecifications,
867-
);
868-
869867
// Separate search request without facets
870868
const searchRequest = {
871869
...this.state.request,
872870
fetchFacets: false, // Disable facet fetching for initial search
873871
};
874872

875873
// Execute search immediately
876-
const searchResults = await entryScape.solrSearch(
874+
const searchResults = await this.entryScape.solrSearch(
877875
searchRequest,
878876
this.state.dcatmeta,
879877
);
@@ -885,7 +883,7 @@ class SearchProvider extends Component<SearchProviderProps, SearchContextData> {
885883
}
886884

887885
// Update UI with search results immediately
888-
await this.setState({
886+
this.setState({
889887
loadingHits: false,
890888
result: {
891889
...this.state.result,
@@ -908,24 +906,26 @@ class SearchProvider extends Component<SearchProviderProps, SearchContextData> {
908906
take: 1, // Minimize result set since we only need facets
909907
};
910908

911-
const facetResults = await entryScape.solrSearch(
909+
const facetResults = await this.entryScape.solrSearch(
912910
facetRequest,
913911
this.state.dcatmeta,
914912
);
915913

916914
if (facetResults.esFacets) {
917-
const facets = await entryScape.getFacets(
915+
const facets = await this.entryScape.getFacets(
918916
facetResults.esFacets,
919917
this.state.dcatmeta!,
920918
);
921919

922-
await this.setState({
920+
this.setState({
923921
loadingFacets: false,
924922
allFacets: facets,
925923
});
926924

925+
await this.mergeAllFacetsAndResult();
926+
await this.updateFacetStatsGrouped();
927+
927928
if (reSortOnDone) {
928-
await this.mergeAllFacetsAndResult();
929929
this.sortAllFacets();
930930
}
931931
}

0 commit comments

Comments
 (0)