Skip to content

Commit

Permalink
refactor: use generateSelectedEntityNamesParam to build an entity q…
Browse files Browse the repository at this point in the history
…uery param
  • Loading branch information
marcelgerber committed Dec 3, 2024
1 parent ec58302 commit bff21d4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,18 @@ export const getSelectedEntityNamesParam = (
: undefined
}

export const generateSelectedEntityNamesParam = (
entityNames: EntityName[]
): string => entityNamesToV2Param(entityNames.map(entityNameToCode))

export const setSelectedEntityNamesParam = (
url: Url,
entityNames: EntityName[] | undefined
): Url => {
// Expects an already-migrated URL as input
return url.updateQueryParams({
country: entityNames
? entityNamesToV2Param(entityNames.map(entityNameToCode))
? generateSelectedEntityNamesParam(entityNames)
: undefined,
})
}
12 changes: 7 additions & 5 deletions packages/@ourworldindata/grapher/src/core/Grapher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ import { TooltipManager } from "../tooltip/TooltipProps"

import { DimensionSlot } from "../chart/DimensionSlot"
import {
generateSelectedEntityNamesParam,
getSelectedEntityNamesParam,
setSelectedEntityNamesParam,
} from "./EntityUrlBuilder"
import { AxisConfig, AxisManager } from "../axis/AxisConfig"
import { ColorScaleConfig } from "../color/ColorScaleConfig"
Expand Down Expand Up @@ -3334,10 +3334,12 @@ export class Grapher
? "1"
: "0"
params.showNoDataArea = this.showNoDataArea ? "1" : "0"
return setSelectedEntityNamesParam(
Url.fromQueryParams(params),
this.selectedEntitiesIfDifferentThanAuthors
).queryParams
params.country = this.selectedEntitiesIfDifferentThanAuthors
? generateSelectedEntityNamesParam(
this.selectedEntitiesIfDifferentThanAuthors
)
: undefined
return params
}

@computed private get selectedEntitiesIfDifferentThanAuthors():
Expand Down
1 change: 1 addition & 0 deletions packages/@ourworldindata/grapher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export {
setSelectedEntityNamesParam,
migrateSelectedEntityNamesParam,
getSelectedEntityNamesParam,
generateSelectedEntityNamesParam,
} from "./core/EntityUrlBuilder"
export {
type SlideShowManager,
Expand Down
14 changes: 6 additions & 8 deletions site/search/SearchUtils.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HitAttributeHighlightResult } from "instantsearch.js"
import { EntityName } from "@ourworldindata/types"
import { EntityName, GrapherQueryParams } from "@ourworldindata/types"
import {
Region,
getRegionByNameOrVariantName,
Expand All @@ -9,7 +9,7 @@ import {
lazy,
Url,
} from "@ourworldindata/utils"
import { setSelectedEntityNamesParam } from "@ourworldindata/grapher"
import { generateSelectedEntityNamesParam } from "@ourworldindata/grapher"

/**
* The below code is used to search for entities we can highlight in charts and explorer results.
Expand Down Expand Up @@ -121,12 +121,10 @@ export const getEntityQueryStr = (
) => {
if (!entities?.length) return existingQueryStr
else {
return setSelectedEntityNamesParam(
return Url.fromQueryStr(existingQueryStr).updateQueryParams({
// If we have any entities pre-selected, we want to show the chart tab
Url.fromQueryStr(existingQueryStr).updateQueryParams({
tab: "chart",
}),
entities
).queryStr
tab: "chart",
country: generateSelectedEntityNamesParam(entities),
} satisfies GrapherQueryParams).queryStr
}
}

0 comments on commit bff21d4

Please sign in to comment.