Skip to content
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
2 changes: 1 addition & 1 deletion apps/src/music/views/MusicLabView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ const MusicLabView: React.FunctionComponent<MusicLabViewProps> = ({
() => progressManager?.resetValidation(),
[progressManager]
);
usePlaybackUpdate(doPlaybackUpdate, resetValidation);
usePlaybackUpdate(isPlaying, doPlaybackUpdate, resetValidation);

const onInstructionsTextClick = useCallback(
(id: string) => {
Expand Down
10 changes: 10 additions & 0 deletions apps/src/music/views/Timeline/TimelineContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {createContext} from 'react';

import useRequiredContext from '@cdo/apps/util/hooks/useRequiredContext';

import {TimelineProps} from './TimelineUI';

export const TimelineContext = createContext<TimelineProps | null>(null);

export const useTimelineContext = () =>
useRequiredContext(TimelineContext, 'TimelineContext');
28 changes: 11 additions & 17 deletions apps/src/music/views/Timeline/TimelineElement.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import classNames from 'classnames';
import React from 'react';
import {useDispatch} from 'react-redux';

import {isChordEvent} from '@cdo/apps/music/player/interfaces/ChordEvent';
import {isInstrumentEvent} from '@cdo/apps/music/player/interfaces/InstrumentEvent';
import {PlaybackEvent} from '@cdo/apps/music/player/interfaces/PlaybackEvent';
import {isSoundEvent} from '@cdo/apps/music/player/interfaces/SoundEvent';
import {selectBlockId} from '@cdo/apps/music/redux/musicRedux';
import SoundStyle from '@cdo/apps/music/utils/SoundStyle';
import {useAppSelector} from '@cdo/apps/util/reduxHooks';

import {useTimelineContext} from './TimelineContext';

import moduleStyles from './timeline.module.scss';

Expand All @@ -34,23 +33,18 @@ const TimelineElement: React.FunctionComponent<TimelineElementProps> = ({
top,
left,
}) => {
const isPlaying = useAppSelector(state => state.music.isPlaying);
const selectedBlockId = useAppSelector(state => state.music.selectedBlockId);
const dispatch = useDispatch();
const {isPlaying, selectedBlockId, currentPlayheadPosition, selectBlockId} =
useTimelineContext();
const isInsideRandom = eventData.skipContext?.insideRandom;
const isSkipSound = isPlaying && eventData.skipContext?.skipSound;
const isThinBorder = height <= 4;

const isCurrentlyPlaying = useAppSelector(state => {
const currentPlayheadPosition = state.music.currentPlayheadPosition;
return (
isPlaying &&
!isSkipSound &&
currentPlayheadPosition !== 0 &&
currentPlayheadPosition >= eventData.when &&
currentPlayheadPosition < eventData.when + eventData.length
);
});
const isCurrentlyPlaying =
isPlaying &&
!isSkipSound &&
currentPlayheadPosition !== 0 &&
currentPlayheadPosition >= eventData.when &&
currentPlayheadPosition < eventData.when + eventData.length;

const isBlockSelected = eventData.blockId === selectedBlockId;

Expand Down Expand Up @@ -90,7 +84,7 @@ const TimelineElement: React.FunctionComponent<TimelineElementProps> = ({
left,
}}
onClick={event => {
dispatch(selectBlockId(eventData.blockId));
selectBlockId?.(eventData.blockId);
event.stopPropagation();
}}
>
Expand Down
4 changes: 2 additions & 2 deletions apps/src/music/views/Timeline/TimelineSampleEvents.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import PropTypes from 'prop-types';
import React, {useRef} from 'react';
import {useSelector} from 'react-redux';

import UniqueSounds from '@cdo/apps/music/utils/UniqueSounds';

import {useTimelineContext} from './TimelineContext';
import TimelineElement from './TimelineElement';

/**
Expand All @@ -16,7 +16,7 @@ const TimelineSampleEvents = ({
getEventHeight,
getEventVerticalSpace,
}) => {
const soundEvents = useSelector(state => state.music.playbackEvents);
const {playbackEvents: soundEvents} = useTimelineContext();

const uniqueSoundsRef = useRef(new UniqueSounds());
// Let's cache the value of getUniqueSounds() so that the various helpers
Expand Down
11 changes: 3 additions & 8 deletions apps/src/music/views/Timeline/TimelineSimple2Events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {collectBlockIdsRecursively} from '@cdo/apps/music/blockly/blockUtils';
import {MAX_FUNCTION_BOUNDS_RENDER_DEPTH} from '@cdo/apps/music/constants';
import {FunctionEvents} from '@cdo/apps/music/player/interfaces/FunctionEvents';
import {PlaybackEvent} from '@cdo/apps/music/player/interfaces/PlaybackEvent';
import {useAppSelector} from '@cdo/apps/util/reduxHooks';

import {useTimelineContext} from './TimelineContext';
import TimelineElement from './TimelineElement';

const timelineLayoutParam = AppConfig.getValue('timeline-layout');
Expand Down Expand Up @@ -199,9 +199,8 @@ const TimelineSimple2Events: React.FunctionComponent<
getEventHeight,
getEventVerticalSpace,
}) => {
const soundEventsOriginal = useAppSelector(
state => state.music.playbackEvents
);
const {playbackEvents: soundEventsOriginal, orderedFunctions} =
useTimelineContext();

// soundEventsOriginal has sounds sorted primarily by the immediate function
// that generates them, and next by when they are played.
Expand All @@ -216,10 +215,6 @@ const TimelineSimple2Events: React.FunctionComponent<
soundEvents = getOrderedByWhenSoundEvents(soundEventsOriginal);
}

const orderedFunctions = useAppSelector(
state => state.music.orderedFunctions
);

// Generate a list of unique sounds, with uniqueness being a combination of
// the function name and the sound ID.
// Let's cache the value of currentUniqueSounds so that the various helpers
Expand Down
Loading