mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-10-30 18:47:51 +00:00 
			
		
		
		
	 1ccfb4e3db
			
		
	
	1ccfb4e3db
	
	
	
		
			
			1. Ensure audio player ref is accessible before triggering calls ([Sentry](https://chatwoot-p3.sentry.io/issues/6221981610)) 2. Use correct default for attachments, this was incorrectly set to `null` in a previous PR ([Sentry](https://chatwoot-p3.sentry.io/issues/5966738120)) 3. Fix `lastNonActivityMessage` is not present ([Sentry](https://chatwoot-p3.sentry.io/issues/6116038455)) 4. Fix `Alt+J` & `Alt+K` shortcuts not working ([Sentry](https://chatwoot-p3.sentry.io/issues/6075125384)) --------- Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
		
			
				
	
	
		
			141 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			Vue
		
	
	
	
	
	
			
		
		
	
	
			141 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			Vue
		
	
	
	
	
	
| <script setup>
 | |
| import { computed, onMounted, useTemplateRef, ref } from 'vue';
 | |
| import Icon from 'next/icon/Icon.vue';
 | |
| import { timeStampAppendedURL } from 'dashboard/helper/URLHelper';
 | |
| 
 | |
| const { attachment } = defineProps({
 | |
|   attachment: {
 | |
|     type: Object,
 | |
|     required: true,
 | |
|   },
 | |
| });
 | |
| 
 | |
| defineOptions({
 | |
|   inheritAttrs: false,
 | |
| });
 | |
| 
 | |
| const timeStampURL = computed(() => {
 | |
|   return timeStampAppendedURL(attachment.dataUrl);
 | |
| });
 | |
| 
 | |
| const audioPlayer = useTemplateRef('audioPlayer');
 | |
| 
 | |
| const isPlaying = ref(false);
 | |
| const isMuted = ref(false);
 | |
| const currentTime = ref(0);
 | |
| const duration = ref(0);
 | |
| 
 | |
| const onLoadedMetadata = () => {
 | |
|   duration.value = audioPlayer.value?.duration;
 | |
| };
 | |
| 
 | |
| // There maybe a chance that the audioPlayer ref is not available
 | |
| // When the onLoadMetadata is called, so we need to set the duration
 | |
| // value when the component is mounted
 | |
| onMounted(() => {
 | |
|   duration.value = audioPlayer.value?.duration;
 | |
| });
 | |
| 
 | |
| const formatTime = time => {
 | |
|   const minutes = Math.floor(time / 60);
 | |
|   const seconds = Math.floor(time % 60);
 | |
|   return `${minutes}:${seconds.toString().padStart(2, '0')}`;
 | |
| };
 | |
| 
 | |
| const toggleMute = () => {
 | |
|   audioPlayer.value.muted = !audioPlayer.value.muted;
 | |
|   isMuted.value = audioPlayer.value.muted;
 | |
| };
 | |
| 
 | |
| const onTimeUpdate = () => {
 | |
|   currentTime.value = audioPlayer.value.currentTime;
 | |
| };
 | |
| 
 | |
| const seek = event => {
 | |
|   const time = Number(event.target.value);
 | |
|   audioPlayer.value.currentTime = time;
 | |
|   currentTime.value = time;
 | |
| };
 | |
| 
 | |
| const playOrPause = () => {
 | |
|   if (isPlaying.value) {
 | |
|     audioPlayer.value.pause();
 | |
|     isPlaying.value = false;
 | |
|   } else {
 | |
|     audioPlayer.value.play();
 | |
|     isPlaying.value = true;
 | |
|   }
 | |
| };
 | |
| 
 | |
| const onEnd = () => {
 | |
|   isPlaying.value = false;
 | |
|   currentTime.value = 0;
 | |
| };
 | |
| 
 | |
| const downloadAudio = async () => {
 | |
|   const response = await fetch(timeStampURL.value);
 | |
|   const blob = await response.blob();
 | |
|   const url = window.URL.createObjectURL(blob);
 | |
|   const anchor = document.createElement('a');
 | |
|   anchor.href = url;
 | |
|   const filename = timeStampURL.value.split('/').pop().split('?')[0] || 'audio';
 | |
|   anchor.download = filename;
 | |
|   document.body.appendChild(anchor);
 | |
|   anchor.click();
 | |
|   window.URL.revokeObjectURL(url);
 | |
|   document.body.removeChild(anchor);
 | |
| };
 | |
| </script>
 | |
| 
 | |
| <template>
 | |
|   <audio
 | |
|     ref="audioPlayer"
 | |
|     controls
 | |
|     class="hidden"
 | |
|     @loadedmetadata="onLoadedMetadata"
 | |
|     @timeupdate="onTimeUpdate"
 | |
|     @ended="onEnd"
 | |
|   >
 | |
|     <source :src="timeStampURL" />
 | |
|   </audio>
 | |
|   <div
 | |
|     v-bind="$attrs"
 | |
|     class="rounded-xl w-full gap-1 p-1.5 bg-n-alpha-white flex items-center border border-n-container shadow-[0px_2px_8px_0px_rgba(94,94,94,0.06)]"
 | |
|   >
 | |
|     <button class="p-0 border-0 size-8" @click="playOrPause">
 | |
|       <Icon
 | |
|         v-if="isPlaying"
 | |
|         class="size-8"
 | |
|         icon="i-teenyicons-pause-small-solid"
 | |
|       />
 | |
|       <Icon v-else class="size-8" icon="i-teenyicons-play-small-solid" />
 | |
|     </button>
 | |
|     <div class="tabular-nums text-xs">
 | |
|       {{ formatTime(currentTime) }} / {{ formatTime(duration) }}
 | |
|     </div>
 | |
|     <div class="flex items-center px-2">
 | |
|       <input
 | |
|         type="range"
 | |
|         min="0"
 | |
|         :max="duration"
 | |
|         :value="currentTime"
 | |
|         class="w-full h-1 bg-n-slate-12/40 rounded-lg appearance-none cursor-pointer accent-current"
 | |
|         @input="seek"
 | |
|       />
 | |
|     </div>
 | |
|     <button
 | |
|       class="p-0 border-0 size-8 grid place-content-center"
 | |
|       @click="toggleMute"
 | |
|     >
 | |
|       <Icon v-if="isMuted" class="size-4" icon="i-lucide-volume-off" />
 | |
|       <Icon v-else class="size-4" icon="i-lucide-volume-2" />
 | |
|     </button>
 | |
|     <button
 | |
|       class="p-0 border-0 size-8 grid place-content-center"
 | |
|       @click="downloadAudio"
 | |
|     >
 | |
|       <Icon class="size-4" icon="i-lucide-download" />
 | |
|     </button>
 | |
|   </div>
 | |
| </template>
 |