Skip to content

Commit

Permalink
fix: fix default id search for runs (#9988)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtonG authored Sep 25, 2024
1 parent 3ca3d30 commit e92c474
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useDrag, useDrop } from 'react-dnd';
import { debounce } from 'throttle-debounce';

import ConjunctionContainer from 'components/FilterForm/components/ConjunctionContainer';
import { FilterFormStore, getInitField } from 'components/FilterForm/components/FilterFormStore';
import { FilterFormStore } from 'components/FilterForm/components/FilterFormStore';
import {
AvailableOperators,
Conjunction,
Expand Down Expand Up @@ -201,7 +201,10 @@ const FilterField = ({
(e: React.KeyboardEvent) => {
// would use isComposing alone but safari has a bug: https://bugs.webkit.org/show_bug.cgi?id=165004
if (e.key === 'Enter' && !inputOpen && !e.nativeEvent.isComposing && e.keyCode !== 229) {
formStore.addChild(parentId, FormKind.Field, { index: index + 1, item: getInitField() });
formStore.addChild(parentId, FormKind.Field, {
index: index + 1,
item: formStore.newField(),
});
// stop panel flashing for selects and dates
if (
field.type === 'COLUMN_TYPE_DATE' ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const jsonReplacer = (key: string, value: unknown): unknown => {
return key === 'id' ? undefined : value;
};

const initField = JSON.parse(JSON.stringify(getInitField(), jsonReplacer));
const initField = JSON.parse(JSON.stringify(getInitField(V1LocationType.EXPERIMENT), jsonReplacer));

const ROOT_ID = 'ROOT';

Expand Down Expand Up @@ -114,12 +114,12 @@ const initData: Readonly<FilterFormSet> = {
describe('FilterFormStore', () => {
describe('Init', () => {
it('should initialize store as NotLoaded', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
expect(filterFormStore.formset.get()).toEqual(NotLoaded);
});

it('should have an empty init() fill with default values', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init();
const loadableFormset = filterFormStore.formset.get();

Expand All @@ -141,7 +141,7 @@ describe('FilterFormStore', () => {
});

it('should initialize store with init data', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init(initData);

const loadableFormset = filterFormStore.formset.get();
Expand All @@ -151,7 +151,7 @@ describe('FilterFormStore', () => {
});

it('should deep clone init data to avoid unexpected data overwrite', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init(initData);
filterFormStore.addChild(ROOT_ID, FormKind.Field);
const jsonWithId = filterFormStore.formset.get();
Expand All @@ -174,14 +174,14 @@ describe('FilterFormStore', () => {

describe('Basic Field and Group Interaction', () => {
it('should sweep invalid groups and conditions', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init(initData);
filterFormStore.sweep();
expect(filterFormStore.fieldCount.get()).toBe(5);
});

it('should add new fields', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init();
filterFormStore.addChild(ROOT_ID, FormKind.Field);

Expand Down Expand Up @@ -211,7 +211,7 @@ describe('FilterFormStore', () => {
});

it('should add new groups', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init();
filterFormStore.addChild(ROOT_ID, FormKind.Group);

Expand Down Expand Up @@ -266,7 +266,7 @@ describe('FilterFormStore', () => {
});

it('should add new fields/group comprehensively', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init();
filterFormStore.addChild(ROOT_ID, FormKind.Group);
filterFormStore.addChild(ROOT_ID, FormKind.Field);
Expand Down Expand Up @@ -319,7 +319,7 @@ describe('FilterFormStore', () => {
});

it('should remove field', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init();
filterFormStore.addChild(ROOT_ID, FormKind.Field);
filterFormStore.addChild(ROOT_ID, FormKind.Field);
Expand All @@ -338,7 +338,7 @@ describe('FilterFormStore', () => {
});

it('should remove empty group', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init();
filterFormStore.addChild(ROOT_ID, FormKind.Group);
let formset = getFormset(filterFormStore);
Expand All @@ -353,7 +353,7 @@ describe('FilterFormStore', () => {
});

it('should remove non-empty group', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init();
filterFormStore.addChild(ROOT_ID, FormKind.Group);
const loadableFormset = filterFormStore.formset.get();
Expand All @@ -367,7 +367,7 @@ describe('FilterFormStore', () => {
});

it('should clear all (remove ROOT_ID)', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init();
filterFormStore.removeChild(ROOT_ID);
expect(filterFormStore.asJsonString.get()).toStrictEqual(JSON.stringify(EMPTY_DATA));
Expand All @@ -376,7 +376,7 @@ describe('FilterFormStore', () => {
filterFormStore.removeChild(ROOT_ID);
expect(filterFormStore.asJsonString.get()).toStrictEqual(JSON.stringify(EMPTY_DATA));

const filterFormStoreWithInit = new FilterFormStore();
const filterFormStoreWithInit = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStoreWithInit.init(initData);
filterFormStoreWithInit.removeChild(ROOT_ID);
expect(filterFormStoreWithInit.asJsonString.get()).toStrictEqual(
Expand All @@ -385,7 +385,7 @@ describe('FilterFormStore', () => {
});

it('should change `show archived` value', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init();
filterFormStore.setArchivedValue(true);
let loadableFormset = filterFormStore.formset.get();
Expand All @@ -402,7 +402,7 @@ describe('FilterFormStore', () => {
});

it('should `show archived` value remain the same after clear all', () => {
const filterFormStoreWithInit = new FilterFormStore();
const filterFormStoreWithInit = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStoreWithInit.init(initData);
let loadableFormset = filterFormStoreWithInit.formset.get();
let formset = Loadable.getOrElse(null, loadableFormset);
Expand Down Expand Up @@ -431,7 +431,7 @@ describe('FilterFormStore', () => {
};

it('should change the field order', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init();

filterFormStore.addChild(ROOT_ID, FormKind.Field);
Expand All @@ -456,7 +456,7 @@ describe('FilterFormStore', () => {
});

it('should move field into different group', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init();

filterFormStore.addChild(ROOT_ID, FormKind.Group);
Expand Down Expand Up @@ -489,7 +489,7 @@ describe('FilterFormStore', () => {

describe('Field Value Interaction', () => {
it('should change column name', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init();
filterFormStore.addChild(ROOT_ID, FormKind.Field);

Expand All @@ -507,7 +507,7 @@ describe('FilterFormStore', () => {
});

it('should change operator', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init();
filterFormStore.addChild(ROOT_ID, FormKind.Field);

Expand All @@ -519,7 +519,7 @@ describe('FilterFormStore', () => {
});

it('should change value', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init();
filterFormStore.addChild(ROOT_ID, FormKind.Field);

Expand All @@ -534,7 +534,7 @@ describe('FilterFormStore', () => {

describe('Group Value Interaction', () => {
it('should change conjunction', () => {
const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
filterFormStore.init();
filterFormStore.addChild(ROOT_ID, FormKind.Group);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ const getInitGroup = (): FormGroup => ({
kind: FormKind.Group,
});

export const getInitField = (): FormField => ({
export const getInitField = (location: V1LocationType): FormField => ({
columnName: 'id',
id: uuidv4(),
kind: FormKind.Field,
location: V1LocationType.EXPERIMENT,
location,
operator: AvailableOperators[V1ColumnType.NUMBER][0],
type: V1ColumnType.NUMBER,
value: null,
Expand All @@ -47,6 +47,11 @@ const isNotUndefined = <T>(arg: T): arg is Exclude<T, undefined> => arg !== unde

export class FilterFormStore {
#formset: WritableObservable<Loadable<FilterFormSet>> = observable(NotLoaded);
#defaultLocation: V1LocationType;

constructor(defaultLocation: V1LocationType) {
this.#defaultLocation = defaultLocation;
}

public init(data?: Readonly<FilterFormSet>): void {
this.#formset.update(() => Loaded(structuredClone(data ? data : INIT_FORMSET)));
Expand Down Expand Up @@ -265,6 +270,10 @@ export class FilterFormStore {
return this.#updateField(id, (form) => (form.value === value ? form : { ...form, value }));
}

public newField(): FormField {
return getInitField(this.#defaultLocation);
}

public addChild(
id: string,
addType: FormKind,
Expand All @@ -275,7 +284,7 @@ export class FilterFormStore {
? form.children
.slice(0, obj.index)
.concat([structuredClone(obj.item)], form.children.slice(obj.index))
: [...form.children, addType === FormKind.Group ? getInitGroup() : getInitField()];
: [...form.children, addType === FormKind.Group ? getInitGroup() : this.newField()];
return {
...form,
children,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import { useObservable } from 'micro-observables';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';

import { V1LocationType } from 'services/api-ts-sdk';

import { FilterFormStore, ROOT_ID } from './FilterFormStore';
import FilterGroup from './FilterGroup';
import { FormKind } from './type';

const filterFormStore = new FilterFormStore();
const filterFormStore = new FilterFormStore(V1LocationType.EXPERIMENT);
window.HTMLElement.prototype.scrollIntoView = vi.fn();

const Component = ({ filterFormStore }: { filterFormStore: FilterFormStore }): JSX.Element => {
Expand Down
2 changes: 1 addition & 1 deletion webui/react/src/components/Searches/Searches.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const parseSortString = (sortString: string): Sort[] => {
});
};

const formStore = new FilterFormStore();
const formStore = new FilterFormStore(V1LocationType.EXPERIMENT);

export const PAGE_SIZE = 100;
const INITIAL_LOADING_EXPERIMENTS: Loadable<ExperimentWithTrial>[] = new Array(PAGE_SIZE).fill(
Expand Down
2 changes: 1 addition & 1 deletion webui/react/src/pages/F_ExpList/F_ExperimentList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const parseSortString = (sortString: string): Sort[] => {
});
};

const formStore = new FilterFormStore();
const formStore = new FilterFormStore(V1LocationType.EXPERIMENT);

export const PAGE_SIZE = 100;
const INITIAL_LOADING_EXPERIMENTS: Loadable<ExperimentWithTrial>[] = new Array(PAGE_SIZE).fill(
Expand Down
2 changes: 1 addition & 1 deletion webui/react/src/pages/FlatRuns/FlatRuns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const BANNED_SORT_COLUMNS = new Set(['tags', 'searcherMetricsVal']);

const NO_PINS_WIDTH = 200;

export const formStore = new FilterFormStore();
export const formStore = new FilterFormStore(V1LocationType.RUN);

interface Props {
projectId: number;
Expand Down

0 comments on commit e92c474

Please sign in to comment.