-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Add project creation UI with configuration support #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
6
commits into
yerek
Choose a base branch
from
copilot/create-new-projects-feature
base: yerek
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+11,693
−3
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0892d05
Initial plan
Copilot 479f812
Initial commit - understanding repository structure
Copilot 0e801cf
Add project creation feature with UI and API integration
Copilot 055ae7c
Add Textarea component for project description
Copilot 2835c28
Add documentation comment to createProject component
Copilot 2726407
Move create project button to dropdown and improve config rendering
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,308 @@ | ||
| "use client"; | ||
|
|
||
| // Component for creating new Incus projects | ||
| import { Button } from "@/app/_components/ui/button"; | ||
| import { | ||
| Dialog, | ||
| DialogContent, | ||
| DialogDescription, | ||
| DialogFooter, | ||
| DialogHeader, | ||
| DialogTitle, | ||
| } from "@/app/_components/ui/dialog"; | ||
| import { | ||
| Form, | ||
| FormControl, | ||
| FormDescription, | ||
| FormField, | ||
| FormItem, | ||
| FormLabel, | ||
| FormMessage, | ||
| } from "@/app/_components/ui/form"; | ||
| import { Input } from "@/app/_components/ui/input"; | ||
| import { useState, use } from "react"; | ||
| import { useForm } from "react-hook-form"; | ||
| import { zodResolver } from "@hookform/resolvers/zod"; | ||
| import z from "zod"; | ||
| import { createProject } from "@/app/(main)/_lib/projects"; | ||
| import { mutate } from "swr"; | ||
| import { toast } from "sonner"; | ||
| import { Textarea } from "@/app/_components/textarea"; | ||
| import { useConfigurableOptions } from "@/app/_hooks/server"; | ||
| import { | ||
| Accordion, | ||
| AccordionContent, | ||
| AccordionItem, | ||
| AccordionTrigger, | ||
| } from "@/app/_components/ui/accordion"; | ||
| import ProjectsContext from "@/app/(main)/_context/projects"; | ||
| import { Checkbox } from "@/app/_components/ui/checkbox"; | ||
|
|
||
| const formSchema = z.object({ | ||
| name: z | ||
| .string() | ||
| .min(1, "Project name is required") | ||
| .max(63, "Project name must be at most 63 characters long") | ||
| .regex( | ||
| /^[a-z][a-z0-9-]*[a-z0-9]$/, | ||
| "Project name must start with a lowercase letter and can only contain lowercase letters, numbers, and dashes. It cannot end with a dash." | ||
| ), | ||
| description: z.string().optional(), | ||
| config: z.record(z.string()).optional(), | ||
| }); | ||
|
|
||
| interface ProjectConfigField { | ||
| key: string; | ||
| type?: string; | ||
| shortdesc?: string; | ||
| longdesc?: string; | ||
| defaultdesc?: string; | ||
| initialvaluedesc?: string; | ||
| } | ||
|
|
||
| interface ConfigCategory { | ||
| keys: Array<Record<string, ProjectConfigField>>; | ||
| } | ||
|
|
||
| interface ProjectConfigOptions { | ||
| [category: string]: ConfigCategory; | ||
| } | ||
|
|
||
| interface CreateProjectProps { | ||
| open: boolean; | ||
| onOpenChange: (open: boolean) => void; | ||
| } | ||
|
|
||
| export default function CreateProject({ open, onOpenChange }: CreateProjectProps) { | ||
| const [isSubmitting, setIsSubmitting] = useState(false); | ||
| const { data: configurableOptions } = useConfigurableOptions(); | ||
| const { setProject } = use(ProjectsContext); | ||
|
|
||
| const form = useForm<z.infer<typeof formSchema>>({ | ||
| resolver: zodResolver(formSchema), | ||
| defaultValues: { | ||
| name: "", | ||
| description: "", | ||
| config: {}, | ||
| }, | ||
| }); | ||
|
|
||
| // Parse project configuration options from metadata | ||
| const projectConfigOptions: Array<{ category: string; fields: Array<{ name: string; field: ProjectConfigField }> }> = []; | ||
|
|
||
| if (configurableOptions?.configs?.project) { | ||
| const projectConfig = configurableOptions.configs.project as ProjectConfigOptions; | ||
|
|
||
| Object.entries(projectConfig).forEach(([category, categoryData]) => { | ||
| if (categoryData.keys && Array.isArray(categoryData.keys)) { | ||
| const fields = categoryData.keys.map((keyObj) => { | ||
| const [name, field] = Object.entries(keyObj)[0]; | ||
| return { name, field }; | ||
| }); | ||
| projectConfigOptions.push({ category, fields }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| const onSubmit = async (values: z.infer<typeof formSchema>) => { | ||
| try { | ||
| setIsSubmitting(true); | ||
|
|
||
| // Filter out empty config values | ||
| const config = Object.entries(values.config || {}).reduce( | ||
| (acc, [key, value]) => { | ||
| if (value && value.trim() !== "") { | ||
| acc[key] = value; | ||
| } | ||
| return acc; | ||
| }, | ||
| {} as Record<string, string> | ||
| ); | ||
|
|
||
| await createProject({ | ||
| name: values.name, | ||
| description: values.description, | ||
| config: Object.keys(config).length > 0 ? config : undefined, | ||
| }); | ||
|
|
||
| // Invalidate the projects cache to refresh the list | ||
| await mutate("/1.0/projects?recursion=1"); | ||
|
|
||
| // Set the newly created project as the current project | ||
| setProject(values.name); | ||
|
|
||
| onOpenChange(false); | ||
| form.reset(); | ||
| } catch (error) { | ||
| console.error("Failed to create project:", error); | ||
| toast.error("Failed to create project", { | ||
| description: | ||
| error instanceof Error ? error.message : "An unknown error occurred", | ||
| }); | ||
| } finally { | ||
| setIsSubmitting(false); | ||
| } | ||
| }; | ||
|
|
||
| const renderConfigField = ( | ||
| name: string, | ||
| field: ProjectConfigField, | ||
| formField: { | ||
| value: string; | ||
| onChange: (value: string) => void; | ||
| onBlur: () => void; | ||
| name: string; | ||
| ref: React.Ref<HTMLInputElement>; | ||
| } | ||
| ) => { | ||
| const defaultDesc = field.defaultdesc || field.initialvaluedesc || ""; | ||
|
|
||
| if (field.type === "bool") { | ||
| return ( | ||
| <div className="flex items-center space-x-2"> | ||
| <Checkbox | ||
| id={name} | ||
| checked={formField.value === "true"} | ||
| onCheckedChange={(checked) => { | ||
| formField.onChange(checked ? "true" : "false"); | ||
| }} | ||
| disabled={isSubmitting} | ||
| /> | ||
| <label | ||
| htmlFor={name} | ||
| className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70" | ||
| > | ||
| {name} | ||
| </label> | ||
| </div> | ||
| ); | ||
| } else if (field.type === "integer") { | ||
| return ( | ||
| <Input | ||
| type="number" | ||
| placeholder={defaultDesc} | ||
| {...formField} | ||
| disabled={isSubmitting} | ||
| /> | ||
| ); | ||
| } else { | ||
| return ( | ||
| <Input | ||
| placeholder={defaultDesc} | ||
| {...formField} | ||
| disabled={isSubmitting} | ||
| /> | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <Dialog open={open} onOpenChange={onOpenChange}> | ||
| <DialogContent className="max-h-[90vh] overflow-y-auto"> | ||
| <DialogHeader> | ||
| <DialogTitle>Create Project</DialogTitle> | ||
| <DialogDescription> | ||
| Create a new project with optional configuration | ||
| </DialogDescription> | ||
| </DialogHeader> | ||
| <Form {...form}> | ||
| <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> | ||
| <FormField | ||
| control={form.control} | ||
| name="name" | ||
| render={({ field }) => ( | ||
| <FormItem> | ||
| <FormLabel>Name</FormLabel> | ||
| <FormControl> | ||
| <Input | ||
| placeholder="my-project" | ||
| {...field} | ||
| disabled={isSubmitting} | ||
| /> | ||
| </FormControl> | ||
| <FormDescription> | ||
| The name must start with a lowercase letter and contain only | ||
| lowercase letters, numbers, and dashes. | ||
| </FormDescription> | ||
| <FormMessage /> | ||
| </FormItem> | ||
| )} | ||
| /> | ||
| <FormField | ||
| control={form.control} | ||
| name="description" | ||
| render={({ field }) => ( | ||
| <FormItem> | ||
| <FormLabel>Description (Optional)</FormLabel> | ||
| <FormControl> | ||
| <Textarea | ||
| placeholder="Project description" | ||
| {...field} | ||
| disabled={isSubmitting} | ||
| /> | ||
| </FormControl> | ||
| <FormMessage /> | ||
| </FormItem> | ||
| )} | ||
| /> | ||
|
|
||
| {projectConfigOptions.length > 0 && ( | ||
| <Accordion type="multiple" className="w-full"> | ||
| {projectConfigOptions.map(({ category, fields }) => ( | ||
| <AccordionItem key={category} value={category}> | ||
| <AccordionTrigger className="capitalize"> | ||
| {category} | ||
| </AccordionTrigger> | ||
| <AccordionContent> | ||
| <div className="space-y-4 pt-4"> | ||
| {fields.map(({ name, field }) => ( | ||
| <FormField | ||
| key={name} | ||
| control={form.control} | ||
| name={`config.${name}`} | ||
| render={({ field: formField }) => ( | ||
| <FormItem> | ||
| {field.type !== "bool" && ( | ||
| <FormLabel className="text-xs"> | ||
| {name} | ||
| </FormLabel> | ||
| )} | ||
| <FormControl> | ||
| {renderConfigField(name, field, formField)} | ||
| </FormControl> | ||
| {(field.longdesc || field.shortdesc) && ( | ||
| <FormDescription className="text-xs"> | ||
| {field.shortdesc} | ||
| </FormDescription> | ||
| )} | ||
| <FormMessage /> | ||
| </FormItem> | ||
| )} | ||
| /> | ||
| ))} | ||
| </div> | ||
| </AccordionContent> | ||
| </AccordionItem> | ||
| ))} | ||
| </Accordion> | ||
| )} | ||
|
|
||
| <DialogFooter> | ||
| <Button | ||
| type="button" | ||
| variant="outline" | ||
| onClick={() => onOpenChange(false)} | ||
| disabled={isSubmitting} | ||
| > | ||
| Cancel | ||
| </Button> | ||
| <Button type="submit" disabled={isSubmitting}> | ||
| {isSubmitting ? "Creating..." : "Create Project"} | ||
| </Button> | ||
| </DialogFooter> | ||
| </form> | ||
| </Form> | ||
| </DialogContent> | ||
| </Dialog> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,36 @@ | ||
| import { jsonFetcher } from "../../_lib/fetcher"; | ||
| import type { ProjectsMetadata } from "./projects.d"; | ||
| import type { Project, ProjectsMetadata } from "./projects.d"; | ||
|
|
||
| export async function getProjects() { | ||
| return jsonFetcher("/1.0/projects?recursion=1").then( | ||
| (data) => data.metadata as ProjectsMetadata | ||
| ); | ||
| } | ||
|
|
||
| export interface CreateProjectRequest { | ||
| name: string; | ||
| description?: string; | ||
| config?: Record<string, string>; | ||
| } | ||
|
|
||
| export async function createProject( | ||
| projectData: CreateProjectRequest | ||
| ): Promise<Project> { | ||
| const endpoint = new URL(window.location.origin + "/1.0/projects"); | ||
|
|
||
| const response = await fetch(endpoint.toString(), { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify(projectData), | ||
| }); | ||
|
|
||
| const data = await response.json(); | ||
|
|
||
| if (data.type === "error") { | ||
| throw new Error(data.error || "Failed to create project"); | ||
| } | ||
|
|
||
| return data.metadata as Project; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit 2726407:
type: "bool") render as checkboxestype: "integer") render as number inputskeysarrays containing config objects