refactor: streamline audio track handling by consolidating clip addition logic in timeline store

This commit is contained in:
DevloperAmanSingh
2025-06-27 00:14:29 +05:30
parent 6262f2b379
commit 0bdbd7e2b3

View File

@ -443,16 +443,42 @@ export const useTimelineStore = create<TimelineStore>((set, get) => ({
get().pushHistory(); get().pushHistory();
// Find or create an audio track // Find existing audio track or prepare to create one
let audioTrackId = tracks.find((t) => t.type === "audio")?.id; const existingAudioTrack = tracks.find((t) => t.type === "audio");
const audioClipId = crypto.randomUUID();
if (!audioTrackId) { if (existingAudioTrack) {
audioTrackId = crypto.randomUUID(); // 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 = { const newAudioTrack: TimelineTrack = {
id: audioTrackId, id: crypto.randomUUID(),
name: "Audio Track", name: "Audio Track",
type: "audio", type: "audio",
clips: [], clips: [
{
...clip,
id: audioClipId,
name: getClipNameWithSuffix(clip.name, "audio"),
},
],
muted: false, muted: false,
}; };
@ -461,26 +487,6 @@ export const useTimelineStore = create<TimelineStore>((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; return audioClipId;
}, },