Skip to content

Commit 9079eaa

Browse files
authored
Derive custom titles from URL params (#78)
1 parent 39bda95 commit 9079eaa

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/data/DataSet.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default class DataSet {
2828
public readonly data: readonly EpiPoint[],
2929
public title = '',
3030
public readonly params: Record<string, unknown> | unknown[] | null = null,
31+
public customTitle: string | null = null,
3132
public color = getRandomColor(),
3233
) {
3334
this.gap = computeGap(data);

src/deriveLinkDefaults.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function endpointParams(endpoint: string, params: unknown[]): Record<string, unk
9393
return r;
9494
}
9595

96-
function patchDataSet(title: string, color: string) {
96+
function patchDataSet(title: string, color: string, customTitle: string) {
9797
return (dg: DataGroup | null) => {
9898
if (!dg) {
9999
return null;
@@ -102,6 +102,7 @@ function patchDataSet(title: string, color: string) {
102102
const d = datasets.find((di) => di.title === title);
103103
if (d) {
104104
d.color = color;
105+
d.customTitle = customTitle;
105106
}
106107
return d;
107108
};
@@ -120,12 +121,34 @@ export function initialLoader(datasets: ILinkConfig['datasets']) {
120121
}
121122
const key = `${endpoint}:${JSON.stringify(params)}`;
122123
const existing = loadingDataSets.get(key);
124+
125+
/* eslint-disable @typescript-eslint/restrict-template-expressions */
126+
let customTitle = title;
127+
if (params.custom_title) {
128+
// Custom title present (e.g. from signal documentation) - apply directly
129+
customTitle = `${params.custom_title}`;
130+
} else if (params._endpoint) {
131+
// Derive custom title from params
132+
customTitle = `${params._endpoint}`;
133+
if (params.data_source && params.signal) {
134+
customTitle += ` > ${params.data_source}:${params.signal}`;
135+
}
136+
if (params.geo_type && params.geo_value) {
137+
customTitle += ` > ${params.geo_type}:${params.geo_value}`;
138+
}
139+
if (params.regions) {
140+
customTitle += ` > ${params.regions}`;
141+
}
142+
customTitle += ` > ${title}`;
143+
}
144+
/* eslint-enable @typescript-eslint/restrict-template-expressions */
145+
123146
if (existing) {
124-
resolvedDataSets.push(existing.then(patchDataSet(title, color)));
147+
resolvedDataSets.push(existing.then(patchDataSet(title, color, customTitle)));
125148
} else {
126149
const loadingDataSet = func(params);
127150
loadingDataSets.set(key, loadingDataSet);
128-
resolvedDataSets.push(loadingDataSet.then(patchDataSet(title, color)));
151+
resolvedDataSets.push(loadingDataSet.then(patchDataSet(title, color, customTitle)));
129152
}
130153
}
131154

@@ -156,21 +179,8 @@ export function initialLoader(datasets: ILinkConfig['datasets']) {
156179
return Promise.all(resolvedDataSets).then((data) => {
157180
const cleaned = data.filter((d): d is DataSet => d != null);
158181
cleaned.forEach((d) => {
159-
if (d.params && !Array.isArray(d.params) && d.params._endpoint) {
160-
/* eslint-disable @typescript-eslint/restrict-template-expressions */
161-
const col_name = d.title;
162-
d.title = `${d.params._endpoint}`;
163-
if (d.params.data_source && d.params.signal) {
164-
d.title += ` > ${d.params.data_source}:${d.params.signal}`;
165-
}
166-
if (d.params.geo_type && d.params.geo_value) {
167-
d.title += ` > ${d.params.geo_type}:${d.params.geo_value}`;
168-
}
169-
if (d.params.regions) {
170-
d.title += ` > ${d.params.regions}`;
171-
}
172-
d.title += ` > ${col_name}`;
173-
/* eslint-enable @typescript-eslint/restrict-template-expressions */
182+
if (d.customTitle) {
183+
d.title = d.customTitle;
174184
}
175185
add(d);
176186
});

0 commit comments

Comments
 (0)