| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 1 | import { useProjectId, useGithubService, useStateStore } from "@/lib/state"; |
| 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 | |
| 12 | const schema = 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 | |
| 16 | export function Integrations() { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 17 | const { toast } = useToast(); |
| 18 | const store = useStateStore(); |
| 19 | const projectId = useProjectId(); |
| 20 | const [isEditing, setIsEditing] = useState(false); |
| 21 | const githubService = useGithubService(); |
| 22 | const [isSaving, setIsSaving] = useState(false); |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 23 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 24 | const form = useForm<z.infer<typeof schema>>({ |
| 25 | resolver: zodResolver(schema), |
| 26 | mode: "onChange", |
| 27 | }); |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 28 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 29 | const onSubmit = useCallback( |
| 30 | async (data: z.infer<typeof schema>) => { |
| 31 | if (!projectId) return; |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 32 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 33 | setIsSaving(true); |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 34 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 35 | try { |
| 36 | const response = await fetch(`/api/project/${projectId}/github-token`, { |
| 37 | method: "POST", |
| 38 | headers: { |
| 39 | "Content-Type": "application/json", |
| 40 | }, |
| 41 | body: JSON.stringify({ githubToken: data.githubToken }), |
| 42 | }); |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 43 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 44 | if (!response.ok) { |
| 45 | throw new Error("Failed to save GitHub token"); |
| 46 | } |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 47 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 48 | await store.refreshEnv(); |
| 49 | setIsEditing(false); |
| 50 | form.reset(); |
| 51 | toast({ |
| 52 | title: "GitHub token saved successfully", |
| 53 | }); |
| 54 | } catch (error) { |
| 55 | toast({ |
| 56 | variant: "destructive", |
| 57 | title: "Failed to save GitHub token", |
| 58 | description: error instanceof Error ? error.message : "Unknown error", |
| 59 | }); |
| 60 | } finally { |
| 61 | setIsSaving(false); |
| 62 | } |
| 63 | }, |
| 64 | [projectId, store, form, toast, setIsEditing, setIsSaving], |
| 65 | ); |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 66 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 67 | const handleCancel = () => { |
| 68 | setIsEditing(false); |
| 69 | form.reset(); |
| 70 | }; |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 71 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 72 | return ( |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 73 | <div className="px-4 py-1"> |
| 74 | <div className="flex flex-col gap-1"> |
| 75 | <div className="flex flex-row items-center gap-1"> |
| 76 | {githubService ? <CircleCheck /> : <CircleX />} |
| 77 | <div>Github</div> |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 78 | </div> |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 79 | |
| 80 | {!!githubService && !isEditing && ( |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 81 | <Button variant="outline" className="w-fit" onClick={() => setIsEditing(true)}> |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 82 | Update Access Token |
| 83 | </Button> |
| 84 | )} |
| 85 | |
| 86 | {(!githubService || isEditing) && ( |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 87 | <div className="flex flex-row items-center gap-1 text-sm"> |
| gio | 3304672 | 2025-05-16 14:49:55 +0000 | [diff] [blame^] | 88 | <div> |
| 89 | Follow the link to generate new PAT:{" "} |
| 90 | <a href="https://github.com/settings/personal-access-tokens" target="_blank"> |
| 91 | https://github.com/settings/personal-access-tokens |
| 92 | </a> |
| 93 | <br /> |
| 94 | Grant following <b>Repository</b> permissions: |
| 95 | <ul> |
| 96 | <li> |
| 97 | <b>Contents</b> - Read-only access so dodo can clone the repository |
| 98 | </li> |
| 99 | <li> |
| 100 | <b>Metadata</b> - Read-only access so dodo can search for repositories |
| 101 | </li> |
| 102 | <li> |
| 103 | <b>Administration</b> - Read and write access so dodo automatically add deploy keys |
| 104 | to repositories |
| 105 | </li> |
| 106 | </ul> |
| 107 | </div> |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 108 | </div> |
| 109 | )} |
| 110 | {(!githubService || isEditing) && ( |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 111 | <Form {...form}> |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 112 | <form className="space-y-2" onSubmit={form.handleSubmit(onSubmit)}> |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 113 | <FormField |
| 114 | control={form.control} |
| 115 | name="githubToken" |
| 116 | render={({ field }) => ( |
| 117 | <FormItem> |
| 118 | <FormControl> |
| 119 | <Input |
| 120 | type="password" |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 121 | placeholder="Personal Access Token" |
| 122 | className="w-1/4" |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 123 | {...field} |
| 124 | /> |
| 125 | </FormControl> |
| 126 | <FormMessage /> |
| 127 | </FormItem> |
| 128 | )} |
| 129 | /> |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 130 | <div className="flex flex-row items-center gap-1"> |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 131 | <Button type="submit" disabled={isSaving}> |
| 132 | {isSaving ? "Saving..." : "Save"} |
| 133 | </Button> |
| 134 | {!!githubService && ( |
| 135 | <Button type="button" variant="outline" onClick={handleCancel} disabled={isSaving}> |
| 136 | Cancel |
| 137 | </Button> |
| 138 | )} |
| 139 | </div> |
| 140 | </form> |
| 141 | </Form> |
| 142 | )} |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 143 | </div> |
| 144 | </div> |
| 145 | ); |
| 146 | } |