|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState } from "react"; |
| 4 | +import { Github, CheckCircle2, AlertCircle, Loader2 } from "lucide-react"; |
| 5 | +import { Button } from "@/components/ui/button"; |
| 6 | +import { |
| 7 | + Card, |
| 8 | + CardContent, |
| 9 | + CardDescription, |
| 10 | + CardFooter, |
| 11 | + CardHeader, |
| 12 | + CardTitle, |
| 13 | +} from "@/components/ui/card"; |
| 14 | +import { Input } from "@/components/ui/input"; |
| 15 | +import { Label } from "@/components/ui/label"; |
| 16 | +import { GITHUB_CONFIG } from "@/constants/integrations/github"; |
| 17 | +import { GitHubService } from "@/services/integrations/github.service"; |
| 18 | + |
| 19 | +export function GitHubConnectCard() { |
| 20 | + const [repoUrl, setRepoUrl] = useState(""); |
| 21 | + const [isLoading, setIsLoading] = useState(false); |
| 22 | + const [status, setStatus] = useState<{ |
| 23 | + type: "success" | "error" | null; |
| 24 | + message: string; |
| 25 | + }>({ type: null, message: "" }); |
| 26 | + |
| 27 | + const handleConnect = async () => { |
| 28 | + if (!repoUrl) { |
| 29 | + setStatus({ type: "error", message: GITHUB_CONFIG.TEXT.ERROR_EMPTY_URL }); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + setIsLoading(true); |
| 34 | + setStatus({ type: null, message: "" }); |
| 35 | + |
| 36 | + try { |
| 37 | + const response = await GitHubService.connectRepository(repoUrl); |
| 38 | + |
| 39 | + if (response.success) { |
| 40 | + setStatus({ |
| 41 | + type: "success", |
| 42 | + message: GITHUB_CONFIG.TEXT.SUCCESS_MESSAGE(response.data.full_name), |
| 43 | + }); |
| 44 | + setRepoUrl(""); |
| 45 | + } else { |
| 46 | + setStatus({ |
| 47 | + type: "error", |
| 48 | + message: response.message || GITHUB_CONFIG.TEXT.ERROR_GENERIC, |
| 49 | + }); |
| 50 | + } |
| 51 | + } catch (error) { |
| 52 | + setStatus({ |
| 53 | + type: "error", |
| 54 | + message: |
| 55 | + error instanceof Error |
| 56 | + ? error.message |
| 57 | + : GITHUB_CONFIG.TEXT.ERROR_GENERIC, |
| 58 | + }); |
| 59 | + } finally { |
| 60 | + setIsLoading(false); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + return ( |
| 65 | + <Card className="flex flex-col"> |
| 66 | + <CardHeader> |
| 67 | + <div className="flex items-center gap-2"> |
| 68 | + <Github className="h-6 w-6" /> |
| 69 | + <CardTitle>{GITHUB_CONFIG.TEXT.TITLE}</CardTitle> |
| 70 | + </div> |
| 71 | + <CardDescription>{GITHUB_CONFIG.TEXT.DESCRIPTION}</CardDescription> |
| 72 | + </CardHeader> |
| 73 | + <CardContent className="flex-1"> |
| 74 | + <div className="grid w-full items-center gap-4"> |
| 75 | + <div className="flex flex-col space-y-1.5"> |
| 76 | + <Label htmlFor="github-repo-url"> |
| 77 | + {GITHUB_CONFIG.TEXT.INPUT_LABEL} |
| 78 | + </Label> |
| 79 | + <Input |
| 80 | + id="github-repo-url" |
| 81 | + placeholder={GITHUB_CONFIG.TEXT.INPUT_PLACEHOLDER} |
| 82 | + value={repoUrl} |
| 83 | + onChange={(e) => setRepoUrl(e.target.value)} |
| 84 | + /> |
| 85 | + </div> |
| 86 | + |
| 87 | + {status.message && ( |
| 88 | + <div |
| 89 | + className={`flex items-center gap-2 text-sm p-3 rounded-md ${ |
| 90 | + status.type === "success" |
| 91 | + ? "bg-green-50 text-green-700 dark:bg-green-900/20 dark:text-green-400" |
| 92 | + : "bg-red-50 text-red-700 dark:bg-red-900/20 dark:text-red-400" |
| 93 | + }`} |
| 94 | + > |
| 95 | + {status.type === "success" ? ( |
| 96 | + <CheckCircle2 className="h-4 w-4 shrink-0" /> |
| 97 | + ) : ( |
| 98 | + <AlertCircle className="h-4 w-4 shrink-0" /> |
| 99 | + )} |
| 100 | + <span>{status.message}</span> |
| 101 | + </div> |
| 102 | + )} |
| 103 | + </div> |
| 104 | + </CardContent> |
| 105 | + <CardFooter className="flex flex-col items-start gap-4 border-t pt-6"> |
| 106 | + <Button className="w-full" onClick={handleConnect} disabled={isLoading}> |
| 107 | + {isLoading ? ( |
| 108 | + <> |
| 109 | + <Loader2 className="mr-2 h-4 w-4 animate-spin" /> |
| 110 | + {GITHUB_CONFIG.TEXT.BUTTON_CONNECTING} |
| 111 | + </> |
| 112 | + ) : ( |
| 113 | + GITHUB_CONFIG.TEXT.BUTTON_CONNECT |
| 114 | + )} |
| 115 | + </Button> |
| 116 | + <p className="text-xs text-muted-foreground"> |
| 117 | + {GITHUB_CONFIG.TEXT.FOOTER_NOTE} |
| 118 | + </p> |
| 119 | + </CardFooter> |
| 120 | + </Card> |
| 121 | + ); |
| 122 | +} |
0 commit comments