File tree Expand file tree Collapse file tree 7 files changed +28
-26
lines changed Expand file tree Collapse file tree 7 files changed +28
-26
lines changed Original file line number Diff line number Diff line change @@ -44,6 +44,7 @@ import '../../../utils/htmlmixed';
44
44
import '../../../utils/p5-javascript' ;
45
45
import Timer from '../components/Timer' ;
46
46
import EditorAccessibility from '../components/EditorAccessibility' ;
47
+ import { selectActiveFile } from '../selectors/files' ;
47
48
import AssetPreview from './AssetPreview' ;
48
49
import { metaKey } from '../../../utils/metaKey' ;
49
50
import './show-hint' ;
@@ -605,10 +606,7 @@ Editor.propTypes = {
605
606
function mapStateToProps ( state ) {
606
607
return {
607
608
files : state . files ,
608
- file :
609
- state . files . find ( ( file ) => file . isSelectedFile ) ||
610
- state . files . find ( ( file ) => file . name === 'sketch.js' ) ||
611
- state . files . find ( ( file ) => file . name !== 'root' ) ,
609
+ file : selectActiveFile ( state ) ,
612
610
htmlFile : getHTMLFile ( state . files ) ,
613
611
ide : state . ide ,
614
612
preferences : state . preferences ,
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ import { setLanguage } from '../../actions/preferences';
14
14
import NavBar from '../../../../components/Nav/NavBar' ;
15
15
import CaretLeftIcon from '../../../../images/left-arrow.svg' ;
16
16
import LogoIcon from '../../../../images/p5js-logo-small.svg' ;
17
+ import { selectRootFile } from '../../selectors/files' ;
17
18
import { selectSketchPath } from '../../selectors/project' ;
18
19
import { metaKey , metaKeyName } from '../../../../utils/metaKey' ;
19
20
import { useSketchActions } from '../../hooks' ;
@@ -102,17 +103,14 @@ const DashboardMenu = () => {
102
103
) ;
103
104
} ;
104
105
105
- const ProjectMenu = ( props ) => {
106
+ const ProjectMenu = ( ) => {
106
107
const isUserOwner = useSelector ( getIsUserOwner ) ;
107
108
const project = useSelector ( ( state ) => state . project ) ;
108
109
const user = useSelector ( ( state ) => state . user ) ;
109
110
110
111
const isUnsaved = ! project ?. id ;
111
112
112
- // TODO: use selectRootFile selector
113
- const rootFile = useSelector (
114
- ( state ) => state . files . filter ( ( file ) => file . name === 'root' ) [ 0 ]
115
- ) ;
113
+ const rootFile = useSelector ( selectRootFile ) ;
116
114
117
115
const cmRef = useContext ( CmControllerContext ) ;
118
116
Original file line number Diff line number Diff line change 9
9
openProjectOptions ,
10
10
openUploadFileModal
11
11
} from '../actions/ide' ;
12
+ import { selectRootFile } from '../selectors/files' ;
12
13
import { getAuthenticated , selectCanEditSketch } from '../selectors/users' ;
13
14
14
15
import ConnectedFileNode from './FileNode' ;
@@ -23,9 +24,7 @@ export default function SideBar() {
23
24
24
25
const [ isFocused , setIsFocused ] = useState ( false ) ;
25
26
26
- const files = useSelector ( ( state ) => state . files ) ;
27
- // TODO: use `selectRootFile` defined in another PR
28
- const rootFile = files . filter ( ( file ) => file . name === 'root' ) [ 0 ] ;
27
+ const rootFile = useSelector ( selectRootFile ) ;
29
28
const projectOptionsVisible = useSelector (
30
29
( state ) => state . ide . projectOptionsVisible
31
30
) ;
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ import About from '../components/About';
33
33
import AddToCollectionList from '../components/AddToCollectionList' ;
34
34
import Feedback from '../components/Feedback' ;
35
35
import { CollectionSearchbar } from '../components/Searchbar' ;
36
+ import { selectActiveFile } from '../selectors/files' ;
36
37
import { getIsUserOwner } from '../selectors/users' ;
37
38
import RootPage from '../../../components/RootPage' ;
38
39
@@ -548,10 +549,7 @@ IDEView.propTypes = {
548
549
549
550
function mapStateToProps ( state ) {
550
551
return {
551
- selectedFile :
552
- state . files . find ( ( file ) => file . isSelectedFile ) ||
553
- state . files . find ( ( file ) => file . name === 'sketch.js' ) ||
554
- state . files . find ( ( file ) => file . name !== 'root' ) ,
552
+ selectedFile : selectActiveFile ( state ) ,
555
553
htmlFile : getHTMLFile ( state . files ) ,
556
554
ide : state . ide ,
557
555
preferences : state . preferences ,
Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ import { remSize } from '../../../theme';
41
41
import ActionStrip from '../../../components/mobile/ActionStrip' ;
42
42
import useAsModal from '../../../components/useAsModal' ;
43
43
import Dropdown from '../../../components/Dropdown' ;
44
+ import { selectActiveFile } from '../selectors/files' ;
44
45
import { getIsUserOwner } from '../selectors/users' ;
45
46
46
47
import {
@@ -475,10 +476,7 @@ MobileIDEView.propTypes = {
475
476
476
477
function mapStateToProps ( state ) {
477
478
return {
478
- selectedFile :
479
- state . files . find ( ( file ) => file . isSelectedFile ) ||
480
- state . files . find ( ( file ) => file . name === 'sketch.js' ) ||
481
- state . files . find ( ( file ) => file . name !== 'root' ) ,
479
+ selectedFile : selectActiveFile ( state ) ,
482
480
ide : state . ide ,
483
481
files : state . files ,
484
482
unsavedChanges : state . ide . unsavedChanges ,
Original file line number Diff line number Diff line change
1
+ import { createSelector } from 'reselect' ;
2
+
3
+ const selectFiles = ( state ) => state . files ;
4
+
5
+ export const selectRootFile = createSelector ( selectFiles , ( files ) =>
6
+ files . find ( ( file ) => file . name === 'root' )
7
+ ) ;
8
+
9
+ export const selectActiveFile = createSelector (
10
+ selectFiles ,
11
+ ( files ) =>
12
+ files . find ( ( file ) => file . isSelectedFile ) ||
13
+ files . find ( ( file ) => file . name === 'sketch.js' ) ||
14
+ files . find ( ( file ) => file . name !== 'root' )
15
+ ) ;
Original file line number Diff line number Diff line change @@ -16,19 +16,15 @@ import { getHTMLFile } from '../IDE/reducers/files';
16
16
17
17
import { ExitIcon } from '../../common/icons' ;
18
18
import Footer from '../../components/mobile/Footer' ;
19
+ import { selectActiveFile } from '../IDE/selectors/files' ;
19
20
import Content from './MobileViewContent' ;
20
21
21
22
const MobileSketchView = ( ) => {
22
23
const { files, ide, preferences } = useSelector ( ( state ) => state ) ;
23
24
24
25
const htmlFile = useSelector ( ( state ) => getHTMLFile ( state . files ) ) ;
25
26
const projectName = useSelector ( ( state ) => state . project . name ) ;
26
- const selectedFile = useSelector (
27
- ( state ) =>
28
- state . files . find ( ( file ) => file . isSelectedFile ) ||
29
- state . files . find ( ( file ) => file . name === 'sketch.js' ) ||
30
- state . files . find ( ( file ) => file . name !== 'root' )
31
- ) ;
27
+ const selectedFile = useSelector ( selectActiveFile ) ;
32
28
33
29
const {
34
30
setTextOutput,
You can’t perform that action at this time.
0 commit comments