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, IconRuler,
IconSettings, IconSettings,
} from '@tabler/icons-solidjs' } from '@tabler/icons-solidjs'
import { import { For, ParentComponent, Show, createMemo, createSignal } from 'solid-js'
For,
ParentComponent,
Show,
createEffect,
createMemo,
createSignal,
} from 'solid-js'
import { twMerge } from 'tailwind-merge' import { twMerge } from 'tailwind-merge'
import { LANG, ROUTE } from '~/config/enum' import { LANG, ROUTE } from '~/config/enum'
import { themes } from '~/constants' import { themes } from '~/constants'
import { import { setCurTheme, setSelectedEndpoint } from '~/signals'
endpoint,
selectedEndpoint,
setCurTheme,
setSelectedEndpoint,
} from '~/signals'
import { useProxies } from '~/signals/proxies' import { useProxies } from '~/signals/proxies'
const Nav: ParentComponent<{ href: string; tooltip: string }> = ({ const Nav: ParentComponent<{ href: string; tooltip: string }> = ({
@ -80,14 +68,6 @@ const ThemeSwitcher = () => (
export const Header = () => { export const Header = () => {
const [t, { locale }] = useI18n() const [t, { locale }] = useI18n()
const { proxyProviders } = useProxies() 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 navs = createMemo(() => {
const list = [ const list = [
{ {

View File

@ -3,6 +3,17 @@ import ky from 'ky'
import { createSignal } from 'solid-js' import { createSignal } from 'solid-js'
import { themes } from '~/constants' 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( export const [selectedEndpoint, setSelectedEndpoint] = makePersisted(
createSignal(''), createSignal(''),
{ {
@ -33,14 +44,3 @@ export const endpoint = () =>
export const secret = () => endpoint()?.secret export const secret = () => endpoint()?.secret
export const wsEndpointURL = () => endpoint()?.url.replace('http', 'ws') 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 { createEffect, createSignal } from 'solid-js'
import { useRequest } from '~/signals' import { endpoint, selectedEndpoint, useRequest } from '~/signals'
import { autoCloseConns, urlForDelayTest } from '~/signals/config' import { autoCloseConns, urlForDelayTest } from '~/signals/config'
import type { Proxy, ProxyNode, ProxyProvider } from '~/types' import type { Proxy, ProxyNode, ProxyProvider } from '~/types'
@ -140,3 +140,9 @@ export function useProxies() {
healthCheckByProviderName, healthCheckByProviderName,
} }
} }
createEffect(() => {
if (selectedEndpoint() && endpoint()) {
useProxies().updateProxy()
}
})