Skip to content

Commit

Permalink
feat(app): optimize rendering by wrapping onMousedown in callback 🤓
Browse files Browse the repository at this point in the history
  • Loading branch information
mukuljainx committed May 17, 2021
1 parent f72ee18 commit b54889e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
6 changes: 4 additions & 2 deletions src/apps/appManager/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ interface IProps {
path: string;
instanceId: string;
appName: string;
onMouseDown: (event: React.MouseEvent) => void;
dragId: string;
onMouseDown: (event: React.MouseEvent, dragId: string) => void;
}

const AppManager = ({ onMouseDown, appName, instanceId }: IProps) => {
const AppManager = ({ onMouseDown, appName, instanceId, dragId }: IProps) => {
const [apps, setApps] = React.useState<IApp[]>([]);
const [selectedApp, setSelectedApp] = React.useState(-1);
const [view, setView] =
Expand Down Expand Up @@ -61,6 +62,7 @@ const AppManager = ({ onMouseDown, appName, instanceId }: IProps) => {
instanceId={instanceId}
icon="photo"
name="App Manager"
dragId={dragId}
>
<Wrapper>
<If condition={localHistory.getCurrent() === "/list"}>
Expand Down
9 changes: 5 additions & 4 deletions src/apps/folder/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ interface IProps {
app: IApp;
id: string;
metaData: IMetaData;
onMouseDown: (event: React.MouseEvent) => void;
dragId: string;
onMouseDown: (event: React.MouseEvent, dragId: string) => void;
}

const getPathName = (path: string) => {
return path.split("/").pop() || "/";
};

const Folder = ({ path, app, id, onMouseDown }: IProps) => {
const Folder = ({ path, app, id, dragId, onMouseDown }: IProps) => {
const { firstRender } = useDidUpdate();
const dispatchFolderLoadEvent = React.useCallback(() => {
// dispatch a fake animation complete event so dependent can do their work on route change
Expand Down Expand Up @@ -154,7 +155,7 @@ const Folder = ({ path, app, id, onMouseDown }: IProps) => {

const handleTopBarMouseDown = React.useCallback(
(event: React.MouseEvent) => {
onMouseDown(event);
onMouseDown(event, dragId);
dispatch(bringToTop({ appName: app.appName, instanceId: app.id }));
},
[onMouseDown]
Expand Down Expand Up @@ -205,4 +206,4 @@ const Folder = ({ path, app, id, onMouseDown }: IProps) => {
</Container>
);
};
export default Folder;
export default React.memo(Folder);
19 changes: 8 additions & 11 deletions src/apps/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@ interface IProps {
data: any;
metaData: IMetaData;
style: React.CSSProperties;
onMouseDown: (event: React.MouseEvent) => void;
onMouseDown: (event: React.MouseEvent, dragId: string) => void;
dragId: string;
weight: number;
}

const App = ({
app,
data,
id,
metaData,
style,
weight,
onMouseDown,
}: IProps) => {
const App = (props: IProps) => {
const { app, data, id, metaData, style, weight, dragId, onMouseDown } = props;
switch (app.appName) {
case "folder": {
return (
Expand All @@ -41,6 +35,7 @@ const App = ({
key={id}
app={app}
id={id}
dragId={dragId}
{...data}
/>
</AnimatedFileWrapper>
Expand All @@ -58,6 +53,7 @@ const App = ({
<Photo
{...(data as any)}
instanceId={id}
dragId={dragId}
appName={app.appName}
onMouseDown={onMouseDown}
/>
Expand All @@ -76,6 +72,7 @@ const App = ({
<AppManager
{...(data as any)}
instanceId={id}
dragId={dragId}
appName={app.appName}
onMouseDown={onMouseDown}
/>
Expand All @@ -87,4 +84,4 @@ const App = ({
return null;
};

export default App;
export default React.memo(App);
4 changes: 3 additions & 1 deletion src/apps/photo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ interface IProps {
instanceId: string;
appName: string;
onMouseDown: (event: React.MouseEvent) => void;
dragId: string;
}

const Photo = ({ path, appName, instanceId, onMouseDown }: IProps) => {
const Photo = ({ path, appName, instanceId, onMouseDown, dragId }: IProps) => {
const { render } = useAnimationEndRender({ instanceId });

return (
<AppShell
dragId={dragId}
onMouseDown={onMouseDown}
appName={appName}
instanceId={instanceId}
Expand Down
12 changes: 11 additions & 1 deletion src/apps/shell/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type IProps = ReactHTMLElement<
children: React.ReactChild;
width?: React.CSSProperties["width"];
height?: React.CSSProperties["height"];
onMouseDown: (event: React.MouseEvent, dragId: string) => void;
dragId: string;
}
>;

Expand All @@ -48,8 +50,16 @@ const AppShell = ({
width,
height,
style,
dragId,
...rest
}: IProps) => {
const handleMouseDownDrag = React.useCallback(
(event: React.MouseEvent) => {
onMouseDown(event, dragId);
},
[onMouseDown, dragId]
);

return (
<Wrapper
{...rest}
Expand All @@ -61,7 +71,7 @@ const AppShell = ({
>
<Stack fullWidth fullHeight flexDirection="column">
<StackItem flexShrink={0} data-test="topbar">
<TopBar data-id="app-shell-top-bar" onMouseDown={onMouseDown}>
<TopBar data-id="app-shell-top-bar" onMouseDown={handleMouseDownDrag}>
<Stack
alignItems="center"
justifyContent="space-between"
Expand Down
10 changes: 9 additions & 1 deletion src/base/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ const Base = ({}: IProps) => {
dispatch(initRoutes(user?.name || "Guest user"));
}, []);

const handleMouseDownEvent = React.useCallback(
(event: React.MouseEvent, dragId: string) => {
handleMouseDown(event, dragId);
},
[]
);

return (
<Desktop>
<Wrapper ref={wrapperRef}>
Expand All @@ -67,12 +74,13 @@ const Base = ({}: IProps) => {
return (
<App
weight={app.weight + index}
onMouseDown={(event) => handleMouseDown(event, dragId)}
onMouseDown={handleMouseDownEvent}
style={{
transform: store.elements[dragId]?.translate.x
? `translate(${store.elements[dragId]?.translate.x}px, ${store.elements[dragId]?.translate.y}px)`
: undefined,
}}
dragId={dragId}
key={dragId}
app={instance}
data={instance.data}
Expand Down

0 comments on commit b54889e

Please sign in to comment.