1
1
import React from "react" ;
2
- import { connect } from "react-redux" ;
3
2
import classnames from "classnames" ;
4
3
import { WINDOWS , MEDIA_STATUS , LOAD_STYLE } from "../../constants" ;
5
4
import * as Actions from "../../actionCreators" ;
6
- import { getWindowShade } from "../../selectors" ;
7
5
8
6
import DropTarget from "../DropTarget" ;
9
7
import MiniTime from "../MiniTime" ;
@@ -30,51 +28,30 @@ import Minimize from "./Minimize";
30
28
import Shuffle from "./Shuffle" ;
31
29
import Time from "./Time" ;
32
30
import MainVolume from "./MainVolume" ;
31
+ import * as Selectors from "../../selectors" ;
33
32
34
33
import "../../../css/main-window.css" ;
35
- import {
36
- MediaStatus ,
37
- WindowId ,
38
- AppState ,
39
- Dispatch ,
40
- FilePicker ,
41
- } from "../../types" ;
34
+ import { FilePicker } from "../../types" ;
42
35
import FocusTarget from "../FocusTarget" ;
36
+ import { useActionCreator , useTypedSelector } from "../../hooks" ;
43
37
44
- interface StateProps {
45
- focused : WindowId | null ;
46
- loading : boolean ;
47
- doubled : boolean ;
48
- mainShade : boolean ;
49
- llama : boolean ;
50
- working : boolean ;
51
- status : MediaStatus | null ;
52
- }
53
-
54
- interface DispatchProps {
55
- scrollVolume ( e : React . WheelEvent < HTMLDivElement > ) : void ;
56
- toggleMainWindowShadeMode ( ) : void ;
57
- loadMedia ( e : React . DragEvent < HTMLDivElement > ) : void ;
58
- }
59
-
60
- interface OwnProps {
38
+ interface Props {
61
39
analyser : AnalyserNode ;
62
40
filePickers : FilePicker [ ] ;
63
41
}
64
42
65
- type Props = StateProps & DispatchProps & OwnProps ;
43
+ function loadMediaAndPlay ( e : React . DragEvent < HTMLDivElement > ) {
44
+ return Actions . loadMedia ( e , LOAD_STYLE . PLAY ) ;
45
+ }
66
46
67
- const MainWindow = ( props : Props ) => {
68
- const {
69
- focused,
70
- loading,
71
- doubled,
72
- mainShade,
73
- llama,
74
- status,
75
- working,
76
- filePickers,
77
- } = props ;
47
+ const MainWindow = React . memo ( ( { analyser, filePickers } : Props ) => {
48
+ const mainShade = useTypedSelector ( Selectors . getWindowShade ) ( "main" ) ;
49
+ const status = useTypedSelector ( Selectors . getMediaStatus ) ;
50
+ const focused = useTypedSelector ( Selectors . getFocusedWindow ) ;
51
+ const loading = useTypedSelector ( Selectors . getLoading ) ;
52
+ const doubled = useTypedSelector ( Selectors . getDoubled ) ;
53
+ const llama = useTypedSelector ( Selectors . getLlamaMode ) ;
54
+ const working = useTypedSelector ( Selectors . getWorking ) ;
78
55
79
56
const className = classnames ( {
80
57
window : true ,
@@ -89,19 +66,25 @@ const MainWindow = (props: Props) => {
89
66
llama,
90
67
} ) ;
91
68
69
+ const toggleMainWindowShadeMode = useActionCreator (
70
+ Actions . toggleMainWindowShadeMode
71
+ ) ;
72
+ const scrollVolume = useActionCreator ( Actions . scrollVolume ) ;
73
+ const loadMedia = useActionCreator ( loadMediaAndPlay ) ;
74
+
92
75
return (
93
76
< React . StrictMode >
94
77
< DropTarget
95
78
id = "main-window"
96
79
className = { className }
97
- handleDrop = { props . loadMedia }
98
- onWheel = { props . scrollVolume }
80
+ handleDrop = { loadMedia }
81
+ onWheel = { scrollVolume }
99
82
>
100
83
< FocusTarget windowId = { WINDOWS . MAIN } >
101
84
< div
102
85
id = "title-bar"
103
86
className = "selected draggable"
104
- onDoubleClick = { props . toggleMainWindowShadeMode }
87
+ onDoubleClick = { toggleMainWindowShadeMode }
105
88
>
106
89
< ContextMenuTarget
107
90
id = "option-context"
@@ -126,7 +109,7 @@ const MainWindow = (props: Props) => {
126
109
</ div >
127
110
< Visualizer
128
111
// @ts -ignore Visualizer is not typed yet
129
- analyser = { props . analyser }
112
+ analyser = { analyser }
130
113
/>
131
114
< div className = "media-info" >
132
115
< Marquee />
@@ -157,38 +140,6 @@ const MainWindow = (props: Props) => {
157
140
</ DropTarget >
158
141
</ React . StrictMode >
159
142
) ;
160
- } ;
161
-
162
- const mapStateToProps = ( state : AppState ) : StateProps => {
163
- const {
164
- media : { status } ,
165
- display : { loading, doubled, llama, working } ,
166
- windows : { focused } ,
167
- } = state ;
168
- return {
169
- mainShade : Boolean ( getWindowShade ( state ) ( "main" ) ) ,
170
- status,
171
- loading,
172
- doubled,
173
- llama,
174
- working,
175
- focused,
176
- } ;
177
- } ;
143
+ } ) ;
178
144
179
- const mapDispatchToProps = ( dispatch : Dispatch ) : DispatchProps => {
180
- return {
181
- toggleMainWindowShadeMode : ( ) =>
182
- dispatch ( Actions . toggleMainWindowShadeMode ( ) ) ,
183
- scrollVolume : ( e : React . WheelEvent < HTMLDivElement > ) =>
184
- dispatch ( Actions . scrollVolume ( e ) ) ,
185
- loadMedia : ( e : React . DragEvent < HTMLDivElement > ) =>
186
- dispatch ( Actions . loadMedia ( e , LOAD_STYLE . PLAY ) ) ,
187
- } ;
188
- } ;
189
- export default connect (
190
- mapStateToProps ,
191
- mapDispatchToProps ,
192
- null ,
193
- { forwardRef : true }
194
- ) ( MainWindow ) ;
145
+ export default MainWindow ;
0 commit comments