Skip to content

Create selector functions for root file, active file #2341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions client/modules/IDE/components/Editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import '../../../utils/htmlmixed';
import '../../../utils/p5-javascript';
import Timer from '../components/Timer';
import EditorAccessibility from '../components/EditorAccessibility';
import { selectActiveFile } from '../selectors/files';
import AssetPreview from './AssetPreview';
import { metaKey } from '../../../utils/metaKey';
import './show-hint';
Expand Down Expand Up @@ -605,10 +606,7 @@ Editor.propTypes = {
function mapStateToProps(state) {
return {
files: state.files,
file:
state.files.find((file) => file.isSelectedFile) ||
state.files.find((file) => file.name === 'sketch.js') ||
state.files.find((file) => file.name !== 'root'),
file: selectActiveFile(state),
htmlFile: getHTMLFile(state.files),
ide: state.ide,
preferences: state.preferences,
Expand Down
8 changes: 3 additions & 5 deletions client/modules/IDE/components/Header/Nav.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { setLanguage } from '../../actions/preferences';
import NavBar from '../../../../components/Nav/NavBar';
import CaretLeftIcon from '../../../../images/left-arrow.svg';
import LogoIcon from '../../../../images/p5js-logo-small.svg';
import { selectRootFile } from '../../selectors/files';
import { selectSketchPath } from '../../selectors/project';
import { metaKey, metaKeyName } from '../../../../utils/metaKey';
import { useSketchActions } from '../../hooks';
Expand Down Expand Up @@ -102,17 +103,14 @@ const DashboardMenu = () => {
);
};

const ProjectMenu = (props) => {
const ProjectMenu = () => {
const isUserOwner = useSelector(getIsUserOwner);
const project = useSelector((state) => state.project);
const user = useSelector((state) => state.user);

const isUnsaved = !project?.id;

// TODO: use selectRootFile selector
const rootFile = useSelector(
(state) => state.files.filter((file) => file.name === 'root')[0]
);
const rootFile = useSelector(selectRootFile);

const cmRef = useContext(CmControllerContext);

Expand Down
5 changes: 2 additions & 3 deletions client/modules/IDE/components/Sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
openProjectOptions,
openUploadFileModal
} from '../actions/ide';
import { selectRootFile } from '../selectors/files';
import { getAuthenticated, selectCanEditSketch } from '../selectors/users';

import ConnectedFileNode from './FileNode';
Expand All @@ -23,9 +24,7 @@ export default function SideBar() {

const [isFocused, setIsFocused] = useState(false);

const files = useSelector((state) => state.files);
// TODO: use `selectRootFile` defined in another PR
const rootFile = files.filter((file) => file.name === 'root')[0];
const rootFile = useSelector(selectRootFile);
const projectOptionsVisible = useSelector(
(state) => state.ide.projectOptionsVisible
);
Expand Down
6 changes: 2 additions & 4 deletions client/modules/IDE/pages/IDEView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import About from '../components/About';
import AddToCollectionList from '../components/AddToCollectionList';
import Feedback from '../components/Feedback';
import { CollectionSearchbar } from '../components/Searchbar';
import { selectActiveFile } from '../selectors/files';
import { getIsUserOwner } from '../selectors/users';
import RootPage from '../../../components/RootPage';

Expand Down Expand Up @@ -548,10 +549,7 @@ IDEView.propTypes = {

function mapStateToProps(state) {
return {
selectedFile:
state.files.find((file) => file.isSelectedFile) ||
state.files.find((file) => file.name === 'sketch.js') ||
state.files.find((file) => file.name !== 'root'),
selectedFile: selectActiveFile(state),
htmlFile: getHTMLFile(state.files),
ide: state.ide,
preferences: state.preferences,
Expand Down
6 changes: 2 additions & 4 deletions client/modules/IDE/pages/MobileIDEView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { remSize } from '../../../theme';
import ActionStrip from '../../../components/mobile/ActionStrip';
import useAsModal from '../../../components/useAsModal';
import Dropdown from '../../../components/Dropdown';
import { selectActiveFile } from '../selectors/files';
import { getIsUserOwner } from '../selectors/users';

import {
Expand Down Expand Up @@ -475,10 +476,7 @@ MobileIDEView.propTypes = {

function mapStateToProps(state) {
return {
selectedFile:
state.files.find((file) => file.isSelectedFile) ||
state.files.find((file) => file.name === 'sketch.js') ||
state.files.find((file) => file.name !== 'root'),
selectedFile: selectActiveFile(state),
ide: state.ide,
files: state.files,
unsavedChanges: state.ide.unsavedChanges,
Expand Down
15 changes: 15 additions & 0 deletions client/modules/IDE/selectors/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createSelector } from 'reselect';

const selectFiles = (state) => state.files;

export const selectRootFile = createSelector(selectFiles, (files) =>
files.find((file) => file.name === 'root')
);

export const selectActiveFile = createSelector(
selectFiles,
(files) =>
files.find((file) => file.isSelectedFile) ||
files.find((file) => file.name === 'sketch.js') ||
files.find((file) => file.name !== 'root')
);
8 changes: 2 additions & 6 deletions client/modules/Mobile/MobileSketchView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@ import { getHTMLFile } from '../IDE/reducers/files';

import { ExitIcon } from '../../common/icons';
import Footer from '../../components/mobile/Footer';
import { selectActiveFile } from '../IDE/selectors/files';
import Content from './MobileViewContent';

const MobileSketchView = () => {
const { files, ide, preferences } = useSelector((state) => state);

const htmlFile = useSelector((state) => getHTMLFile(state.files));
const projectName = useSelector((state) => state.project.name);
const selectedFile = useSelector(
(state) =>
state.files.find((file) => file.isSelectedFile) ||
state.files.find((file) => file.name === 'sketch.js') ||
state.files.find((file) => file.name !== 'root')
);
const selectedFile = useSelector(selectActiveFile);

const {
setTextOutput,
Expand Down