| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 1 | import { useProjectId, useGithubService, useStateStore, useGeminiService } 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 | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 20 | export function Integrations() { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 21 | const { toast } = useToast(); |
| 22 | const store = useStateStore(); |
| 23 | const projectId = useProjectId(); |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 24 | const [isEditingGithub, setIsEditingGithub] = useState(false); |
| 25 | const [isEditingGemini, setIsEditingGemini] = useState(false); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 26 | const githubService = useGithubService(); |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 27 | const geminiService = useGeminiService(); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 28 | const [isSaving, setIsSaving] = useState(false); |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 29 | |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 30 | const githubForm = useForm<z.infer<typeof githubSchema>>({ |
| 31 | resolver: zodResolver(githubSchema), |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 32 | mode: "onChange", |
| 33 | }); |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 34 | |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 35 | const geminiForm = useForm<z.infer<typeof geminiSchema>>({ |
| 36 | resolver: zodResolver(geminiSchema), |
| 37 | mode: "onChange", |
| 38 | }); |
| 39 | |
| 40 | const onGithubSubmit = useCallback( |
| 41 | async (data: z.infer<typeof githubSchema>) => { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 42 | if (!projectId) return; |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 43 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 44 | setIsSaving(true); |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 45 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 46 | try { |
| 47 | const response = await fetch(`/api/project/${projectId}/github-token`, { |
| 48 | method: "POST", |
| 49 | headers: { |
| 50 | "Content-Type": "application/json", |
| 51 | }, |
| 52 | body: JSON.stringify({ githubToken: data.githubToken }), |
| 53 | }); |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 54 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 55 | if (!response.ok) { |
| 56 | throw new Error("Failed to save GitHub token"); |
| 57 | } |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 58 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 59 | await store.refreshEnv(); |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 60 | setIsEditingGithub(false); |
| 61 | githubForm.reset(); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 62 | toast({ |
| 63 | title: "GitHub token saved successfully", |
| 64 | }); |
| 65 | } catch (error) { |
| 66 | toast({ |
| 67 | variant: "destructive", |
| 68 | title: "Failed to save GitHub token", |
| 69 | description: error instanceof Error ? error.message : "Unknown error", |
| 70 | }); |
| 71 | } finally { |
| 72 | setIsSaving(false); |
| 73 | } |
| 74 | }, |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 75 | [projectId, store, githubForm, toast, setIsEditingGithub, setIsSaving], |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 76 | ); |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 77 | |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 78 | const onGeminiSubmit = useCallback( |
| 79 | async (data: z.infer<typeof geminiSchema>) => { |
| 80 | if (!projectId) return; |
| 81 | setIsSaving(true); |
| 82 | try { |
| 83 | const response = await fetch(`/api/project/${projectId}/gemini-token`, { |
| 84 | method: "POST", |
| 85 | headers: { |
| 86 | "Content-Type": "application/json", |
| 87 | }, |
| 88 | body: JSON.stringify({ geminiApiKey: data.geminiApiKey }), |
| 89 | }); |
| 90 | if (!response.ok) { |
| 91 | throw new Error("Failed to save Gemini token"); |
| 92 | } |
| 93 | await store.refreshEnv(); |
| 94 | setIsEditingGemini(false); |
| 95 | geminiForm.reset(); |
| 96 | toast({ |
| 97 | title: "Gemini token saved successfully", |
| 98 | }); |
| 99 | } catch (error) { |
| 100 | toast({ |
| 101 | variant: "destructive", |
| 102 | title: "Failed to save Gemini token", |
| 103 | description: error instanceof Error ? error.message : "Unknown error", |
| 104 | }); |
| 105 | } finally { |
| 106 | setIsSaving(false); |
| 107 | } |
| 108 | }, |
| 109 | [projectId, store, geminiForm, toast, setIsEditingGemini, setIsSaving], |
| 110 | ); |
| 111 | |
| 112 | const handleCancelGithub = () => { |
| 113 | setIsEditingGithub(false); |
| 114 | githubForm.reset(); |
| 115 | }; |
| 116 | |
| 117 | const handleCancelGemini = () => { |
| 118 | setIsEditingGemini(false); |
| 119 | geminiForm.reset(); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 120 | }; |
| gio | 7f98e77 | 2025-05-07 11:00:14 +0000 | [diff] [blame] | 121 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 122 | return ( |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 123 | <div className="px-4 py-1"> |
| 124 | <div className="flex flex-col gap-1"> |
| 125 | <div className="flex flex-row items-center gap-1"> |
| 126 | {githubService ? <CircleCheck /> : <CircleX />} |
| 127 | <div>Github</div> |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 128 | </div> |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 129 | |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 130 | {!!githubService && !isEditingGithub && ( |
| 131 | <Button variant="outline" className="w-fit" onClick={() => setIsEditingGithub(true)}> |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 132 | Update Access Token |
| 133 | </Button> |
| 134 | )} |
| 135 | |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 136 | {(!githubService || isEditingGithub) && ( |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 137 | <div className="flex flex-row items-center gap-1 text-sm"> |
| gio | 3304672 | 2025-05-16 14:49:55 +0000 | [diff] [blame] | 138 | <div> |
| 139 | Follow the link to generate new PAT:{" "} |
| 140 | <a href="https://github.com/settings/personal-access-tokens" target="_blank"> |
| 141 | https://github.com/settings/personal-access-tokens |
| 142 | </a> |
| 143 | <br /> |
| 144 | Grant following <b>Repository</b> permissions: |
| 145 | <ul> |
| 146 | <li> |
| 147 | <b>Contents</b> - Read-only access so dodo can clone the repository |
| 148 | </li> |
| 149 | <li> |
| 150 | <b>Metadata</b> - Read-only access so dodo can search for repositories |
| 151 | </li> |
| 152 | <li> |
| gio | eb148c8 | 2025-05-19 16:17:22 +0000 | [diff] [blame] | 153 | <b>Webhooks</b> - Read and write access so dodo can register webhooks with Github |
| 154 | and receive updates |
| 155 | </li> |
| 156 | <li> |
| gio | 3304672 | 2025-05-16 14:49:55 +0000 | [diff] [blame] | 157 | <b>Administration</b> - Read and write access so dodo automatically add deploy keys |
| 158 | to repositories |
| 159 | </li> |
| 160 | </ul> |
| 161 | </div> |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 162 | </div> |
| 163 | )} |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 164 | {(!githubService || isEditingGithub) && ( |
| 165 | <Form {...githubForm}> |
| 166 | <form className="space-y-2" onSubmit={githubForm.handleSubmit(onGithubSubmit)}> |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 167 | <FormField |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 168 | control={githubForm.control} |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 169 | name="githubToken" |
| 170 | render={({ field }) => ( |
| 171 | <FormItem> |
| 172 | <FormControl> |
| 173 | <Input |
| 174 | type="password" |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 175 | placeholder="Personal Access Token" |
| 176 | className="w-1/4" |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 177 | {...field} |
| 178 | /> |
| 179 | </FormControl> |
| 180 | <FormMessage /> |
| 181 | </FormItem> |
| 182 | )} |
| 183 | /> |
| gio | 02f1cad | 2025-05-13 11:51:55 +0000 | [diff] [blame] | 184 | <div className="flex flex-row items-center gap-1"> |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 185 | <Button type="submit" disabled={isSaving}> |
| 186 | {isSaving ? "Saving..." : "Save"} |
| 187 | </Button> |
| 188 | {!!githubService && ( |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 189 | <Button |
| 190 | type="button" |
| 191 | variant="outline" |
| 192 | onClick={handleCancelGithub} |
| 193 | disabled={isSaving} |
| 194 | > |
| 195 | Cancel |
| 196 | </Button> |
| 197 | )} |
| 198 | </div> |
| 199 | </form> |
| 200 | </Form> |
| 201 | )} |
| 202 | </div> |
| 203 | <div className="flex flex-col gap-1"> |
| 204 | <div className="flex flex-row items-center gap-1"> |
| 205 | {geminiService ? <CircleCheck /> : <CircleX />} |
| 206 | <div>Gemini</div> |
| 207 | </div> |
| 208 | |
| 209 | {!!geminiService && !isEditingGemini && ( |
| 210 | <Button variant="outline" className="w-fit" onClick={() => setIsEditingGemini(true)}> |
| 211 | Update API Key |
| 212 | </Button> |
| 213 | )} |
| 214 | |
| 215 | {(!geminiService || isEditingGemini) && ( |
| 216 | <div className="flex flex-row items-center gap-1 text-sm"> |
| 217 | <div> |
| 218 | Follow the link to generate new API Key:{" "} |
| 219 | <a href="https://aistudio.google.com/app/apikey" target="_blank"> |
| 220 | https://aistudio.google.com/app/apikey |
| 221 | </a> |
| 222 | </div> |
| 223 | </div> |
| 224 | )} |
| 225 | {(!geminiService || isEditingGemini) && ( |
| 226 | <Form {...geminiForm}> |
| 227 | <form className="space-y-2" onSubmit={geminiForm.handleSubmit(onGeminiSubmit)}> |
| 228 | <FormField |
| 229 | control={geminiForm.control} |
| 230 | name="geminiApiKey" |
| 231 | render={({ field }) => ( |
| 232 | <FormItem> |
| 233 | <FormControl> |
| 234 | <Input |
| 235 | type="password" |
| 236 | placeholder="Gemini API Token" |
| 237 | className="w-1/4" |
| 238 | {...field} |
| 239 | /> |
| 240 | </FormControl> |
| 241 | <FormMessage /> |
| 242 | </FormItem> |
| 243 | )} |
| 244 | /> |
| 245 | <div className="flex flex-row items-center gap-1"> |
| 246 | <Button type="submit" disabled={isSaving}> |
| 247 | {isSaving ? "Saving..." : "Save"} |
| 248 | </Button> |
| 249 | {!!geminiService && ( |
| 250 | <Button |
| 251 | type="button" |
| 252 | variant="outline" |
| 253 | onClick={handleCancelGemini} |
| 254 | disabled={isSaving} |
| 255 | > |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 256 | Cancel |
| 257 | </Button> |
| 258 | )} |
| 259 | </div> |
| 260 | </form> |
| 261 | </Form> |
| 262 | )} |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 263 | </div> |
| 264 | </div> |
| 265 | ); |
| 266 | } |