Skip to content

Commit 0b019cd

Browse files
committed
Factor out helper function to get major version
1 parent 51642d5 commit 0b019cd

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

client/modules/IDE/components/Preferences/index.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ import {
2121
setLinewrap,
2222
setPreferencesTab
2323
} from '../../actions/preferences';
24-
import { p5SoundURL, p5URL, useP5Version } from '../../hooks/useP5Version';
24+
import {
25+
majorVersion,
26+
p5SoundURL,
27+
p5URL,
28+
useP5Version
29+
} from '../../hooks/useP5Version';
2530
import VersionPicker from '../VersionPicker';
2631
import { updateFileContent } from '../../actions/files';
2732
import { CmControllerContext } from '../../pages/IDEView';
@@ -55,7 +60,7 @@ export default function Preferences() {
5560
const timerRef = useRef(null);
5661
const pickerRef = useRef(null);
5762
const onChangeVersion = (version) => {
58-
const shouldShowStars = version.startsWith('2.');
63+
const shouldShowStars = majorVersion(version) === '2';
5964
const box = pickerRef.current?.getBoundingClientRect();
6065
if (shouldShowStars) {
6166
setShowStars({ left: box?.left || 0, top: box?.top || 0 });

client/modules/IDE/hooks/useP5Version.jsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ export const p5Versions = [
144144

145145
export const currentP5Version = '1.11.5'; // Don't update to 2.x until 2026
146146

147+
export const majorVersion = (version) => version.split('.')[0];
148+
147149
export const p5SoundURLOldTemplate =
148150
'https://cdnjs.cloudflare.com/ajax/libs/p5.js/$VERSION/addons/p5.sound.min.js';
149151
export const p5SoundURLOld = p5SoundURLOldTemplate.replace(
@@ -227,7 +229,7 @@ export function P5VersionProvider(props) {
227229
const newNode = document.createElement('script');
228230
newNode.setAttribute(
229231
'src',
230-
version.startsWith('2')
232+
majorVersion(version) === '2'
231233
? p5SoundURL
232234
: p5SoundURLOldTemplate.replace('$VERSION', version)
233235
);
@@ -255,14 +257,14 @@ export function P5VersionProvider(props) {
255257
);
256258

257259
if (p5SoundNode) {
258-
if (version.startsWith('2.') !== newVersion.startsWith('2.')) {
260+
if (majorVersion(version) !== majorVersion(newVersion)) {
259261
// Turn off p5.sound if the user switched from 1.x to 2.x
260262
setP5Sound(false);
261263
} else {
262264
// Replace the existing p5.sound with the one compatible with
263265
// the new version
264266
setP5SoundURL(
265-
version.startsWith('2.')
267+
majorVersion(version) === '2'
266268
? p5SoundURL
267269
: p5SoundURLOldTemplate.replace('$VERSION', newVersion)
268270
);
@@ -316,7 +318,7 @@ export function P5VersionProvider(props) {
316318

317319
return {
318320
version,
319-
isVersion2: version.startsWith('2.'),
321+
isVersion2: majorVersion(version) === '2',
320322
minified,
321323
replaceVersion,
322324
p5Sound: !!p5SoundNode,

0 commit comments

Comments
 (0)