From 0bdbd7e2b3feb698ba35b03ec834dd924d4e21d3 Mon Sep 17 00:00:00 2001 From: DevloperAmanSingh Date: Fri, 27 Jun 2025 00:14:29 +0530 Subject: [PATCH] refactor: streamline audio track handling by consolidating clip addition logic in timeline store --- apps/web/src/stores/timeline-store.ts | 58 +++++++++++++++------------ 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/apps/web/src/stores/timeline-store.ts b/apps/web/src/stores/timeline-store.ts index dd97879..04b2f6f 100644 --- a/apps/web/src/stores/timeline-store.ts +++ b/apps/web/src/stores/timeline-store.ts @@ -443,16 +443,42 @@ export const useTimelineStore = create((set, get) => ({ get().pushHistory(); - // Find or create an audio track - let audioTrackId = tracks.find((t) => t.type === "audio")?.id; + // Find existing audio track or prepare to create one + const existingAudioTrack = tracks.find((t) => t.type === "audio"); + const audioClipId = crypto.randomUUID(); - if (!audioTrackId) { - audioTrackId = crypto.randomUUID(); + if (existingAudioTrack) { + // Add audio clip to existing audio track + set((state) => ({ + tracks: state.tracks.map((track) => + track.id === existingAudioTrack.id + ? { + ...track, + clips: [ + ...track.clips, + { + ...clip, + id: audioClipId, + name: getClipNameWithSuffix(clip.name, "audio"), + }, + ], + } + : track + ), + })); + } else { + // Create new audio track with the audio clip in a single atomic update const newAudioTrack: TimelineTrack = { - id: audioTrackId, + id: crypto.randomUUID(), name: "Audio Track", type: "audio", - clips: [], + clips: [ + { + ...clip, + id: audioClipId, + name: getClipNameWithSuffix(clip.name, "audio"), + }, + ], muted: false, }; @@ -461,26 +487,6 @@ export const useTimelineStore = create((set, get) => ({ })); } - const audioClipId = crypto.randomUUID(); - - set((state) => ({ - tracks: state.tracks.map((track) => - track.id === audioTrackId - ? { - ...track, - clips: [ - ...track.clips, - { - ...clip, - id: audioClipId, - name: getClipNameWithSuffix(clip.name, "audio"), - }, - ], - } - : track - ), - })); - return audioClipId; },