mirror of
https://github.com/outline/outline.git
synced 2026-12-06 11:12:32 +03:00
* chore: Use react-hook-form in group, API key, Gitea and GitLab forms * Rely on native validation in Gitea and GitLab connect forms * Sync Input character count for uncontrolled fields on mount
102 lines
3.0 KiB
TypeScript
102 lines
3.0 KiB
TypeScript
import * as React from "react";
|
|
import { useForm } from "react-hook-form";
|
|
import { useTranslation, Trans } from "react-i18next";
|
|
import { toast } from "sonner";
|
|
import { errToString } from "@shared/utils/error";
|
|
import Button from "~/components/Button";
|
|
import Flex from "~/components/Flex";
|
|
import Input from "~/components/Input";
|
|
import Text from "~/components/Text";
|
|
import useStores from "~/hooks/useStores";
|
|
import { client } from "~/utils/ApiClient";
|
|
import { redirectTo } from "~/utils/urls";
|
|
import { GiteaUtils } from "../../shared/GiteaUtils";
|
|
|
|
interface FormData {
|
|
url: string;
|
|
clientId: string;
|
|
clientSecret: string;
|
|
}
|
|
|
|
/**
|
|
* Dialog that collects the details of a Gitea instance and its OAuth
|
|
* application, then starts the OAuth flow.
|
|
*/
|
|
export function GiteaConnectDialog() {
|
|
const { t } = useTranslation();
|
|
const { dialogs } = useStores();
|
|
const { register, handleSubmit, formState } = useForm<FormData>({
|
|
defaultValues: { url: "", clientId: "", clientSecret: "" },
|
|
});
|
|
const redirectUri = GiteaUtils.callbackUrl();
|
|
|
|
const handleFormSubmit = React.useCallback(
|
|
async (data: FormData) => {
|
|
try {
|
|
const res = await client.post("/gitea.connect", {
|
|
url: data.url.trim().replace(/\/+$/, ""),
|
|
clientId: data.clientId.trim(),
|
|
clientSecret: data.clientSecret.trim(),
|
|
});
|
|
dialogs.closeAllModals();
|
|
redirectTo(res.data.redirectUrl);
|
|
} catch (err) {
|
|
toast.error(errToString(err));
|
|
}
|
|
},
|
|
[dialogs]
|
|
);
|
|
|
|
const handleFocusRedirectUri = React.useCallback(
|
|
(ev: React.FocusEvent<HTMLInputElement>) => ev.currentTarget.select(),
|
|
[]
|
|
);
|
|
|
|
return (
|
|
<form onSubmit={handleSubmit(handleFormSubmit)}>
|
|
<Flex column gap={12}>
|
|
<Text as="p" type="secondary">
|
|
<Trans>
|
|
Create an OAuth2 application in the settings of your Gitea or
|
|
Forgejo instance with the redirect URI below, then enter its
|
|
details.
|
|
</Trans>
|
|
</Text>
|
|
<Input
|
|
label={t("Redirect URI")}
|
|
value={redirectUri}
|
|
onFocus={handleFocusRedirectUri}
|
|
readOnly
|
|
/>
|
|
<Input
|
|
label={t("Gitea URL")}
|
|
placeholder="https://gitea.example.com"
|
|
{...register("url")}
|
|
pattern="https://.*"
|
|
title={t("URL must start with https")}
|
|
required
|
|
autoFocus
|
|
/>
|
|
<Input
|
|
label={t("Client ID")}
|
|
placeholder={t("OAuth application ID")}
|
|
{...register("clientId")}
|
|
required
|
|
/>
|
|
<Input
|
|
label={t("Client secret")}
|
|
placeholder={t("OAuth application secret")}
|
|
{...register("clientSecret")}
|
|
type="password"
|
|
required
|
|
/>
|
|
<Flex justify="flex-end">
|
|
<Button type="submit" disabled={formState.isSubmitting}>
|
|
{formState.isSubmitting ? `${t("Connecting")}…` : t("Connect")}
|
|
</Button>
|
|
</Flex>
|
|
</Flex>
|
|
</form>
|
|
);
|
|
}
|