refactor: extract search params business

This commit is contained in:
kunish 2024-10-10 21:15:17 +08:00
parent 99e4f48106
commit 7c310e7c66
No known key found for this signature in database
GPG Key ID: 647A12B4F782C430
3 changed files with 29 additions and 36 deletions

View File

@ -11,6 +11,22 @@ import {
RuleProvider, RuleProvider,
} from '~/types' } from '~/types'
export const checkEndpointAPI = (url: string, secret: string) =>
ky
.get(url, {
headers: secret
? {
Authorization: `Bearer ${secret}`,
}
: {},
})
.then(({ ok }) => ok)
.catch((err) => {
const { message } = err as Error
toast.error(message)
})
export const closeAllConnectionsAPI = () => { export const closeAllConnectionsAPI = () => {
const request = useRequest() const request = useRequest()
@ -239,9 +255,7 @@ export const isUpdateAvailableAPI = async (versionResponse: string) => {
const repositoryURL = 'https://api.github.com/repos/MetaCubeX/mihomo' const repositoryURL = 'https://api.github.com/repos/MetaCubeX/mihomo'
const match = /(alpha|beta|meta)-?(\w+)/.exec(versionResponse) const match = /(alpha|beta|meta)-?(\w+)/.exec(versionResponse)
if (!match) { if (!match) return false
return false
}
const channel = match[1], const channel = match[1],
version = match[2] version = match[2]

View File

@ -1,10 +1,10 @@
import { createForm } from '@felte/solid' import { createForm } from '@felte/solid'
import { validator } from '@felte/validator-zod' import { validator } from '@felte/validator-zod'
import { IconX } from '@tabler/icons-solidjs' import { IconX } from '@tabler/icons-solidjs'
import ky from 'ky'
import { toast } from 'solid-toast' import { toast } from 'solid-toast'
import { v4 as uuid } from 'uuid' import { v4 as uuid } from 'uuid'
import { z } from 'zod' import { z } from 'zod'
import { checkEndpointAPI } from '~/apis'
import { Button } from '~/components' import { Button } from '~/components'
import DocumentTitle from '~/components/DocumentTitle' import DocumentTitle from '~/components/DocumentTitle'
import { transformEndpointURL } from '~/helpers' import { transformEndpointURL } from '~/helpers'
@ -31,28 +31,12 @@ export default () => {
navigate('/overview', { replace: true }) navigate('/overview', { replace: true })
} }
const checkEndpoint = (url: string, secret: string) =>
ky
.get(url, {
headers: secret
? {
Authorization: `Bearer ${secret}`,
}
: {},
})
.then(({ ok }) => ok)
.catch((err) => {
const { message } = err as Error
toast.error(message)
})
const onEndpointSelect = async (id: string) => { const onEndpointSelect = async (id: string) => {
const endpoint = endpointList().find((e) => e.id === id) const endpoint = endpointList().find((e) => e.id === id)
if (!endpoint) return if (!endpoint) return
if (!(await checkEndpoint(endpoint.url, endpoint.secret))) return if (!(await checkEndpointAPI(endpoint.url, endpoint.secret))) return
onSetupSuccess(id) onSetupSuccess(id)
} }
@ -60,7 +44,7 @@ export default () => {
const onSubmit = async ({ url, secret }: { url: string; secret: string }) => { const onSubmit = async ({ url, secret }: { url: string; secret: string }) => {
const transformedURL = transformEndpointURL(url) const transformedURL = transformEndpointURL(url)
if (!(await checkEndpoint(transformedURL, secret))) return if (!(await checkEndpointAPI(transformedURL, secret))) return
const id = uuid() const id = uuid()
const list = endpointList().slice() const list = endpointList().slice()
@ -102,21 +86,18 @@ export default () => {
setEndpointList(endpointList().filter((e) => e.id !== id)) setEndpointList(endpointList().filter((e) => e.id !== id))
} }
onMount(() => { onMount(async () => {
let search = location.search || window.location.search const search =
location.search ||
window.location.search ||
location.hash.match(/\?.*$/)?.[0]?.replace('?', '')
if (search) { if (!search) return
const searchList = location.hash.match(/\?.*$/)
if (Array.isArray(searchList) && searchList[0]) {
search = searchList[0].replace('?', '')
}
}
const query = new URLSearchParams(search) const query = new URLSearchParams(search)
if (query.has('hostname')) { if (query.has('hostname')) {
void onSubmit({ await onSubmit({
url: `${query.get('http') ? 'http:' : query.get('https') ? 'https:' : window.location.protocol}//${query.get('hostname')}${ url: `${query.get('http') ? 'http:' : query.get('https') ? 'https:' : window.location.protocol}//${query.get('hostname')}${
query.get('port') ? `:${query.get('port')}` : '' query.get('port') ? `:${query.get('port')}` : ''
}`, }`,
@ -127,7 +108,7 @@ export default () => {
we only try auto login when there is nothing in endpoint list we only try auto login when there is nothing in endpoint list
or user who is using default config won't be able to switch to another endpoint ever or user who is using default config won't be able to switch to another endpoint ever
*/ */
void onSubmit({ await onSubmit({
url: 'http://127.0.0.1:9090', url: 'http://127.0.0.1:9090',
secret: '', secret: '',
}) })

View File

@ -17,9 +17,7 @@ export const useLogs = () => {
createEffect(() => { createEffect(() => {
const data = logsData() const data = logsData()
if (!data || paused()) { if (!data || paused()) return
return
}
setLogs((logs) => [{ ...data, seq }, ...logs].slice(0, logMaxRows())) setLogs((logs) => [{ ...data, seq }, ...logs].slice(0, logMaxRows()))
seq++ seq++