| gio | 69ff759 | 2025-07-03 06:27:21 +0000 | [diff] [blame^] | 1 | import { useProjectId, useGithubService, useStateStore, useGeminiService, useAnthropicService } from "@/lib/state"; |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 2 | import { Form, FormControl, FormField, FormItem, FormMessage } from "./components/ui/form"; |
| 3 | import { Input } from "./components/ui/input"; |
| 4 | import { useForm } from "react-hook-form"; |
| 5 | import { z } from "zod"; |
| 6 | import { zodResolver } from "@hookform/resolvers/zod"; |
| 7 | import { Button } from "./components/ui/button"; |
| 8 | import { useToast } from "@/hooks/use-toast"; |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 9 | import { CircleCheck, CircleX } from "lucide-react"; |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 10 | import { useState, useCallback } from "react"; |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 11 | |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 12 | const githubSchema = z.object({ |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 13 | githubToken: z.string().min(1, "GitHub token is required"), |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 14 | }); |
| 15 | |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 16 | const geminiSchema = z.object({ |
| 17 | geminiApiKey: z.string().min(1, "Gemini API token is required"), |
| 18 | }); |
| 19 | |
| gio | 69ff759 | 2025-07-03 06:27:21 +0000 | [diff] [blame^] | 20 | const anthropicSchema = z.object({ |
| 21 | anthropicApiKey: z.string().min(1, "Anthropic API token is required"), |
| 22 | }); |
| 23 | |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 24 | export function Integrations() { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 25 | const { toast } = useToast(); |
| 26 | const store = useStateStore(); |
| 27 | const projectId = useProjectId(); |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 28 | const [isEditingGithub, setIsEditingGithub] = useState(false); |
| 29 | const [isEditingGemini, setIsEditingGemini] = useState(false); |
| gio | 69ff759 | 2025-07-03 06:27:21 +0000 | [diff] [blame^] | 30 | const [isEditingAnthropic, setIsEditingAnthropic] = useState(false); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 31 | const githubService = useGithubService(); |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 32 | const geminiService = useGeminiService(); |
| gio | 69ff759 | 2025-07-03 06:27:21 +0000 | [diff] [blame^] | 33 | const anthropicService = useAnthropicService(); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 34 | const [isSaving, setIsSaving] = useState(false); |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 35 | |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 36 | const githubForm = useForm<z.infer<typeof githubSchema>>({ |
| 37 | resolver: zodResolver(githubSchema), |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 38 | mode: "onChange", |
| 39 | }); |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 40 | |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 41 | const geminiForm = useForm<z.infer<typeof geminiSchema>>({ |
| 42 | resolver: zodResolver(geminiSchema), |
| 43 | mode: "onChange", |
| 44 | }); |
| 45 | |
| gio | 69ff759 | 2025-07-03 06:27:21 +0000 | [diff] [blame^] | 46 | const anthropicForm = useForm<z.infer<typeof anthropicSchema>>({ |
| 47 | resolver: zodResolver(anthropicSchema), |
| 48 | mode: "onChange", |
| 49 | }); |
| 50 | |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 51 | const onGithubSubmit = useCallback( |
| 52 | async (data: z.infer<typeof githubSchema>) => { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 53 | if (!projectId) return; |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 54 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 55 | setIsSaving(true); |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 56 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 57 | try { |
| 58 | const response = await fetch(`/api/project/${projectId}/github-token`, { |
| 59 | method: "POST", |
| 60 | headers: { |
| 61 | "Content-Type": "application/json", |
| 62 | }, |
| 63 | body: JSON.stringify({ githubToken: data.githubToken }), |
| 64 | }); |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 65 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 66 | if (!response.ok) { |
| 67 | throw new Error("Failed to save GitHub token"); |
| 68 | } |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 69 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 70 | await store.refreshEnv(); |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 71 | setIsEditingGithub(false); |
| 72 | githubForm.reset(); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 73 | toast({ |
| 74 | title: "GitHub token saved successfully", |
| 75 | }); |
| 76 | } catch (error) { |
| 77 | toast({ |
| 78 | variant: "destructive", |
| 79 | title: "Failed to save GitHub token", |
| 80 | description: error instanceof Error ? error.message : "Unknown error", |
| 81 | }); |
| 82 | } finally { |
| 83 | setIsSaving(false); |
| 84 | } |
| 85 | }, |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 86 | [projectId, store, githubForm, toast, setIsEditingGithub, setIsSaving], |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 87 | ); |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 88 | |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 89 | const onGeminiSubmit = useCallback( |
| 90 | async (data: z.infer<typeof geminiSchema>) => { |
| 91 | if (!projectId) return; |
| 92 | setIsSaving(true); |
| 93 | try { |
| 94 | const response = await fetch(`/api/project/${projectId}/gemini-token`, { |
| 95 | method: "POST", |
| 96 | headers: { |
| 97 | "Content-Type": "application/json", |
| 98 | }, |
| 99 | body: JSON.stringify({ geminiApiKey: data.geminiApiKey }), |
| 100 | }); |
| 101 | if (!response.ok) { |
| 102 | throw new Error("Failed to save Gemini token"); |
| 103 | } |
| 104 | await store.refreshEnv(); |
| 105 | setIsEditingGemini(false); |
| 106 | geminiForm.reset(); |
| 107 | toast({ |
| 108 | title: "Gemini token saved successfully", |
| 109 | }); |
| 110 | } catch (error) { |
| 111 | toast({ |
| 112 | variant: "destructive", |
| 113 | title: "Failed to save Gemini token", |
| 114 | description: error instanceof Error ? error.message : "Unknown error", |
| 115 | }); |
| 116 | } finally { |
| 117 | setIsSaving(false); |
| 118 | } |
| 119 | }, |
| 120 | [projectId, store, geminiForm, toast, setIsEditingGemini, setIsSaving], |
| 121 | ); |
| 122 | |
| gio | 69ff759 | 2025-07-03 06:27:21 +0000 | [diff] [blame^] | 123 | const onAnthropicSubmit = useCallback( |
| 124 | async (data: z.infer<typeof anthropicSchema>) => { |
| 125 | if (!projectId) return; |
| 126 | setIsSaving(true); |
| 127 | try { |
| 128 | const response = await fetch(`/api/project/${projectId}/anthropic-token`, { |
| 129 | method: "POST", |
| 130 | headers: { |
| 131 | "Content-Type": "application/json", |
| 132 | }, |
| 133 | body: JSON.stringify({ anthropicApiKey: data.anthropicApiKey }), |
| 134 | }); |
| 135 | if (!response.ok) { |
| 136 | throw new Error("Failed to save Anthropic token"); |
| 137 | } |
| 138 | await store.refreshEnv(); |
| 139 | setIsEditingAnthropic(false); |
| 140 | anthropicForm.reset(); |
| 141 | toast({ |
| 142 | title: "Anthropic token saved successfully", |
| 143 | }); |
| 144 | } catch (error) { |
| 145 | toast({ |
| 146 | variant: "destructive", |
| 147 | title: "Failed to save Anthropic token", |
| 148 | description: error instanceof Error ? error.message : "Unknown error", |
| 149 | }); |
| 150 | } finally { |
| 151 | setIsSaving(false); |
| 152 | } |
| 153 | }, |
| 154 | [projectId, store, anthropicForm, toast, setIsEditingAnthropic, setIsSaving], |
| 155 | ); |
| 156 | |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 157 | const handleCancelGithub = () => { |
| 158 | setIsEditingGithub(false); |
| 159 | githubForm.reset(); |
| 160 | }; |
| 161 | |
| 162 | const handleCancelGemini = () => { |
| 163 | setIsEditingGemini(false); |
| 164 | geminiForm.reset(); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 165 | }; |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 166 | |
| gio | 69ff759 | 2025-07-03 06:27:21 +0000 | [diff] [blame^] | 167 | const handleCancelAnthropic = () => { |
| 168 | setIsEditingAnthropic(false); |
| 169 | anthropicForm.reset(); |
| 170 | }; |
| 171 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 172 | return ( |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 173 | <div className="px-4 py-1"> |
| 174 | <div className="flex flex-col gap-1"> |
| 175 | <div className="flex flex-row items-center gap-1"> |
| 176 | {githubService ? <CircleCheck /> : <CircleX />} |
| 177 | <div>Github</div> |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 178 | </div> |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 179 | |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 180 | {!!githubService && !isEditingGithub && ( |
| 181 | <Button variant="outline" className="w-fit" onClick={() => setIsEditingGithub(true)}> |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 182 | Update Access Token |
| 183 | </Button> |
| 184 | )} |
| 185 | |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 186 | {(!githubService || isEditingGithub) && ( |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 187 | <div className="flex flex-row items-center gap-1 text-sm"> |
| gio | 3304672 | 2025-05-16 14:49:55 +0000 | [diff] [blame] | 188 | <div> |
| 189 | Follow the link to generate new PAT:{" "} |
| 190 | <a href="https://github.com/settings/personal-access-tokens" target="_blank"> |
| 191 | https://github.com/settings/personal-access-tokens |
| 192 | </a> |
| 193 | <br /> |
| 194 | Grant following <b>Repository</b> permissions: |
| 195 | <ul> |
| 196 | <li> |
| 197 | <b>Contents</b> - Read-only access so dodo can clone the repository |
| 198 | </li> |
| 199 | <li> |
| 200 | <b>Metadata</b> - Read-only access so dodo can search for repositories |
| 201 | </li> |
| 202 | <li> |
| gio | eb148c8 | 2025-05-19 16:17:22 +0000 | [diff] [blame] | 203 | <b>Webhooks</b> - Read and write access so dodo can register webhooks with Github |
| 204 | and receive updates |
| 205 | </li> |
| 206 | <li> |
| gio | 3304672 | 2025-05-16 14:49:55 +0000 | [diff] [blame] | 207 | <b>Administration</b> - Read and write access so dodo automatically add deploy keys |
| 208 | to repositories |
| 209 | </li> |
| 210 | </ul> |
| 211 | </div> |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 212 | </div> |
| 213 | )} |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 214 | {(!githubService || isEditingGithub) && ( |
| 215 | <Form {...githubForm}> |
| 216 | <form className="space-y-2" onSubmit={githubForm.handleSubmit(onGithubSubmit)}> |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 217 | <FormField |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 218 | control={githubForm.control} |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 219 | name="githubToken" |
| 220 | render={({ field }) => ( |
| 221 | <FormItem> |
| 222 | <FormControl> |
| 223 | <Input |
| 224 | type="password" |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 225 | placeholder="Personal Access Token" |
| 226 | className="w-1/4" |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 227 | {...field} |
| 228 | /> |
| 229 | </FormControl> |
| 230 | <FormMessage /> |
| 231 | </FormItem> |
| 232 | )} |
| 233 | /> |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 234 | <div className="flex flex-row items-center gap-1"> |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 235 | <Button type="submit" disabled={isSaving}> |
| 236 | {isSaving ? "Saving..." : "Save"} |
| 237 | </Button> |
| 238 | {!!githubService && ( |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 239 | <Button |
| 240 | type="button" |
| 241 | variant="outline" |
| 242 | onClick={handleCancelGithub} |
| 243 | disabled={isSaving} |
| 244 | > |
| 245 | Cancel |
| 246 | </Button> |
| 247 | )} |
| 248 | </div> |
| 249 | </form> |
| 250 | </Form> |
| 251 | )} |
| 252 | </div> |
| 253 | <div className="flex flex-col gap-1"> |
| 254 | <div className="flex flex-row items-center gap-1"> |
| 255 | {geminiService ? <CircleCheck /> : <CircleX />} |
| 256 | <div>Gemini</div> |
| 257 | </div> |
| 258 | |
| 259 | {!!geminiService && !isEditingGemini && ( |
| 260 | <Button variant="outline" className="w-fit" onClick={() => setIsEditingGemini(true)}> |
| 261 | Update API Key |
| 262 | </Button> |
| 263 | )} |
| 264 | |
| 265 | {(!geminiService || isEditingGemini) && ( |
| 266 | <div className="flex flex-row items-center gap-1 text-sm"> |
| 267 | <div> |
| 268 | Follow the link to generate new API Key:{" "} |
| 269 | <a href="https://aistudio.google.com/app/apikey" target="_blank"> |
| 270 | https://aistudio.google.com/app/apikey |
| 271 | </a> |
| 272 | </div> |
| 273 | </div> |
| 274 | )} |
| 275 | {(!geminiService || isEditingGemini) && ( |
| 276 | <Form {...geminiForm}> |
| 277 | <form className="space-y-2" onSubmit={geminiForm.handleSubmit(onGeminiSubmit)}> |
| 278 | <FormField |
| 279 | control={geminiForm.control} |
| 280 | name="geminiApiKey" |
| 281 | render={({ field }) => ( |
| 282 | <FormItem> |
| 283 | <FormControl> |
| 284 | <Input |
| 285 | type="password" |
| 286 | placeholder="Gemini API Token" |
| 287 | className="w-1/4" |
| 288 | {...field} |
| 289 | /> |
| 290 | </FormControl> |
| 291 | <FormMessage /> |
| 292 | </FormItem> |
| 293 | )} |
| 294 | /> |
| 295 | <div className="flex flex-row items-center gap-1"> |
| 296 | <Button type="submit" disabled={isSaving}> |
| 297 | {isSaving ? "Saving..." : "Save"} |
| 298 | </Button> |
| 299 | {!!geminiService && ( |
| 300 | <Button |
| 301 | type="button" |
| 302 | variant="outline" |
| 303 | onClick={handleCancelGemini} |
| 304 | disabled={isSaving} |
| 305 | > |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 306 | Cancel |
| 307 | </Button> |
| 308 | )} |
| 309 | </div> |
| 310 | </form> |
| 311 | </Form> |
| 312 | )} |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 313 | </div> |
| gio | 69ff759 | 2025-07-03 06:27:21 +0000 | [diff] [blame^] | 314 | <div className="flex flex-col gap-1"> |
| 315 | <div className="flex flex-row items-center gap-1"> |
| 316 | {anthropicService ? <CircleCheck /> : <CircleX />} |
| 317 | <div>Anthropic</div> |
| 318 | </div> |
| 319 | |
| 320 | {!!anthropicService && !isEditingAnthropic && ( |
| 321 | <Button variant="outline" className="w-fit" onClick={() => setIsEditingAnthropic(true)}> |
| 322 | Update API Key |
| 323 | </Button> |
| 324 | )} |
| 325 | |
| 326 | {(!anthropicService || isEditingAnthropic) && ( |
| 327 | <div className="flex flex-row items-center gap-1 text-sm"> |
| 328 | <div> |
| 329 | Follow the link to generate new API Key:{" "} |
| 330 | <a href="https://console.anthropic.com/settings/keys" target="_blank"> |
| 331 | https://console.anthropic.com/settings/keys |
| 332 | </a> |
| 333 | </div> |
| 334 | </div> |
| 335 | )} |
| 336 | {(!anthropicService || isEditingAnthropic) && ( |
| 337 | <Form {...anthropicForm}> |
| 338 | <form className="space-y-2" onSubmit={anthropicForm.handleSubmit(onAnthropicSubmit)}> |
| 339 | <FormField |
| 340 | control={anthropicForm.control} |
| 341 | name="anthropicApiKey" |
| 342 | render={({ field }) => ( |
| 343 | <FormItem> |
| 344 | <FormControl> |
| 345 | <Input |
| 346 | type="password" |
| 347 | placeholder="Anthropic API Token" |
| 348 | className="w-1/4" |
| 349 | {...field} |
| 350 | /> |
| 351 | </FormControl> |
| 352 | <FormMessage /> |
| 353 | </FormItem> |
| 354 | )} |
| 355 | /> |
| 356 | <div className="flex flex-row items-center gap-1"> |
| 357 | <Button type="submit" disabled={isSaving}> |
| 358 | {isSaving ? "Saving..." : "Save"} |
| 359 | </Button> |
| 360 | {!!anthropicService && ( |
| 361 | <Button |
| 362 | type="button" |
| 363 | variant="outline" |
| 364 | onClick={handleCancelAnthropic} |
| 365 | disabled={isSaving} |
| 366 | > |
| 367 | Cancel |
| 368 | </Button> |
| 369 | )} |
| 370 | </div> |
| 371 | </form> |
| 372 | </Form> |
| 373 | )} |
| 374 | </div> |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 375 | </div> |
| 376 | ); |
| 377 | } |