mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-01 14:06:15 +00:00
151 lines
5.0 KiB
TypeScript
151 lines
5.0 KiB
TypeScript
import { LoaderCircle } from 'lucide-react';
|
|
import { Button } from '@/components/ui/button';
|
|
import {
|
|
Dialog,
|
|
DialogClose,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogTrigger,
|
|
} from '@/components/ui/dialog';
|
|
import { useForm, usePage } from '@inertiajs/react';
|
|
import { FormEventHandler, ReactNode, useEffect, useState } from 'react';
|
|
import { Label } from '@/components/ui/label';
|
|
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
|
|
import InputError from '@/components/ui/input-error';
|
|
import { Form, FormField, FormFields } from '@/components/ui/form';
|
|
import { Input } from '@/components/ui/input';
|
|
import { SharedData } from '@/types';
|
|
import { Checkbox } from '@/components/ui/checkbox';
|
|
|
|
type SourceControlForm = {
|
|
provider: string;
|
|
name: string;
|
|
global: boolean;
|
|
};
|
|
|
|
export default function ConnectSourceControl({
|
|
providers,
|
|
defaultProvider,
|
|
onProviderAdded,
|
|
children,
|
|
}: {
|
|
providers: string[];
|
|
defaultProvider?: string;
|
|
onProviderAdded?: () => void;
|
|
children: ReactNode;
|
|
}) {
|
|
const [open, setOpen] = useState(false);
|
|
|
|
const page = usePage<SharedData>();
|
|
|
|
const form = useForm<Required<SourceControlForm>>({
|
|
provider: 'github',
|
|
name: '',
|
|
global: false,
|
|
});
|
|
|
|
const submit: FormEventHandler = (e) => {
|
|
e.preventDefault();
|
|
form.post(route('source-controls.store'), {
|
|
onSuccess: () => {
|
|
setOpen(false);
|
|
if (onProviderAdded) {
|
|
onProviderAdded();
|
|
}
|
|
},
|
|
});
|
|
};
|
|
|
|
useEffect(() => {
|
|
form.setData('provider', defaultProvider ?? 'github');
|
|
}, [defaultProvider]);
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
<DialogTrigger asChild>{children}</DialogTrigger>
|
|
<DialogContent className="sm:max-w-xl">
|
|
<DialogHeader>
|
|
<DialogTitle>Connect to source control</DialogTitle>
|
|
<DialogDescription className="sr-only">Connect to a new source control</DialogDescription>
|
|
</DialogHeader>
|
|
<Form id="create-source-control-form" onSubmit={submit} className="p-4">
|
|
<FormFields>
|
|
<FormField>
|
|
<Label htmlFor="provider">Provider</Label>
|
|
<Select
|
|
value={form.data.provider}
|
|
onValueChange={(value) => {
|
|
form.setData('provider', value);
|
|
form.clearErrors();
|
|
}}
|
|
>
|
|
<SelectTrigger id="provider">
|
|
<SelectValue placeholder="Select a provider" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectGroup>
|
|
{providers.map((provider) => (
|
|
<SelectItem key={provider} value={provider}>
|
|
{provider}
|
|
</SelectItem>
|
|
))}
|
|
</SelectGroup>
|
|
</SelectContent>
|
|
</Select>
|
|
<InputError message={form.errors.provider} />
|
|
</FormField>
|
|
<FormField>
|
|
<Label htmlFor="name">Name</Label>
|
|
<Input
|
|
type="text"
|
|
name="name"
|
|
id="name"
|
|
placeholder="Name"
|
|
value={form.data.name}
|
|
onChange={(e) => form.setData('name', e.target.value)}
|
|
/>
|
|
<InputError message={form.errors.name} />
|
|
</FormField>
|
|
{page.props.configs.source_control_providers_custom_fields[form.data.provider]?.map((item: string) => (
|
|
<FormField key={item}>
|
|
<Label htmlFor={item} className="capitalize">
|
|
{item}
|
|
</Label>
|
|
<Input
|
|
type="text"
|
|
name={item}
|
|
id={item}
|
|
value={(form.data[item as keyof SourceControlForm] as string) ?? ''}
|
|
onChange={(e) => form.setData(item as keyof SourceControlForm, e.target.value)}
|
|
/>
|
|
<InputError message={form.errors[item as keyof SourceControlForm]} />
|
|
</FormField>
|
|
))}
|
|
<FormField>
|
|
<div className="flex items-center space-x-3">
|
|
<Checkbox id="global" name="global" checked={form.data.global} onClick={() => form.setData('global', !form.data.global)} />
|
|
<Label htmlFor="global">Is global (accessible in all projects)</Label>
|
|
</div>
|
|
<InputError message={form.errors.global} />
|
|
</FormField>
|
|
</FormFields>
|
|
</Form>
|
|
<DialogFooter>
|
|
<DialogClose asChild>
|
|
<Button type="button" variant="outline">
|
|
Cancel
|
|
</Button>
|
|
</DialogClose>
|
|
<Button type="button" onClick={submit} disabled={form.processing}>
|
|
{form.processing && <LoaderCircle className="animate-spin" />}
|
|
Connect
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|