| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  | 'use client' | 
					
						
							| 
									
										
										
										
											2024-10-21 19:04:54 +08:00
										 |  |  | import { useState } from 'react' | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  | import { t } from 'i18next' | 
					
						
							|  |  |  | import { useParams, usePathname } from 'next/navigation' | 
					
						
							|  |  |  | import s from './style.module.css' | 
					
						
							|  |  |  | import Tooltip from '@/app/components/base/tooltip' | 
					
						
							| 
									
										
										
										
											2024-04-09 07:10:58 +00:00
										 |  |  | import Loading from '@/app/components/base/loading' | 
					
						
							| 
									
										
										
										
											2024-07-09 11:33:58 +08:00
										 |  |  | import { AudioPlayerManager } from '@/app/components/base/audio-btn/audio.player.manager' | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  | type AudioBtnProps = { | 
					
						
							| 
									
										
										
										
											2024-07-09 11:33:58 +08:00
										 |  |  |   id?: string | 
					
						
							| 
									
										
										
										
											2024-03-04 17:50:06 +08:00
										 |  |  |   voice?: string | 
					
						
							| 
									
										
										
										
											2024-07-09 11:33:58 +08:00
										 |  |  |   value?: string | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  |   className?: string | 
					
						
							| 
									
										
										
										
											2024-02-22 16:06:17 +08:00
										 |  |  |   isAudition?: boolean | 
					
						
							| 
									
										
										
										
											2024-07-09 11:33:58 +08:00
										 |  |  |   noCache?: boolean | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-09 07:10:58 +00:00
										 |  |  | type AudioState = 'initial' | 'loading' | 'playing' | 'paused' | 'ended' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  | const AudioBtn = ({ | 
					
						
							| 
									
										
										
										
											2024-07-09 11:33:58 +08:00
										 |  |  |   id, | 
					
						
							| 
									
										
										
										
											2024-03-04 17:50:06 +08:00
										 |  |  |   voice, | 
					
						
							| 
									
										
										
										
											2024-07-09 11:33:58 +08:00
										 |  |  |   value, | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  |   className, | 
					
						
							| 
									
										
										
										
											2024-02-22 16:06:17 +08:00
										 |  |  |   isAudition, | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  | }: AudioBtnProps) => { | 
					
						
							| 
									
										
										
										
											2024-04-09 07:10:58 +00:00
										 |  |  |   const [audioState, setAudioState] = useState<AudioState>('initial') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  |   const params = useParams() | 
					
						
							|  |  |  |   const pathname = usePathname() | 
					
						
							| 
									
										
										
										
											2025-04-14 16:06:10 +08:00
										 |  |  |   const audio_finished_call = (event: string): void => { | 
					
						
							| 
									
										
										
										
											2024-07-09 11:33:58 +08:00
										 |  |  |     switch (event) { | 
					
						
							|  |  |  |       case 'ended': | 
					
						
							|  |  |  |         setAudioState('ended') | 
					
						
							|  |  |  |         break | 
					
						
							|  |  |  |       case 'paused': | 
					
						
							|  |  |  |         setAudioState('ended') | 
					
						
							|  |  |  |         break | 
					
						
							|  |  |  |       case 'loaded': | 
					
						
							|  |  |  |         setAudioState('loading') | 
					
						
							|  |  |  |         break | 
					
						
							|  |  |  |       case 'play': | 
					
						
							|  |  |  |         setAudioState('playing') | 
					
						
							|  |  |  |         break | 
					
						
							|  |  |  |       case 'error': | 
					
						
							|  |  |  |         setAudioState('ended') | 
					
						
							|  |  |  |         break | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-07-09 11:33:58 +08:00
										 |  |  |   let url = '' | 
					
						
							|  |  |  |   let isPublic = false | 
					
						
							| 
									
										
										
										
											2024-04-09 07:10:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-09 11:33:58 +08:00
										 |  |  |   if (params.token) { | 
					
						
							|  |  |  |     url = '/text-to-audio' | 
					
						
							|  |  |  |     isPublic = true | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else if (params.appId) { | 
					
						
							|  |  |  |     if (pathname.search('explore/installed') > -1) | 
					
						
							|  |  |  |       url = `/installed-apps/${params.appId}/text-to-audio` | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       url = `/apps/${params.appId}/text-to-audio` | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-05-25 12:55:04 +08:00
										 |  |  |   const handleToggle = async () => { | 
					
						
							| 
									
										
										
										
											2024-07-09 11:33:58 +08:00
										 |  |  |     if (audioState === 'playing' || audioState === 'loading') { | 
					
						
							| 
									
										
										
										
											2024-07-26 14:55:49 +08:00
										 |  |  |       setTimeout(() => setAudioState('paused'), 1) | 
					
						
							| 
									
										
										
										
											2024-07-09 11:33:58 +08:00
										 |  |  |       AudioPlayerManager.getInstance().getAudioPlayer(url, isPublic, id, value, voice, audio_finished_call).pauseAudio() | 
					
						
							| 
									
										
										
										
											2024-05-25 12:55:04 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-07-09 11:33:58 +08:00
										 |  |  |     else { | 
					
						
							| 
									
										
										
										
											2024-07-26 14:55:49 +08:00
										 |  |  |       setTimeout(() => setAudioState('loading'), 1) | 
					
						
							| 
									
										
										
										
											2024-07-09 11:33:58 +08:00
										 |  |  |       AudioPlayerManager.getInstance().getAudioPlayer(url, isPublic, id, value, voice, audio_finished_call).playAudio() | 
					
						
							| 
									
										
										
										
											2024-04-09 07:10:58 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-07-09 11:33:58 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-04-09 07:10:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const tooltipContent = { | 
					
						
							|  |  |  |     initial: t('appApi.play'), | 
					
						
							|  |  |  |     ended: t('appApi.play'), | 
					
						
							|  |  |  |     paused: t('appApi.pause'), | 
					
						
							|  |  |  |     playing: t('appApi.playing'), | 
					
						
							|  |  |  |     loading: t('appApi.loading'), | 
					
						
							|  |  |  |   }[audioState] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2024-08-26 13:00:02 +08:00
										 |  |  |     <div className={`inline-flex items-center justify-center ${(audioState === 'loading' || audioState === 'playing') ? 'mr-1' : className}`}> | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  |       <Tooltip | 
					
						
							| 
									
										
										
										
											2024-08-26 13:00:02 +08:00
										 |  |  |         popupContent={tooltipContent} | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  |       > | 
					
						
							| 
									
										
										
										
											2024-04-09 07:10:58 +00:00
										 |  |  |         <button | 
					
						
							|  |  |  |           disabled={audioState === 'loading'} | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           className={`box-border flex h-6 w-6 cursor-pointer items-center justify-center ${isAudition ? 'p-0.5' : 'rounded-md bg-white p-0'}`} | 
					
						
							| 
									
										
										
										
											2024-05-25 12:55:04 +08:00
										 |  |  |           onClick={handleToggle} | 
					
						
							|  |  |  |         > | 
					
						
							|  |  |  |           {audioState === 'loading' | 
					
						
							|  |  |  |             ? ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |               <div className='flex h-full w-full items-center justify-center rounded-md'> | 
					
						
							| 
									
										
										
										
											2024-05-25 12:55:04 +08:00
										 |  |  |                 <Loading /> | 
					
						
							|  |  |  |               </div> | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             : ( | 
					
						
							| 
									
										
										
										
											2025-04-14 15:28:20 +08:00
										 |  |  |               <div className={'flex h-full w-full items-center justify-center rounded-md hover:bg-gray-50'}> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 <div className={`h-4 w-4 ${(audioState === 'playing') ? s.pauseIcon : s.playIcon}`}></div> | 
					
						
							| 
									
										
										
										
											2024-08-26 13:00:02 +08:00
										 |  |  |               </div> | 
					
						
							| 
									
										
										
										
											2024-05-25 12:55:04 +08:00
										 |  |  |             )} | 
					
						
							| 
									
										
										
										
											2024-04-09 07:10:58 +00:00
										 |  |  |         </button> | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  |       </Tooltip> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default AudioBtn |