diff --git a/src/pages/Setup.tsx b/src/pages/Setup.tsx index 2319dc6..3c39865 100644 --- a/src/pages/Setup.tsx +++ b/src/pages/Setup.tsx @@ -16,9 +16,19 @@ const schema = z.object({ export default () => { const navigate = useNavigate() - const { form } = createForm>({ - extend: validator({ schema }), - async onSubmit({ url, secret }) { + const onSetupSuccess = (id: string) => { + setSelectedEndpoint(id) + navigate('/overview') + } + + const checkEndpoint = async ({ + url, + secret, + }: { + url: string + secret: string + }) => { + try { const { ok } = await ky.get(url, { headers: secret ? { @@ -27,12 +37,48 @@ export default () => { : {}, }) - if (!ok) return 1 + return ok + } catch { + return false + } + } + + const onEndpointSelect = async (id: string) => { + const endpoint = endpointList().find((e) => e.id === id) + + if (!endpoint) { + return + } + + if ( + !(await checkEndpoint({ url: endpoint.url, secret: endpoint.secret })) + ) { + return + } + + onSetupSuccess(id) + } + + const { form } = createForm>({ + extend: validator({ schema }), + async onSubmit({ url, secret }) { + const endpointFromHistory = endpointList().find( + (history) => history.url === url && history.secret === secret, + ) + + if (endpointFromHistory) { + onSetupSuccess(endpointFromHistory.id) + + return + } + + if (!(await checkEndpoint({ url, secret }))) { + return + } const id = uuid() setEndpointList([{ id, url, secret }, ...endpointList()]) - setSelectedEndpoint(id) - navigate('/overview') + onSetupSuccess(id) }, }) @@ -74,10 +120,7 @@ export default () => { {({ id, url }) => (
{ - setSelectedEndpoint(id) - navigate('/overview') - }} + onClick={() => onEndpointSelect(id)} > {url}