Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 10 additions & 140 deletions packages/ui/src/features/inbox/components/DataSourceSetup.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useHostTRPC } from "@posthog/host-router/react";
import { Button } from "@posthog/quill";
import { useAuthenticatedClient } from "@posthog/ui/features/auth/authClient";
import { useAuthStateValue } from "@posthog/ui/features/auth/store";
Expand All @@ -14,8 +13,7 @@ import {
} from "@posthog/ui/features/integrations/useIntegrations";
import { toast } from "@posthog/ui/primitives/toast";
import { Box, Flex, Text, TextField } from "@radix-ui/themes";
import { useMutation } from "@tanstack/react-query";
import { useCallback, useEffect, useRef, useState } from "react";
import { useCallback, useEffect, useState } from "react";

type DataSourceType = "github" | "linear" | "jira" | "zendesk" | "pganalyze";

Expand Down Expand Up @@ -53,7 +51,15 @@ export function DataSourceSetup({
case "github":
return <GitHubSetup onComplete={onComplete} onCancel={onCancel} />;
case "linear":
return <LinearSetup onComplete={onComplete} onCancel={onCancel} />;
return (
<DynamicSourceSetup
sourceType="Linear"
title="Connect Linear"
schemas={schemasPayload("linear")}
onComplete={onComplete}
onCancel={onCancel}
/>
);
case "jira":
return (
<DynamicSourceSetup
Expand Down Expand Up @@ -272,142 +278,6 @@ function GitHubSetup({ onComplete, onCancel }: SetupFormProps) {
);
}

const POLL_INTERVAL_MS = 3_000;
const POLL_TIMEOUT_MS = 5 * 60 * 1000;

function LinearSetup({ onComplete }: SetupFormProps) {
const cloudRegion = useAuthStateValue((state) => state.cloudRegion);
const projectId = useAuthStateValue((state) => state.currentProjectId);
const client = useAuthenticatedClient();
const trpc = useHostTRPC();
const [loading, setLoading] = useState(false);
const [oauthConnected, setOauthConnected] = useState(false);
const [linearIntegrationId, setLinearIntegrationId] = useState<
number | string | null
>(null);
const [pollError, setPollError] = useState<string | null>(null);
const pollTimerRef = useRef<ReturnType<typeof setInterval> | null>(null);
const pollTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);

const stopPolling = useCallback(() => {
if (pollTimerRef.current) {
clearInterval(pollTimerRef.current);
pollTimerRef.current = null;
}
if (pollTimeoutRef.current) {
clearTimeout(pollTimeoutRef.current);
pollTimeoutRef.current = null;
}
}, []);

useEffect(() => stopPolling, [stopPolling]);

const startLinearFlow = useMutation(
trpc.linearIntegration.startFlow.mutationOptions(),
);

const handleOAuthConnect = useCallback(async () => {
if (!cloudRegion || !projectId || !client) return;
setLoading(true);
setPollError(null);
try {
await startLinearFlow.mutateAsync({
region: cloudRegion,
projectId,
});

pollTimerRef.current = setInterval(async () => {
try {
const integrations =
await client.getIntegrationsForProject(projectId);
const linearIntegration = integrations.find(
(i: { kind: string }) => i.kind === "linear",
) as { id: number | string } | undefined;
if (linearIntegration) {
stopPolling();
setLoading(false);
setOauthConnected(true);
setLinearIntegrationId(linearIntegration.id);
toast.success("Linear connected");
}
} catch {
// Ignore individual poll failures
}
}, POLL_INTERVAL_MS);

pollTimeoutRef.current = setTimeout(() => {
stopPolling();
setLoading(false);
setPollError("Connection timed out. Please try again.");
}, POLL_TIMEOUT_MS);
} catch (error) {
setLoading(false);
toast.error(
error instanceof Error ? error.message : "Failed to connect Linear",
);
}
}, [cloudRegion, projectId, client, stopPolling, startLinearFlow]);

const handleSubmit = useCallback(async () => {
if (!projectId || !client || !linearIntegrationId) return;

setLoading(true);
try {
await client.createExternalDataSource(projectId, {
source_type: "Linear",
payload: {
linear_integration_id: linearIntegrationId,
schemas: schemasPayload("linear"),
},
});
toast.success("Linear data source created");
onComplete();
} catch (error) {
toast.error(
error instanceof Error ? error.message : "Failed to create data source",
);
} finally {
setLoading(false);
}
}, [projectId, client, linearIntegrationId, onComplete]);

return (
<SetupFormContainer title="Connect Linear">
<Flex direction="column" gap="3">
<Button
type="button"
variant="primary"
size="sm"
onClick={handleOAuthConnect}
disabled={loading || oauthConnected}
>
{oauthConnected
? "Linear connected"
: loading
? "Waiting for authorization..."
: "Log into Linear to continue"}
</Button>

{pollError && (
<Text className="text-(--red-11) text-sm">{pollError}</Text>
)}

<Flex gap="2" justify="end">
<Button
type="button"
variant="primary"
size="sm"
onClick={handleSubmit}
disabled={!oauthConnected || loading}
>
{loading ? "Creating..." : "Create source"}
</Button>
</Flex>
</Flex>
</SetupFormContainer>
);
}

function ZendeskSetup({ onComplete, onCancel }: SetupFormProps) {
const projectId = useAuthStateValue((state) => state.currentProjectId);
const client = useAuthenticatedClient();
Expand Down
Loading
Loading