Skip to content

Commit

Permalink
feat(setup): prefix endpoint url with current url protocol if missing
Browse files Browse the repository at this point in the history
  • Loading branch information
kunish committed Sep 9, 2023
1 parent 730ab86 commit e935aed
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 33 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"solid-apexcharts": "^0.3.2",
"solid-infinite-scroll": "^1.0.1",
"solid-js": "^1.7.11",
"solid-toast": "^0.5.0",
"sort-package-json": "^2.5.1",
"tailwind-merge": "^1.14.0",
"tailwindcss": "^3.3.3",
Expand Down
25 changes: 16 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Navigate, Route, Routes, useNavigate } from '@solidjs/router'
import { Show, createEffect, lazy, onMount } from 'solid-js'
import { Toaster } from 'solid-toast'
import { twMerge } from 'tailwind-merge'
import { Header } from '~/components'
import { ROUTES } from '~/constants'
Expand Down Expand Up @@ -71,6 +72,8 @@ export const App = () => {
/>
</Routes>
</div>

<Toaster position="bottom-center" />
</div>
)
}
3 changes: 3 additions & 0 deletions src/helpers/global.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { createSignal } from 'solid-js'

export const transformEndpointURL = (url: string) =>
/^https?/.test(url) ? url : `${window.location.protocol}//${url}`

export const useStringBooleanMap = () => {
const [map, setMap] = createSignal<Record<string, boolean>>({})
const set = (name: string, value: boolean) => {
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ export default {
sequence: 'Sequence',
payload: 'Payload',
details: 'Details',
endpointURL: 'Endpoint URL',
secret: 'Secret',
}
2 changes: 2 additions & 0 deletions src/i18n/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ export default {
sequence: '序列号',
payload: '内容',
details: '详情',
endpointURL: '后端地址',
secret: '密钥',
}
75 changes: 51 additions & 24 deletions src/pages/Setup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { useNavigate } from '@solidjs/router'
import { IconX } from '@tabler/icons-solidjs'
import ky from 'ky'
import { For, onMount } from 'solid-js'
import { toast } from 'solid-toast'
import { v4 as uuid } from 'uuid'
import { z } from 'zod'
import { Button } from '~/components'
import { transformEndpointURL } from '~/helpers'
import {
endpointList,
selectedEndpoint,
Expand All @@ -16,7 +18,7 @@ import {
} from '~/signals'

const schema = z.object({
url: z.string().url().nonempty(),
url: z.string().nonempty(),
secret: z.string(),
})

Expand All @@ -39,7 +41,11 @@ export default () => {
: {},
})
.then(({ ok }) => ok)
.catch(() => false)
.catch((err) => {
const { message } = err as Error

toast.error(message)
})

const onEndpointSelect = async (id: string) => {
const endpoint = endpointList().find((e) => e.id === id)
Expand All @@ -56,17 +62,19 @@ export default () => {
}

const onSubmit = async ({ url, secret }: { url: string; secret: string }) => {
if (!(await checkEndpoint(url, secret))) {
const transformedURL = transformEndpointURL(url)

if (!(await checkEndpoint(transformedURL, secret))) {
return
}

const id = uuid()
const list = endpointList().slice()
const point = list.find((history) => history.url === url)
const point = list.find((history) => history.url === transformedURL)

if (!point) {
// new host and secret
setEndpointList([{ id, url, secret }, ...list])
setEndpointList([{ id, url: transformedURL, secret }, ...list])
onSetupSuccess(id)

return
Expand All @@ -80,9 +88,16 @@ export default () => {
onSetupSuccess(id)
}

const onError = (err: unknown) => {
const { message } = err as Error

toast.error(message)
}

const { form } = createForm<z.infer<typeof schema>>({
extend: validator({ schema }),
onSubmit,
onError,
})

const onRemove = (id: string) => {
Expand Down Expand Up @@ -116,27 +131,39 @@ export default () => {
})

return (
<div class="mx-auto flex flex-col items-center gap-4 py-10 sm:w-2/3">
<div class="mx-auto flex max-w-screen-md flex-col items-center gap-4 py-10">
<form class="contents" use:form={form}>
<div class="flex w-full flex-col gap-4">
<input
name="url"
type="url"
class="input input-bordered"
placeholder="host url"
list="defaultEndpoints"
/>

<datalist id="defaultEndpoints">
<option value="http://127.0.0.1:9090" />
</datalist>

<input
name="secret"
type="password"
class="input input-bordered"
placeholder="secret"
/>
<div class="flex-1">
<label class="label">
<span class="label-text">{t('endpointURL')}</span>
</label>

<input
name="url"
type="url"
class="input input-bordered w-full"
placeholder="http://127.0.0.1:9090"
list="defaultEndpoints"
/>

<datalist id="defaultEndpoints">
<option value="http://127.0.0.1:9090" />
</datalist>
</div>

<div class="flex-1">
<label class="label">
<span class="label-text">{t('secret')}</span>
</label>

<input
name="secret"
type="password"
class="input input-bordered w-full"
placeholder="secret"
/>
</div>

<Button type="submit" class="btn-primary join-item uppercase">
{t('add')}
Expand Down

0 comments on commit e935aed

Please sign in to comment.