Skip to content

Commit

Permalink
0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Aug 7, 2024
1 parent fd86c0d commit 8093aa9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mihomo-party",
"version": "0.1.6",
"version": "0.2.0",
"description": "Mihomo Party",
"main": "./out/main/index.js",
"author": "mihomo-party",
Expand Down
15 changes: 15 additions & 0 deletions src/main/utils/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { triggerSysProxy } from '../resolve/sysproxy'
import { checkUpdate } from '../resolve/autoUpdater'
import { exePath, mihomoCorePath } from './dirs'
import { execSync } from 'child_process'
import fs from 'fs'

export function registerIpcMainHandlers(): void {
ipcMain.handle('mihomoVersion', mihomoVersion)
Expand Down Expand Up @@ -70,13 +71,27 @@ export function registerIpcMainHandlers(): void {
ipcMain.handle('triggerSysProxy', (_e, enable) => triggerSysProxy(enable))
ipcMain.handle('isEncryptionAvailable', isEncryptionAvailable)
ipcMain.handle('encryptString', (_e, str) => safeStorage.encryptString(str))
ipcMain.handle('getFilePath', getFilePath)
ipcMain.handle('readTextFile', (_e, filePath) => readTextFile(filePath))
ipcMain.handle('checkUpdate', () => checkUpdate())
ipcMain.handle('getVersion', () => app.getVersion())
ipcMain.handle('platform', () => process.platform)
ipcMain.handle('setupFirewall', setupFirewall)
ipcMain.handle('quitApp', () => app.quit())
}

function getFilePath(): string[] | undefined {
return dialog.showOpenDialogSync({
title: '选择订阅文件',
filters: [{ name: 'Yaml Files', extensions: ['yml', 'yaml'] }],
properties: ['openFile']
})
}

function readTextFile(filePath: string): string {
return fs.readFileSync(filePath, 'utf8')
}

async function setupFirewall(): Promise<void> {
return new Promise((resolve, reject) => {
const removeCommand = `
Expand Down
35 changes: 24 additions & 11 deletions src/renderer/src/pages/profiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Button, Input } from '@nextui-org/react'
import BasePage from '@renderer/components/base/base-page'
import ProfileItem from '@renderer/components/profiles/profile-item'
import { useProfileConfig } from '@renderer/hooks/use-profile-config'
import { getFilePath, readTextFile } from '@renderer/utils/ipc'
import { useEffect, useRef, useState } from 'react'
import { MdContentPaste } from 'react-icons/md'

Expand Down Expand Up @@ -40,22 +41,18 @@ const Profiles: React.FC = () => {
e.stopPropagation()
setFileOver(false)
})
pageRef.current?.addEventListener('drop', (event) => {
pageRef.current?.addEventListener('drop', async (event) => {
event.preventDefault()
event.stopPropagation()
if (event.dataTransfer?.files) {
const file = event.dataTransfer.files[0]
if (file.name.endsWith('.yml') || file.name.endsWith('.yaml')) {
const reader = new FileReader()
reader.onload = async (e): Promise<void> => {
const content = e.target?.result as string
try {
await addProfileItem({ name: file.name, type: 'local', file: content })
} finally {
setFileOver(false)
}
const content = await readTextFile(file.path)
try {
await addProfileItem({ name: file.name, type: 'local', file: content })
} finally {
setFileOver(false)
}
reader.readAsText(file)
} else {
alert('不支持的文件类型')
}
Expand All @@ -74,7 +71,6 @@ const Profiles: React.FC = () => {
<div className="sticky top-[48px] z-40 backdrop-blur bg-background/40 flex p-2">
<Input
variant="bordered"
className="mr-2"
size="sm"
value={url}
onValueChange={setUrl}
Expand All @@ -96,12 +92,29 @@ const Profiles: React.FC = () => {
<Button
size="sm"
color="primary"
className="ml-2"
isDisabled={url === ''}
isLoading={importing}
onPress={handleImport}
>
导入
</Button>
<Button
size="sm"
color="primary"
className="ml-2"
onPress={() => {
getFilePath().then(async (files) => {
if (files?.length) {
const content = await readTextFile(files[0])
const fileName = files[0].split('/').pop()?.split('\\').pop()
await addProfileItem({ name: fileName, type: 'local', file: content })
}
})
}}
>
打开
</Button>
</div>
<div
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`}
Expand Down
8 changes: 8 additions & 0 deletions src/renderer/src/utils/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ export async function encryptString(str: string): Promise<Buffer> {
return await window.electron.ipcRenderer.invoke('encryptString', str)
}

export async function getFilePath(): Promise<string[] | undefined> {
return await window.electron.ipcRenderer.invoke('getFilePath')
}

export async function readTextFile(filePath: string): Promise<string> {
return await window.electron.ipcRenderer.invoke('readTextFile', filePath)
}

export async function checkUpdate(): Promise<string | undefined> {
return await window.electron.ipcRenderer.invoke('checkUpdate')
}
Expand Down

0 comments on commit 8093aa9

Please sign in to comment.