Skip to content

Commit

Permalink
feat(integration): use the oAuthConfig presentness to determine integ…
Browse files Browse the repository at this point in the history
…ration setup flow (#1606)

Because

- use the oAuthConfig presentness to determine integration setup flow

This commit

- use the oAuthConfig presentness to determine integration setup flow
  • Loading branch information
EiffelFly authored Nov 19, 2024
1 parent 37411cf commit 975af94
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 32 deletions.
7 changes: 4 additions & 3 deletions packages/toolkit/src/lib/integrations/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"use client";

import { Integration } from "instill-sdk";
import { signIn } from "next-auth/react";
import { z } from "zod";

import type { IntegrationProvider } from "./types";
import { AvailableOAuthIntegration, resourceIdPrefix } from "../../constant";
import { resourceIdPrefix } from "../../constant";
import { formatResourceId } from "../../server";
import { createNaiveRandomString } from "../createNaiveRandomString";

Expand Down Expand Up @@ -43,8 +44,8 @@ export async function initializeIntegrationConnection({
}
}

export function isOAuthAvailable(integrationId: string) {
return AvailableOAuthIntegration.includes(integrationId);
export function isOAuthAvailable(integration: Integration) {
return integration.oAuthConfig && integration.oAuthConfig.authUrl;
}

export function getPrefilledOAuthIntegrationConnectionId({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export const Integrations = () => {
key={item.id}
integration={item}
namespaceId={selectedNamespaceId}
accessToken={accessToken}
/>
))
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,36 @@ import * as React from "react";
import Image from "next/image";
import { Integration, Nullable } from "instill-sdk";

import { Button } from "@instill-ai/design-system";
import { Button, useToast } from "@instill-ai/design-system";

import { initializeIntegrationConnection } from "../../../../lib";
import { LoadingSpin } from "../../../../components";
import {
getInstillAPIClient,
initializeIntegrationConnection,
toastInstillError,
useQueryClient,
} from "../../../../lib";
import { isOAuthAvailable } from "../../../../lib/integrations/helpers";
import { queryKeyStore } from "../../../../lib/react-query-service/queryKeyStore";
import { IntegrationProvider } from "../../../../server";
import { ManualSettingDialog } from "./ManualSettingDialog";

export type ConnectableIntegrationProps = {
integration: Integration;
namespaceId: Nullable<string>;
accessToken: Nullable<string>;
};

export const ConnectableIntegration = ({
integration,
namespaceId,
accessToken,
}: ConnectableIntegrationProps) => {
const queryClient = useQueryClient();
const { toast } = useToast();
const [isProcessing, setIsProcessing] = React.useState(false);
const [editingDialogIsOpen, setEditingDialogIsOpen] = React.useState(false);

const oAuthIsAvailable = isOAuthAvailable(integration.id);

return (
<div className="[&:not(:last-child)]:border-b [&:not(:last-child)]:border-semantic-bg-line flex flex-row gap-4 items-center p-4 w-full">
<div className="w-12 h-12 rounded-sm border-semantic-bg-line border flex items-center justify-center">
Expand All @@ -39,44 +49,81 @@ export const ConnectableIntegration = ({
</span>
<Button
variant="primary"
className="ml-auto"
onClick={() => {
if (oAuthIsAvailable) {
if (!namespaceId) {
return;
}
className="ml-auto w-20 items-center justify-center"
onClick={async () => {
if (!accessToken) {
return;
}

let provider: Nullable<IntegrationProvider> = null;

switch (integration.id) {
case "github":
provider = "github";
break;
case "slack":
provider = "slack";
break;
case "google-drive":
provider = "google-drive";
break;
default:
break;
}
setIsProcessing(true);

try {
const client = getInstillAPIClient({
accessToken,
});

const integrationFull =
await client.core.integration.getIntegration({
integrationId: integration.id,
view: "VIEW_FULL",
});

queryClient.setQueryData(
queryKeyStore.integration.getUseGetIntegrationQueryKey({
integrationId: integration.id,
view: "VIEW_FULL",
}),
integrationFull.integration,
);

if (isOAuthAvailable(integrationFull.integration)) {
if (!namespaceId) {
setIsProcessing(false);
return;
}

let provider: Nullable<IntegrationProvider> = null;

switch (integration.id) {
case "github":
provider = "github";
break;
case "slack":
provider = "slack";
break;
case "google-drive":
provider = "google-drive";
break;
default:
break;
}

if (!provider) {
setIsProcessing(false);
return;
}

setIsProcessing(false);

if (provider) {
initializeIntegrationConnection({
provider,
namespaceId,
integrationId: integration.id,
});
}

return;
} catch (error) {
toastInstillError({
toast,
error,
title: "Failed to connect to integration",
});
}

setIsProcessing(false);
setEditingDialogIsOpen(true);
}}
>
Connect
{isProcessing ? <LoadingSpin className="w-4 h-4" /> : "Connect"}
</Button>
<ManualSettingDialog
isOpen={editingDialogIsOpen}
Expand Down

0 comments on commit 975af94

Please sign in to comment.