Skip to content

Commit a651645

Browse files
authored
Merge pull request #30 from waoai/performance-refactoring
Major Performance Enhancements and Refactoring
2 parents 92e141e + 8314560 commit a651645

File tree

35 files changed

+1315
-836
lines changed

35 files changed

+1315
-836
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
"react-markdown": "^4.0.6",
1616
"react-monaco-editor": "^0.25.1",
1717
"react-remove-scroll": "^2.0.4",
18-
"react-select": "^2.2.0",
18+
"react-select": "^3.0.8",
1919
"react-syntax-highlighter": "^12.2.1",
20-
"react-use": "^13.26.3",
20+
"react-use": "^13.27.0",
2121
"seamless-immutable": "^7.1.4",
2222
"transformation-matrix-js": "^2.7.6",
23+
"use-event-callback": "^0.1.0",
2324
"use-key-hook": "^1.3.0"
2425
},
2526
"peerDependencies": {

src/Annotator/index.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
} from "../MainLayout/types"
1212
import SettingsProvider from "../SettingsProvider"
1313
import reducer from "./reducer"
14+
import makeImmutable from "seamless-immutable"
1415

1516
type Props = {
1617
taskDescription: string,
@@ -28,7 +29,7 @@ type Props = {
2829
onExit: MainLayoutState => any
2930
}
3031

31-
export default ({
32+
export const Annotator = ({
3233
images,
3334
allowedArea,
3435
selectedImage = images.length > 0 ? images[0].src : undefined,
@@ -43,24 +44,27 @@ export default ({
4344
taskDescription,
4445
onExit
4546
}: Props) => {
46-
const [state, dispatchToReducer] = useReducer(reducer, {
47-
showTags,
48-
allowedArea,
49-
selectedImage,
50-
showPointDistances,
51-
pointDistancePrecision,
52-
selectedTool: "select",
53-
mode: null,
54-
taskDescription,
55-
images,
56-
labelImages: imageClsList.length > 0 || imageTagList.length > 0,
57-
regionClsList,
58-
regionTagList,
59-
imageClsList,
60-
imageTagList,
61-
enabledTools,
62-
history: []
63-
})
47+
const [state, dispatchToReducer] = useReducer(
48+
reducer,
49+
makeImmutable({
50+
showTags,
51+
allowedArea,
52+
selectedImage,
53+
showPointDistances,
54+
pointDistancePrecision,
55+
selectedTool: "select",
56+
mode: null,
57+
taskDescription,
58+
images,
59+
labelImages: imageClsList.length > 0 || imageTagList.length > 0,
60+
regionClsList,
61+
regionTagList,
62+
imageClsList,
63+
imageTagList,
64+
enabledTools,
65+
history: []
66+
})
67+
)
6468

6569
const dispatch = (action: Action) => {
6670
if (
@@ -82,3 +86,5 @@ export default ({
8286
</SettingsProvider>
8387
)
8488
}
89+
90+
export default Annotator

src/Crosshairs/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import React, { Fragment, useEffect, useState } from "react"
44

5-
export default ({
5+
export const Crosshairs = ({
66
mousePosition,
77
x,
88
y
@@ -58,3 +58,5 @@ export default ({
5858
</Fragment>
5959
)
6060
}
61+
62+
export default Crosshairs

src/DebugSidebarBox/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import React from "react"
44
import SidebarBoxContainer from "../SidebarBoxContainer"
55

6-
export default ({ state, lastAction }: any) => {
6+
export const DebugSidebarBox = ({ state, lastAction }: any) => {
77
const image = (state.images || []).find(
88
img => img.src === state.selectedImage
99
)
@@ -28,3 +28,5 @@ export default ({ state, lastAction }: any) => {
2828
</SidebarBoxContainer>
2929
)
3030
}
31+
32+
export default DebugSidebarBox

src/Fullscreen/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// @flow
22

33
import React from "react"
4-
import Fullscreen from "react-full-screen"
4+
import ReactFullscreen from "react-full-screen"
55

6-
export default props => {
6+
export const Fullscreen = props => {
77
if (!props.enabled) return props.children
8-
return <Fullscreen {...props}>{props.children}</Fullscreen>
8+
return <ReactFullscreen {...props}>{props.children}</ReactFullscreen>
99
}
10+
11+
export default Fullscreen

src/Header/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Props = {
2121
multipleImages?: boolean
2222
}
2323

24-
export default ({
24+
export const Header = ({
2525
onHeaderButtonClick,
2626
title,
2727
inFullScreen,
@@ -53,3 +53,5 @@ export default ({
5353
</div>
5454
)
5555
}
56+
57+
export default Header

src/HeaderButton/index.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
// @flow
22

3-
import React, { createContext } from "react"
3+
import React, { memo, createContext, useContext } from "react"
4+
import { styled } from "@material-ui/core/styles"
45
import Button from "@material-ui/core/Button"
6+
import useEventCallback from "use-event-callback"
7+
8+
const StyledButton = styled(Button)({
9+
width: 80,
10+
margin: 2
11+
})
12+
13+
const IconName = styled("div")({
14+
fontWeight: "bold"
15+
})
516

617
export const HeaderButtonContext = createContext()
718

8-
export default ({ name, Icon }: { name: string, Icon: any }) => {
9-
return (
10-
<HeaderButtonContext.Consumer>
11-
{({ onHeaderButtonClick }: any) => (
12-
<Button
13-
onClick={() => onHeaderButtonClick(name)}
14-
style={{ width: 80, margin: 2 }}
15-
>
16-
<div>
17-
<Icon style={{}} />
18-
<div style={{ fontWeight: "bold" }}>{name}</div>
19-
</div>
20-
</Button>
21-
)}
22-
</HeaderButtonContext.Consumer>
23-
)
19+
const MemoizedHeaderButton = memo(
20+
({ name, Icon, onClick }) => (
21+
<StyledButton onClick={onClick}>
22+
<div>
23+
<Icon />
24+
<IconName>{name}</IconName>
25+
</div>
26+
</StyledButton>
27+
),
28+
(prevProps, nextProps) => prevProps.name === nextProps.name
29+
)
30+
31+
export const HeaderButton = ({ name, Icon }: { name: string, Icon: any }) => {
32+
const { onHeaderButtonClick } = useContext(HeaderButtonContext)
33+
const onClick = useEventCallback(() => onHeaderButtonClick(name))
34+
return <MemoizedHeaderButton name={name} Icon={Icon} onClick={onClick} />
2435
}
36+
37+
export default HeaderButton

src/HighlightBox/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const useStyles = makeStyles({
1010
to: { strokeDashoffset: 100 }
1111
},
1212
highlightBox: {
13+
zIndex: 2,
1314
transition: "opacity 500ms",
1415
"&:not(.highlighted)": {
1516
opacity: 0
@@ -32,7 +33,7 @@ const useStyles = makeStyles({
3233
}
3334
})
3435

35-
export default ({
36+
export const HighlightBox = ({
3637
mouseEvents,
3738
dragWithPrimary,
3839
zoomWithPrimary,
@@ -106,3 +107,5 @@ export default ({
106107
</svg>
107108
)
108109
}
110+
111+
export default HighlightBox

src/HistorySidebarBox/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow
22

3-
import React, { setState } from "react"
3+
import React, { setState, memo } from "react"
44
import { makeStyles } from "@material-ui/core/styles"
55
import SidebarBoxContainer from "../SidebarBoxContainer"
66
import HistoryIcon from "@material-ui/icons/History"
@@ -12,6 +12,7 @@ import ListItemSecondaryAction from "@material-ui/core/ListItemSecondaryAction"
1212
import UndoIcon from "@material-ui/icons/Undo"
1313
import moment from "moment"
1414
import { grey } from "@material-ui/core/colors"
15+
import isEqual from "lodash/isEqual"
1516

1617
const useStyles = makeStyles({
1718
emptyText: {
@@ -23,7 +24,7 @@ const useStyles = makeStyles({
2324
}
2425
})
2526

26-
export default ({
27+
export const HistorySidebarBox = ({
2728
history,
2829
onRestoreHistory
2930
}: {
@@ -60,3 +61,10 @@ export default ({
6061
</SidebarBoxContainer>
6162
)
6263
}
64+
65+
export default memo(HistorySidebarBox, (prevProps, nextProps) =>
66+
isEqual(
67+
prevProps.history.map(a => [a.name, a.time]),
68+
nextProps.history.map(a => [a.name, a.time])
69+
)
70+
)

src/IconTools/index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow
22

3-
import React from "react"
3+
import React, { useMemo } from "react"
44
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
55
import {
66
faArrowsAlt,
@@ -37,18 +37,22 @@ type Props = {
3737
onClickTool: string => any
3838
}
3939

40-
export default ({
40+
const defaultTools = ["select", "create-point", "create-box", "create-polygon"]
41+
42+
export const IconTools = ({
4143
showTags,
4244
selectedTool,
4345
onClickTool,
44-
enabledTools = ["select", "create-point", "create-box", "create-polygon"]
46+
enabledTools = defaultTools
4547
}: Props) => {
4648
const classes = useStyles()
49+
const selectedToolContextValue = useMemo(
50+
() => ({ enabledTools, selectedTool, onClickTool }),
51+
[enabledTools, selectedTool]
52+
)
4753
return (
4854
<div className={classes.iconTools}>
49-
<SelectedTool.Provider
50-
value={{ enabledTools, selectedTool, onClickTool }}
51-
>
55+
<SelectedTool.Provider value={selectedToolContextValue}>
5256
<SmallToolButton
5357
id="select"
5458
name="Select Region"
@@ -108,3 +112,5 @@ export default ({
108112
</div>
109113
)
110114
}
115+
116+
export default IconTools

0 commit comments

Comments
 (0)