import * as React from "react"; import { useForm } from "react-hook-form"; import { useTranslation, Trans } from "react-i18next"; import styled from "styled-components"; import { toast } from "sonner"; import { errToString } from "@shared/utils/error"; import { s } from "@shared/styles"; import Button from "~/components/Button"; import Flex from "~/components/Flex"; import Input from "~/components/Input"; import Text from "~/components/Text"; import env from "~/env"; import useStores from "~/hooks/useStores"; import { client } from "~/utils/ApiClient"; import { redirectTo } from "~/utils/urls"; interface FormData { url: string; clientId: string; clientSecret: string; } /** * Dialog that presents the user with options to connect to GitLab Cloud * or a self-managed GitLab instance. */ function GitLabConnectDialog() { const { t } = useTranslation(); const { dialogs } = useStores(); const [showCustomForm, setShowCustomForm] = React.useState( !env.GITLAB_CLIENT_ID ); const { register, handleSubmit } = useForm({ defaultValues: { url: "", clientId: "", clientSecret: "" }, }); const [saving, setSaving] = React.useState(false); const handleConnect = React.useCallback( async (params: { url?: string; clientId?: string; clientSecret?: string; }) => { setSaving(true); try { const res = await client.post("/gitlab.connect", params); dialogs.closeAllModals(); redirectTo(res.data.redirectUrl); } catch (err) { toast.error(errToString(err)); } finally { setSaving(false); } }, [dialogs] ); const handleConnectCloud = React.useCallback(async () => { await handleConnect({}); }, [handleConnect]); const handleConnectCustom = React.useCallback( async (data: FormData) => { await handleConnect({ url: data.url.trim().replace(/\/+$/, ""), clientId: data.clientId.trim() || undefined, clientSecret: data.clientSecret.trim() || undefined, }); }, [handleConnect] ); if (showCustomForm) { return (
Enter the details for your GitLab instance. {env.GITLAB_CLIENT_ID && ( )}
); } return ( Choose which GitLab instance to connect to. ); } const Option = styled.button` display: flex; flex-direction: column; align-items: flex-start; gap: 2px; padding: 12px; border: 1px solid ${s("inputBorder")}; border-radius: 8px; background: ${s("background")}; cursor: pointer; text-align: left; width: 100%; &:hover { background: ${s("listItemHoverBackground")}; border-color: ${s("textTertiary")}; } &:disabled { opacity: 0.5; } `; const OptionTitle = styled.span` font-size: 14px; font-weight: 500; color: ${s("text")}; `; const OptionDescription = styled.span` font-size: 13px; color: ${s("textTertiary")}; `; export default GitLabConnectDialog;