Skip to content

Commit c77e1cf

Browse files
🔨 (grapher) replace some enum types / TAS-709 (#4170)
Refactors Grapher types by replacing enums with compile-time-only types. I introduced `GrapherTabName` in the previous PR, which is either a `ChartTypeName` or `"WorldMap"` or `"Table"`. But enums currently [can't be extended or merged](microsoft/TypeScript#17592), which means that I had to copy-paste all chart type names into `GrapherTabName`. As a result, TypeScript doesn't do a good job of type narrowing, forcing me to write (unnecessary) type assertions in many places. This PR refactors `ChartTypeName` and other tab-related types so they don't use enums. However, I still wanted the convenience of a single object to access chart types, for example. To get that, the types are inferred from const objects: ```ts const GRAPHER_CHART_TYPES = { LineChart: "LineChart", ScatterPlot: "ScatterPlot" } as const type GrapherChartType = keyof typeof GRAPHER_CHART_TYPES ``` I first did it the other way around (first defining the types, then using them for the objects), but it turns out the way it's done now leads to better type narrowing. **Newly introduced types:** - `GrapherMapType`: only "WorldMap" - `GrapherChartType`: "LineChart", "ScatterPlot", etc. (without "WorldMap") - `GrapherChartOrMapType`: `GrapherChartType` or `GrapherMapType` - `GrapherTabName`: Internal tab names used in Grapher (a chart type name or "WorldMap" or "Table") - `GrapherTabOption`: Grapher tab specified in the config that determines the default tab to show ("chart", "map" or "tab") - `GrapherTabQueryParam`: Valid values for the `tab` query parameter in Grapher ("chart", "map", "line", "slope", etc.)
1 parent cfe0245 commit c77e1cf

60 files changed

Lines changed: 593 additions & 514 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎adminSiteClient/ChartList.tsx‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { runInAction, observable } from "mobx"
44
import { bind } from "decko"
55
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js"
66
import {
7-
ChartTypeName,
7+
GrapherChartType,
88
GrapherInterface,
9-
GrapherTabOption,
9+
GRAPHER_CHART_TYPES,
10+
GRAPHER_TAB_OPTIONS,
1011
} from "@ourworldindata/types"
1112
import { startCase, DbChartTagJoin } from "@ourworldindata/utils"
1213
import { References, getFullReferencesCount } from "./ChartEditor.js"
@@ -24,7 +25,7 @@ export interface ChartListItem {
2425
tab: GrapherInterface["tab"]
2526
hasMapTab: GrapherInterface["hasMapTab"]
2627

27-
type?: ChartTypeName
28+
type?: GrapherChartType
2829
hasChartTab: boolean
2930

3031
lastEditedAt: string
@@ -152,11 +153,11 @@ export function showChartType(chart: ChartListItem): string {
152153

153154
if (!chartType) return "Map"
154155

155-
const displayType = ChartTypeName[chartType]
156-
? startCase(ChartTypeName[chartType])
156+
const displayType = GRAPHER_CHART_TYPES[chartType]
157+
? startCase(GRAPHER_CHART_TYPES[chartType])
157158
: "Unknown"
158159

159-
if (chart.tab === GrapherTabOption.map) {
160+
if (chart.tab === GRAPHER_TAB_OPTIONS.map) {
160161
if (chart.hasChartTab) return `Map + ${displayType}`
161162
else return "Map"
162163
} else {

‎adminSiteClient/ColorSchemeDropdown.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react"
22
import { computed, action } from "mobx"
33
import Select from "react-select"
4-
import { ChartTypeName } from "@ourworldindata/types"
4+
import { GrapherChartOrMapType } from "@ourworldindata/types"
55
import {
66
ColorScheme,
77
getColorSchemeForChartType,
@@ -21,7 +21,7 @@ interface ColorSchemeDropdownProps {
2121
value?: string
2222
gradientColorCount: number
2323
invertedColorScheme: boolean
24-
chartType: ChartTypeName
24+
chartType: GrapherChartOrMapType
2525
onChange: (selected: ColorSchemeOption) => void
2626
onBlur?: () => void
2727
}

‎adminSiteClient/EditorBasicTab.tsx‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import {
99
} from "mobx"
1010
import { observer } from "mobx-react"
1111
import {
12-
ChartTypeName,
1312
EntitySelectionMode,
1413
StackMode,
14+
ALL_GRAPHER_CHART_TYPES,
15+
GrapherChartType,
1516
} from "@ourworldindata/types"
1617
import {
1718
DimensionSlot,
@@ -362,7 +363,9 @@ export class EditorBasicTab<
362363
const { grapher } = this.props.editor
363364

364365
grapher.chartTypes =
365-
value === this.chartTypeOptionNone ? [] : [value as ChartTypeName]
366+
value === this.chartTypeOptionNone
367+
? []
368+
: [value as GrapherChartType]
366369

367370
if (grapher.isMarimekko) {
368371
grapher.hideRelativeToggle = false
@@ -403,11 +406,7 @@ export class EditorBasicTab<
403406
value: string
404407
label: string
405408
}[] {
406-
const allChartTypes = Object.keys(ChartTypeName).filter(
407-
(chartType) => chartType !== ChartTypeName.WorldMap
408-
)
409-
410-
const chartTypeOptions = allChartTypes.map((key) => ({
409+
const chartTypeOptions = ALL_GRAPHER_CHART_TYPES.map((key) => ({
411410
value: key,
412411
label: startCase(key),
413412
}))

‎adminSiteClient/EditorColorScaleSection.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { faPlus, faMinus } from "@fortawesome/free-solid-svg-icons"
77
import {
88
ColorSchemeName,
99
BinningStrategy,
10-
ChartTypeName,
10+
GrapherChartOrMapType,
1111
Color,
1212
} from "@ourworldindata/types"
1313
import {
@@ -43,7 +43,7 @@ interface EditorColorScaleSectionFeatures {
4343
@observer
4444
export class EditorColorScaleSection extends React.Component<{
4545
scale: ColorScale
46-
chartType: ChartTypeName
46+
chartType: GrapherChartOrMapType
4747
features: EditorColorScaleSectionFeatures
4848
showLineChartColors: boolean
4949
onChange?: () => void
@@ -132,7 +132,7 @@ class ColorLegendSection extends React.Component<{
132132
@observer
133133
class ColorsSection extends React.Component<{
134134
scale: ColorScale
135-
chartType: ChartTypeName
135+
chartType: GrapherChartOrMapType
136136
showLineChartColors: boolean
137137
onChange?: () => void
138138
}> {

‎adminSiteClient/EditorCustomizeTab.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
ColorSchemeName,
77
FacetAxisDomain,
88
FacetStrategy,
9-
ChartTypeName,
9+
GRAPHER_CHART_TYPES,
1010
} from "@ourworldindata/types"
1111
import { Grapher } from "@ourworldindata/grapher"
1212
import {
@@ -161,7 +161,7 @@ export class ColorSchemeSelector extends React.Component<{
161161
onBlur={this.onBlur}
162162
chartType={
163163
this.props.grapher.chartType ??
164-
ChartTypeName.LineChart
164+
GRAPHER_CHART_TYPES.LineChart
165165
}
166166
invertedColorScheme={!!grapher.invertColorScheme}
167167
additionalOptions={[
@@ -755,7 +755,9 @@ export class EditorCustomizeTab<
755755
{grapher.chartInstanceExceptMap.colorScale && (
756756
<EditorColorScaleSection
757757
scale={grapher.chartInstanceExceptMap.colorScale}
758-
chartType={grapher.chartType ?? ChartTypeName.LineChart}
758+
chartType={
759+
grapher.chartType ?? GRAPHER_CHART_TYPES.LineChart
760+
}
759761
showLineChartColors={grapher.isLineChart}
760762
features={{
761763
visualScaling: true,

‎adminSiteClient/EditorMapTab.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
2-
ChartTypeName,
32
GrapherInterface,
43
MapProjectionName,
4+
GRAPHER_MAP_TYPE,
55
} from "@ourworldindata/types"
66
import {
77
ChartDimension,
@@ -202,7 +202,7 @@ export class EditorMapTab<
202202
<TimelineSection mapConfig={mapConfig} />
203203
<EditorColorScaleSection
204204
scale={colorScale}
205-
chartType={ChartTypeName.WorldMap}
205+
chartType={GRAPHER_MAP_TYPE}
206206
showLineChartColors={false}
207207
features={{
208208
visualScaling: true,

‎adminSiteClient/GrapherConfigGridEditor.tsx‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import { BaseEditorComponent, HotColumn, HotTable } from "@handsontable/react"
4444
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js"
4545

4646
import Handsontable from "handsontable"
47-
import { ChartTypeName } from "@ourworldindata/types"
47+
import { GRAPHER_CHART_TYPES, GRAPHER_MAP_TYPE } from "@ourworldindata/types"
4848
import {
4949
Grapher,
5050
GrapherProgrammaticInterface,
@@ -488,7 +488,7 @@ export class GrapherConfigGridEditor extends React.Component<GrapherConfigGridEd
488488
return colorScale ? (
489489
<EditorColorScaleSection
490490
scale={colorScale}
491-
chartType={ChartTypeName.WorldMap}
491+
chartType={GRAPHER_MAP_TYPE}
492492
features={{
493493
visualScaling: true,
494494
legendDescription: false,
@@ -513,7 +513,8 @@ export class GrapherConfigGridEditor extends React.Component<GrapherConfigGridEd
513513
<EditorColorScaleSection
514514
scale={colorScale}
515515
chartType={
516-
grapher.chartType ?? ChartTypeName.LineChart
516+
grapher.chartType ??
517+
GRAPHER_CHART_TYPES.LineChart
517518
}
518519
features={{
519520
visualScaling: true,

‎adminSiteClient/VariableEditPage.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import { SourceList } from "./SourceList.js"
4343
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js"
4444
import { Base64 } from "js-base64"
4545
import {
46-
GrapherTabOption,
46+
GRAPHER_TAB_OPTIONS,
4747
GrapherInterface,
4848
OwidVariableRoundingMode,
4949
} from "@ourworldindata/types"
@@ -691,13 +691,13 @@ class VariableEditor extends React.Component<{
691691
return {
692692
...grapherConfig,
693693
hasMapTab: true,
694-
tab: GrapherTabOption.map,
694+
tab: GRAPHER_TAB_OPTIONS.map,
695695
}
696696
else
697697
return {
698698
yAxis: { min: 0 },
699699
map: { columnSlug: this.props.variable.id.toString() },
700-
tab: GrapherTabOption.map,
700+
tab: GRAPHER_TAB_OPTIONS.map,
701701
hasMapTab: true,
702702
dimensions: [
703703
{

‎adminSiteServer/testPageRouter.tsx‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ import {
2323
} from "@ourworldindata/utils"
2424
import { grapherToSVG } from "../baker/GrapherImageBaker.js"
2525
import {
26-
ChartTypeName,
2726
ColorSchemeName,
2827
DbRawChartConfig,
2928
DbPlainChart,
3029
EntitySelectionMode,
31-
GrapherTabOption,
30+
GRAPHER_TAB_OPTIONS,
3231
StackMode,
3332
parseChartConfig,
33+
GRAPHER_MAP_TYPE,
34+
GrapherTabOption,
35+
GrapherChartOrMapType,
3436
} from "@ourworldindata/types"
3537
import { ExplorerAdminServer } from "../explorerAdminServer/ExplorerAdminServer.js"
3638
import { GIT_CMS_DIR } from "../gitCms/GitCmsConstants.js"
@@ -101,7 +103,7 @@ interface EmbedTestPageQueryParams {
101103
readonly page?: string
102104
readonly random?: string
103105
readonly tab?: GrapherTabOption
104-
readonly type?: ChartTypeName
106+
readonly type?: GrapherChartOrMapType
105107
readonly logLinear?: string
106108
readonly comparisonLines?: string
107109
readonly stackMode?: StackMode
@@ -148,41 +150,41 @@ async function propsFromQueryParams(
148150
let tab = params.tab
149151

150152
if (params.type) {
151-
if (params.type === ChartTypeName.WorldMap) {
153+
if (params.type === GRAPHER_MAP_TYPE) {
152154
query = query.andWhereRaw(`cc.full->>"$.hasMapTab" = "true"`)
153-
tab = tab || GrapherTabOption.map
155+
tab = tab || GRAPHER_TAB_OPTIONS.map
154156
} else {
155157
query = query.andWhereRaw(`cc.chartType = :type`, {
156158
type: params.type,
157159
})
158-
tab = tab || GrapherTabOption.chart
160+
tab = tab || GRAPHER_TAB_OPTIONS.chart
159161
}
160162
}
161163

162164
if (params.logLinear) {
163165
query = query.andWhereRaw(
164166
`cc.full->>'$.yAxis.canChangeScaleType' = "true" OR cc.full->>'$.xAxis.canChangeScaleType' = "true"`
165167
)
166-
tab = GrapherTabOption.chart
168+
tab = GRAPHER_TAB_OPTIONS.chart
167169
}
168170

169171
if (params.comparisonLines) {
170172
query = query.andWhereRaw(
171173
`cc.full->'$.comparisonLines[0].yEquals' != ''`
172174
)
173-
tab = GrapherTabOption.chart
175+
tab = GRAPHER_TAB_OPTIONS.chart
174176
}
175177

176178
if (params.stackMode) {
177179
query = query.andWhereRaw(`cc.full->'$.stackMode' = :stackMode`, {
178180
stackMode: params.stackMode,
179181
})
180-
tab = GrapherTabOption.chart
182+
tab = GRAPHER_TAB_OPTIONS.chart
181183
}
182184

183185
if (params.relativeToggle) {
184186
query = query.andWhereRaw(`cc.full->>'$.hideRelativeToggle' = "false"`)
185-
tab = GrapherTabOption.chart
187+
tab = GRAPHER_TAB_OPTIONS.chart
186188
}
187189

188190
if (params.categoricalLegend) {
@@ -192,7 +194,7 @@ async function propsFromQueryParams(
192194
query = query.andWhereRaw(
193195
`json_length(cc.full->'$.map.colorScale.customCategoryColors') > 1`
194196
)
195-
tab = GrapherTabOption.map
197+
tab = GRAPHER_TAB_OPTIONS.map
196198
}
197199

198200
if (params.mixedTimeTypes) {
@@ -232,9 +234,9 @@ async function propsFromQueryParams(
232234
query = query.andWhereRaw(`charts.id IN (${params.ids})`)
233235
}
234236

235-
if (tab === GrapherTabOption.map) {
237+
if (tab === GRAPHER_TAB_OPTIONS.map) {
236238
query = query.andWhereRaw(`cc.full->>"$.hasMapTab" = "true"`)
237-
} else if (tab === GrapherTabOption.chart) {
239+
} else if (tab === GRAPHER_TAB_OPTIONS.chart) {
238240
query = query.andWhereRaw(`cc.chartType IS NOT NULL`)
239241
}
240242

‎baker/updateChartEntities.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
ChartsXEntitiesTableName,
1010
DbPlainChart,
1111
GrapherInterface,
12-
GrapherTabOption,
12+
GRAPHER_TAB_OPTIONS,
1313
MultipleOwidVariableDataDimensionsMap,
1414
OwidVariableDataMetadataDimensions,
1515
DbRawChartConfig,
@@ -97,7 +97,7 @@ const obtainAvailableEntitiesForGrapherConfig = async (
9797

9898
// If the grapher has a chart tab, then the available entities there are the "most interesting" ones to us
9999
if (grapher.hasChartTab) {
100-
grapher.tab = GrapherTabOption.chart
100+
grapher.tab = GRAPHER_TAB_OPTIONS.chart
101101

102102
// If the grapher allows for changing or multi-selecting entities, then let's index all entities the
103103
// user can choose from. Otherwise, we'll just use the default-selected entities.
@@ -112,7 +112,7 @@ const obtainAvailableEntitiesForGrapherConfig = async (
112112
return grapher.tableForSelection.availableEntityNames as string[]
113113
else return grapher.selectedEntityNames ?? []
114114
} else if (grapher.hasMapTab) {
115-
grapher.tab = GrapherTabOption.map
115+
grapher.tab = GRAPHER_TAB_OPTIONS.map
116116
// On a map tab, tableAfterAuthorTimelineAndActiveChartTransform contains all
117117
// mappable entities for which data is available
118118
return grapher.tableAfterAuthorTimelineAndActiveChartTransform

0 commit comments

Comments
 (0)