Skip to content

Commit

Permalink
#8607 Rename useAddElement -> useAddNewModComponent hook (#8640)
Browse files Browse the repository at this point in the history
* refactor rename useAddElement -> useAddMocComponent

* refactor rename AddElement -> AddModComponent

* refactor rename editorSlice addElement -> addModComponentFormState

* refactor rename checkActiveElementAvailability -> checkActiveModComponentAvailability

* refactor rename type ElementConfig -> ModComponentFormStateAdapter

* refactor rename useAddModComponent -> useAddNewModComponent

* rename straggling vars and types in useAddNewModComponent.ts

* refactor rename comments in ModComponentFormStateAdapter type

* fix lint error introduced by prettier
  • Loading branch information
mnholtz authored Jun 18, 2024
1 parent 68b0ef0 commit eb2ee7e
Show file tree
Hide file tree
Showing 45 changed files with 194 additions and 156 deletions.
8 changes: 4 additions & 4 deletions src/background/deploymentUpdater.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ describe("syncDeployments", () => {
).fromExtension(modComponent)) as ActionFormState;
editorState = editorSlice.reducer(
editorState,
editorSlice.actions.addElement(element),
editorSlice.actions.addModComponentFormState(element),
);
await saveEditorState(editorState);

Expand Down Expand Up @@ -443,7 +443,7 @@ describe("syncDeployments", () => {
)) as ActionFormState;
editorState = editorSlice.reducer(
editorState,
editorSlice.actions.addElement(element),
editorSlice.actions.addModComponentFormState(element),
);
await saveEditorState(editorState);

Expand Down Expand Up @@ -736,15 +736,15 @@ describe("syncDeployments", () => {
).fromExtension(standaloneModComponent)) as ActionFormState;
editorState = editorSlice.reducer(
editorState,
editorSlice.actions.addElement(personalElement),
editorSlice.actions.addModComponentFormState(personalElement),
);

const deploymentElement = (await ADAPTERS.get(
deploymentStarterBrick.definition.type,
).fromExtension(deploymentModComponent)) as ActionFormState;
editorState = editorSlice.reducer(
editorState,
editorSlice.actions.addElement(deploymentElement),
editorSlice.actions.addModComponentFormState(deploymentElement),
);

await saveModComponentState({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe("VarMenu", () => {
/>,
{
setupRedux(dispatch) {
dispatch(editorActions.addElement(formState));
dispatch(editorActions.addModComponentFormState(formState));
dispatch(editorActions.selectElement(formState.uuid));
},
},
Expand Down Expand Up @@ -101,7 +101,7 @@ describe("VarMenu", () => {
/>,
{
async setupRedux(dispatch) {
dispatch(editorActions.addElement(formState));
dispatch(editorActions.addModComponentFormState(formState));
dispatch(editorActions.selectElement(formState.uuid));
dispatch(
editorActions.setElementActiveNodeId(
Expand Down Expand Up @@ -146,7 +146,7 @@ describe("VarMenu", () => {
/>,
{
async setupRedux(dispatch) {
dispatch(editorActions.addElement(formState));
dispatch(editorActions.addModComponentFormState(formState));
dispatch(editorActions.selectElement(formState.uuid));
dispatch(
editorActions.setElementActiveNodeId(
Expand Down
4 changes: 2 additions & 2 deletions src/pageEditor/EditorContent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe("error alerting in the UI", () => {
const formState = formStateFactory();
render(<EditorContent />, {
async setupRedux(dispatch) {
dispatch(editorActions.addElement(formState));
dispatch(editorActions.addModComponentFormState(formState));
dispatch(editorActions.selectElement(formState.uuid));
await dispatch(editorActions.checkAvailableDynamicElements());
},
Expand All @@ -73,7 +73,7 @@ describe("error alerting in the UI", () => {
const formState = formStateFactory();
render(<EditorContent />, {
async setupRedux(dispatch) {
dispatch(editorActions.addElement(formState));
dispatch(editorActions.addModComponentFormState(formState));
dispatch(editorActions.selectElement(formState.uuid));
await dispatch(editorActions.checkAvailableInstalledExtensions());
},
Expand Down
2 changes: 1 addition & 1 deletion src/pageEditor/PanelContent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("Listen to navigationEvent", () => {
const formState = formStateFactory();
render(<PanelContent />, {
setupRedux(dispatch) {
dispatch(editorActions.addElement(formState));
dispatch(editorActions.addModComponentFormState(formState));
dispatch(editorActions.selectElement(formState.uuid));
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe("DocumentOptions", () => {
{
initialValues: formState,
setupRedux(dispatch) {
dispatch(actions.addElement(formState));
dispatch(actions.addModComponentFormState(formState));
dispatch(actions.selectElement(formState.uuid));
dispatch(
actions.setElementActiveNodeId(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function renderDocumentPreview(documentElement: DocumentElement) {
return render(<PreviewContainer />, {
initialValues: formState,
setupRedux(dispatch) {
dispatch(actions.addElement(formState));
dispatch(actions.addModComponentFormState(formState));
dispatch(actions.selectElement(formState.uuid));
dispatch(
actions.setElementActiveNodeId(
Expand Down Expand Up @@ -186,7 +186,7 @@ describe("Show live preview", () => {
return render(<PreviewContainer />, {
initialValues: formState,
setupRedux(dispatch) {
dispatch(actions.addElement(formState));
dispatch(actions.addModComponentFormState(formState));
dispatch(actions.selectElement(formState.uuid));
dispatch(
actions.setElementActiveNodeId(pipelineField.__value__[0].instanceId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const renderElementPreview = (
return render(<ElementPreview {...props} />, {
initialValues: formState,
setupRedux(dispatch) {
dispatch(actions.addElement(formState));
dispatch(actions.addModComponentFormState(formState));
dispatch(actions.selectElement(formState.uuid));
dispatch(
actions.setElementActiveNodeId(
Expand Down
2 changes: 1 addition & 1 deletion src/pageEditor/fields/FormModalOptions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe("FormModalOptions", () => {
{
initialValues,
setupRedux(dispatch) {
dispatch(editorActions.addElement(initialValues));
dispatch(editorActions.addModComponentFormState(initialValues));
dispatch(editorActions.selectElement(initialValues.uuid));
dispatch(editorActions.setElementActiveNodeId(brick.instanceId));
},
Expand Down
6 changes: 3 additions & 3 deletions src/pageEditor/fields/FormRendererOptions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("FormRendererOptions", () => {
{
initialValues,
setupRedux(dispatch) {
dispatch(editorActions.addElement(initialValues));
dispatch(editorActions.addModComponentFormState(initialValues));
dispatch(editorActions.selectElement(initialValues.uuid));
dispatch(editorActions.setElementActiveNodeId(brick.instanceId));
},
Expand All @@ -71,7 +71,7 @@ describe("FormRendererOptions", () => {
{
initialValues,
setupRedux(dispatch) {
dispatch(editorActions.addElement(initialValues));
dispatch(editorActions.addModComponentFormState(initialValues));
dispatch(editorActions.selectElement(initialValues.uuid));
dispatch(editorActions.setElementActiveNodeId(brick.instanceId));
},
Expand Down Expand Up @@ -104,7 +104,7 @@ describe("FormRendererOptions", () => {
{
initialValues,
setupRedux(dispatch) {
dispatch(editorActions.addElement(initialValues));
dispatch(editorActions.addModComponentFormState(initialValues));
dispatch(editorActions.selectElement(initialValues.uuid));
dispatch(editorActions.setElementActiveNodeId(brick.instanceId));
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import notify from "@/utils/notify";
import { actions } from "@/pageEditor/slices/editorSlice";
import { internalStarterBrickMetaFactory } from "@/pageEditor/starterBricks/base";
import { isSpecificError } from "@/errors/errorHelpers";
import { type ElementConfig } from "@/pageEditor/starterBricks/elementConfig";
import { type ModComponentFormStateAdapter } from "@/pageEditor/starterBricks/modComponentFormStateAdapter";
import { updateDynamicElement } from "@/contentScript/messenger/api";
import { type SettingsState } from "@/store/settings/settingsTypes";
import useFlags from "@/hooks/useFlags";
Expand All @@ -35,58 +35,69 @@ import {
inspectedTab,
} from "@/pageEditor/context/connection";

type AddElement = (config: ElementConfig) => void;
type AddNewModComponent = (config: ModComponentFormStateAdapter) => void;

function useAddElement(): AddElement {
function useAddNewModComponent(): AddNewModComponent {
const dispatch = useDispatch();
const { flagOff } = useFlags();
const suggestElements = useSelector<{ settings: SettingsState }, boolean>(
(x) => x.settings.suggestElements ?? false,
);

return useCallback(
async (config: ElementConfig) => {
if (config.flag && flagOff(config.flag)) {
async (modComponentFormStateAdapter: ModComponentFormStateAdapter) => {
if (
modComponentFormStateAdapter.flag &&
flagOff(modComponentFormStateAdapter.flag)
) {
dispatch(actions.betaError());
return;
}

dispatch(actions.toggleInsert(config.elementType));
dispatch(actions.toggleInsert(modComponentFormStateAdapter.elementType));

if (!config.selectNativeElement) {
if (!modComponentFormStateAdapter.selectNativeElement) {
// If the foundation is not for a native element, stop after toggling insertion mode
return;
}

try {
const element = await config.selectNativeElement(
const element = await modComponentFormStateAdapter.selectNativeElement(
inspectedTab,
suggestElements,
);
const url = await getCurrentInspectedURL();

const metadata = internalStarterBrickMetaFactory();

const initialState = config.fromNativeElement(url, metadata, element);
const initialState = modComponentFormStateAdapter.fromNativeElement(
url,
metadata,
element,
);

updateDynamicElement(
allFramesInInspectedTab,
config.asDynamicElement(initialState),
modComponentFormStateAdapter.asDynamicElement(initialState),
);

dispatch(actions.addElement(initialState as ModComponentFormState));
dispatch(actions.checkActiveElementAvailability());
dispatch(
actions.addModComponentFormState(
initialState as ModComponentFormState,
),
);
dispatch(actions.checkActiveModComponentAvailability());

reportEvent(Events.MOD_COMPONENT_ADD_NEW, {
type: config.elementType,
type: modComponentFormStateAdapter.elementType,
});
} catch (error) {
if (isSpecificError(error, CancelError)) {
return;
}

notify.error({
message: `Error adding ${config.label.toLowerCase()}`,
message: `Error adding ${modComponentFormStateAdapter.label.toLowerCase()}`,
error,
});
} finally {
Expand All @@ -97,4 +108,4 @@ function useAddElement(): AddElement {
);
}

export default useAddElement;
export default useAddNewModComponent;
2 changes: 1 addition & 1 deletion src/pageEditor/hooks/useBuildAndValidateMod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe("useBuildAndValidateMod", () => {
isReactivate: false,
}),
);
dispatch(editorActions.addElement(dirtyFormState1));
dispatch(editorActions.addModComponentFormState(dirtyFormState1));
},
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ describe("useCheckModStarterBrickInvariants", () => {
}

for (const formState of newFormStates) {
dispatch(editorActions.addElement(formState));
dispatch(editorActions.addModComponentFormState(formState));
}
},
});
Expand Down Expand Up @@ -265,7 +265,7 @@ describe("useCheckModStarterBrickInvariants", () => {

const { result } = renderHook(() => useCheckModStarterBrickInvariants(), {
setupRedux(dispatch) {
dispatch(editorActions.addElement(formState));
dispatch(editorActions.addModComponentFormState(formState));
},
});

Expand Down Expand Up @@ -304,7 +304,7 @@ describe("useCheckModStarterBrickInvariants", () => {

const { result } = renderHook(() => useCheckModStarterBrickInvariants(), {
setupRedux(dispatch) {
dispatch(editorActions.addElement(formState));
dispatch(editorActions.addModComponentFormState(formState));
},
});
const checkModStarterBrickInvariants = result.current;
Expand Down
Loading

0 comments on commit eb2ee7e

Please sign in to comment.