Skip to content

Commit

Permalink
fix: infinite rerender + types + defaultCamera
Browse files Browse the repository at this point in the history
  • Loading branch information
fgarrec0397 committed Jan 1, 2023
1 parent 9c52f02 commit 80fecc5
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 34 deletions.
12 changes: 11 additions & 1 deletion app/src/App/Scenes/components/SceneDefaultCamera.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { uidGenerator } from "@app/Common/utilities";
import useEditor from "@app/Editor/_actions/hooks/useEditor";
import useCameras from "@app/Scenes/_actions/hooks/useCameras";
import { OrbitControls, PerspectiveCamera } from "@react-three/drei";
import { FC, useEffect, useRef } from "react";
import { PerspectiveCamera as PerspectiveCameraType, Vector3 } from "three";

import { DefaultCameras } from "../_actions/scenesConstants";
import { SceneCamera } from "../_actions/scenesTypes";
import TransformControls from "./TransformControls";

const EditorCamera: FC = () => {
Expand All @@ -13,7 +15,15 @@ const EditorCamera: FC = () => {
const cameraRef = useRef<PerspectiveCameraType>(null!);

useEffect(() => {
const newCamera = addCamera(cameraRef, DefaultCameras.EditorCamera);
const newCameraId = cameraRef.current?.uuid || uidGenerator();
const newSceneCamera: SceneCamera = {
id: newCameraId,
name: DefaultCameras.EditorCamera,
isDefault: true,
cameraRef,
position: [0, 0, 0],
};
const newCamera = addCamera(newSceneCamera);

setCurrentCamera(newCamera.id);

Expand Down
2 changes: 1 addition & 1 deletion app/src/App/Scenes/components/TransformControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const TransformControlsComponent: FC<Props> = ({ children }) => {
(!previousSelectedWidgets || !isEqual(selectedWidgets, previousSelectedWidgets))
) {
setMeshToAttach(getMeshByWidget(selectedWidgets[0]));
} else if (!selectedWidgets.length) {
} else if (previousSelectedWidgets?.length && !selectedWidgets.length) {
setMeshToAttach(undefined);
}
}, [
Expand Down
49 changes: 32 additions & 17 deletions app/src/App/Widgets/_actions/_data/hooks/useWidgetDispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ import {
export default () => {
const dispatch = useAppDispatch();

const dispatchAddDictionary = (widgetObjectInfo: Required<WidgetObjectInfo>) => {
return dispatch(addWidgetDictionary(widgetObjectInfo));
};
const dispatchAddDictionary = useCallback(
(widgetObjectInfo: Required<WidgetObjectInfo>) => {
return dispatch(addWidgetDictionary(widgetObjectInfo));
},
[dispatch]
);

const dispatchAddBatchDictionary = useCallback(
(widgetsObjectInfoDictionary: Required<WidgetObjectInfoDictionary>) => {
Expand All @@ -41,23 +44,35 @@ export default () => {
[dispatch]
);

const dispatchRemoveWidgetDictionary = (widgetId: string | undefined) => {
if (widgetId) {
return dispatch(removeWidgetDictionary(widgetId));
}
};
const dispatchRemoveWidgetDictionary = useCallback(
(widgetId: string | undefined) => {
if (widgetId) {
return dispatch(removeWidgetDictionary(widgetId));
}
},
[dispatch]
);

const dispatchRemoveBatchWidgetDictionary = (widgetIds: string[]) => {
return dispatch(removeBatchWidgetDictionary(widgetIds));
};
const dispatchRemoveBatchWidgetDictionary = useCallback(
(widgetIds: string[]) => {
return dispatch(removeBatchWidgetDictionary(widgetIds));
},
[dispatch]
);

const dispatchOverrideWidgetDictionary = (widgetDictionary: WidgetObjectInfoDictionary) => {
return dispatch(overrideWidgetDictionary(widgetDictionary));
};
const dispatchOverrideWidgetDictionary = useCallback(
(widgetDictionary: WidgetObjectInfoDictionary) => {
return dispatch(overrideWidgetDictionary(widgetDictionary));
},
[dispatch]
);

const dispatchSetCurrentWidgetProperties = (widgetProperties: WidgetProperties) => {
return dispatch(setCurrentWidgetProperties(widgetProperties));
};
const dispatchSetCurrentWidgetProperties = useCallback(
(widgetProperties: WidgetProperties) => {
return dispatch(setCurrentWidgetProperties(widgetProperties));
},
[dispatch]
);

return {
dispatchAddDictionary,
Expand Down
10 changes: 5 additions & 5 deletions app/src/App/Widgets/_actions/_data/hooks/useWidgetsSelector.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { AppState, useAppSelector } from "@app/Core/_actions/_data/state/store";
import { createSelector } from "@reduxjs/toolkit";
// import { createSelector } from "@reduxjs/toolkit";

const widgetsInfoSelector = (state: AppState) => state.widgets.widgetsObjectInfoDictionary;
const displayedInformationSelector = (state: AppState) => state.widgets.displayedInformation;

const selectWidgets = createSelector(widgetsInfoSelector, (widgetsObjectInfoDictionary) => {
return widgetsObjectInfoDictionary;
});
// const selectWidgets = createSelector(widgetsInfoSelector, (widgetsObjectInfoDictionary) => {
// return widgetsObjectInfoDictionary;
// });

export default () =>
useAppSelector((state) => {
const widgetsObjectInfoDictionary = selectWidgets(state);
const widgetsObjectInfoDictionary = widgetsInfoSelector(state);
const displayedInformations = displayedInformationSelector(state);

return {
Expand Down
6 changes: 3 additions & 3 deletions app/src/App/Widgets/_actions/_data/hooks/useWidgetsService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback } from "react";
import { useCallback, useMemo } from "react";

import filterWidgets from "../../utilities/filterWidgets";
import {
Expand Down Expand Up @@ -27,7 +27,8 @@ export default () => {
dispatchOverrideWidgetDictionary,
dispatchRemoveBatchWidgetDictionary,
} = useWidgetDispatch();
const { widgetsObjects, widgetsUI } = filterWidgets(widgets);

const { widgetsObjects, widgetsUI } = useMemo(() => filterWidgets(widgets), [widgets]);

const add = useCallback(
(newWidget: WidgetDictionaryItem, newWidgetObjectInfo?: WidgetObjectInfo) => {
Expand All @@ -36,7 +37,6 @@ export default () => {
newWidgetObjectInfo as Required<WidgetObjectInfo>;
dispatchAddDictionary(requiredWidgetDictionaryItem);
}

setWidgets((prevWidgets) => ({
...prevWidgets,
[newWidget.id]: { ...newWidget },
Expand Down
4 changes: 2 additions & 2 deletions app/src/App/Widgets/_actions/hooks/useGetMeshByWidget.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useThree } from "@react-three/fiber";

import getWidgetName from "../utilities/getWidgetName";
import { WidgetObjectsDictionaryItem } from "../widgetsTypes";
import { WidgetDictionaryItem } from "../widgetsTypes";

export default () => {
const { scene } = useThree();

const getMeshByWidget = (widget: WidgetObjectsDictionaryItem) => {
const getMeshByWidget = (widget: WidgetDictionaryItem) => {
const meshName = getWidgetName(widget);
return scene.getObjectByName(meshName);
};
Expand Down
1 change: 0 additions & 1 deletion app/src/App/Widgets/_actions/hooks/useWidgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ export default () => {
const updateCurrentWidget = useCallback(
(widgetProperties: WidgetProperties, updateOnlyProperties?: boolean) => {
const currentWidget = selectedWidgets[0];

updateWidget(currentWidget, widgetProperties, updateOnlyProperties);
},
[selectedWidgets, updateWidget]
Expand Down
24 changes: 20 additions & 4 deletions app/src/App/Widgets/_actions/widgetsTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export type WidgetObjectEditorOptions = {
meshHolder?: ReactNode | Object3D;
};

/// ---------------------- Widgets Objects Dictionary ---------------------- ///
/// ---------------------- Widgets Object Dictionary ---------------------- ///

/**
* A dictionary containing informations about all WidgetObjectsDictionary
Expand Down Expand Up @@ -152,13 +152,23 @@ export type SerializedWidgetObjectDictionaryItem<Props = FeaturesWidgetsProps> =
/// ---------------------- Widgets Info Dictionary ---------------------- ///

/**
* A dictionary containing editable informations about a WidgetObjectsDictionaryItem
* A dictionary containing editable informations about all Widget types
*/
export type WidgetObjectInfoDictionary = Dictionary<WidgetObjectInfo>;
export type WidgetInfoDictionary = Dictionary<WidgetInfo>;

export type WidgetObjectInfo = {
export type WidgetInfo = {
id: string;
displayName?: string;
};

/// ---------------------- Widgets Object Info Dictionary ---------------------- ///

/**
* A dictionary containing editable informations about a WidgetObject
*/
export type WidgetObjectInfoDictionary = Dictionary<WidgetObjectInfo>;

export type WidgetObjectInfo = WidgetInfo & {
properties?: WidgetProperties;
options?: WidgetOptionsValues;
};
Expand All @@ -174,6 +184,12 @@ export type WidgetProperties = {
scale: Vector3Array;
};

/// ---------------------- Widgets UI Info Dictionary ---------------------- ///

export type WidgetUIInfoDictionary = Dictionary<WidgetUIInfo>;

export type WidgetUIInfo = WidgetInfo;

/// ---------------------- Widget UI Module ---------------------- ///

/**
Expand Down

0 comments on commit 80fecc5

Please sign in to comment.