refactor: a little better update proxies

This commit is contained in:
Zephyruso 2023-09-02 20:37:08 +08:00
parent 467d0bcce2
commit 06845963c6
3 changed files with 21 additions and 35 deletions

View File

@ -13,23 +13,11 @@ import {
IconRuler,
IconSettings,
} from '@tabler/icons-solidjs'
import {
For,
ParentComponent,
Show,
createEffect,
createMemo,
createSignal,
} from 'solid-js'
import { For, ParentComponent, Show, createMemo, createSignal } from 'solid-js'
import { twMerge } from 'tailwind-merge'
import { LANG, ROUTE } from '~/config/enum'
import { themes } from '~/constants'
import {
endpoint,
selectedEndpoint,
setCurTheme,
setSelectedEndpoint,
} from '~/signals'
import { setCurTheme, setSelectedEndpoint } from '~/signals'
import { useProxies } from '~/signals/proxies'
const Nav: ParentComponent<{ href: string; tooltip: string }> = ({
@ -80,14 +68,6 @@ const ThemeSwitcher = () => (
export const Header = () => {
const [t, { locale }] = useI18n()
const { proxyProviders } = useProxies()
createEffect(() => {
// Need fix: useRequest is not reactive so we need to useProxies again or request wont have endpoint
if (selectedEndpoint() && endpoint()) {
useProxies().updateProxy()
}
})
const navs = createMemo(() => {
const list = [
{

View File

@ -3,6 +3,17 @@ import ky from 'ky'
import { createSignal } from 'solid-js'
import { themes } from '~/constants'
export const useRequest = () => {
const e = endpoint()
return ky.create({
prefixUrl: e?.url,
headers: {
Authorization: e?.secret ? `Bearer ${e.secret}` : '',
},
})
}
export const [selectedEndpoint, setSelectedEndpoint] = makePersisted(
createSignal(''),
{
@ -33,14 +44,3 @@ export const endpoint = () =>
export const secret = () => endpoint()?.secret
export const wsEndpointURL = () => endpoint()?.url.replace('http', 'ws')
export const useRequest = () => {
const e = endpoint()
return ky.create({
prefixUrl: e?.url,
headers: {
Authorization: e?.secret ? `Bearer ${e.secret}` : '',
},
})
}

View File

@ -1,5 +1,5 @@
import { createSignal } from 'solid-js'
import { useRequest } from '~/signals'
import { createEffect, createSignal } from 'solid-js'
import { endpoint, selectedEndpoint, useRequest } from '~/signals'
import { autoCloseConns, urlForDelayTest } from '~/signals/config'
import type { Proxy, ProxyNode, ProxyProvider } from '~/types'
@ -140,3 +140,9 @@ export function useProxies() {
healthCheckByProviderName,
}
}
createEffect(() => {
if (selectedEndpoint() && endpoint()) {
useProxies().updateProxy()
}
})