"use client" import { useState } from "react" import { zodResolver } from "@hookform/resolvers/zod" import { useForm } from "react-hook-form" import { z } from "zod" import { Card, CardHeader, CardTitle, CardDescription, CardContent } from "@/components/ui/card" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { Button } from "@/components/ui/button" import { SiGitea } from "react-icons/si" import { AlertCircle, Loader2 } from "lucide-react" import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert" import Link from "next/link" const giteaFormSchema = z.object({ username: z .string() .min(2, { message: "Username must be at least 2 characters" }) .max(39, { message: "Username cannot exceed 39 characters" }) .regex(/^[a-zA-Z][a-zA-Z0-9-_]*[a-zA-Z0-9]$/, { message: "Username must start with a letter and can only contain letters, numbers, hyphens, and underscores", }) .regex(/^(?!.*[-_]{2})/, { message: "Username cannot contain consecutive hyphens or underscores", }), }) type GiteaFormValues = z.infer export function LinkGitea({ linked }: { linked: boolean }) { const [loading, setLoading] = useState(false) const [unlinkLoading, setUnlinkLoading] = useState(false) const [linkError, setLinkError] = useState("") const [unlinkError, setUnlinkError] = useState("") const form = useForm({ resolver: zodResolver(giteaFormSchema), defaultValues: { username: "", }, }) const onSubmit = async (data: GiteaFormValues) => { setLoading(true) try { const response = await fetch("/api/git/link", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ username: data.username }), }) const responseData = await response.json() if (responseData.success) { console.log("Gitea account linked:", responseData) location.reload() } else if (responseData.error) { setLinkError(responseData.error) setLoading(false) } else { setLinkError("Failed to link") setLoading(false) throw new Error("Failed to link Gitea account") } } catch (error) { setLoading(false) setLinkError("Failed to link") console.error("Error linking Gitea account:", error) } } const onUnlink = async () => { setUnlinkLoading(true) try { const response = await fetch("/api/git/unlink", { method: "POST", }) const responseData = await response.json() if (responseData.success) { console.log("Gitea account unlinked") location.reload() } else { setUnlinkError(responseData.error) console.error("Failed to unlink:", responseData.error) setUnlinkLoading(false) } } catch (error) { setUnlinkLoading(false) setUnlinkError("Failed to unlink") console.error("Error unlinking Gitea account:", error) } } if (!linked) { return ( Gitea Link To link your Gitea account to your LibreCloud account, add your p0ntus mail account to your Gitea account, then click the button.
{linkError && ( Oops! Something went wrong. {linkError} )}
( Gitea Username )} /> {loading ? (
) : (
)}
) } else { return ( Gitea Link Your Gitea account is currently linked to your LibreCloud account. {unlinkError && ( Oops! Something went wrong. {unlinkError} )}

Unlinking your Gitea account will not delete your Gitea account. You can delete your Gitea account here.

{unlinkLoading ? ( ) : ( )}
) } }