-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathConnectOAuthButton.tsx
More file actions
152 lines (138 loc) · 4.02 KB
/
Copy pathConnectOAuthButton.tsx
File metadata and controls
152 lines (138 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import Pizzly from "@nangohq/pizzly-frontend";
import { useFetcher } from "@remix-run/react";
import type { SerializableProvider } from "@trigger.dev/providers";
import { useCallback, useEffect } from "react";
import invariant from "tiny-invariant";
import type { Response as CreateResponse } from "~/routes/resources/connection";
import type {
Request as UpdateRequest,
Response as UpdateResponse,
} from "~/routes/resources/connection/$connectionId";
import type { Status } from "./ConnectButton";
export function ConnectOAuthButton({
integration,
organizationId,
sourceId,
serviceId,
className,
children,
}: {
integration: SerializableProvider;
organizationId: string;
sourceId?: string;
serviceId?: string;
className?: string;
children: (status: Status) => React.ReactNode;
}) {
const { createFetcher, status } = useCreateConnection(sourceId, serviceId);
return (
<createFetcher.Form method="post" action="/resources/connection?index">
<input type="hidden" name="type" value="oauth" />
<input type="hidden" name="organizationId" value={organizationId} />
<input type="hidden" name="service" value={integration.slug} />
<button
type="submit"
disabled={status === "loading"}
className={className}
>
{children(status)}
</button>
</createFetcher.Form>
);
}
export function useCreateConnection(sourceId?: string, serviceId?: string) {
const createConnectionFetcher = useFetcher<CreateResponse>();
const completeConnectionFetcher = useFetcher<UpdateResponse>();
const status: Status =
createConnectionFetcher.state === "idle" &&
completeConnectionFetcher.state === "idle"
? "idle"
: "loading";
const completeFlow = useCallback(
async ({
pizzlyHost,
connectionId,
service,
sourceId,
serviceId,
}: {
pizzlyHost: string;
connectionId: string;
service: string;
sourceId?: string;
serviceId?: string;
}) => {
try {
const pizzly = new Pizzly(pizzlyHost);
await pizzly.auth(service, connectionId);
let completeData: UpdateRequest = {
connectionId: connectionId,
};
if (sourceId) {
completeData = {
...completeData,
sourceId,
};
}
if (serviceId) {
completeData = {
...completeData,
serviceId,
};
}
completeConnectionFetcher.submit(completeData, {
method: "put",
action: `/resources/connection/${connectionId}`,
});
} catch (error: any) {
console.error(
`There was an error in the OAuth flow for integration "${error.providerConfigKey}" and connection-id "${error.connectionId}": ${error.error.type} - ${error.error.message}`,
error
);
}
},
[completeConnectionFetcher]
);
useEffect(() => {
if (
createConnectionFetcher.state !== "idle" ||
createConnectionFetcher.type !== "done"
)
return;
if (
completeConnectionFetcher.state !== "idle" ||
completeConnectionFetcher.type !== "init"
)
return;
if (createConnectionFetcher.data === undefined) return;
completeConnectionFetcher.type = "init";
if (!createConnectionFetcher.data.success) {
throw new Error(
`There was an error creating the connection: ${createConnectionFetcher.data.errors}`
);
}
invariant(
createConnectionFetcher.data.pizzlyHost,
"pizzlyHost is required for oauth"
);
completeFlow({
pizzlyHost: createConnectionFetcher.data.pizzlyHost,
connectionId: createConnectionFetcher.data.connectionId,
service: createConnectionFetcher.data.service,
sourceId,
serviceId,
});
}, [
completeConnectionFetcher,
completeConnectionFetcher.data,
completeConnectionFetcher.submit,
completeFlow,
createConnectionFetcher,
serviceId,
sourceId,
]);
return {
createFetcher: createConnectionFetcher,
status,
};
}