Skip to content

Commit 8093aa9

Browse files
author
pompurin404
committed
0.2.0
1 parent fd86c0d commit 8093aa9

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mihomo-party",
3-
"version": "0.1.6",
3+
"version": "0.2.0",
44
"description": "Mihomo Party",
55
"main": "./out/main/index.js",
66
"author": "mihomo-party",

src/main/utils/ipc.ts

+15
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { triggerSysProxy } from '../resolve/sysproxy'
3535
import { checkUpdate } from '../resolve/autoUpdater'
3636
import { exePath, mihomoCorePath } from './dirs'
3737
import { execSync } from 'child_process'
38+
import fs from 'fs'
3839

3940
export function registerIpcMainHandlers(): void {
4041
ipcMain.handle('mihomoVersion', mihomoVersion)
@@ -70,13 +71,27 @@ export function registerIpcMainHandlers(): void {
7071
ipcMain.handle('triggerSysProxy', (_e, enable) => triggerSysProxy(enable))
7172
ipcMain.handle('isEncryptionAvailable', isEncryptionAvailable)
7273
ipcMain.handle('encryptString', (_e, str) => safeStorage.encryptString(str))
74+
ipcMain.handle('getFilePath', getFilePath)
75+
ipcMain.handle('readTextFile', (_e, filePath) => readTextFile(filePath))
7376
ipcMain.handle('checkUpdate', () => checkUpdate())
7477
ipcMain.handle('getVersion', () => app.getVersion())
7578
ipcMain.handle('platform', () => process.platform)
7679
ipcMain.handle('setupFirewall', setupFirewall)
7780
ipcMain.handle('quitApp', () => app.quit())
7881
}
7982

83+
function getFilePath(): string[] | undefined {
84+
return dialog.showOpenDialogSync({
85+
title: '选择订阅文件',
86+
filters: [{ name: 'Yaml Files', extensions: ['yml', 'yaml'] }],
87+
properties: ['openFile']
88+
})
89+
}
90+
91+
function readTextFile(filePath: string): string {
92+
return fs.readFileSync(filePath, 'utf8')
93+
}
94+
8095
async function setupFirewall(): Promise<void> {
8196
return new Promise((resolve, reject) => {
8297
const removeCommand = `

src/renderer/src/pages/profiles.tsx

+24-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Button, Input } from '@nextui-org/react'
22
import BasePage from '@renderer/components/base/base-page'
33
import ProfileItem from '@renderer/components/profiles/profile-item'
44
import { useProfileConfig } from '@renderer/hooks/use-profile-config'
5+
import { getFilePath, readTextFile } from '@renderer/utils/ipc'
56
import { useEffect, useRef, useState } from 'react'
67
import { MdContentPaste } from 'react-icons/md'
78

@@ -40,22 +41,18 @@ const Profiles: React.FC = () => {
4041
e.stopPropagation()
4142
setFileOver(false)
4243
})
43-
pageRef.current?.addEventListener('drop', (event) => {
44+
pageRef.current?.addEventListener('drop', async (event) => {
4445
event.preventDefault()
4546
event.stopPropagation()
4647
if (event.dataTransfer?.files) {
4748
const file = event.dataTransfer.files[0]
4849
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)
5755
}
58-
reader.readAsText(file)
5956
} else {
6057
alert('不支持的文件类型')
6158
}
@@ -74,7 +71,6 @@ const Profiles: React.FC = () => {
7471
<div className="sticky top-[48px] z-40 backdrop-blur bg-background/40 flex p-2">
7572
<Input
7673
variant="bordered"
77-
className="mr-2"
7874
size="sm"
7975
value={url}
8076
onValueChange={setUrl}
@@ -96,12 +92,29 @@ const Profiles: React.FC = () => {
9692
<Button
9793
size="sm"
9894
color="primary"
95+
className="ml-2"
9996
isDisabled={url === ''}
10097
isLoading={importing}
10198
onPress={handleImport}
10299
>
103100
导入
104101
</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>
105118
</div>
106119
<div
107120
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`}

src/renderer/src/utils/ipc.ts

+8
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ export async function encryptString(str: string): Promise<Buffer> {
131131
return await window.electron.ipcRenderer.invoke('encryptString', str)
132132
}
133133

134+
export async function getFilePath(): Promise<string[] | undefined> {
135+
return await window.electron.ipcRenderer.invoke('getFilePath')
136+
}
137+
138+
export async function readTextFile(filePath: string): Promise<string> {
139+
return await window.electron.ipcRenderer.invoke('readTextFile', filePath)
140+
}
141+
134142
export async function checkUpdate(): Promise<string | undefined> {
135143
return await window.electron.ipcRenderer.invoke('checkUpdate')
136144
}

0 commit comments

Comments
 (0)