11import React from "react" ;
2- import { connect } from "react-redux" ;
32import classnames from "classnames" ;
43import { WINDOWS , MEDIA_STATUS , LOAD_STYLE } from "../../constants" ;
54import * as Actions from "../../actionCreators" ;
6- import { getWindowShade } from "../../selectors" ;
75
86import DropTarget from "../DropTarget" ;
97import MiniTime from "../MiniTime" ;
@@ -30,51 +28,30 @@ import Minimize from "./Minimize";
3028import Shuffle from "./Shuffle" ;
3129import Time from "./Time" ;
3230import MainVolume from "./MainVolume" ;
31+ import * as Selectors from "../../selectors" ;
3332
3433import "../../../css/main-window.css" ;
35- import {
36- MediaStatus ,
37- WindowId ,
38- AppState ,
39- Dispatch ,
40- FilePicker ,
41- } from "../../types" ;
34+ import { FilePicker } from "../../types" ;
4235import FocusTarget from "../FocusTarget" ;
36+ import { useActionCreator , useTypedSelector } from "../../hooks" ;
4337
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 {
6139 analyser : AnalyserNode ;
6240 filePickers : FilePicker [ ] ;
6341}
6442
65- type Props = StateProps & DispatchProps & OwnProps ;
43+ function loadMediaAndPlay ( e : React . DragEvent < HTMLDivElement > ) {
44+ return Actions . loadMedia ( e , LOAD_STYLE . PLAY ) ;
45+ }
6646
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 ) ;
7855
7956 const className = classnames ( {
8057 window : true ,
@@ -89,19 +66,25 @@ const MainWindow = (props: Props) => {
8966 llama,
9067 } ) ;
9168
69+ const toggleMainWindowShadeMode = useActionCreator (
70+ Actions . toggleMainWindowShadeMode
71+ ) ;
72+ const scrollVolume = useActionCreator ( Actions . scrollVolume ) ;
73+ const loadMedia = useActionCreator ( loadMediaAndPlay ) ;
74+
9275 return (
9376 < React . StrictMode >
9477 < DropTarget
9578 id = "main-window"
9679 className = { className }
97- handleDrop = { props . loadMedia }
98- onWheel = { props . scrollVolume }
80+ handleDrop = { loadMedia }
81+ onWheel = { scrollVolume }
9982 >
10083 < FocusTarget windowId = { WINDOWS . MAIN } >
10184 < div
10285 id = "title-bar"
10386 className = "selected draggable"
104- onDoubleClick = { props . toggleMainWindowShadeMode }
87+ onDoubleClick = { toggleMainWindowShadeMode }
10588 >
10689 < ContextMenuTarget
10790 id = "option-context"
@@ -126,7 +109,7 @@ const MainWindow = (props: Props) => {
126109 </ div >
127110 < Visualizer
128111 // @ts -ignore Visualizer is not typed yet
129- analyser = { props . analyser }
112+ analyser = { analyser }
130113 />
131114 < div className = "media-info" >
132115 < Marquee />
@@ -157,38 +140,6 @@ const MainWindow = (props: Props) => {
157140 </ DropTarget >
158141 </ React . StrictMode >
159142 ) ;
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+ } ) ;
178144
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