Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions addon/components/hyper-table-v2/manage-columns.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,44 @@
data-control-name="column_definitions_search_input"
/>
</div>
<div class="fields-list">
{{#each-in this.orderedFilteredClusters as |clusterName fields|}}
{{#if clusterName}}
<div
class="fields-list"
{{did-insert this.registerFieldsList}}
{{will-destroy this.unregisterFieldsList}}
{{on-bottom-reached this.renderMore offset=200}}
>
{{#each this.renderedRows key="id" as |row|}}
{{#if (eq row.type "cluster")}}
<div class="cluster-name font-weight-semibold margin-top-px-6">
{{clusterName}}
{{row.name}}
</div>
{{/if}}
{{#each fields as |field|}}
{{else}}
<div
class="field"
role="button"
{{on "mousedown" this.noop}}
{{on "click" (fn this.columnVisibilityUpdate field)}}
{{on "click" (fn this.columnVisibilityUpdate row.definition)}}
>
{{#if field.isLoading}}
{{#if (this.isLoading row.definition)}}
<div><OSS::Icon @icon="fa-spinner fa-spin" /></div>
{{else}}
<OSS::Checkbox
@checked={{field.visible}}
@onChange={{fn this.columnVisibilityUpdate field}}
@checked={{this.isVisible row.definition}}
@onChange={{fn this.columnVisibilityUpdate row.definition}}
@size="sm"
data-control-name={{concat "column_definition_toggle_checkbox_" field.definition.key}}
data-control-name={{concat "column_definition_toggle_checkbox_" row.definition.key}}
/>
{{/if}}
<div class="margin-left-px-6">
{{field.definition.name}}
{{row.definition.name}}
</div>
</div>
{{/each}}
{{/if}}
{{else}}
<div class="field margin-top-md">
{{t "hypertable.features.manage_fields.no_columns"}}
</div>
{{/each-in}}
{{/each}}
</div>
</div>
</div>
Expand Down
223 changes: 150 additions & 73 deletions addon/components/hyper-table-v2/manage-columns.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { set, action } from '@ember/object';
import { action } from '@ember/object';
import { later, next } from '@ember/runloop';
import { compare, isEmpty } from '@ember/utils';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { cached, tracked } from '@glimmer/tracking';

import TableHandler from '@upfluence/hypertable/core/handler';
import { ColumnDefinition } from '@upfluence/hypertable/core/interfaces';

type ManagedColumn = {
definition: ColumnDefinition;
visible: boolean;
isLoading: boolean;
};
type ClusterRow = { id: string; type: 'cluster'; name: string };
type FieldRow = { id: string; type: 'field'; definition: ColumnDefinition };
type Row = ClusterRow | FieldRow;

type IndexedField = { row: FieldRow; searchableName: string };
type IndexedCluster = { header: ClusterRow | null; fields: IndexedField[] };

export const PAGE_SIZE = 100;
const DROPDOWN_HIDE_DELAY = 300;
const DROPDOWN_FOCUS_DELAY = 150;

interface HyperTableV2ManageColumnsArgs {
handler: TableHandler;
Expand All @@ -23,7 +28,12 @@ export default class HyperTableV2ManageColumns extends Component<HyperTableV2Man
@tracked activeColumnCategory: null | string = null;
@tracked searchColumnDefinitionKeyword: null | string = null;
@tracked dropdownVisibility: 'visible' | 'invisible' = 'invisible';
@tracked renderLimit: number = PAGE_SIZE;
@tracked loadingKeys: Set<string> = new Set();

fieldsListElement: HTMLElement | null = null;

@cached
get _columnCategories(): string[] {
return (this.args.handler.columnDefinitions || [])
.reduce((categories: string[], columnDefinition) => {
Expand All @@ -35,68 +45,108 @@ export default class HyperTableV2ManageColumns extends Component<HyperTableV2Man
.sort((a, b) => a.localeCompare(b));
}

get orderedFilteredClusters(): Map<string, ManagedColumn[]> {
const fields = (this.args.handler.columnDefinitions || [])
.filter((columnDefinition) => {
const search = (this.searchColumnDefinitionKeyword || '').toLowerCase();
const hasSearched =
!this.searchColumnDefinitionKeyword || columnDefinition.name.toLowerCase().indexOf(search) >= 0;
const hasActiveGroup = !this.activeColumnCategory || columnDefinition.category === this.activeColumnCategory;
return hasActiveGroup && hasSearched;
})
.sort((a, b) => compare(a.name, b.name));

return this.groupByClusteringKey(fields);
}

groupByClusteringKey(columnDefinitions: ColumnDefinition[]): Map<string, ManagedColumn[]> {
const map = new Map();

columnDefinitions.forEach((columnDefinition) => {
if (this.args.handler.columns.length > 0 && columnDefinition.position?.sticky) {
return;
}
const key = columnDefinition.clustering_key === 'twitter' ? 'x' : columnDefinition.clustering_key;
const cluster = map.get(key);
const manageColumn: ManagedColumn = {
definition: columnDefinition,
visible: !isEmpty(this.args.handler.columns.find((column) => column?.definition.key === columnDefinition.key)),
isLoading: false
};

if (!cluster) {
map.set(key, [manageColumn]);
} else {
cluster.push(manageColumn);
}
@cached
get _indexedClusters(): IndexedCluster[] {
const clusters = new Map<string, IndexedField[]>();

[...(this.args.handler.columnDefinitions ?? [])]
.sort((a, b) => compare(a.name, b.name))
.forEach((definition) => {
const clusterKey = (definition.clustering_key === 'twitter' ? 'x' : definition.clustering_key) ?? '';
const field = {
row: { id: `field:${definition.key}`, type: 'field' as const, definition },
searchableName: (definition.name || '').toLowerCase()
};

const cluster = clusters.get(clusterKey);
if (cluster) {
cluster.push(field);
} else {
clusters.set(clusterKey, [field]);
}
});

return [...clusters]
.sort((a, b) => a[0].localeCompare(b[0]))
.map(([name, fields]) => ({
header: name ? { id: `cluster:${name}`, type: 'cluster' as const, name } : null,
fields
}));
}

@cached

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did not know this one, good to know ^^

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's one of the decorators we gained w/ the upgrade. https://api.emberjs.com/ember/6.12/functions/@glimmer%2Ftracking/cached/

When dealing with large sets of data that can expensive to recompute, it ensures the getter is evaluated is only done when the watched properties's cache is invalidated, and not every time it's accessed :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To use sparingly if i remember the docs 😉

get _visibleKeys(): Set<string> {
return new Set(this.args.handler.columns.map((column) => column?.definition?.key));
}

@cached
get filteredRows(): Row[] {
const search = (this.searchColumnDefinitionKeyword || '').toLowerCase();
const hideStickyColumns = this.args.handler.columns.length > 0;
const rows: Row[] = [];

this._indexedClusters.forEach(({ header, fields }) => {
const matchingFields = fields.filter(({ row: { definition }, searchableName }) => {
if (hideStickyColumns && definition.position?.sticky) return false;
if (this.activeColumnCategory && definition.category !== this.activeColumnCategory) return false;
return !search || searchableName.includes(search);
});

if (matchingFields.length === 0) return;
if (header) rows.push(header);
matchingFields.forEach(({ row }) => rows.push(row));
});

return new Map([...map].sort((a, b) => a[0].localeCompare(b[0])));
return rows;
}

get renderedRows(): Row[] {
return this.filteredRows.slice(0, this.renderLimit);
}

noop(event: Event) {
event.preventDefault();
}

@action
columnVisibilityUpdate(column: ManagedColumn, event: PointerEvent): void {
set(column, 'isLoading', true);
event?.stopPropagation?.();
if (column.visible) {
this.args.handler.removeColumn(column.definition).then(() => {
set(column, 'isLoading', false);
});
} else {
this.args.handler.addColumn(column.definition).then(() => {
set(column, 'isLoading', false);
this.args.didInsertColumn?.();
});
registerFieldsList(element: HTMLElement): void {
this.fieldsListElement = element;
}

@action
unregisterFieldsList(): void {
this.fieldsListElement = null;
}

@action
renderMore(): void {
if (this.renderLimit < this.filteredRows.length) {
this.renderLimit += PAGE_SIZE;
}
}

@action
columnVisibilityUpdate(definition: ColumnDefinition, event?: PointerEvent): void {
event?.stopPropagation?.();

if (this.isLoading(definition)) return;

this.setLoading(definition.key, true);
const update = this.isVisible(definition)
? this.args.handler.removeColumn(definition)
: this.args.handler.addColumn(definition).then(() => {
this.args.didInsertColumn?.();
});

update.finally(() => {
this.setLoading(definition.key, false);
});
}

@action
setFieldCategory(category: string): void {
this.activeColumnCategory = category;
this.resetRenderedRows();
}

@action
Expand All @@ -105,35 +155,62 @@ export default class HyperTableV2ManageColumns extends Component<HyperTableV2Man
this.closeAvailableFields();
} else {
this.displayAvailableFields = true;
next(this, () => {
this.dropdownVisibility = 'visible';

later(
this,
() => {
document.querySelector<HTMLInputElement>('.available-fields-wrapper input')?.focus();
},
150
);
});
next(this, this.dropdownBecameVisible);
}
}

@action
closeAvailableFields(): void {
if (!this.displayAvailableFields) return;

this.dropdownVisibility = 'invisible';
later(
this,
() => {
this.displayAvailableFields = false;
this.searchColumnDefinitionKeyword = '';
},
300
);
later(this, this.dropdownBecameHidden, DROPDOWN_HIDE_DELAY);
}

@action
onSearchUpdate(value: string): void {
this.searchColumnDefinitionKeyword = value;
this.resetRenderedRows();
}

private isVisible = (definition: ColumnDefinition): boolean => {
return this._visibleKeys.has(definition.key);
}

private isLoading = (definition: ColumnDefinition): boolean => {
return this.loadingKeys.has(definition.key);
}

private resetRenderedRows(): void {
this.renderLimit = PAGE_SIZE;
if (this.fieldsListElement) {
this.fieldsListElement.scrollTop = 0;
}
}

private setLoading(key: string, loading: boolean): void {
const loadingKeys = new Set(this.loadingKeys);
if (loading) {
loadingKeys.add(key);
} else {
loadingKeys.delete(key);
}
this.loadingKeys = loadingKeys;
}

private dropdownBecameVisible(): void {
this.dropdownVisibility = 'visible';

later(this, this.focusSearchInput, DROPDOWN_FOCUS_DELAY);
}

private dropdownBecameHidden(): void {
this.displayAvailableFields = false;
this.searchColumnDefinitionKeyword = '';
this.resetRenderedRows();
}

private focusSearchInput(): void {
document.querySelector<HTMLInputElement>('.available-fields-wrapper input')?.focus();
}
}
Loading
Loading