diff --git a/packages/builder/src/stores/builder/components.ts b/packages/builder/src/stores/builder/components.ts index bce7fcb71d..7eb351e9da 100644 --- a/packages/builder/src/stores/builder/components.ts +++ b/packages/builder/src/stores/builder/components.ts @@ -36,8 +36,8 @@ import { utils } from "@budibase/shared-core" export interface ComponentState { components: Record customComponents: string[] - selectedComponentId?: string | null - componentToPaste?: Component | null + selectedComponentId?: string + componentToPaste?: Component settingsCache: Record selectedScreenId?: string | null } @@ -68,8 +68,6 @@ export interface ComponentSetting { export const INITIAL_COMPONENTS_STATE: ComponentState = { components: {}, customComponents: [], - selectedComponentId: null, - componentToPaste: null, settingsCache: {}, } @@ -443,7 +441,7 @@ export class ComponentStore extends BudiStore { */ createInstance(componentName: string, presetProps: any, parent: any) { const screen = get(selectedScreen) - if (!screen || !selectedScreen) { + if (!screen) { throw "A valid screen must be selected" } @@ -548,7 +546,7 @@ export class ComponentStore extends BudiStore { // Find the selected component let selectedComponentId = state.selectedComponentId if (selectedComponentId?.startsWith(`${screen._id}-`)) { - selectedComponentId = screen.props._id || null + selectedComponentId = screen.props._id } const currentComponent = findComponent( screen.props, @@ -659,7 +657,7 @@ export class ComponentStore extends BudiStore { // Determine the next component to select, and select it before deletion // to avoid an intermediate state of no component selection const state = get(this.store) - let nextId: string | null = "" + let nextId: string = "" if (state.selectedComponentId === component._id) { nextId = this.getNext() if (!nextId) { @@ -746,7 +744,7 @@ export class ComponentStore extends BudiStore { if (!state.componentToPaste) { return } - let newComponentId: string | null = "" + let newComponentId: string = "" // Remove copied component if cutting, regardless if pasting works let componentToPaste = cloneDeep(state.componentToPaste) @@ -1169,7 +1167,7 @@ export class ComponentStore extends BudiStore { } async handleEjectBlock(componentId: string, ejectedDefinition: Component) { - let nextSelectedComponentId: string | null = null + let nextSelectedComponentId: string | undefined await screenStore.patch((screen: Screen) => { const block = findComponent(screen.props, componentId) @@ -1205,7 +1203,7 @@ export class ComponentStore extends BudiStore { (x: Component) => x._id === componentId ) parent._children[index] = ejectedDefinition - nextSelectedComponentId = ejectedDefinition._id ?? null + nextSelectedComponentId = ejectedDefinition._id }, null) // Select new root component diff --git a/packages/builder/src/stores/builder/screens.ts b/packages/builder/src/stores/builder/screens.ts index a749ded1f9..5163c6a3ea 100644 --- a/packages/builder/src/stores/builder/screens.ts +++ b/packages/builder/src/stores/builder/screens.ts @@ -25,7 +25,6 @@ import { ComponentDefinition } from "./components" interface ScreenState { screens: Screen[] selectedScreenId?: string - selected?: Screen } export const initialScreenState: ScreenState = { @@ -65,7 +64,7 @@ export class ScreenStore extends BudiStore { if (!get(selectedComponent)) { this.update(state => ({ ...state, - selectedComponentId: get(this.store).selected?.props._id, + selectedComponentId: get(selectedScreen)?.props._id, })) } }, @@ -400,10 +399,10 @@ export class ScreenStore extends BudiStore { deletedIds.includes(state.selectedScreenId) ) { delete state.selectedScreenId - componentStore.update(state => ({ - ...state, - selectedComponentId: null, - })) + componentStore.update(state => { + delete state.selectedComponentId + return state + }) } // Update routing diff --git a/packages/builder/src/stores/builder/tests/screens.test.js b/packages/builder/src/stores/builder/tests/screens.test.js index 87eb03ea04..ea916c8d59 100644 --- a/packages/builder/src/stores/builder/tests/screens.test.js +++ b/packages/builder/src/stores/builder/tests/screens.test.js @@ -544,7 +544,7 @@ describe("Screens store", () => { await bb.screenStore.delete(existingScreens[2].json()) expect(bb.store.screens.length).toBe(2) - expect(get(componentStore).selectedComponentId).toBeNull() + expect(get(componentStore).selectedComponentId).toBeUndefined() expect(bb.store.selectedScreenId).toBeUndefined() })