Skip to content

A self-hosted media server and file management system

fix: transcript ui has no error handling

aminnausin184a0bf841e6
diff --git a/resources/js/components/video/transcript/PlayerTranscript.vue b/resources/js/components/video/transcript/PlayerTranscript.vue
index 353cb0f7..5b7a1d43 100644
--- a/resources/js/components/video/transcript/PlayerTranscript.vue
+++ b/resources/js/components/video/transcript/PlayerTranscript.vue
@@ -1,5 +1,5 @@
<script setup lang="ts">
-import { computed, nextTick, onBeforeUnmount, ref, useTemplateRef, watch } from 'vue';
+import { computed, nextTick, ref, useTemplateRef, watch } from 'vue';
import { ButtonBase, ButtonCorner } from '@/components/cedar-ui/button';
import { useReactiveBreakpoints } from '@/service/breakpoints/useReactiveBreakpoints';
import { formatTimestamp } from '@/components/video/transcript/transcriptUtil';
@@ -8,9 +8,12 @@ import { useAppStore } from '@/stores/AppStore';
import { cn } from '@aminnausin/cedar-ui';
import GenerateTranscript from '@/components/video/transcript/GenerateTranscript.vue';
+import TranscriptStatus from '@/components/video/transcript/TranscriptStatus.vue';
import TranscriptLine from '@/components/video/transcript/TranscriptLine.vue';
+import ProiconsAlertTriangle from '~icons/proicons/alert-triangle';
import ProiconsPanelRight from '~icons/proicons/panel-right';
+import ProiconsArrowSync from '~icons/proicons/arrow-sync';
import ProiconsCancel from '~icons/proicons/cancel';
const props = defineProps<{ isVisible?: boolean }>();
@@ -18,30 +21,14 @@ const props = defineProps<{ isVisible?: boolean }>();
const { cycleSideBar } = useAppStore();
const { isDesktop } = useReactiveBreakpoints();
-const {
- isNormalView,
- subtitleTrack,
- rawTranscript,
- parsedTranscript,
- activeIndex,
- isActiveLive,
- loadedTranscriptUrl,
- loadTranscript,
- isLoading,
- generated,
- seek,
- close,
- placement,
-} = useTranscript();
+const { isNormalView, subtitleTrack, parsedTranscript, activeIndex, isActiveLive, isLoading, hasError, retry, generated, seek, close, placement } = useTranscript();
const transcriptContainer = useTemplateRef('transcript-container');
-const transcriptLineRefs = ref<(HTMLElement | null)[]>([]);
-
-const isFollowing = ref(true);
-
const transcriptDuration = computed(() => parsedTranscript.value.at(-1)?.end ?? 0);
const timestamps = computed(() => parsedTranscript.value.map((line) => formatTimestamp(line.start, transcriptDuration.value)));
+const isFollowing = ref(true);
+
function onSelect(start: number) {
isFollowing.value = true;
seek(start);
@@ -75,65 +62,31 @@ function handleUserScroll() {
isFollowing.value = false;
}
-function resync() {
+async function resync(scrollBehavior: ScrollBehavior = 'smooth', waitForTick = false) {
isFollowing.value = true;
- if (activeIndex.value !== -1) scrollToLine(activeIndex.value);
+ if (waitForTick) await nextTick();
+ if (activeIndex.value !== -1) scrollToLine(activeIndex.value, scrollBehavior);
}
-watch(
- transcriptContainer,
- (el, oldEl) => {
- oldEl?.removeEventListener('wheel', handleUserScroll);
- oldEl?.removeEventListener('touchmove', handleUserScroll);
- el?.addEventListener('wheel', handleUserScroll, { passive: true });
- el?.addEventListener('touchmove', handleUserScroll, { passive: true });
- },
- { immediate: true },
-);
-
-watch(
- () => subtitleTrack.value,
- () => {
- rawTranscript.value = '';
- transcriptLineRefs.value = [];
- loadedTranscriptUrl.value = undefined;
-
- if (props.isVisible) {
- void loadTranscript();
- }
- },
-);
-
watch(
() => props.isVisible,
- (isVisible) => {
- if (isVisible) {
- void loadTranscript();
- }
+ async (visible) => {
+ if (!visible) return;
+ resync('auto', true);
},
{ immediate: true },
);
watch(activeIndex, async (index) => {
if (index === -1 || !isFollowing.value) return;
-
await nextTick();
-
scrollToLine(index);
});
-
-onBeforeUnmount(() => {
- transcriptContainer.value?.removeEventListener('wheel', handleUserScroll);
- transcriptContainer.value?.removeEventListener('touchmove', handleUserScroll);
-});
-
-import { onRenderTriggered } from 'vue';
-onRenderTriggered(console.log);
</script>
<template>
<div
- v-show="isVisible"
+ v-if="isVisible"
:class="[
'pointer-events-auto w-full max-w-80 flex-1 overflow-y-auto xl:max-w-120',
{ 'rounded-xl border border-neutral-700/10 bg-neutral-800/90 p-1.5 text-[10px] backdrop-blur-md sm:text-xs': placement !== 'sidebar' },
@@ -143,6 +96,8 @@ onRenderTriggered(console.log);
<div
ref="transcript-container"
:class="['scrollbar-minimal scrollbar-dark relative flex h-full flex-col gap-1 overflow-y-auto pe-1', { 'max-h-(--min-page-height-derived)': placement === 'sidebar' }]"
+ @wheel.passive="handleUserScroll"
+ @touchmove.passive="handleUserScroll"
>
<div
:class="cn('sticky top-0 z-10 flex items-center justify-between gap-2 rounded-lg bg-neutral-800 px-2 py-1.5 backdrop-blur-xs', { hidden: placement === 'sidebar' })"
@@ -174,13 +129,28 @@ onRenderTriggered(console.log);
</ButtonCorner>
</div>
</div>
- <div class="w-full space-y-1" v-if="isVisible && subtitleTrack">
- <div v-if="isLoading">
+ <div class="w-full space-y-1" v-if="subtitleTrack">
+ <div v-if="isLoading && !hasError">
<button type="button" :class="['group flex w-full animate-pulse cursor-pointer items-start gap-2 rounded-lg px-2 py-1.5 text-left transition-colors']">
<span class="shrink-0 rounded-md px-1.5 py-0.5 font-mono text-xs tabular-nums transition-colors" :class="'bg-white/5 text-transparent'"> 00:00 </span>
<span class="h-5 w-full rounded-md bg-white/15"></span>
</button>
</div>
+ <TranscriptStatus
+ v-if="hasError"
+ :is-busy="isLoading"
+ :copy="isLoading ? { heading: 'Loading transcript', button: 'Loading...' } : { heading: 'Failed', description: 'Transcript failed to load', button: 'Retry' }"
+ :badge-class="cn({ 'bg-danger-2/10 text-danger-2': hasError && !isLoading })"
+ @action="retry"
+ >
+ <template #icon>
+ <ProiconsAlertTriangle class="size-4 sm:size-5" />
+ </template>
+ <template #buttonIcon>
+ <ProiconsArrowSync class="size-3.5" />
+ </template>
+ </TranscriptStatus>
+
<div class="space-y-0.5" v-else>
<TranscriptLine
v-for="line in parsedTranscript"
@@ -218,7 +188,7 @@ onRenderTriggered(console.log);
type="button"
class="text-foreground-0 dark:text-foreground-i absolute bottom-2 left-1/2 z-20 -translate-x-1/2 rounded-full bg-white px-2 py-1.5 font-medium transition hover:bg-white/90"
:use-size="false"
- @click="resync"
+ @click="resync()"
>
Jump to current
</ButtonBase>
diff --git a/resources/js/components/video/transcript/useTranscript.ts b/resources/js/components/video/transcript/useTranscript.ts
index ee5cda44..5ec03a98 100644
--- a/resources/js/components/video/transcript/useTranscript.ts
+++ b/resources/js/components/video/transcript/useTranscript.ts
@@ -21,6 +21,11 @@ const currentTime = ref(0);
const rawTranscript = ref('');
const loadedTranscriptUrl = ref<string>();
+const hasError = ref(false);
+const transcriptUrl = computed(() => buildTranscriptUrl(subtitleTrack.value));
+
+let controller: AbortController | null = null;
+
const canUseSidebar = computed(() => isNormalView.value && isDesktop.value);
const placement = computed<'hidden' | 'sidebar' | 'overlay'>(() => {
@@ -82,28 +87,48 @@ function handleTimeUpdate(this: HTMLVideoElement) {
currentTime.value = this.currentTime;
}
-async function loadTranscript() {
- const url = buildTranscriptUrl(subtitleTrack.value);
+function reset() {
+ controller?.abort();
+ controller = null;
+ rawTranscript.value = '';
+ loadedTranscriptUrl.value = undefined;
+ hasError.value = false;
+ isLoading.value = false;
+}
- if (!url || loadedTranscriptUrl.value === url) return;
+async function loadTranscript(url: string | undefined, force = false) {
+ // const url = buildTranscriptUrl(subtitleTrack.value);
- try {
- isLoading.value = true;
+ // if (!url || loadedTranscriptUrl.value === url) return;
+
+ if (!url || (!force && url === loadedTranscriptUrl.value)) return;
- const response = await fetch(url);
+ controller?.abort();
+ const current = (controller = new AbortController());
+ isLoading.value = true;
+ try {
+ const response = await fetch(url, { signal: current.signal });
if (!response.ok) throw new Error(`Failed to fetch transcript: ${response.status}`);
rawTranscript.value = await response.text();
loadedTranscriptUrl.value = url;
+ hasError.value = false;
} catch (error) {
+ if (current.signal.aborted) return;
+
+ hasError.value = true;
toast.error('Failed to load transcript');
console.error('Failed to load transcript:', error);
} finally {
- isLoading.value = false;
+ if (controller === current) isLoading.value = false;
}
}
+function retry() {
+ void loadTranscript(transcriptUrl.value, true);
+}
+
//#endregion
watch(player, (newPlayer, oldPlayer) => {
@@ -116,19 +141,26 @@ watch(isShowingTranscript, (show) => {
else if (!show && selectedSideBar.value === 'transcript') selectedSideBar.value = '';
});
+watch(transcriptUrl, reset);
+
+watch([isShowingTranscript, transcriptUrl], ([show, url]) => show && void loadTranscript(url), { immediate: true });
+
export function useTranscript() {
return {
// transcript
isShowingTranscript,
rawTranscript,
currentTime,
- isLoading,
- loadedTranscriptUrl,
parsedTranscript,
activeIndex,
isActiveLive,
- loadTranscript,
placement,
+ // api
+ loadedTranscriptUrl,
+ isLoading,
+ hasError,
+ loadTranscript,
+ retry,
// player-owned state
player,
isNormalView,