wangzhibo
2023-10-31 9da39812105bfc387db3b3b7c4896983e03d247d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { createForm } from '@felte/solid'
import { validator } from '@felte/validator-zod'
import { useLocation, 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 { useI18n } from '~/i18n'
import {
  endpointList,
  selectedEndpoint,
  setEndpointList,
  setSelectedEndpoint,
} from '~/signals'
 
const schema = z.object({
  url: z.string().min(1),
  secret: z.string(),
})
 
export default () => {
  const [t] = useI18n()
  const location = useLocation()
  const navigate = useNavigate()
 
  const onSetupSuccess = (id: string) => {
    setSelectedEndpoint(id)
    navigate('/overview')
  }
 
  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
    }
 
    onSetupSuccess(id)
  }
 
  const onSubmit = async ({ url, secret }: { url: string; secret: string }) => {
    const transformedURL = transformEndpointURL(url)
 
    if (!(await checkEndpoint(transformedURL, secret))) {
      return
    }
 
    const id = uuid()
    const list = endpointList().slice()
    const point = list.find((history) => history.url === transformedURL)
 
    if (!point) {
      // new host and secret
      setEndpointList([{ id, url: transformedURL, secret }, ...list])
      onSetupSuccess(id)
 
      return
    }
 
    // exist host we update secret and id no matter if secret is equal or not
    point.secret = secret
    point.id = id
 
    setEndpointList(list)
    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) => {
    if (selectedEndpoint() === id) {
      setSelectedEndpoint('')
    }
 
    setEndpointList(endpointList().filter((e) => e.id !== id))
  }
 
  onMount(() => {
    const query = new URLSearchParams(location.search)
 
    if (query.has('hostname')) {
      void onSubmit({
        url: `${window.location.protocol}//${query.get('hostname')}${
          query.get('port') ? `:${query.get('port')}` : ''
        }`,
        secret: query.get('secret') ?? '',
      })
    } else if (endpointList().length === 0) {
      /**
        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({
        url: 'http://127.0.0.1:9090',
        secret: '',
      })
    }
  })
 
  return (
    <div class="mx-auto flex max-w-screen-sm flex-col items-center gap-4 py-10">
      <form class="contents" use:form={form}>
        <div class="flex w-full flex-col gap-4">
          <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 uppercase">
            {t('add')}
          </Button>
        </div>
      </form>
 
      <div class="grid w-full grid-cols-2 gap-4">
        <For each={endpointList()}>
          {({ id, url }) => (
            <div
              class="badge badge-info flex w-full cursor-pointer items-center justify-between gap-4 py-4"
              onClick={() => onEndpointSelect(id)}
            >
              <span class="truncate">{url}</span>
 
              <Button
                class="btn-circle btn-ghost btn-xs text-white"
                onClick={(e) => {
                  e.stopPropagation()
                  onRemove(id)
                }}
              >
                <IconX />
              </Button>
            </div>
          )}
        </For>
      </div>
    </div>
  )
}