| 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"; |
| 9 | import { Checkbox } from "./components/ui/checkbox"; |
| 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 | bc47f9f | 2025-05-12 08:31:07 +0000 | [diff] [blame] | 73 | <div className="px-4 py-1 space-y-6"> |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 74 | <div> |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 75 | <div className="flex items-center space-x-2"> |
| 76 | <Checkbox checked={!!githubService} disabled /> |
| 77 | <label className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"> |
| 78 | <h3 className="text-md font-medium mb-2">GitHub</h3> |
| 79 | </label> |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 80 | </div> |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 81 | |
| 82 | {!!githubService && !isEditing && ( |
| 83 | <Button variant="outline" onClick={() => setIsEditing(true)}> |
| 84 | Update Access Token |
| 85 | </Button> |
| 86 | )} |
| 87 | |
| 88 | {(!githubService || isEditing) && ( |
| 89 | <Form {...form}> |
| 90 | <form className="space-y-4" onSubmit={form.handleSubmit(onSubmit)}> |
| 91 | <FormField |
| 92 | control={form.control} |
| 93 | name="githubToken" |
| 94 | render={({ field }) => ( |
| 95 | <FormItem> |
| 96 | <FormControl> |
| 97 | <Input |
| 98 | type="password" |
| 99 | placeholder="GitHub Personal Access Token" |
| 100 | className="border border-black" |
| 101 | {...field} |
| 102 | /> |
| 103 | </FormControl> |
| 104 | <FormMessage /> |
| 105 | </FormItem> |
| 106 | )} |
| 107 | /> |
| 108 | <div className="flex space-x-2"> |
| 109 | <Button type="submit" disabled={isSaving}> |
| 110 | {isSaving ? "Saving..." : "Save"} |
| 111 | </Button> |
| 112 | {!!githubService && ( |
| 113 | <Button type="button" variant="outline" onClick={handleCancel} disabled={isSaving}> |
| 114 | Cancel |
| 115 | </Button> |
| 116 | )} |
| 117 | </div> |
| 118 | </form> |
| 119 | </Form> |
| 120 | )} |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 121 | </div> |
| 122 | </div> |
| 123 | ); |
| 124 | } |