Compare commits

...
10 Commits
3 changed files with 38 additions and 11 deletions
@@ -1,7 +1,9 @@
<script setup>
import { computed } from 'vue';
import { computed, onMounted } from 'vue';
import BaseBubble from './Base.vue';
import AudioChip from 'next/message/chips/Audio.vue';
import Icon from 'next/icon/Icon.vue';
import { useLoadWithRetry } from 'dashboard/composables/loadWithRetry';
import { useMessageContext } from '../provider.js';
const { attachments } = useMessageContext();
@@ -9,11 +11,28 @@ const { attachments } = useMessageContext();
const attachment = computed(() => {
return attachments.value[0];
});
const { isLoaded, hasError, loadWithRetry } = useLoadWithRetry({
mediaType: 'audio',
});
onMounted(() => {
if (attachment.value?.dataUrl) {
loadWithRetry(attachment.value.dataUrl);
}
});
</script>
<template>
<BaseBubble class="bg-transparent" data-bubble-name="audio">
<div v-if="hasError" class="flex items-center gap-1 text-center rounded-lg">
<Icon icon="i-lucide-circle-off" class="text-n-slate-11" />
<p class="mb-0 text-n-slate-11">
{{ $t('COMPONENTS.MEDIA.AUDIO_UNAVAILABLE') }}
</p>
</div>
<AudioChip
v-else-if="isLoaded"
:attachment="attachment"
class="p-2 text-n-slate-12 skip-context-menu"
/>
@@ -1,8 +1,9 @@
import { ref } from 'vue';
export const useLoadWithRetry = (config = {}) => {
const maxRetry = config.max_retry || 3;
const backoff = config.backoff || 1000;
const maxRetry = (config.max_retry ?? config.maxRetry) || 3;
const backoff = (config.backoff ?? config.backOff) || 1000;
const mediaType = (config.mediaType ?? config.media_type) || 'image';
const isLoaded = ref(false);
const hasError = ref(false);
@@ -10,19 +11,25 @@ export const useLoadWithRetry = (config = {}) => {
const loadWithRetry = async url => {
const attemptLoad = () => {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
const onSuccess = () => {
isLoaded.value = true;
hasError.value = false;
resolve();
};
img.onerror = () => {
reject(new Error('Failed to load image'));
};
img.src = url;
let element;
if (mediaType === 'audio') {
element = new Audio();
element.preload = 'metadata';
element.onloadedmetadata = onSuccess;
} else {
element = new Image();
element.onload = onSuccess;
}
element.onerror = () => reject(new Error('Failed to load resource'));
const cacheBustedUrl = new URL(url);
cacheBustedUrl.searchParams.set('t', Date.now());
element.src = cacheBustedUrl.toString();
});
};
@@ -296,6 +296,7 @@
},
"MEDIA": {
"IMAGE_UNAVAILABLE": "This image is no longer available.",
"AUDIO_UNAVAILABLE": "This audio is no longer available.",
"LOADING_FAILED": "Loading failed"
}
},