import { FC, useState } from "react"; import isEmpty from "lodash/isEmpty"; import Link from "next/link"; import { useForm } from "react-hook-form"; // types import { IFormattedInstanceConfiguration, TInstanceGithubAuthenticationConfigurationKeys } 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 GithubConfigFormValues = Record; export const InstanceGithubConfigForm: 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: { GITHUB_CLIENT_ID: config["GITHUB_CLIENT_ID"], GITHUB_CLIENT_SECRET: config["GITHUB_CLIENT_SECRET"], }, }); const originURL = !isEmpty(API_BASE_URL) ? API_BASE_URL : typeof window !== "undefined" ? window.location.origin : ""; const GITHUB_FORM_FIELDS: TControllerInputFormField[] = [ { key: "GITHUB_CLIENT_ID", type: "text", label: "Client ID", description: ( <> You will get this from your{" "} GitHub OAuth application settings. ), placeholder: "70a44354520df8bd9bcd", error: Boolean(errors.GITHUB_CLIENT_ID), required: true, }, { key: "GITHUB_CLIENT_SECRET", type: "password", label: "Client secret", description: ( <> Your client secret is also found in your{" "} GitHub OAuth application settings. ), placeholder: "9b0050f94ec1b744e32ce79ea4ffacd40d4119cb", error: Boolean(errors.GITHUB_CLIENT_SECRET), required: true, }, ]; const GITHUB_SERVICE_FIELD: TCopyField[] = [ { key: "Origin_URL", label: "Origin URL", url: originURL, description: ( <> We will auto-generate this. Paste this into the Authorized origin URL field{" "} here. ), }, { key: "Callback_URI", label: "Callback URI", url: `${originURL}/auth/github/callback/`, description: ( <> We will auto-generate this. Paste this into your Authorized Callback URI field{" "} here. ), }, ]; const onSubmit = async (formData: GithubConfigFormValues) => { const payload: Partial = { ...formData }; await updateInstanceConfigurations(payload) .then((response = []) => { setToast({ type: TOAST_TYPE.SUCCESS, title: "Success", message: "Github Configuration Settings updated successfully", }); reset({ GITHUB_CLIENT_ID: response.find((item) => item.key === "GITHUB_CLIENT_ID")?.value, GITHUB_CLIENT_SECRET: response.find((item) => item.key === "GITHUB_CLIENT_SECRET")?.value, }); }) .catch((err) => console.error(err)); }; const handleGoBack = (e: React.MouseEvent) => { if (isDirty) { e.preventDefault(); setIsDiscardChangesModalOpen(true); } }; return ( <> setIsDiscardChangesModalOpen(false)} />
Configuration
{GITHUB_FORM_FIELDS.map((field) => ( ))}
Go back
Service provider details
{GITHUB_SERVICE_FIELD.map((field) => ( ))}
); };