WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 5aa6caf

Browse files
committed
Refactor subtitle and chapters URL cleanup logic
1 parent fac67c0 commit 5aa6caf

File tree

1 file changed

+18
-32
lines changed

1 file changed

+18
-32
lines changed

apps/web/app/embed/[videoId]/_components/EmbedVideo.tsx

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ export const EmbedVideo = forwardRef<
9292
}
9393
}, [transcriptContent, transcriptError]);
9494

95-
// Handle subtitle URL creation
9695
useEffect(() => {
9796
if (
9897
data.transcriptionStatus === "COMPLETE" &&
@@ -102,51 +101,38 @@ export const EmbedVideo = forwardRef<
102101
const vttContent = formatTranscriptAsVTT(transcriptData);
103102
const blob = new Blob([vttContent], { type: "text/vtt" });
104103
const newUrl = URL.createObjectURL(blob);
105-
106-
// Clean up previous URL
107-
if (subtitleUrl) {
108-
URL.revokeObjectURL(subtitleUrl);
109-
}
110-
111-
setSubtitleUrl(newUrl);
112-
104+
setSubtitleUrl((prev) => {
105+
if (prev) URL.revokeObjectURL(prev);
106+
return newUrl;
107+
});
113108
return () => {
114109
URL.revokeObjectURL(newUrl);
115110
};
116-
} else {
117-
// Clean up if no longer needed
118-
if (subtitleUrl) {
119-
URL.revokeObjectURL(subtitleUrl);
120-
setSubtitleUrl(null);
121-
}
122111
}
123-
}, [data.transcriptionStatus, transcriptData, subtitleUrl]);
112+
setSubtitleUrl((prev) => {
113+
if (prev) URL.revokeObjectURL(prev);
114+
return null;
115+
});
116+
}, [data.transcriptionStatus, transcriptData]);
124117

125-
// Handle chapters URL creation
126118
useEffect(() => {
127119
if (chapters?.length > 0) {
128120
const vttContent = formatChaptersAsVTT(chapters);
129121
const blob = new Blob([vttContent], { type: "text/vtt" });
130122
const newUrl = URL.createObjectURL(blob);
131-
132-
// Clean up previous URL
133-
if (chaptersUrl) {
134-
URL.revokeObjectURL(chaptersUrl);
135-
}
136-
137-
setChaptersUrl(newUrl);
138-
123+
setChaptersUrl((prev) => {
124+
if (prev) URL.revokeObjectURL(prev);
125+
return newUrl;
126+
});
139127
return () => {
140128
URL.revokeObjectURL(newUrl);
141129
};
142-
} else {
143-
// Clean up if no longer needed
144-
if (chaptersUrl) {
145-
URL.revokeObjectURL(chaptersUrl);
146-
setChaptersUrl(null);
147-
}
148130
}
149-
}, [chapters, chaptersUrl]);
131+
setChaptersUrl((prev) => {
132+
if (prev) URL.revokeObjectURL(prev);
133+
return null;
134+
});
135+
}, [chapters]);
150136

151137
const isMp4Source =
152138
data.source.type === "desktopMP4" || data.source.type === "webMP4";

0 commit comments

Comments
 (0)