refactor: streamline audio track handling by consolidating clip addition logic in timeline store
This commit is contained in:
@ -443,29 +443,15 @@ 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");
|
||||||
|
|
||||||
if (!audioTrackId) {
|
|
||||||
audioTrackId = crypto.randomUUID();
|
|
||||||
const newAudioTrack: TimelineTrack = {
|
|
||||||
id: audioTrackId,
|
|
||||||
name: "Audio Track",
|
|
||||||
type: "audio",
|
|
||||||
clips: [],
|
|
||||||
muted: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
set((state) => ({
|
|
||||||
tracks: [...state.tracks, newAudioTrack],
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
const audioClipId = crypto.randomUUID();
|
const audioClipId = crypto.randomUUID();
|
||||||
|
|
||||||
|
if (existingAudioTrack) {
|
||||||
|
// Add audio clip to existing audio track
|
||||||
set((state) => ({
|
set((state) => ({
|
||||||
tracks: state.tracks.map((track) =>
|
tracks: state.tracks.map((track) =>
|
||||||
track.id === audioTrackId
|
track.id === existingAudioTrack.id
|
||||||
? {
|
? {
|
||||||
...track,
|
...track,
|
||||||
clips: [
|
clips: [
|
||||||
@ -480,6 +466,26 @@ export const useTimelineStore = create<TimelineStore>((set, get) => ({
|
|||||||
: track
|
: track
|
||||||
),
|
),
|
||||||
}));
|
}));
|
||||||
|
} else {
|
||||||
|
// Create new audio track with the audio clip in a single atomic update
|
||||||
|
const newAudioTrack: TimelineTrack = {
|
||||||
|
id: crypto.randomUUID(),
|
||||||
|
name: "Audio Track",
|
||||||
|
type: "audio",
|
||||||
|
clips: [
|
||||||
|
{
|
||||||
|
...clip,
|
||||||
|
id: audioClipId,
|
||||||
|
name: getClipNameWithSuffix(clip.name, "audio"),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
muted: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
set((state) => ({
|
||||||
|
tracks: [...state.tracks, newAudioTrack],
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
return audioClipId;
|
return audioClipId;
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user