@@ -2,6 +2,7 @@ import { Button, Input } from '@nextui-org/react'
2
2
import BasePage from '@renderer/components/base/base-page'
3
3
import ProfileItem from '@renderer/components/profiles/profile-item'
4
4
import { useProfileConfig } from '@renderer/hooks/use-profile-config'
5
+ import { getFilePath , readTextFile } from '@renderer/utils/ipc'
5
6
import { useEffect , useRef , useState } from 'react'
6
7
import { MdContentPaste } from 'react-icons/md'
7
8
@@ -40,22 +41,18 @@ const Profiles: React.FC = () => {
40
41
e . stopPropagation ( )
41
42
setFileOver ( false )
42
43
} )
43
- pageRef . current ?. addEventListener ( 'drop' , ( event ) => {
44
+ pageRef . current ?. addEventListener ( 'drop' , async ( event ) => {
44
45
event . preventDefault ( )
45
46
event . stopPropagation ( )
46
47
if ( event . dataTransfer ?. files ) {
47
48
const file = event . dataTransfer . files [ 0 ]
48
49
if ( file . name . endsWith ( '.yml' ) || file . name . endsWith ( '.yaml' ) ) {
49
- const reader = new FileReader ( )
50
- reader . onload = async ( e ) : Promise < void > => {
51
- const content = e . target ?. result as string
52
- try {
53
- await addProfileItem ( { name : file . name , type : 'local' , file : content } )
54
- } finally {
55
- setFileOver ( false )
56
- }
50
+ const content = await readTextFile ( file . path )
51
+ try {
52
+ await addProfileItem ( { name : file . name , type : 'local' , file : content } )
53
+ } finally {
54
+ setFileOver ( false )
57
55
}
58
- reader . readAsText ( file )
59
56
} else {
60
57
alert ( '不支持的文件类型' )
61
58
}
@@ -74,7 +71,6 @@ const Profiles: React.FC = () => {
74
71
< div className = "sticky top-[48px] z-40 backdrop-blur bg-background/40 flex p-2" >
75
72
< Input
76
73
variant = "bordered"
77
- className = "mr-2"
78
74
size = "sm"
79
75
value = { url }
80
76
onValueChange = { setUrl }
@@ -96,12 +92,29 @@ const Profiles: React.FC = () => {
96
92
< Button
97
93
size = "sm"
98
94
color = "primary"
95
+ className = "ml-2"
99
96
isDisabled = { url === '' }
100
97
isLoading = { importing }
101
98
onPress = { handleImport }
102
99
>
103
100
导入
104
101
</ Button >
102
+ < Button
103
+ size = "sm"
104
+ color = "primary"
105
+ className = "ml-2"
106
+ onPress = { ( ) => {
107
+ getFilePath ( ) . then ( async ( files ) => {
108
+ if ( files ?. length ) {
109
+ const content = await readTextFile ( files [ 0 ] )
110
+ const fileName = files [ 0 ] . split ( '/' ) . pop ( ) ?. split ( '\\' ) . pop ( )
111
+ await addProfileItem ( { name : fileName , type : 'local' , file : content } )
112
+ }
113
+ } )
114
+ } }
115
+ >
116
+ 打开
117
+ </ Button >
105
118
</ div >
106
119
< div
107
120
className = { `${ fileOver ? 'blur-sm' : '' } grid sm:grid-cols-2 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-2 mx-2` }
0 commit comments