diff --git a/packages/@ourworldindata/grapher/src/entitySelector/EntitySelector.scss b/packages/@ourworldindata/grapher/src/entitySelector/EntitySelector.scss index fb59d698304..f209b20b533 100644 --- a/packages/@ourworldindata/grapher/src/entitySelector/EntitySelector.scss +++ b/packages/@ourworldindata/grapher/src/entitySelector/EntitySelector.scss @@ -173,6 +173,17 @@ background: #ebeef2; z-index: -1; } + + .label-with-location-icon { + display: flex; + align-items: center; + + svg { + margin-left: 8px; + font-size: 0.9em; + color: #a1a1a1; + } + } } .animated-entity { diff --git a/packages/@ourworldindata/grapher/src/entitySelector/EntitySelector.tsx b/packages/@ourworldindata/grapher/src/entitySelector/EntitySelector.tsx index 9af114d7206..78781914b0b 100644 --- a/packages/@ourworldindata/grapher/src/entitySelector/EntitySelector.tsx +++ b/packages/@ourworldindata/grapher/src/entitySelector/EntitySelector.tsx @@ -13,14 +13,17 @@ import { isFiniteWithGuard, CoreValueType, clamp, - sortBy, last, + getUserCountryInformation, + regions, + sortBy, } from "@ourworldindata/utils" import { Checkbox } from "@ourworldindata/components" import { FuzzySearch } from "../controls/FuzzySearch" import { faCircleXmark, faMagnifyingGlass, + faLocationArrow, } from "@fortawesome/free-solid-svg-icons" import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js" import { SelectionArray } from "../selection/SelectionArray" @@ -44,6 +47,7 @@ import { scaleLinear, type ScaleLinear } from "d3-scale" export interface EntitySelectorState { searchInput: string sortConfig: SortConfig + localEntityNames?: string[] mostRecentlySelectedEntityName?: string } @@ -65,7 +69,7 @@ interface SortConfig { order: SortOrder } -type SearchableEntity = { name: string } & Record< +type SearchableEntity = { name: string; local?: boolean } & Record< Slug, CoreValueType | undefined > @@ -95,6 +99,8 @@ export class EntitySelector extends React.Component<{ } componentDidMount(): void { + void this.populateLocalEntities() + if (this.props.autoFocus && !isTouchDevice()) this.searchField.current?.focus() @@ -136,6 +142,34 @@ export class EntitySelector extends React.Component<{ } } + @action.bound async populateLocalEntities(): Promise { + try { + const localCountryInfo = await getUserCountryInformation() + if (!localCountryInfo) return + + const userEntityCodes = [ + localCountryInfo.code, + ...(localCountryInfo.regions ?? []), + ] + + const userRegions = regions.filter((region) => + userEntityCodes.includes(region.code) + ) + + const sortedUserRegions = sortBy(userRegions, (region) => + userEntityCodes.indexOf(region.code) + ) + + if (sortedUserRegions) { + this.set({ + localEntityNames: sortedUserRegions.map( + (region) => region.name + ), + }) + } + } catch (err) {} + } + private clearSearchInput(): void { this.set({ searchInput: "" }) } @@ -181,6 +215,10 @@ export class EntitySelector extends React.Component<{ ) } + @computed private get localEntityNames(): string[] | undefined { + return this.manager.entitySelectorState.localEntityNames + } + @computed private get table(): OwidTable { return this.manager.tableForSelection } @@ -257,6 +295,11 @@ export class EntitySelector extends React.Component<{ return this.availableEntityNames.map((entityName) => { const searchableEntity: SearchableEntity = { name: entityName } + if (this.localEntityNames) { + searchableEntity.local = + this.localEntityNames.includes(entityName) + } + for (const column of this.sortColumns) { const rows = column.owidRowsByEntityName.get(entityName) ?? [] const sortedRows = sortBy(rows, (row) => row.time) @@ -267,13 +310,16 @@ export class EntitySelector extends React.Component<{ }) } - private sortEntities(entities: SearchableEntity[]): SearchableEntity[] { + private sortEntities( + entities: SearchableEntity[], + options: { sortLocalsToTop: boolean } = { sortLocalsToTop: true } + ): SearchableEntity[] { const { sortConfig } = this const shouldBeSortedByName = this.hasSlugName(sortConfig) - // sort by name - if (shouldBeSortedByName) { + // sort by name, ignoring local entities + if (shouldBeSortedByName && !options.sortLocalsToTop) { return orderBy( entities, (entity: SearchableEntity) => entity.name, @@ -281,6 +327,28 @@ export class EntitySelector extends React.Component<{ ) } + // sort by name, with local entities at the top + if (shouldBeSortedByName && options.sortLocalsToTop) { + const [localEntities, otherEntities] = partition( + entities, + (entity: SearchableEntity) => entity.local + ) + + const sortedLocalEntities = sortBy( + localEntities, + (entity: SearchableEntity) => + this.localEntityNames?.indexOf(entity.name) + ) + + const sortedOtherEntities = orderBy( + otherEntities, + (entity: SearchableEntity) => entity.name, + sortConfig.order + ) + + return [...sortedLocalEntities, ...sortedOtherEntities] + } + // sort by number column, with missing values at the end const [withValues, withoutValues] = partition( entities, @@ -341,7 +409,7 @@ export class EntitySelector extends React.Component<{ ) return { - selected: this.sortEntities(selected), + selected: this.sortEntities(selected, { sortLocalsToTop: false }), unselected: this.sortEntities(unselected), } } @@ -471,8 +539,9 @@ export class EntitySelector extends React.Component<{ name={entity.name} type={this.isMultiMode ? "checkbox" : "radio"} checked={this.isEntitySelected(entity)} - bar={this.getBarConfigForEntity(entity)} onChange={() => this.onChange(entity.name)} + bar={this.getBarConfigForEntity(entity)} + local={entity.local} /> ))} @@ -491,6 +560,7 @@ export class EntitySelector extends React.Component<{ checked={this.isEntitySelected(entity)} bar={this.getBarConfigForEntity(entity)} onChange={() => this.onChange(entity.name)} + local={entity.local} /> ))} @@ -574,6 +644,7 @@ export class EntitySelector extends React.Component<{ onChange={() => this.onChange(entity.name) } + local={entity.local} /> @@ -614,6 +685,7 @@ export class EntitySelector extends React.Component<{ onChange={() => this.onChange(entity.name) } + local={entity.local} /> @@ -682,18 +754,29 @@ function SelectableEntity({ type, bar, onChange, + local, }: { name: React.ReactNode checked: boolean type: "checkbox" | "radio" bar?: BarConfig onChange: () => void + local?: boolean }) { const Input = { checkbox: Checkbox, radio: RadioButton, }[type] + const label = local ? ( + + {name} + + + ) : ( + name + ) + return (
)} - + {bar && ( {bar.formattedValue}