summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorDevloperAmanSingh <amansinghneh211205@gmail.com>2025-06-27 00:14:29 +0530
committerDevloperAmanSingh <amansinghneh211205@gmail.com>2025-06-27 00:14:29 +0530
commit0bdbd7e2b3feb698ba35b03ec834dd924d4e21d3 (patch)
tree8513f9b88c58d4fdcccf9a0d374d3fb2fab8543d /apps
parent6262f2b3797623cc1b62c82a8e810ad3aaf1d2b1 (diff)
downloadopencut-0bdbd7e2b3feb698ba35b03ec834dd924d4e21d3.tar.gz
refactor: streamline audio track handling by consolidating clip addition logic in timeline store
Diffstat (limited to 'apps')
-rw-r--r--apps/web/src/stores/timeline-store.ts58
1 files changed, 32 insertions, 26 deletions
diff --git a/apps/web/src/stores/timeline-store.ts b/apps/web/src/stores/timeline-store.ts
index dd97879f..04b2f6fb 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<TimelineStore>((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<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;
},