1- import { useCallback , useEffect , useRef , useState } from 'react'
1+ import { useEffect , useRef , useState } from 'react'
22import { usePlayheadScrub } from './usePlayheadScrub'
33import c from './Timeline.module.css'
44import { TimelineTrack } from './TimelineTrack'
55import { TimelineManagerTrack } from '@/types'
6+ import { findTrackById } from '@/utils/findTrackById'
67
78export interface TimelineProps {
89 /** Timeline data */
@@ -12,6 +13,8 @@ export interface TimelineProps {
1213 }
1314 /** Current playhead position in milliseconds */
1415 playheadPositionMs ?: number
16+ selectedTrackId ?: string | null
17+ setSelectedTrackId ?: ( trackId : string | null ) => void
1518 /** Called when the user clicks on the track area to set the playhead */
1619 onPlayheadChange ?: ( time : number ) => void
1720 /** Called when a keyframe should be deleted */
@@ -23,34 +26,25 @@ export interface TimelineProps {
2326export function Timeline ( {
2427 timeline,
2528 playheadPositionMs = 0 ,
29+ selectedTrackId : controlledSelectedTrackId ,
30+ setSelectedTrackId : controlledSetSelectedTrackId ,
2631 onPlayheadChange,
2732 onKeyframeDelete,
2833 onKeyframeInsert,
2934} : TimelineProps ) {
3035 const { durationMs, tracks } = timeline
3136 const rulerAreaRef = useRef < HTMLDivElement > ( null )
32- const [ selectedTrackId , setSelectedTrackId ] = useState < string | null > ( null )
33- const [ selectedKeyframes , setSelectedKeyframes ] = useState < string [ ] | null > ( null )
34-
35- const findTrackById = useCallback (
36- ( trackList : TimelineManagerTrack [ ] , trackId : string ) : TimelineManagerTrack | null => {
37- for ( const track of trackList ) {
38- if ( track . id === trackId ) {
39- return track
40- }
41-
42- if ( track . trackType === 'vector' ) {
43- const childTrack = findTrackById ( track . childTracks , trackId )
44- if ( childTrack ) {
45- return childTrack
46- }
47- }
48- }
49-
50- return null
51- } ,
52- [ ] ,
37+ const [ uncontrolledSelectedTrackId , setUncontrolledSelectedTrackId ] = useState < string | null > (
38+ null ,
5339 )
40+ const isControlled = controlledSetSelectedTrackId !== undefined
41+ const selectedTrackId = isControlled
42+ ? ( controlledSelectedTrackId ?? null )
43+ : uncontrolledSelectedTrackId
44+ const setSelectedTrackId = isControlled
45+ ? controlledSetSelectedTrackId
46+ : setUncontrolledSelectedTrackId
47+ const [ selectedKeyframes , setSelectedKeyframes ] = useState < string [ ] | null > ( null )
5448
5549 useEffect ( ( ) => {
5650 const getChildKeyframeTrackIds = ( track : TimelineManagerTrack ) : string [ ] => {
0 commit comments