2023-04-18 05:24:45 +00:00
|
|
|
import { useState } from "react";
|
2023-02-23 12:42:07 +00:00
|
|
|
|
|
|
|
import Image from "next/image";
|
2023-02-22 14:10:57 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
2023-03-03 09:00:21 +00:00
|
|
|
import useSWR, { mutate } from "swr";
|
|
|
|
|
2023-02-23 12:42:07 +00:00
|
|
|
// services
|
2023-04-05 19:21:15 +00:00
|
|
|
import IntegrationService from "services/integration";
|
2023-02-23 12:42:07 +00:00
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
2023-04-18 05:24:45 +00:00
|
|
|
import useIntegrationPopup from "hooks/use-integration-popup";
|
2023-02-23 12:42:07 +00:00
|
|
|
// ui
|
2023-04-22 16:24:50 +00:00
|
|
|
import { DangerButton, Loader, PrimaryButton } from "components/ui";
|
2023-02-23 12:42:07 +00:00
|
|
|
// icons
|
2023-04-18 05:24:45 +00:00
|
|
|
import GithubLogo from "public/services/github.png";
|
|
|
|
import SlackLogo from "public/services/slack.png";
|
2023-03-03 09:00:21 +00:00
|
|
|
// types
|
2023-04-18 05:24:45 +00:00
|
|
|
import { IAppIntegration, IWorkspaceIntegration } from "types";
|
2023-03-03 09:00:21 +00:00
|
|
|
// fetch-keys
|
|
|
|
import { WORKSPACE_INTEGRATIONS } from "constants/fetch-keys";
|
2023-02-23 12:42:07 +00:00
|
|
|
|
2023-04-18 05:24:45 +00:00
|
|
|
type Props = {
|
|
|
|
integration: IAppIntegration;
|
|
|
|
};
|
|
|
|
|
|
|
|
const integrationDetails: { [key: string]: any } = {
|
|
|
|
github: {
|
|
|
|
logo: GithubLogo,
|
|
|
|
installed:
|
|
|
|
"Activate GitHub integrations on individual projects to sync with specific repositories.",
|
|
|
|
notInstalled: "Connect with GitHub with your Plane workspace to sync project issues.",
|
|
|
|
},
|
|
|
|
slack: {
|
|
|
|
logo: SlackLogo,
|
2023-04-22 16:24:50 +00:00
|
|
|
installed: "Activate Slack integrations on individual projects to sync with specific channels.",
|
2023-04-18 05:24:45 +00:00
|
|
|
notInstalled: "Connect with Slack with your Plane workspace to sync project issues.",
|
|
|
|
},
|
|
|
|
};
|
2023-02-23 12:42:07 +00:00
|
|
|
|
2023-04-18 05:24:45 +00:00
|
|
|
export const SingleIntegrationCard: React.FC<Props> = ({ integration }) => {
|
|
|
|
const [deletingIntegration, setDeletingIntegration] = useState(false);
|
2023-02-22 14:10:57 +00:00
|
|
|
|
|
|
|
const router = useRouter();
|
2023-02-23 12:42:07 +00:00
|
|
|
const { workspaceSlug } = router.query;
|
|
|
|
|
|
|
|
const { setToastAlert } = useToast();
|
2023-02-22 14:10:57 +00:00
|
|
|
|
2023-04-18 05:24:45 +00:00
|
|
|
const { startAuth, isConnecting: isInstalling } = useIntegrationPopup(integration.provider);
|
2023-02-22 14:10:57 +00:00
|
|
|
|
2023-02-23 12:42:07 +00:00
|
|
|
const { data: workspaceIntegrations } = useSWR(
|
|
|
|
workspaceSlug ? WORKSPACE_INTEGRATIONS(workspaceSlug as string) : null,
|
|
|
|
() =>
|
2023-04-05 19:21:15 +00:00
|
|
|
workspaceSlug
|
|
|
|
? IntegrationService.getWorkspaceIntegrationsList(workspaceSlug as string)
|
|
|
|
: null
|
2023-02-23 12:42:07 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const handleRemoveIntegration = async () => {
|
|
|
|
if (!workspaceSlug || !integration || !workspaceIntegrations) return;
|
|
|
|
|
|
|
|
const workspaceIntegrationId = workspaceIntegrations?.find(
|
|
|
|
(i) => i.integration === integration.id
|
|
|
|
)?.id;
|
|
|
|
|
|
|
|
setDeletingIntegration(true);
|
|
|
|
|
2023-04-05 19:21:15 +00:00
|
|
|
await IntegrationService.deleteWorkspaceIntegration(
|
|
|
|
workspaceSlug as string,
|
|
|
|
workspaceIntegrationId ?? ""
|
|
|
|
)
|
2023-02-23 12:42:07 +00:00
|
|
|
.then(() => {
|
2023-04-18 05:24:45 +00:00
|
|
|
mutate<IWorkspaceIntegration[]>(
|
2023-02-23 12:42:07 +00:00
|
|
|
WORKSPACE_INTEGRATIONS(workspaceSlug as string),
|
|
|
|
(prevData) => prevData?.filter((i) => i.id !== workspaceIntegrationId),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
setDeletingIntegration(false);
|
|
|
|
|
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
title: "Deleted successfully!",
|
|
|
|
message: `${integration.title} integration deleted successfully.`,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setDeletingIntegration(false);
|
|
|
|
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: `${integration.title} integration could not be deleted. Please try again.`,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const isInstalled = workspaceIntegrations?.find(
|
|
|
|
(i: any) => i.integration_detail.id === integration.id
|
|
|
|
);
|
|
|
|
|
2023-02-22 14:10:57 +00:00
|
|
|
return (
|
2023-07-17 07:30:44 +00:00
|
|
|
<div className="flex items-center justify-between gap-2 rounded-[10px] border border-custom-border-300 bg-custom-background-100 p-5">
|
2023-02-23 12:42:07 +00:00
|
|
|
<div className="flex items-start gap-4">
|
2023-03-03 09:00:21 +00:00
|
|
|
<div className="h-12 w-12 flex-shrink-0">
|
2023-04-18 05:24:45 +00:00
|
|
|
<Image
|
|
|
|
src={integrationDetails[integration.provider].logo}
|
|
|
|
alt={`${integration.title} Logo`}
|
|
|
|
/>
|
2023-02-23 12:42:07 +00:00
|
|
|
</div>
|
|
|
|
<div>
|
2023-03-03 09:00:21 +00:00
|
|
|
<h3 className="flex items-center gap-4 text-xl font-semibold">
|
2023-02-23 12:42:07 +00:00
|
|
|
{integration.title}
|
2023-02-23 17:04:36 +00:00
|
|
|
{workspaceIntegrations ? (
|
|
|
|
isInstalled ? (
|
2023-03-03 09:00:21 +00:00
|
|
|
<span className="flex items-center gap-1 text-sm font-normal text-green-500">
|
|
|
|
<span className="h-1.5 w-1.5 flex-shrink-0 rounded-full bg-green-500" /> Installed
|
2023-02-23 17:04:36 +00:00
|
|
|
</span>
|
|
|
|
) : (
|
2023-07-10 07:17:00 +00:00
|
|
|
<span className="flex items-center gap-1 text-sm font-normal text-custom-text-200">
|
|
|
|
<span className="h-1.5 w-1.5 flex-shrink-0 rounded-full bg-custom-background-80" />{" "}
|
|
|
|
Not Installed
|
2023-02-23 17:04:36 +00:00
|
|
|
</span>
|
|
|
|
)
|
|
|
|
) : null}
|
2023-02-23 12:42:07 +00:00
|
|
|
</h3>
|
2023-07-10 07:17:00 +00:00
|
|
|
<p className="text-sm text-custom-text-200">
|
2023-02-23 17:04:36 +00:00
|
|
|
{workspaceIntegrations
|
|
|
|
? isInstalled
|
2023-04-18 05:24:45 +00:00
|
|
|
? integrationDetails[integration.provider].installed
|
|
|
|
: integrationDetails[integration.provider].notInstalled
|
2023-02-23 17:04:36 +00:00
|
|
|
: "Loading..."}
|
2023-02-23 12:42:07 +00:00
|
|
|
</p>
|
|
|
|
</div>
|
2023-02-22 14:10:57 +00:00
|
|
|
</div>
|
2023-02-23 17:04:36 +00:00
|
|
|
|
|
|
|
{workspaceIntegrations ? (
|
|
|
|
isInstalled ? (
|
2023-03-17 05:10:38 +00:00
|
|
|
<DangerButton onClick={handleRemoveIntegration} loading={deletingIntegration}>
|
2023-02-23 17:04:36 +00:00
|
|
|
{deletingIntegration ? "Removing..." : "Remove installation"}
|
2023-03-17 05:10:38 +00:00
|
|
|
</DangerButton>
|
2023-02-23 17:04:36 +00:00
|
|
|
) : (
|
2023-04-22 16:24:50 +00:00
|
|
|
<PrimaryButton onClick={startAuth} loading={isInstalling}>
|
2023-04-18 05:24:45 +00:00
|
|
|
{isInstalling ? "Installing..." : "Add installation"}
|
2023-04-22 16:24:50 +00:00
|
|
|
</PrimaryButton>
|
2023-02-23 17:04:36 +00:00
|
|
|
)
|
2023-02-23 12:42:07 +00:00
|
|
|
) : (
|
2023-02-23 17:04:36 +00:00
|
|
|
<Loader>
|
|
|
|
<Loader.Item height="35px" width="150px" />
|
|
|
|
</Loader>
|
2023-02-23 12:42:07 +00:00
|
|
|
)}
|
|
|
|
</div>
|
2023-02-22 14:10:57 +00:00
|
|
|
);
|
|
|
|
};
|