-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathToolbar.jsx
161 lines (155 loc) · 4.82 KB
/
Toolbar.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import React from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import { useTranslation } from 'react-i18next';
import { useDispatch, useSelector } from 'react-redux';
import { Link } from 'react-router-dom';
import {
openPreferences,
startAccessibleSketch,
startSketch,
stopSketch
} from '../../actions/ide';
import {
setAutorefresh,
setGridOutput,
setTextOutput
} from '../../actions/preferences';
import PlayIcon from '../../../../images/play.svg';
import StopIcon from '../../../../images/stop.svg';
import PreferencesIcon from '../../../../images/preferences.svg';
import ProjectName from './ProjectName';
import { changeVisibility } from '../../actions/project';
const Toolbar = (props) => {
const { isPlaying, infiniteLoop, preferencesIsVisible } = useSelector(
(state) => state.ide
);
const project = useSelector((state) => state.project);
const user = useSelector((state) => state.user);
const autorefresh = useSelector((state) => state.preferences.autorefresh);
const dispatch = useDispatch();
const { t } = useTranslation();
console.log(project.visibility);
const userIsOwner = user?.username === project.owner?.username;
const toggleVisibility = (e) => {
try {
const isChecked = e.target.checked;
dispatch(
changeVisibility(
project.id,
project.name,
isChecked ? 'Private' : 'Public'
)
);
} catch (error) {
console.log(error);
}
};
const playButtonClass = classNames({
'toolbar__play-button': true,
'toolbar__play-button--selected': isPlaying
});
const stopButtonClass = classNames({
'toolbar__stop-button': true,
'toolbar__stop-button--selected': !isPlaying
});
const preferencesButtonClass = classNames({
'toolbar__preferences-button': true,
'toolbar__preferences-button--selected': preferencesIsVisible
});
return (
<div className="toolbar">
<button
className="toolbar__play-sketch-button"
onClick={() => {
props.syncFileContent();
dispatch(startAccessibleSketch());
dispatch(setTextOutput(true));
dispatch(setGridOutput(true));
}}
aria-label={t('Toolbar.PlaySketchARIA')}
disabled={infiniteLoop}
>
<PlayIcon focusable="false" aria-hidden="true" />
</button>
<button
className={playButtonClass}
onClick={() => {
props.syncFileContent();
dispatch(startSketch());
}}
aria-label={t('Toolbar.PlayOnlyVisualSketchARIA')}
title={t('Toolbar.PlaySketchARIA')}
disabled={infiniteLoop}
>
<PlayIcon focusable="false" aria-hidden="true" />
</button>
<button
className={stopButtonClass}
onClick={() => dispatch(stopSketch())}
aria-label={t('Toolbar.StopSketchARIA')}
title={t('Toolbar.StopSketchARIA')}
>
<StopIcon focusable="false" aria-hidden="true" />
</button>
<div className="toolbar__autorefresh">
<input
id="autorefresh"
className="checkbox__autorefresh"
type="checkbox"
checked={autorefresh}
onChange={(event) => {
dispatch(setAutorefresh(event.target.checked));
if (event.target.checked) {
dispatch(startSketch());
}
}}
/>
<label htmlFor="autorefresh" className="toolbar__autorefresh-label">
{t('Toolbar.Auto-refresh')}
</label>
</div>
<div className="toolbar__project-name-container">
<ProjectName />
{(() => {
if (project?.owner && userIsOwner) {
return (
<main className="toolbar__makeprivate">
<p>Private</p>
<input
type="checkbox"
className="toolbar__togglevisibility"
defaultChecked={project.visibility === 'Private'}
onChange={toggleVisibility}
/>
</main>
);
}
if (project?.owner && !userIsOwner) {
return (
<p className="toolbar__project-project.owner">
{t('Toolbar.By')}{' '}
<Link to={`/${project.owner.username}/sketches`}>
{project.owner.username}
</Link>
</p>
);
}
return null;
})()}
</div>
<button
className={preferencesButtonClass}
onClick={() => dispatch(openPreferences())}
aria-label={t('Toolbar.OpenPreferencesARIA')}
title={t('Toolbar.OpenPreferencesARIA')}
>
<PreferencesIcon focusable="false" aria-hidden="true" />
</button>
</div>
);
};
Toolbar.propTypes = {
syncFileContent: PropTypes.func.isRequired
};
export default Toolbar;