From 7c310e7c6651412d6ba2b37991d3d8b492c975e8 Mon Sep 17 00:00:00 2001 From: kunish Date: Thu, 10 Oct 2024 21:15:17 +0800 Subject: [PATCH] refactor: extract search params business --- src/apis/index.ts | 20 +++++++++++++++++--- src/pages/Setup.tsx | 41 +++++++++++------------------------------ src/signals/logs.ts | 4 +--- 3 files changed, 29 insertions(+), 36 deletions(-) diff --git a/src/apis/index.ts b/src/apis/index.ts index 22ac48e..41ed000 100644 --- a/src/apis/index.ts +++ b/src/apis/index.ts @@ -11,6 +11,22 @@ import { RuleProvider, } 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 = () => { const request = useRequest() @@ -239,9 +255,7 @@ export const isUpdateAvailableAPI = async (versionResponse: string) => { const repositoryURL = 'https://api.github.com/repos/MetaCubeX/mihomo' const match = /(alpha|beta|meta)-?(\w+)/.exec(versionResponse) - if (!match) { - return false - } + if (!match) return false const channel = match[1], version = match[2] diff --git a/src/pages/Setup.tsx b/src/pages/Setup.tsx index 044d367..4cc073d 100644 --- a/src/pages/Setup.tsx +++ b/src/pages/Setup.tsx @@ -1,10 +1,10 @@ import { createForm } from '@felte/solid' import { validator } from '@felte/validator-zod' import { IconX } from '@tabler/icons-solidjs' -import ky from 'ky' import { toast } from 'solid-toast' import { v4 as uuid } from 'uuid' import { z } from 'zod' +import { checkEndpointAPI } from '~/apis' import { Button } from '~/components' import DocumentTitle from '~/components/DocumentTitle' import { transformEndpointURL } from '~/helpers' @@ -31,28 +31,12 @@ export default () => { 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 endpoint = endpointList().find((e) => e.id === id) if (!endpoint) return - if (!(await checkEndpoint(endpoint.url, endpoint.secret))) return + if (!(await checkEndpointAPI(endpoint.url, endpoint.secret))) return onSetupSuccess(id) } @@ -60,7 +44,7 @@ export default () => { const onSubmit = async ({ url, secret }: { url: string; secret: string }) => { const transformedURL = transformEndpointURL(url) - if (!(await checkEndpoint(transformedURL, secret))) return + if (!(await checkEndpointAPI(transformedURL, secret))) return const id = uuid() const list = endpointList().slice() @@ -102,21 +86,18 @@ export default () => { setEndpointList(endpointList().filter((e) => e.id !== id)) } - onMount(() => { - let search = location.search || window.location.search + onMount(async () => { + const search = + location.search || + window.location.search || + location.hash.match(/\?.*$/)?.[0]?.replace('?', '') - if (search) { - const searchList = location.hash.match(/\?.*$/) - - if (Array.isArray(searchList) && searchList[0]) { - search = searchList[0].replace('?', '') - } - } + if (!search) return const query = new URLSearchParams(search) if (query.has('hostname')) { - void onSubmit({ + await onSubmit({ url: `${query.get('http') ? 'http:' : query.get('https') ? 'https:' : window.location.protocol}//${query.get('hostname')}${ query.get('port') ? `:${query.get('port')}` : '' }`, @@ -127,7 +108,7 @@ export default () => { 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 */ - void onSubmit({ + await onSubmit({ url: 'http://127.0.0.1:9090', secret: '', }) diff --git a/src/signals/logs.ts b/src/signals/logs.ts index e8c5e0c..a57cb7e 100644 --- a/src/signals/logs.ts +++ b/src/signals/logs.ts @@ -17,9 +17,7 @@ export const useLogs = () => { createEffect(() => { const data = logsData() - if (!data || paused()) { - return - } + if (!data || paused()) return setLogs((logs) => [{ ...data, seq }, ...logs].slice(0, logMaxRows())) seq++