Skip to content

Commit

Permalink
fix profile path
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 authored and xishang0128 committed Nov 26, 2024
1 parent 8db7726 commit c0a009f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 72 deletions.
19 changes: 16 additions & 3 deletions src/main/config/profile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getControledMihomoConfig } from './controledMihomo'
import { mihomoProfileWorkDir, profileConfigPath, profilePath } from '../utils/dirs'
import { mihomoProfileWorkDir, mihomoWorkDir, profileConfigPath, profilePath } from '../utils/dirs'
import { addProfileUpdater } from '../core/profileUpdater'
import { readFile, rm, writeFile } from 'fs/promises'
import { restartCore } from '../core/manager'
Expand All @@ -9,6 +9,7 @@ import axios, { AxiosResponse } from 'axios'
import yaml from 'yaml'
import { defaultProfile } from '../utils/template'
import { subStorePort } from '../resolve/server'
import { join } from 'path'

let profileConfig: IProfileConfig // profile.yaml

Expand Down Expand Up @@ -226,17 +227,29 @@ function isAbsolutePath(path: string): boolean {
}

export async function getFileStr(path: string): Promise<string> {
const { diffWorkDir = false } = await getAppConfig()
const { current } = await getProfileConfig()
if (isAbsolutePath(path)) {
return await readFile(path, 'utf-8')
} else {
return await readFile(mihomoProfileWorkDir(path), 'utf-8')
return await readFile(
join(diffWorkDir ? mihomoProfileWorkDir(current) : mihomoWorkDir(), path),
'utf-8'
)
}
}

export async function setFileStr(path: string, content: string): Promise<void> {
const { diffWorkDir = false } = await getAppConfig()
const { current } = await getProfileConfig()
if (isAbsolutePath(path)) {
await writeFile(path, content, 'utf-8')
} else {
await writeFile(mihomoProfileWorkDir(path), content, 'utf-8')
console.log(join(diffWorkDir ? mihomoProfileWorkDir(current) : mihomoWorkDir(), path))
await writeFile(
join(diffWorkDir ? mihomoProfileWorkDir(current) : mihomoWorkDir(), path),
content,
'utf-8'
)
}
}
64 changes: 33 additions & 31 deletions src/renderer/src/components/resources/proxy-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,32 @@ import { calcTraffic } from '@renderer/utils/calc'
import { getHash } from '@renderer/utils/hash'

const ProxyProvider: React.FC = () => {
const [ShowProvider, setShowProvider] = useState(false)
const [ShowPath, setShowPath] = useState('')
const [ShowType, setShowType] = useState('')

const [showDetails, setShowDetails] = useState({
show: false,
path: '',
type: '',
title: ''
})
useEffect(() => {
const fetchProviderPath = async (name: string): Promise<void> => {
try {
const providers = await getRuntimeConfig()
const provider = providers['proxy-providers'][name]
console.log(provider)
if (provider?.path) {
setShowPath(provider.path)
} else if (provider?.url) {
setShowPath(`proxies/` + getHash(provider.url))
if (showDetails.title) {
const fetchProviderPath = async (name: string): Promise<void> => {
try {
const providers= await getRuntimeConfig()
const provider = providers['proxy-providers'][name]
if (provider) {
setShowDetails((prev) => ({
...prev,
show: true,
path: provider?.path || `proxies/${getHash(provider?.url)}`
}))
}
} catch {
setShowDetails((prev) => ({ ...prev, path: '' }))
}
setShowProvider(true)
} catch (error) {
setShowPath('')
}
fetchProviderPath(showDetails.title)
}

if (ShowPath != '') {
fetchProviderPath(ShowPath)
}
}, [ShowProvider, ShowPath])
}, [showDetails.title])

const { data, mutate } = useSWR('mihomoProxyProviders', mihomoProxyProviders)
const providers = useMemo(() => {
Expand Down Expand Up @@ -79,15 +80,12 @@ const ProxyProvider: React.FC = () => {

return (
<SettingCard>
{ShowProvider && (
{showDetails.show && (
<Viewer
onClose={() => {
setShowProvider(false)
setShowPath('')
setShowType('')
}}
path={ShowPath}
type={ShowType}
path={showDetails.path}
type={showDetails.type}
title={showDetails.title}
onClose={() => setShowDetails({ show: false, path: '', type: '', title: '' })}
/>
)}
<SettingItem title="代理集合" divider>
Expand Down Expand Up @@ -125,8 +123,12 @@ const ProxyProvider: React.FC = () => {
className="ml-2"
size="sm"
onPress={() => {
setShowType(provider.vehicleType)
setShowPath(provider.name)
setShowDetails({
show: false,
path: provider.name,
type: provider.vehicleType,
title: provider.name
})
}}
>
{provider.vehicleType == 'File' ? (
Expand Down
76 changes: 40 additions & 36 deletions src/renderer/src/components/resources/rule-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,33 @@ import { MdEditDocument } from 'react-icons/md'
import dayjs from 'dayjs'

const RuleProvider: React.FC = () => {
const [ShowProvider, setShowProvider] = useState(false)
const [ShowPath, setShowPath] = useState('')
const [ShowType, setShowType] = useState('')
const [ShowFormat, setShowFormat] = useState('')
const [showDetails, setShowDetails] = useState({
show: false,
path: '',
type: '',
title: '',
format: ''
})
useEffect(() => {
if (showDetails.title) {
const fetchProviderPath = async (name: string): Promise<void> => {
try {
const providers= await getRuntimeConfig()
const provider = providers['rule-providers'][name]
if (provider) {
setShowDetails((prev) => ({
...prev,
show: true,
path: provider?.path || `rules/${getHash(provider?.url)}`
}))
}
} catch {
setShowDetails((prev) => ({ ...prev, path: '' }))
}
}
fetchProviderPath(showDetails.title)
}
}, [showDetails.title])

const { data, mutate } = useSWR('mihomoRuleProviders', mihomoRuleProviders)
const providers = useMemo(() => {
Expand All @@ -29,26 +52,6 @@ const RuleProvider: React.FC = () => {
}, [data])
const [updating, setUpdating] = useState(Array(providers.length).fill(false))

useEffect(() => {
const fetchProviderPath = async (name: string): Promise<void> => {
try {
const providers = await getRuntimeConfig()
const provider = providers['rule-providers'][name]
if (provider?.path) {
setShowPath(provider.path)
} else if (provider?.url) {
setShowPath(`rules/` + getHash(provider.url))
}
setShowProvider(true)
} catch (error) {
setShowPath('')
}
}
if (ShowPath != '') {
fetchProviderPath(ShowPath)
}
}, [ShowProvider, ShowPath])

const onUpdate = async (name: string, index: number): Promise<void> => {
setUpdating((prev) => {
prev[index] = true
Expand All @@ -73,16 +76,13 @@ const RuleProvider: React.FC = () => {

return (
<SettingCard>
{ShowProvider && (
{showDetails.show && (
<Viewer
path={ShowPath}
type={ShowType}
format={ShowFormat}
onClose={() => {
setShowProvider(false)
setShowPath('')
setShowType('')
}}
path={showDetails.path}
type={showDetails.type}
title={showDetails.title}
format={showDetails.format}
onClose={() => setShowDetails({ show: false, path: '', type: '', title: '', format: '' })}
/>
)}
<SettingItem title="规则集合" divider>
Expand Down Expand Up @@ -117,9 +117,13 @@ const RuleProvider: React.FC = () => {
className="ml-2"
size="sm"
onPress={() => {
setShowType(provider.vehicleType)
setShowFormat(provider.format)
setShowPath(provider.name)
setShowDetails({
show: false,
path: provider.name,
type: provider.vehicleType,
title: provider.name,
format: provider.format
})
}}
>
{provider.vehicleType == 'File' ? (
Expand Down
5 changes: 3 additions & 2 deletions src/renderer/src/components/resources/viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ interface Props {
onClose: () => void
path: string
type: string
title: string
format?: string
}
const Viewer: React.FC<Props> = (props) => {
const { type, path, format, onClose } = props
const { type, path, title, format, onClose } = props
const [currData, setCurrData] = useState('')
const language: Language = !format || format === 'YamlRule' ? 'yaml' : 'text'

Expand All @@ -34,7 +35,7 @@ const Viewer: React.FC<Props> = (props) => {
scrollBehavior="inside"
>
<ModalContent className="h-full w-[calc(100%-100px)]">
<ModalHeader className="flex pb-0 app-drag">Provider 内容</ModalHeader>
<ModalHeader className="flex pb-0 app-drag">{title}</ModalHeader>
<ModalBody className="h-full">
<BaseEditor
language={language}
Expand Down

0 comments on commit c0a009f

Please sign in to comment.