This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-9382-cfca8e1e5de816c4f0d3f0d97e2b8fe3276f9c34 in repository https://gitbox.apache.org/repos/asf/texera.git
commit a302e9d4f8e98d10e74e66392495e3681633f47b Author: Xinyuan Lin <[email protected]> AuthorDate: Tue Nov 10 05:21:45 2026 +0000 refactor(frontend): remove unused preset wrapper state (#9382) ### What changes were proposed in this PR? Remove the preset wrapper's write-only base state, unused typing subscription, unreachable search branch, constant menu flag with its `nzVisible` binding, and unused template index. Opening and save events still refresh the preset list, which is now a `readonly Preset[]` holding the arrays the service emits rather than copies of them; apply/save/delete behavior is preserved. A field without `parent.fieldGroup` is still rejected at initialization, now by the guard with a message, before the preset fetch subscribes, instead of by a `TypeError` from a discarded `filterPresetFromForm()` call. **+18/−196 lines.** The menu flag has never been enabled by production code, and the base state has no live reader. Both were introduced in [#1164](https://github.com/apache/texera/pull/1164) on January 13, 2022. ### Any related issues, documentation, discussions? Closes #9379. ### How was this PR tested? From `frontend/`: ``` node .yarn/releases/yarn-4.14.1.cjs format:ci node node_modules/typescript/bin/tsc --noEmit -p src/tsconfig.app.json node node_modules/typescript/bin/tsc --noEmit -p src/tsconfig.spec.json node .yarn/releases/yarn-4.14.1.cjs build:ci node node_modules/@angular/cli/bin/ng.js test --watch=false --include=src/app/common/formly/preset-wrapper/preset-wrapper.component.spec.ts --include=src/app/workspace/service/preset/preset.service.spec.ts --include=src/app/workspace/component/property-editor/operator-property-edit-frame/operator-property-edit-frame.component.spec.ts ``` 350 passed; one existing skipped case. Removing the `parent.fieldGroup` check from the guard fails only its initialization test. ### Was this PR authored or co-authored using generative AI tooling? Generated-by: Codex (GPT-6), Claude Code (Claude Opus 5.5) --- .../preset-wrapper/preset-wrapper.component.html | 3 +- .../preset-wrapper.component.spec.ts | 124 ++------------------- .../preset-wrapper/preset-wrapper.component.ts | 87 ++------------- 3 files changed, 18 insertions(+), 196 deletions(-) diff --git a/frontend/src/app/common/formly/preset-wrapper/preset-wrapper.component.html b/frontend/src/app/common/formly/preset-wrapper/preset-wrapper.component.html index 58b571233d..452a6dfd55 100644 --- a/frontend/src/app/common/formly/preset-wrapper/preset-wrapper.component.html +++ b/frontend/src/app/common/formly/preset-wrapper/preset-wrapper.component.html @@ -25,7 +25,6 @@ nzBackdrop="false" [nzOverlayClassName]="searchResults.length === 0 ? 'preset-dropdown no-results' : 'preset-dropdown'" (nzVisibleChange)="onDropdownVisibilityEvent($event)" - [nzVisible]="presetMenuVisible" nzClickHide="true"> <span class="formly-field" @@ -55,7 +54,7 @@ class="preset-menu"> <li nz-menu-item - *ngFor="let preset of searchResults; index as i"> + *ngFor="let preset of searchResults"> <div class="preset-dropdown-item"> <span class="dropdown-entry" diff --git a/frontend/src/app/common/formly/preset-wrapper/preset-wrapper.component.spec.ts b/frontend/src/app/common/formly/preset-wrapper/preset-wrapper.component.spec.ts index 29d887f282..80a94bd100 100644 --- a/frontend/src/app/common/formly/preset-wrapper/preset-wrapper.component.spec.ts +++ b/frontend/src/app/common/formly/preset-wrapper/preset-wrapper.component.spec.ts @@ -47,7 +47,6 @@ describe("PresetWrapperComponent", () => { getPresets: ReturnType<typeof vi.fn>; isValidPreset: ReturnType<typeof vi.fn>; savePresetsStream: Subject<{ type: string; target: string; presets: Preset[] }>; - applyPresetStream: Subject<{ type: string; target: string; preset: Preset }>; }; let messageStub: { error: ReturnType<typeof vi.fn>; @@ -57,9 +56,9 @@ describe("PresetWrapperComponent", () => { }; // Builds a minimal FormlyFieldConfig sufficient for ngOnInit to run. - // ngOnInit also calls filterPresetFromForm(), which iterates - // field.parent.fieldGroup looking for sibling preset-wrapper fields, so - // we expose a single sibling pointing at an empty model by default. + // ngOnInit also requires field.parent.fieldGroup, where savePreset() looks + // for sibling preset-wrapper fields, so we expose a single sibling pointing + // at an empty model by default. const buildField = (overrides: Partial<FormlyFieldConfig> = {}): FormlyFieldConfig => { const self = { key: fieldKey, @@ -85,7 +84,6 @@ describe("PresetWrapperComponent", () => { getPresets: vi.fn().mockReturnValue(of([])), isValidPreset: vi.fn().mockReturnValue(true), savePresetsStream: new Subject(), - applyPresetStream: new Subject(), }; messageStub = { error: vi.fn(), @@ -128,6 +126,11 @@ describe("PresetWrapperComponent", () => { expect(() => component.ngOnInit()).toThrow(); }); + it("throws when parent.fieldGroup is missing", () => { + component.field = buildField({ parent: undefined }); + expect(() => component.ngOnInit()).toThrow(".parent.fieldGroup"); + }); + it("populates searchResults from presetService.getPresets on init", () => { presetServiceStub.getPresets.mockReturnValue(of([testPreset, otherPreset])); component.field = buildField(); @@ -137,24 +140,6 @@ describe("PresetWrapperComponent", () => { expect(presetServiceStub.getPresets).toHaveBeenCalledWith(presetKey.presetType, presetKey.saveTarget); expect(component.searchResults).toEqual([testPreset, otherPreset]); }); - - it("seeds the search term from the form control's value", () => { - formControl.setValue("seeded"); - component.field = buildField(); - - component.ngOnInit(); - - expect(component["searchTerm"]).toBe("seeded"); - }); - - it("seeds an empty search term when the form control holds null", () => { - formControl.setValue(null); - component.field = buildField(); - - component.ngOnInit(); - - expect(component["searchTerm"]).toBe(""); - }); }); describe("setupFieldConfig", () => { @@ -213,35 +198,6 @@ describe("PresetWrapperComponent", () => { }) ).toBe("first, second"); }); - - describe("getSearchResults", () => { - it("returns a copy of all presets when showAllResults is true", () => { - const presets: Preset[] = [testPreset, otherPreset]; - const results = component.getSearchResults(presets, "anything", true); - expect(results).toEqual(presets); - expect(results).not.toBe(presets); - }); - - it("returns all presets when showAllResults is true even if the search term doesn't match", () => { - expect(component.getSearchResults([testPreset], "no-match", true)).toEqual([testPreset]); - }); - - it("filters by case-insensitive prefix match on the entry title when showAllResults is false", () => { - const presets: Preset[] = [testPreset, otherPreset]; - // testPreset title 'testPresetValue' starts with 'TEST' - expect(component.getSearchResults(presets, "TEST", false)).toEqual([testPreset]); - // otherPreset title 'otherPresetValue2' starts with 'other' - expect(component.getSearchResults(presets, "other", false)).toEqual([otherPreset]); - }); - - it("returns the full list when search term is empty and showAllResults is false", () => { - expect(component.getSearchResults([testPreset], "", false)).toEqual([testPreset]); - }); - - it("returns an empty list when the search term matches nothing", () => { - expect(component.getSearchResults([testPreset], "zzzz", false)).toEqual([]); - }); - }); }); describe("dropdown visibility", () => { @@ -307,70 +263,6 @@ describe("PresetWrapperComponent", () => { expect(component.searchResults).toEqual([]); }); - it("does not refresh searchResults from form value changes while the dropdown is closed", async () => { - const baselineCalls = presetServiceStub.getPresets.mock.calls.length; - component.presetMenuVisible = false; - - formControl.setValue("typing"); - // the handler is debounced(0); without this tick it would not have run at all and - // the assertion below would hold no matter what the menu state is - await new Promise(resolve => setTimeout(resolve, 0)); - - // the term is still tracked, but the menu being closed suppresses the refetch - expect(component["searchTerm"]).toBe("typing"); - expect(presetServiceStub.getPresets.mock.calls.length).toBe(baselineCalls); - }); - - it("refreshes searchResults from form value changes while the dropdown is open", async () => { - component.presetMenuVisible = true; - presetServiceStub.getPresets.mockReturnValue(of([testPreset])); - const baselineCalls = presetServiceStub.getPresets.mock.calls.length; - - formControl.setValue("typing"); - // The valueChanges handler is debounced(0) — wait one microtask tick. - await new Promise(resolve => setTimeout(resolve, 0)); - - expect(presetServiceStub.getPresets.mock.calls.length).toBe(baselineCalls + 1); - }); - - it("adopts the preset carried by a matching applyPresetStream event", () => { - presetServiceStub.applyPresetStream.next({ - type: presetKey.presetType, - target: presetKey.applyTarget, - preset: testPreset, - }); - - expect(component["basePreset"]).toBe(testPreset); - }); - - it("ignores applyPresetStream events for a different presetType or applyTarget", () => { - const before = component["basePreset"]; - - presetServiceStub.applyPresetStream.next({ - type: "someOtherType", - target: presetKey.applyTarget, - preset: testPreset, - }); - presetServiceStub.applyPresetStream.next({ - type: presetKey.presetType, - target: "someOtherTarget", - preset: otherPreset, - }); - - expect(component["basePreset"]).toBe(before); - }); - - it("clears the search term when the form value becomes null while the dropdown is open", async () => { - component.presetMenuVisible = true; - component["searchTerm"] = "typed"; - - formControl.setValue(null); - // the valueChanges handler is debounced(0) — wait one tick - await new Promise(resolve => setTimeout(resolve, 0)); - - expect(component["searchTerm"]).toBe(""); - }); - it("stops responding to stream events after ngOnDestroy", () => { component.searchResults = []; component.ngOnDestroy(); diff --git a/frontend/src/app/common/formly/preset-wrapper/preset-wrapper.component.ts b/frontend/src/app/common/formly/preset-wrapper/preset-wrapper.component.ts index 82887b22ae..f75ef6a825 100644 --- a/frontend/src/app/common/formly/preset-wrapper/preset-wrapper.component.ts +++ b/frontend/src/app/common/formly/preset-wrapper/preset-wrapper.component.ts @@ -21,7 +21,7 @@ import { Component, OnDestroy, OnInit } from "@angular/core"; import { FieldWrapper, FormlyFieldConfig } from "@ngx-formly/core"; import { merge } from "lodash"; import { ReplaySubject } from "rxjs"; -import { debounceTime, filter, first, takeUntil } from "rxjs/operators"; +import { filter, first, takeUntil } from "rxjs/operators"; import { Preset, PresetService } from "src/app/workspace/service/preset/preset.service"; import { asType } from "../../util/assert"; import { NzMessageService } from "ng-zorro-antd/message"; @@ -74,13 +74,10 @@ export interface PresetKey { ], }) export class PresetWrapperComponent extends FieldWrapper implements OnInit, OnDestroy { - public searchResults: Preset[] = []; // the list of presets shown in the dropdown - public presetMenuVisible = false; - private searchTerm: string = ""; // a copy of the form field value, used as a search term to narrow suggested presets + public searchResults: readonly Preset[] = []; // the list of presets shown in the dropdown private presetType: string = ""; // corresponds to type used in presetService.getPresets(type, target). Usually "operator" private saveTarget: string = ""; // corresponds to target used in presetService.getPresets(type, target). Usually operator type, i.e. "MySQLSource" private applyTarget: string = ""; // corresponds to target used in presetService.applyPreset(type, target). Usually operatorID, i.e "MySQLSource-operator-8fb88f81-1bb1-4b00-bbd1-3d2f23c5e1d7" - private basePreset: Preset = {}; private teardownObservable: ReplaySubject<boolean> = new ReplaySubject(1); // observable used OnDestroy to tear down subscriptions that takeUntil(teardownObservable) constructor( @@ -94,23 +91,20 @@ export class PresetWrapperComponent extends FieldWrapper implements OnInit, OnDe if ( this.field.key === undefined || this.field.templateOptions === undefined || - this.field.templateOptions.presetKey === undefined + this.field.templateOptions.presetKey === undefined || + this.field.parent?.fieldGroup === undefined ) { throw Error( - `form preset-wrapper field ${this.field} doesn't contain necessary .key and .templateOptions.presetKey attributes` + `form preset-wrapper field ${this.field} doesn't contain necessary .key, .templateOptions.presetKey and .parent.fieldGroup attributes` ); } const presetKey = <PresetKey>this.field.templateOptions.presetKey; - this.searchTerm = this.formControl.value !== null ? this.formControl.value : ""; this.presetType = presetKey.presetType; this.saveTarget = presetKey.saveTarget; this.applyTarget = presetKey.applyTarget; this.updateSearchResults(); - this.basePreset = this.filterPresetFromForm(); this.handleSavePresets(); // handles when presets for this saveTarget change - this.handleApplyPreset(); // handles when presets for this saveTarget change - this.handleFieldValueChanges(); // handles updating search results as the user types } /** @@ -152,26 +146,6 @@ export class PresetWrapperComponent extends FieldWrapper implements OnInit, OnDe .join(", "); } - /** - * Filters a Preset[], allowing only getEntryTitle(Preset) that start with searchTerm - * @param presets Preset[] - * @param searchTerm string - * @param showAllResults whether or not to filter presets or allow all presets - * @returns - */ - public getSearchResults(presets: Readonly<Preset[]>, searchTerm: string, showAllResults: boolean): Preset[] { - if (showAllResults) { - return presets.slice(); - } else { - return presets.filter(preset => - this.getEntryTitle(preset) - .replace(/^\s+|\s+$/g, "") - .toLowerCase() - .startsWith(searchTerm.toLowerCase()) - ); - } - } - /** * updates search results when dropdown is activated (clicking form field opens dropdown) * @param visible Event value, whether or not dropdown is visible @@ -194,12 +168,7 @@ export class PresetWrapperComponent extends FieldWrapper implements OnInit, OnDe public savePreset() { const preset = this.filterPresetFromForm(); if (this.presetService.isValidPreset(preset)) { - this.presetService.createPreset( - this.presetType, - this.saveTarget, - // this.basePreset, - preset - ); + this.presetService.createPreset(this.presetType, this.saveTarget, preset); } else { this.messageService.error("Preset not saved: Fill out all preset fields."); } @@ -217,24 +186,7 @@ export class PresetWrapperComponent extends FieldWrapper implements OnInit, OnDe ) .subscribe({ next: saveEvent => { - this.searchResults = this.getSearchResults(saveEvent.presets, this.searchTerm, true); - }, - }); - } - - /** - * handles when presets for the current presetType are changed due to saving new presets - * updates search results to account for new presets - */ - private handleApplyPreset() { - this.presetService.applyPresetStream - .pipe( - filter(presets => presets.type === this.presetType && presets.target === this.applyTarget), - takeUntil(this.teardownObservable) - ) - .subscribe({ - next: applyEvent => { - this.basePreset = applyEvent.preset; + this.searchResults = saveEvent.presets; }, }); } @@ -254,36 +206,15 @@ export class PresetWrapperComponent extends FieldWrapper implements OnInit, OnDe return preset; } - /** - * handles user typing into form field - * updates earch results to account for filtering - */ - private handleFieldValueChanges() { - // WIERD CODE EXPLANATION: debounceTime(0)? - // After a preset is applied (by clicking a dropdown entry), it changes a field value and - // activates this handler function. - // updating the searchResults (which also changes the HTML template due to binding) too quickly - // can sometimes interrupt the dropdown closing animation. (dropdown should close after clicking dropdown entry, but instead stays open) - // hence the debounceTime(0) to slow this function down. - this.formControl.valueChanges.pipe(debounceTime(0), takeUntil(this.teardownObservable)).subscribe({ - next: (value: string | number | boolean) => { - this.searchTerm = (value ?? "").toString(); - if (this.presetMenuVisible) { - this.updateSearchResults(false); - } - }, - }); - } - /** * updates search results */ - private updateSearchResults(showAllResults = true) { + private updateSearchResults() { this.presetService .getPresets(this.presetType, this.saveTarget) .pipe(first(), takeUntil(this.teardownObservable)) .subscribe(presets => { - this.searchResults = this.getSearchResults(presets, this.searchTerm, showAllResults); + this.searchResults = presets; }); }
