movie-web/src/pages/onboarding/OnboardingProxy.tsx

79 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-01-16 22:07:21 +01:00
import { useState } from "react";
2024-01-20 11:53:24 +01:00
import { useTranslation } from "react-i18next";
2024-01-16 20:28:33 +01:00
import { useNavigate } from "react-router-dom";
2024-01-16 22:07:21 +01:00
import { useAsyncFn } from "react-use";
2024-01-16 20:28:33 +01:00
2024-01-16 22:07:21 +01:00
import { singularProxiedFetch } from "@/backend/helpers/fetch";
2024-01-16 20:28:33 +01:00
import { Button } from "@/components/buttons/Button";
import { Stepper } from "@/components/layout/Stepper";
import { CenterContainer } from "@/components/layout/ThinContainer";
2024-01-16 22:07:21 +01:00
import { AuthInputBox } from "@/components/text-inputs/AuthInputBox";
import { Divider } from "@/components/utils/Divider";
import { ErrorLine } from "@/components/utils/ErrorLine";
2024-01-16 20:28:33 +01:00
import { Heading2, Paragraph } from "@/components/utils/Text";
import { MinimalPageLayout } from "@/pages/layouts/MinimalPageLayout";
2024-01-16 22:07:21 +01:00
import { useRedirectBack } from "@/pages/onboarding/onboardingHooks";
import { Link } from "@/pages/onboarding/utils";
2024-01-16 20:28:33 +01:00
import { PageTitle } from "@/pages/parts/util/PageTitle";
2024-01-16 22:07:21 +01:00
import { useAuthStore } from "@/stores/auth";
const testUrl = "https://postman-echo.com/get";
2024-01-16 20:28:33 +01:00
export function OnboardingProxyPage() {
2024-01-20 11:53:24 +01:00
const { t } = useTranslation();
2024-01-16 20:28:33 +01:00
const navigate = useNavigate();
2024-01-16 22:07:21 +01:00
const { completeAndRedirect } = useRedirectBack();
const [url, setUrl] = useState("");
const setProxySet = useAuthStore((s) => s.setProxySet);
const [{ loading, error }, test] = useAsyncFn(async () => {
2024-01-20 11:53:24 +01:00
if (!url.startsWith("http"))
throw new Error("onboarding.proxy.input.errorInvalidUrl");
try {
const res = await singularProxiedFetch(url, testUrl, {});
2024-01-20 11:53:24 +01:00
if (res.url !== testUrl)
throw new Error("onboarding.proxy.input.errorNotProxy");
setProxySet([url]);
completeAndRedirect();
} catch (e) {
2024-01-20 11:53:24 +01:00
throw new Error("onboarding.proxy.input.errorConnection");
}
2024-01-16 22:07:21 +01:00
}, [url, completeAndRedirect, setProxySet]);
2024-01-16 20:28:33 +01:00
2024-01-20 13:54:32 +01:00
// TODO proper link to proxy deployment docs
2024-01-16 20:28:33 +01:00
return (
<MinimalPageLayout>
2024-01-20 11:53:24 +01:00
<PageTitle subpage k="global.pages.onboarding" />
2024-01-16 20:28:33 +01:00
<CenterContainer>
<Stepper steps={2} current={2} className="mb-12" />
<Heading2 className="!mt-0 !text-3xl max-w-[435px]">
2024-01-20 11:53:24 +01:00
{t("onboarding.proxy.title")}
</Heading2>
<Paragraph className="max-w-[320px] !mb-5">
2024-01-20 11:53:24 +01:00
{t("onboarding.proxy.explainer")}
</Paragraph>
2024-01-20 11:53:24 +01:00
<Link>{t("onboarding.proxy.link")}</Link>
<div className="w-[400px] max-w-full mt-14 mb-28">
<AuthInputBox
2024-01-20 11:53:24 +01:00
label={t("onboarding.proxy.input.label")}
value={url}
onChange={setUrl}
2024-01-20 11:53:24 +01:00
placeholder={t("onboarding.proxy.input.placeholder")}
className="mb-4"
/>
2024-01-20 11:53:24 +01:00
{error ? <ErrorLine>{t(error.message)}</ErrorLine> : null}
</div>
<Divider />
<div className="flex justify-between">
<Button theme="secondary" onClick={() => navigate("/onboarding")}>
2024-01-20 11:53:24 +01:00
{t("onboarding.proxy.back")}
</Button>
<Button theme="purple" loading={loading} onClick={test}>
2024-01-20 11:53:24 +01:00
{t("onboarding.proxy.submit")}
</Button>
</div>
2024-01-16 20:28:33 +01:00
</CenterContainer>
</MinimalPageLayout>
);
}