add: deployment form wip

This commit is contained in:
Elie Baier 2024-03-24 13:17:59 +01:00
parent 44891b9d87
commit 40744a0502
3 changed files with 227 additions and 50 deletions

View File

@ -9,8 +9,12 @@ import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import Link from 'next/link'; import Link from 'next/link';
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { getUserRepository, GithubRepository } from '@/actions/github/repository'; import { getRepositoryBranches, getUserRepository, GithubRepository } from '@/actions/github/repository';
import { signOut, useSession } from 'next-auth/react'; import { signOut, useSession } from 'next-auth/react';
import { Input } from '@/components/ui/input';
import { Checkbox } from '@/components/ui/checkbox';
import { createApplication, NewApplication } from '@/actions/deploy/application';
import { useParams, useRouter } from 'next/navigation';
export const ServiceProviderList = [ export const ServiceProviderList = [
{ {
@ -31,47 +35,53 @@ export const ServiceProviderList = [
}, },
]; ];
export type NewApplication = {
serviceProvider: string;
git: {
repositoryId: number;
repositoryName: string;
};
env: {
[key: string]: string;
};
};
export default function CreateApplicationForm() { export default function CreateApplicationForm() {
const [steps, setSteps] = useState(1); const [steps, setSteps] = useState(1);
const [open, setOpen] = useState(false);
const params = useParams();
const [serviceProvider, setServiceProvider] = useState<string>('github');
const [repositories, setRepositories] = useState<GithubRepository[]>([]); const [repositories, setRepositories] = useState<GithubRepository[]>([]);
const [branches, setBranches] = useState<string[]>([]);
const [newApplication, setNewApplication] = useState<NewApplication>({ const [newApplication, setNewApplication] = useState<NewApplication>({
serviceProvider: '', serviceProvider: 'github',
git: { git: {
repositoryId: 0, repositoryId: 0,
repositoryName: '', repositoryName: '',
branch: '',
path: '',
}, },
autodeploy: true,
env: {}, env: {},
name: '',
}); });
const { data: session } = useSession(); const { data: session } = useSession();
useEffect(() => { useEffect(() => {
async function fetchRepositories() { async function fetchRepositories() {
if (serviceProvider == 'github') { if (newApplication.serviceProvider == 'github') {
const repos = await getUserRepository(session?.user.id as string, session?.user.username as string); const repos = await getUserRepository(session?.user.id as string, session?.user.username as string);
setRepositories(repos); setRepositories(repos);
} }
} }
fetchRepositories(); fetchRepositories();
}, [serviceProvider, session]); }, [newApplication.serviceProvider, session]);
useEffect(() => {
async function fetchBranches() {
if (newApplication.serviceProvider == 'github' && newApplication.git.repositoryId != 0) {
const branches = await getRepositoryBranches(session?.user.id as string, session?.user.username as string, newApplication.git.repositoryName);
setBranches(branches);
}
}
fetchBranches();
}, [newApplication.serviceProvider, newApplication.git.repositoryId, newApplication.git.repositoryName, session]);
return ( return (
<Sheet> <Sheet open={open} onOpenChange={() => setOpen((open) => !open)}>
<SheetTrigger asChild> <SheetTrigger asChild>
<Button className="flex gap-1 justify-center items-center bg-[#3A7BFE]"> <Button className="flex gap-1 justify-center items-center bg-[#3A7BFE]">
<Plus /> <Plus />
@ -96,9 +106,9 @@ export default function CreateApplicationForm() {
<div> <div>
{steps == 1 && ( {steps == 1 && (
<div> <div>
<RadioGroup defaultValue="github" className="flex flex-col gap-2 mb-4" onValueChange={setServiceProvider}> <RadioGroup defaultValue={newApplication.serviceProvider || 'github'} className="flex flex-col gap-2 mb-4" onValueChange={(e) => setNewApplication((prev) => ({ ...prev, serviceProvider: e }))}>
{ServiceProviderList.map((provider) => ( {ServiceProviderList.map((provider) => (
<div key={provider.value} className={cn('flex flex-row items-center gap-4 border-[1px] rounded-lg py-2 px-4', provider.value == serviceProvider ? 'border-[#3A7BFE]' : 'border-gray-300')}> <div key={provider.value} className={cn('flex flex-row items-center gap-4 border-[1px] rounded-lg py-2 px-4', provider.value == newApplication.serviceProvider ? 'border-[#3A7BFE]' : 'border-gray-300')}>
<RadioGroupItem value={provider.value} id={provider.value} /> <RadioGroupItem value={provider.value} id={provider.value} />
<label htmlFor={provider.value} className="flex flex-row items-center gap-4"> <label htmlFor={provider.value} className="flex flex-row items-center gap-4">
<img src={provider.image} alt={provider.name} className="w-8 h-8 rounded-full" /> <img src={provider.image} alt={provider.name} className="w-8 h-8 rounded-full" />
@ -107,30 +117,129 @@ export default function CreateApplicationForm() {
</div> </div>
))} ))}
</RadioGroup> </RadioGroup>
<div className="my-2"> <div className="my-2 flex flex-col gap-2">
{serviceProvider == 'github' && ( {newApplication.serviceProvider == 'github' && !!repositories && (
<Select onValueChange={(e) => setNewApplication((prev) => ({ ...prev, git: { repositoryId: parseInt(e), repositoryName: repositories.find((w) => w.id == parseInt(e))?.full_name as string } }))}> <div>
<SelectTrigger className="w-full"> <p className="text-muted-foreground text-sm">Repository</p>
<SelectValue placeholder="Select a repository" /> <Select defaultValue={newApplication.git.repositoryId.toString() || ''} onValueChange={(e) => setNewApplication((prev) => ({ ...prev, git: { ...prev.git, repositoryId: parseInt(e), repositoryName: repositories.find((w) => w.id == parseInt(e))?.name as string } }))}>
</SelectTrigger> <SelectTrigger className="w-full">
<SelectContent> <SelectValue placeholder="Select a repository" />
<SelectGroup> </SelectTrigger>
{/* <SelectItem value="1">Repository 1</SelectItem> */} <SelectContent>
{repositories?.map((repository) => ( <SelectGroup>
<SelectItem key={repository.id} value={repository.id.toString()}> {repositories?.map((repository) => (
{repository.full_name} <SelectItem key={repository.id} value={repository.id.toString()}>
</SelectItem> {repository.full_name}
))} </SelectItem>
</SelectGroup> ))}
</SelectContent> </SelectGroup>
</Select> </SelectContent>
</Select>
</div>
)}
{newApplication.serviceProvider == 'github' && newApplication.git.repositoryId != 0 && (
<>
<div>
<p className="text-muted-foreground text-sm">Branch</p>
<Select onValueChange={(e) => setNewApplication((prev) => ({ ...prev, git: { ...prev.git, branch: e } }))} defaultValue={newApplication.git.branch}>
<SelectTrigger>
<SelectValue placeholder="Select a branch" />
</SelectTrigger>
<SelectContent>
{branches.map((branch) => (
<SelectItem key={branch} value={branch}>
{branch}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div>
<p className="text-muted-foreground text-sm">Path</p>
<Input placeholder="Path" value={newApplication.git.path} onChange={(e) => setNewApplication((prev) => ({ ...prev, git: { ...prev.git, path: e.target.value } }))} />
</div>
<div>
<div className={cn('flex flex-row items-center gap-4 border-[1px] rounded-lg py-2 px-4', newApplication.autodeploy ? 'border-[#3A7BFE]' : 'border-gray-300')} onClick={() => setNewApplication((prev) => ({ ...prev, autodeploy: !prev.autodeploy }))}>
<Checkbox
checked={newApplication.autodeploy}
onChange={(e) => {
console.log(e);
}}
id={'autodeploy'}
color="bg-[#3A7BFE]"
/>
<div>
<label htmlFor="autodeploy" className="text-sm">
Auto Deploy
</label>
<p className="text-xs text-muted-foreground">Every time an update is made to this branch, your application will be re-deployed.</p>
</div>
</div>
</div>
</>
)} )}
</div> </div>
{(serviceProvider == 'github' || serviceProvider == 'github-registry') && ( {newApplication.serviceProvider == 'github' &&
<p className="text-sm"> (!repositories ? (
<span className="text-muted-foreground">Not seeing the repositories you expected here?</span> <Link href={ServiceProviderList.find((s) => s.value == serviceProvider)?.permission || ''}>Edit Your GitHub Permissions</Link> <p className="text-sm">
</p> <span className="text-muted-foreground">We don't have access to your repositories.</span> <Link href={ServiceProviderList.find((s) => s.value == newApplication.serviceProvider)?.permission || ''}>Link your GitHub account</Link>
)} </p>
) : (
<p className="text-sm">
<span className="text-muted-foreground">Not seeing the repositories you expected here?</span> <Link href={ServiceProviderList.find((s) => s.value == newApplication.serviceProvider)?.permission || ''}>Edit Your GitHub Permissions</Link>
</p>
))}
</div>
)}
{steps == 2 && (
<div>
<p>Resources WIP</p>
<p>For now 1 vCPU & 1024Mb RAM</p>
</div>
)}
{steps == 3 && (
<div>
<p>Environment Variables WIP</p>
</div>
)}
{steps == 4 && (
<div>
<p className="text-muted-foreground text-sm">Application Name</p>
<Input placeholder="App Name" value={newApplication.name} onChange={(e) => setNewApplication((prev) => ({ ...prev, name: e.target.value }))} />
</div>
)}
{steps == 5 && (
<div>
<div className="flex flex-col gap-2">
<div>
<p className="text-muted-foreground text-sm">Service Provider</p>
<p>{newApplication.serviceProvider}</p>
</div>
<div>
<p className="text-muted-foreground text-sm">Repository</p>
<p>{newApplication.git.repositoryName}</p>
</div>
<div>
<p className="text-muted-foreground text-sm">Branch</p>
<p>{newApplication.git.branch}</p>
</div>
<div>
<p className="text-muted-foreground text-sm">Path</p>
<p>{newApplication.git.path}</p>
</div>
<div>
<p className="text-muted-foreground text-sm">Auto Deploy</p>
<p>{newApplication.autodeploy ? 'Enabled' : 'Disabled'}</p>
</div>
<div>
<p className="text-muted-foreground text-sm">Application Name</p>
<p>{newApplication.name}</p>
</div>
</div>
</div> </div>
)} )}
</div> </div>
@ -144,15 +253,40 @@ export default function CreateApplicationForm() {
> >
Back Back
</Button> </Button>
<Button {steps == 5 ? (
className="bg-[#3A7BFE]" <Button
onClick={() => { className="bg-[#3A7BFE]"
setSteps((prev) => (prev + 1 > 5 ? 5 : prev + 1)); onClick={async () => {
console.log(newApplication); const app = await createApplication(newApplication, params.workspace as string);
}} console.log(app);
> setOpen(false);
{steps == 5 ? 'Deploy' : 'Next'} setNewApplication({
</Button> serviceProvider: 'github',
git: {
repositoryId: 0,
repositoryName: '',
branch: '',
path: '',
},
autodeploy: true,
env: {},
name: '',
});
}}
>
Deploy
</Button>
) : (
<Button
className="bg-[#3A7BFE]"
onClick={() => {
setSteps((prev) => (prev + 1 > 5 ? 5 : prev + 1));
console.log(newApplication);
}}
>
Next
</Button>
)}
</div> </div>
</div> </div>
</SheetContent> </SheetContent>

View File

@ -0,0 +1,18 @@
'use client';
import * as React from 'react';
import * as CheckboxPrimitive from '@radix-ui/react-checkbox';
import { Check } from 'lucide-react';
import { cn } from '@/lib/utils';
const Checkbox = React.forwardRef<React.ElementRef<typeof CheckboxPrimitive.Root>, React.ComponentPropsWithoutRef<typeof CheckboxPrimitive.Root>>(({ className, ...props }, ref) => (
<CheckboxPrimitive.Root ref={ref} className={cn('peer h-4 w-4 shrink-0 rounded-sm border border-primary ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground', className)} {...props}>
<CheckboxPrimitive.Indicator className={cn('flex items-center justify-center text-current')}>
<Check className="h-4 w-4" />
</CheckboxPrimitive.Indicator>
</CheckboxPrimitive.Root>
));
Checkbox.displayName = CheckboxPrimitive.Root.displayName;
export { Checkbox };

25
components/ui/input.tsx Normal file
View File

@ -0,0 +1,25 @@
import * as React from "react"
import { cn } from "@/lib/utils"
export interface InputProps
extends React.InputHTMLAttributes<HTMLInputElement> {}
const Input = React.forwardRef<HTMLInputElement, InputProps>(
({ className, type, ...props }, ref) => {
return (
<input
type={type}
className={cn(
"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
className
)}
ref={ref}
{...props}
/>
)
}
)
Input.displayName = "Input"
export { Input }