1-
21import { playerSettingSheetAtom , videoAtom } from '@renderer/atoms/player'
32import { jotaiStore } from '@renderer/atoms/store'
43import {
@@ -24,12 +23,12 @@ import { useSubtitle } from './hooks'
2423
2524export const SubtitleImport = ( ) => {
2625 const player = usePlayerInstance ( )
27- const { subtitlesData, fetchSubtitleBody } = useSubtitle ( )
26+ const { subtitlesData, fetchSubtitleBody, isLoadingEmbeddedSubtitle } = useSubtitle ( )
2827 const { toast } = useToast ( )
2928 const { hash } = useAtomValue ( videoAtom )
3029 const fileInputRef = useRef < HTMLInputElement | null > ( null )
3130 const { data : defaultValue , isFetching } = useQuery ( {
32- queryKey : [ 'subtitlesDefaultValue' , hash ] ,
31+ queryKey : [ 'subtitlesDefaultValue' , hash , isLoadingEmbeddedSubtitle ] ,
3332 queryFn : async ( ) => {
3433 const history = await db . history . get ( hash )
3534 return history ?. subtitles ?. defaultId ?. toString ( ) ?? '-1'
@@ -48,33 +47,47 @@ export const SubtitleImport = () => {
4847 }
4948 } , [ player ] )
5049
51- const importSubtitleFromBrowser = ( e : ChangeEvent < HTMLInputElement > ) => {
50+ const importSubtitleFromBrowser = async ( e : ChangeEvent < HTMLInputElement > ) => {
5251 const changeEvent = e as unknown as ChangeEvent < HTMLInputElement >
5352 const file = changeEvent . target ?. files ?. [ 0 ]
5453
5554 if ( ! file ) {
5655 return
5756 }
5857 const url = URL . createObjectURL ( file )
59- fetchSubtitleBody ( { path : url , fileName : file . name } )
60- toast ( {
61- title : '导入字幕成功' ,
62- duration : 1500 ,
63- } )
64- jotaiStore . set ( playerSettingSheetAtom , false )
58+ try {
59+ await fetchSubtitleBody ( { path : url , fileName : file . name } )
60+ toast ( {
61+ title : '导入字幕成功' ,
62+ duration : 1500 ,
63+ } )
64+ jotaiStore . set ( playerSettingSheetAtom , false )
65+ } catch {
66+ toast ( {
67+ title : '导入字幕失败' ,
68+ duration : 1500 ,
69+ } )
70+ }
6571 }
6672
6773 const importSubtitleFromClient = async ( ) => {
6874 const subtitlePath = await tipcClient ?. importSubtitle ( )
6975 if ( ! subtitlePath ) {
7076 return
7177 }
72- fetchSubtitleBody ( { path : subtitlePath . filePath , fileName : subtitlePath . fileName } )
73- toast ( {
74- title : '导入字幕成功' ,
75- duration : 1500 ,
76- } )
77- jotaiStore . set ( playerSettingSheetAtom , false )
78+ try {
79+ await fetchSubtitleBody ( { path : subtitlePath . filePath , fileName : subtitlePath . fileName } )
80+ toast ( {
81+ title : '导入字幕成功' ,
82+ duration : 1500 ,
83+ } )
84+ jotaiStore . set ( playerSettingSheetAtom , false )
85+ } catch {
86+ toast ( {
87+ title : '导入字幕失败' ,
88+ duration : 1500 ,
89+ } )
90+ }
7891 }
7992
8093 if ( ! defaultValue || isFetching ) {
@@ -85,17 +98,20 @@ export const SubtitleImport = () => {
8598 < Select
8699 defaultValue = { defaultValue . toString ( ) }
87100 onValueChange = { ( id ) => fetchSubtitleBody ( { id : + id } ) }
101+ disabled = { isLoadingEmbeddedSubtitle }
88102 >
89103 < SelectTrigger className = "w-[200px]" >
90104 < SelectValue placeholder = "选中字幕" />
91105 </ SelectTrigger >
92106 < SelectContent >
93107 < SelectGroup >
94- < SelectItem value = { '-1' } > 关闭</ SelectItem >
108+ < SelectItem value = { '-1' } >
109+ { isLoadingEmbeddedSubtitle ? '正在加载字幕中...' : '关闭' }
110+ </ SelectItem >
95111 { subtitlesData ?. tags ?. map ( ( subtitle ) => {
96112 return (
97113 < SelectItem value = { subtitle . id . toString ( ) } key = { subtitle . id } >
98- { subtitle . title }
114+ { isLoadingEmbeddedSubtitle ? '正在加载字幕中...' : subtitle . title }
99115 </ SelectItem >
100116 )
101117 } ) }
@@ -124,4 +140,4 @@ export const SubtitleImport = () => {
124140 ) }
125141 </ >
126142 )
127- }
143+ }
0 commit comments