From 265c81908c37c93019f0ce15fff7a145b354a432 Mon Sep 17 00:00:00 2001 From: sophiamersmann Date: Fri, 5 Apr 2024 14:25:58 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89=20(entity=20selector)=20highlight?= =?UTF-8?q?=20user=20location?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/entitySelector/EntitySelector.scss | 27 +++++ .../src/entitySelector/EntitySelector.tsx | 106 ++++++++++++++++-- 2 files changed, 125 insertions(+), 8 deletions(-) diff --git a/packages/@ourworldindata/grapher/src/entitySelector/EntitySelector.scss b/packages/@ourworldindata/grapher/src/entitySelector/EntitySelector.scss index 0aea1f1dd4e..283b2cb5f5f 100644 --- a/packages/@ourworldindata/grapher/src/entitySelector/EntitySelector.scss +++ b/packages/@ourworldindata/grapher/src/entitySelector/EntitySelector.scss @@ -177,6 +177,21 @@ background: #ebeef2; z-index: -1; } + + .label-with-location-icon { + display: flex; + align-items: center; + + svg { + margin-left: 8px; + font-size: 0.9em; + color: #a1a1a1; + + &:focus { + outline: none; + } + } + } } .animated-entity { @@ -287,4 +302,16 @@ list-style-type: none; } } + + // the tooltip triggered by hovering the location icon + .tippy-box[data-theme="location-icon"] { + background: white; + color: $dark-text; + font: 400 14px/1.5 $sans-serif-font-stack; + box-shadow: 0px 4px 40px 0px rgba(0, 0, 0, 0.15); + + .tippy-arrow { + color: white; + } + } } diff --git a/packages/@ourworldindata/grapher/src/entitySelector/EntitySelector.tsx b/packages/@ourworldindata/grapher/src/entitySelector/EntitySelector.tsx index 6aa82f9d481..d3c7502b1a7 100644 --- a/packages/@ourworldindata/grapher/src/entitySelector/EntitySelector.tsx +++ b/packages/@ourworldindata/grapher/src/entitySelector/EntitySelector.tsx @@ -13,14 +13,18 @@ import { isFiniteWithGuard, CoreValueType, clamp, - sortBy, last, + getUserCountryInformation, + regions, + sortBy, + Tippy, } 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" @@ -45,6 +49,7 @@ import { scaleLinear, type ScaleLinear } from "d3-scale" export interface EntitySelectorState { searchInput: string sortConfig: SortConfig + localEntityNames?: string[] mostRecentlySelectedEntityName?: string } @@ -66,7 +71,7 @@ interface SortConfig { order: SortOrder } -type SearchableEntity = { name: string } & Record< +type SearchableEntity = { name: string; local?: boolean } & Record< Slug, CoreValueType | undefined > @@ -96,6 +101,8 @@ export class EntitySelector extends React.Component<{ } componentDidMount(): void { + void this.populateLocalEntities() + if (this.props.autoFocus && !isTouchDevice()) this.searchField.current?.focus() @@ -137,6 +144,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: "" }) } @@ -182,6 +217,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 } @@ -258,6 +297,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) @@ -268,13 +312,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, @@ -282,6 +329,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, @@ -342,7 +411,7 @@ export class EntitySelector extends React.Component<{ ) return { - selected: this.sortEntities(selected), + selected: this.sortEntities(selected, { sortLocalsToTop: false }), unselected: this.sortEntities(unselected), } } @@ -474,8 +543,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} /> ))} @@ -494,6 +564,7 @@ export class EntitySelector extends React.Component<{ checked={this.isEntitySelected(entity)} bar={this.getBarConfigForEntity(entity)} onChange={() => this.onChange(entity.name)} + local={entity.local} /> ))} @@ -577,6 +648,7 @@ export class EntitySelector extends React.Component<{ onChange={() => this.onChange(entity.name) } + local={entity.local} /> @@ -617,6 +689,7 @@ export class EntitySelector extends React.Component<{ onChange={() => this.onChange(entity.name) } + local={entity.local} /> @@ -693,18 +766,35 @@ 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}