diff --git a/client/modules/IDE/components/Header/Toolbar.jsx b/client/modules/IDE/components/Header/Toolbar.jsx
index 35470e97ad..46aeb9a652 100644
--- a/client/modules/IDE/components/Header/Toolbar.jsx
+++ b/client/modules/IDE/components/Header/Toolbar.jsx
@@ -1,253 +1,191 @@
+import React, { useRef, useState } from 'react';
+import classNames from 'classnames';
import PropTypes from 'prop-types';
-import React from 'react';
-import { connect } from 'react-redux';
+import { useTranslation } from 'react-i18next';
+import { useDispatch, useSelector } from 'react-redux';
import { Link } from 'react-router-dom';
-import classNames from 'classnames';
-import { withTranslation } from 'react-i18next';
-import * as IDEActions from '../../actions/ide';
-import * as preferenceActions from '../../actions/preferences';
-import * as projectActions from '../../actions/project';
+import {
+ hideEditProjectName,
+ showEditProjectName
+} from '../../actions/project';
+import {
+ openPreferences,
+ startAccessibleSketch,
+ startSketch,
+ stopSketch
+} from '../../actions/ide';
+import {
+ setAutorefresh,
+ setGridOutput,
+ setTextOutput
+} from '../../actions/preferences';
+import { useSketchActions } from '../../hooks';
import PlayIcon from '../../../../images/play.svg';
import StopIcon from '../../../../images/stop.svg';
import PreferencesIcon from '../../../../images/preferences.svg';
import EditProjectNameIcon from '../../../../images/pencil.svg';
-class Toolbar extends React.Component {
- constructor(props) {
- super(props);
- this.handleKeyPress = this.handleKeyPress.bind(this);
- this.handleProjectNameChange = this.handleProjectNameChange.bind(this);
- this.handleProjectNameClick = this.handleProjectNameClick.bind(this);
- this.handleProjectNameSave = this.handleProjectNameSave.bind(this);
+const Toolbar = (props) => {
+ const { isPlaying, infiniteLoop, preferencesIsVisible } = useSelector(
+ (state) => state.ide
+ );
+ const project = useSelector((state) => state.project);
+ const autorefresh = useSelector((state) => state.preferences.autorefresh);
+ const dispatch = useDispatch();
- this.state = {
- projectNameInputValue: props.project.name
- };
- }
+ const { t } = useTranslation();
+ const { changeSketchName, canEditProjectName } = useSketchActions();
+
+ const projectNameInputRef = useRef();
+ const [nameInputValue, setNameInputValue] = useState(project.name);
- handleKeyPress(event) {
+ function handleKeyPress(event) {
if (event.key === 'Enter') {
- this.props.hideEditProjectName();
- this.projectNameInput.blur();
+ dispatch(hideEditProjectName());
+ projectNameInputRef.current?.blur();
}
}
- handleProjectNameChange(event) {
- this.setState({ projectNameInputValue: event.target.value });
- }
-
- handleProjectNameClick() {
- if (this.canEditProjectName) {
- this.props.showEditProjectName();
+ function handleProjectNameClick() {
+ if (canEditProjectName) {
+ dispatch(showEditProjectName());
setTimeout(() => {
- this.projectNameInput?.focus();
+ projectNameInputRef.current?.focus();
}, 140);
}
}
- handleProjectNameSave() {
- const newProjectName = this.state.projectNameInputValue.trim();
- if (newProjectName.length === 0) {
- this.setState({
- projectNameInputValue: this.props.project.name
- });
- } else {
- this.props.setProjectName(newProjectName);
- this.props.hideEditProjectName();
- if (this.props.project.id) {
- this.props.saveProject();
- }
+ function handleProjectNameSave() {
+ const newName = nameInputValue;
+ if (newName.length > 0) {
+ dispatch(hideEditProjectName());
+ changeSketchName(newName);
}
}
- canEditProjectName() {
- return (
- (this.props.owner &&
- this.props.owner.username &&
- this.props.owner.username === this.props.currentUser) ||
- !this.props.owner ||
- !this.props.owner.username
- );
- }
-
- render() {
- const playButtonClass = classNames({
- 'toolbar__play-button': true,
- 'toolbar__play-button--selected': this.props.isPlaying
- });
- const stopButtonClass = classNames({
- 'toolbar__stop-button': true,
- 'toolbar__stop-button--selected': !this.props.isPlaying
- });
- const preferencesButtonClass = classNames({
- 'toolbar__preferences-button': true,
- 'toolbar__preferences-button--selected': this.props.preferencesIsVisible
- });
- const nameContainerClass = classNames({
- 'toolbar__project-name-container': true,
- 'toolbar__project-name-container--editing': this.props.project
- .isEditingName
- });
-
- const canEditProjectName = this.canEditProjectName();
-
- return (
-
-
-