import { FC, useState } from "react"; import isEmpty from "lodash/isEmpty"; import Link from "next/link"; import { useForm } from "react-hook-form"; // types import { IFormattedInstanceConfiguration, TInstanceGitlabAuthenticationConfigurationKeys } from "@plane/types"; // ui import { Button, TOAST_TYPE, getButtonStyling, setToast } from "@plane/ui"; // components import { ConfirmDiscardModal, ControllerInput, CopyField, TControllerInputFormField, TCopyField, } from "@/components/common"; // helpers import { API_BASE_URL, cn } from "@/helpers/common.helper"; // hooks import { useInstance } from "@/hooks/store"; type Props = { config: IFormattedInstanceConfiguration; }; type GitlabConfigFormValues = Record; export const InstanceGitlabConfigForm: FC = (props) => { const { config } = props; // states const [isDiscardChangesModalOpen, setIsDiscardChangesModalOpen] = useState(false); // store hooks const { updateInstanceConfigurations } = useInstance(); // form data const { handleSubmit, control, reset, formState: { errors, isDirty, isSubmitting }, } = useForm({ defaultValues: { GITLAB_HOST: config["GITLAB_HOST"], GITLAB_CLIENT_ID: config["GITLAB_CLIENT_ID"], GITLAB_CLIENT_SECRET: config["GITLAB_CLIENT_SECRET"], }, }); const originURL = !isEmpty(API_BASE_URL) ? API_BASE_URL : typeof window !== "undefined" ? window.location.origin : ""; const GITLAB_FORM_FIELDS: TControllerInputFormField[] = [ { key: "GITLAB_HOST", type: "text", label: "Host", description: ( <> This is the GitLab host to use for login, including scheme. ), placeholder: "https://gitlab.com", error: Boolean(errors.GITLAB_HOST), required: true, }, { key: "GITLAB_CLIENT_ID", type: "text", label: "Application ID", description: ( <> Get this from your{" "} GitLab OAuth application settings . ), placeholder: "c2ef2e7fc4e9d15aa7630f5637d59e8e4a27ff01dceebdb26b0d267b9adcf3c3", error: Boolean(errors.GITLAB_CLIENT_ID), required: true, }, { key: "GITLAB_CLIENT_SECRET", type: "password", label: "Secret", description: ( <> The client secret is also found in your{" "} GitLab OAuth application settings . ), placeholder: "gloas-f79cfa9a03c97f6ffab303177a5a6778a53c61e3914ba093412f68a9298a1b28", error: Boolean(errors.GITLAB_CLIENT_SECRET), required: true, }, ]; const GITLAB_SERVICE_FIELD: TCopyField[] = [ { key: "Callback_URL", label: "Callback URL", url: `${originURL}/auth/gitlab/callback/`, description: ( <> We will auto-generate this. Paste this into the Redirect URI field of your{" "} GitLab OAuth application . ), }, ]; const onSubmit = async (formData: GitlabConfigFormValues) => { const payload: Partial = { ...formData }; await updateInstanceConfigurations(payload) .then((response = []) => { setToast({ type: TOAST_TYPE.SUCCESS, title: "Success", message: "GitLab Configuration Settings updated successfully", }); reset({ GITLAB_HOST: response.find((item) => item.key === "GITLAB_HOST")?.value, GITLAB_CLIENT_ID: response.find((item) => item.key === "GITLAB_CLIENT_ID")?.value, GITLAB_CLIENT_SECRET: response.find((item) => item.key === "GITLAB_CLIENT_SECRET")?.value, }); }) .catch((err) => console.error(err)); }; const handleGoBack = (e: React.MouseEvent) => { if (isDirty) { e.preventDefault(); setIsDiscardChangesModalOpen(true); } }; return ( <> setIsDiscardChangesModalOpen(false)} />
Configuration
{GITLAB_FORM_FIELDS.map((field) => ( ))}
Go back
Service provider details
{GITLAB_SERVICE_FIELD.map((field) => ( ))}
); };