Skip to content

Commit 8de3356

Browse files
refactor: move some types around to make table api friendlier (#616)
1 parent b66329f commit 8de3356

File tree

6 files changed

+42
-49
lines changed

6 files changed

+42
-49
lines changed

projects/components/src/table/controls/table-controls-api.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface CheckboxControl {
2121

2222
export interface CheckboxChange {
2323
checkbox: CheckboxControl;
24-
option: TableControlOption<unknown, boolean>;
24+
option: TableControlOption<boolean>;
2525
}
2626

2727
export const enum TableControlOptionType {
@@ -30,26 +30,29 @@ export const enum TableControlOptionType {
3030
UnsetFilter = 'unset-filter'
3131
}
3232

33-
export interface TableControlOption<TMetaValue = unknown, TValue = unknown> {
34-
type: TableControlOptionType;
35-
label: string;
36-
metaValue: TMetaValue; // Used in a query - type based on TableControlOptionType
37-
value?: TValue; // If a control needs to carry a value, use this (example: checkbox boolean)
38-
}
33+
export type TableControlOption<T = unknown> =
34+
| TableUnsetFilterControlOption<T>
35+
| TableFilterControlOption<T>
36+
| TablePropertyControlOption<T>;
3937

40-
export interface TableUnsetFilterControlOption extends TableControlOption<string, undefined> {
38+
interface TableControlOptionBase<T> {
39+
value?: T;
40+
}
41+
export interface TableUnsetFilterControlOption<T = unknown> extends TableControlOptionBase<T> {
4142
type: TableControlOptionType.UnsetFilter;
4243
metaValue: string;
4344
}
4445

45-
export interface TableFilterControlOption extends TableControlOption<TableFilter> {
46+
export interface TableFilterControlOption<T = unknown> extends TableControlOptionBase<T> {
4647
type: TableControlOptionType.Filter;
4748
metaValue: TableFilter;
4849
}
4950

50-
export interface TablePropertyControlOption extends TableControlOption<Dictionary<unknown>> {
51+
export interface TablePropertyControlOption<T = unknown> extends TableControlOptionBase<T> {
5152
type: TableControlOptionType.Property;
5253
metaValue: Dictionary<unknown>;
5354
}
5455

55-
export type TableCheckboxOptions = [TableControlOption<unknown, true>, TableControlOption<unknown, false>];
56+
export type TableCheckboxControlOption<T extends boolean> = TableControlOption<T> & { label: string };
57+
58+
export type TableCheckboxOptions = [TableCheckboxControlOption<true>, TableCheckboxControlOption<false>];

projects/distributed-tracing/src/public-api.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ export * from './shared/graphql/request/builders/specification/trace/trace-statu
1818
// Dashboard Graphql
1919
export * from './shared/dashboard/data/graphql/filter/graphql-filter-data-source.model';
2020
export * from './shared/dashboard/data/graphql/filter/graphql-filter-data-source.model';
21-
export * from './shared/dashboard/data/graphql/graphql-table-control-options-data-source.model';
2221
export * from './shared/dashboard/data/graphql/graphql-data-source.model';
23-
export * from './shared/dashboard/data/graphql/graphql-table-control-options-data-source.model';
2422
export * from './shared/dashboard/data/graphql/graphql-query-event.service';
2523
export * from './shared/dashboard/data/graphql/graphql-data-source.module';
2624
export * from './shared/dashboard/data/graphql/specifiers/attribute-specification.model';

projects/distributed-tracing/src/shared/dashboard/data/graphql/graphql-table-control-options-data-source.model.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

projects/distributed-tracing/src/shared/dashboard/widgets/table/table-widget-control.model.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ export abstract class TableWidgetControlModel {
2121
@ModelInject(MODEL_API)
2222
protected readonly api!: ModelApi;
2323

24-
public getOptions(): Observable<TableControlOption[]> {
25-
return this.api.getData<TableControlOption[]>().pipe(
26-
map((options: TableControlOption[]) => (this.uniqueValues ? this.filterUniqueValues(options) : options)),
27-
map((options: TableControlOption[]) => (this.sort ? this.applySort(options) : options))
24+
public getOptions(): Observable<LabeledTableControlOption[]> {
25+
return this.api.getData<LabeledTableControlOption[]>().pipe(
26+
map(options => (this.uniqueValues ? this.filterUniqueValues(options) : options)),
27+
map(options => (this.sort ? this.applySort(options) : options))
2828
);
2929
}
3030

31-
private filterUniqueValues(options: TableControlOption[]): TableControlOption[] {
32-
return uniqWith(options, (a: TableControlOption, b: TableControlOption) => a.value === b.value);
31+
private filterUniqueValues(options: LabeledTableControlOption[]): LabeledTableControlOption[] {
32+
return uniqWith(options, (a, b) => a.value === b.value);
3333
}
3434

35-
private applySort(options: TableControlOption[]): TableControlOption[] {
36-
return options.sort((a: TableControlOption, b: TableControlOption) => {
35+
private applySort(options: LabeledTableControlOption[]): LabeledTableControlOption[] {
36+
return options.sort((a, b) => {
3737
// Unset option always at the top
3838
if (a.type === TableControlOptionType.UnsetFilter) {
3939
return -1;
@@ -46,3 +46,7 @@ export abstract class TableWidgetControlModel {
4646
});
4747
}
4848
}
49+
50+
export type LabeledTableControlOption = TableControlOption & {
51+
label: string;
52+
};

projects/distributed-tracing/src/shared/dashboard/widgets/table/table-widget-renderer.component.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@ import {
1414
FilterOperator,
1515
SelectChange,
1616
SelectControl,
17-
SelectOption,
1817
StatefulTableRow,
19-
TableCheckboxOptions,
2018
TableColumnConfig,
21-
TableControlOption,
2219
TableControlOptionType,
2320
TableDataSource,
2421
TableFilter,
@@ -168,8 +165,8 @@ export class TableWidgetRendererComponent
168165
// Fetch the values for the selectFilter dropdown
169166
selectControlModel.getOptions().pipe(
170167
first(),
171-
map((options: TableControlOption[]) => {
172-
const selectOptions: SelectOption<TableControlOption>[] = options.map(option => ({
168+
map(options => {
169+
const selectOptions = options.map(option => ({
173170
label: option.label,
174171
value: option
175172
}));
@@ -189,7 +186,7 @@ export class TableWidgetRendererComponent
189186
this.model.getCheckboxControlOptions().map(checkboxControlModel =>
190187
checkboxControlModel.getOptions().pipe(
191188
first(),
192-
map((options: TableCheckboxOptions) => ({
189+
map(options => ({
193190
label: checkboxControlModel.checked ? options[0].label : options[1].label,
194191
value: checkboxControlModel.checked,
195192
options: options
@@ -278,28 +275,28 @@ export class TableWidgetRendererComponent
278275
);
279276
break;
280277
case TableControlOptionType.Filter:
281-
this.selectFilterSubject.next(this.mergeFilters(changed.value.metaValue as TableFilter));
278+
this.selectFilterSubject.next(this.mergeFilters(changed.value.metaValue));
282279
break;
283280
case TableControlOptionType.Property:
284-
this.queryPropertiesSubject.next(this.mergeQueryProperties(changed.value.metaValue as Dictionary<unknown>));
281+
this.queryPropertiesSubject.next(this.mergeQueryProperties(changed.value.metaValue));
285282
break;
286283
default:
287-
assertUnreachable(changed.value.type);
284+
assertUnreachable(changed.value);
288285
}
289286
}
290287

291288
public onCheckboxChange(changed: CheckboxChange): void {
292289
switch (changed.option.type) {
293290
case TableControlOptionType.Property:
294-
this.queryPropertiesSubject.next(this.mergeQueryProperties(changed.option.metaValue as Dictionary<unknown>));
291+
this.queryPropertiesSubject.next(this.mergeQueryProperties(changed.option.metaValue));
295292
break;
296293
case TableControlOptionType.Filter:
297-
this.selectFilterSubject.next(this.mergeFilters(changed.option.metaValue as TableFilter));
294+
this.selectFilterSubject.next(this.mergeFilters(changed.option.metaValue));
298295
break;
299296
case TableControlOptionType.UnsetFilter:
300297
break; // Not supported - No use case yet
301298
default:
302-
assertUnreachable(changed.option.type);
299+
assertUnreachable(changed.option);
303300
}
304301

305302
// Update checkbox option label
@@ -308,7 +305,7 @@ export class TableWidgetRendererComponent
308305
this.model.getCheckboxControlOptions().map(checkboxControlModel =>
309306
checkboxControlModel.getOptions().pipe(
310307
first(),
311-
map((options: TableCheckboxOptions) => {
308+
map(options => {
312309
options.forEach(option => {
313310
if (option === changed.option) {
314311
checkboxControlModel.checked = changed.option.value === true;

projects/observability/src/shared/dashboard/data/graphql/entity/attribute/entities-attribute-options-data-source.model.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {
22
FilterOperator,
3-
TableControlOption,
43
TableControlOptionType,
5-
TableFilter,
4+
TableFilterControlOption,
65
TableUnsetFilterControlOption
76
} from '@hypertrace/components';
87
import { Model, ModelProperty, STRING_PROPERTY } from '@hypertrace/hyperdash';
@@ -21,20 +20,20 @@ export class EntitiesAttributeOptionsDataSourceModel extends EntitiesAttributeDa
2120
})
2221
public unsetLabel: string = 'All';
2322

24-
private buildUnsetOption(attribute: string): TableUnsetFilterControlOption {
23+
private buildUnsetOption(attribute: string): Labeled<TableUnsetFilterControlOption> {
2524
return {
2625
type: TableControlOptionType.UnsetFilter,
2726
label: this.unsetLabel,
2827
metaValue: attribute
2928
};
3029
}
3130

32-
public getData(): Observable<TableControlOption<TableFilter | string>[]> {
31+
public getData(): Observable<(Labeled<TableUnsetFilterControlOption> | Labeled<TableFilterControlOption>)[]> {
3332
return super.getData().pipe(
3433
map((values: unknown[]) => [
3534
this.buildUnsetOption(this.specification.name),
3635
...values.map(value => ({
37-
type: TableControlOptionType.Filter,
36+
type: TableControlOptionType.Filter as const,
3837
label: String(value),
3938
value: value,
4039
metaValue: {
@@ -47,3 +46,5 @@ export class EntitiesAttributeOptionsDataSourceModel extends EntitiesAttributeDa
4746
);
4847
}
4948
}
49+
50+
type Labeled<T> = T & { label: string };

0 commit comments

Comments
 (0)