Skip to content

Commit

Permalink
rename straggling vars and types in useAddNewModComponent.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
mnholtz committed Jun 17, 2024
1 parent b8079c9 commit 7cf8c60
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
29 changes: 18 additions & 11 deletions src/pageEditor/hooks/useAddNewModComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,43 +35,50 @@ import {
inspectedTab,
} from "@/pageEditor/context/connection";

type AddModComponent = (config: ModComponentFormStateAdapter) => void;
type AddNewModComponent = (config: ModComponentFormStateAdapter) => void;

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

return useCallback(
async (config: ModComponentFormStateAdapter) => {
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(
Expand All @@ -82,15 +89,15 @@ function useAddNewModComponent(): AddModComponent {
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 Down
10 changes: 5 additions & 5 deletions src/pageEditor/slices/editorSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,13 @@ export const editorSlice = createSlice({
state,
action: PayloadAction<ModComponentFormState>,
) {
const element = action.payload as Draft<ModComponentFormState>;
const modComponentFormState =
action.payload as Draft<ModComponentFormState>;
state.inserting = null;
state.elements.push(element);
state.dirty[element.uuid] = true;
state.elements.push(modComponentFormState);
state.dirty[modComponentFormState.uuid] = true;

// TODO: remove this side effect? i.e. call "activate element" at call sites
activateElement(state, element);
activateElement(state, modComponentFormState);
},
betaError(state) {
const error = new BusinessError("This feature is in private beta");
Expand Down

0 comments on commit 7cf8c60

Please sign in to comment.