|
| 1 | +import { Component } from "solid-js" |
| 2 | +import { createStore } from "solid-js/store" |
| 3 | +import { useDialog } from "@opencode-ai/ui/context/dialog" |
| 4 | +import { useGlobalSDK } from "@/context/global-sdk" |
| 5 | +import { useLayout } from "@/context/layout" |
| 6 | +import { Dialog } from "@opencode-ai/ui/dialog" |
| 7 | +import { TextField } from "@opencode-ai/ui/text-field" |
| 8 | +import { Button } from "@opencode-ai/ui/button" |
| 9 | +import { Spinner } from "@opencode-ai/ui/spinner" |
| 10 | +import { showToast } from "@opencode-ai/ui/toast" |
| 11 | +import { useNavigate } from "@solidjs/router" |
| 12 | +import { base64Encode } from "@opencode-ai/util/encode" |
| 13 | + |
| 14 | +export const DialogCreateProject: Component = () => { |
| 15 | + const dialog = useDialog() |
| 16 | + const globalSDK = useGlobalSDK() |
| 17 | + const layout = useLayout() |
| 18 | + const navigate = useNavigate() |
| 19 | + |
| 20 | + const [store, setStore] = createStore({ |
| 21 | + path: "", |
| 22 | + error: undefined as string | undefined, |
| 23 | + loading: false, |
| 24 | + }) |
| 25 | + |
| 26 | + function openProject(directory: string) { |
| 27 | + layout.projects.open(directory) |
| 28 | + navigate(`/${base64Encode(directory)}`) |
| 29 | + } |
| 30 | + |
| 31 | + async function handleSubmit(e: SubmitEvent) { |
| 32 | + e.preventDefault() |
| 33 | + |
| 34 | + const path = store.path?.trim() |
| 35 | + if (!path) { |
| 36 | + setStore("error", "Project path is required") |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + if (!path.startsWith("/") && !path.startsWith("~/")) { |
| 41 | + setStore("error", "Path must be absolute (start with / or ~)") |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + setStore("error", undefined) |
| 46 | + setStore("loading", true) |
| 47 | + |
| 48 | + try { |
| 49 | + const result = await globalSDK.client.project.create({ path }) |
| 50 | + |
| 51 | + if (result.error) { |
| 52 | + const errorMessage = (result.error as { message?: string }).message || "Failed to create project" |
| 53 | + setStore("error", errorMessage) |
| 54 | + setStore("loading", false) |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + const project = result.data! |
| 59 | + dialog.close() |
| 60 | + openProject(project.worktree) |
| 61 | + |
| 62 | + showToast({ |
| 63 | + variant: "success", |
| 64 | + icon: "circle-check", |
| 65 | + title: "Project created", |
| 66 | + description: `Created project at ${project.worktree}`, |
| 67 | + }) |
| 68 | + } catch (e: unknown) { |
| 69 | + const errorMessage = e instanceof Error ? e.message : "Failed to create project" |
| 70 | + setStore("error", errorMessage) |
| 71 | + setStore("loading", false) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return ( |
| 76 | + <Dialog title="Create New Project"> |
| 77 | + <form onSubmit={handleSubmit} class="flex flex-col gap-6 px-5 pb-6"> |
| 78 | + <div class="text-14-regular text-text-base"> |
| 79 | + Enter the full path where you want to create your new project. A new directory will be created and initialized |
| 80 | + as a git repository. |
| 81 | + </div> |
| 82 | + <TextField |
| 83 | + autofocus |
| 84 | + type="text" |
| 85 | + label="Project path" |
| 86 | + placeholder="~/projects/my-new-app" |
| 87 | + name="path" |
| 88 | + value={store.path} |
| 89 | + onChange={(value) => setStore("path", value)} |
| 90 | + validationState={store.error ? "invalid" : undefined} |
| 91 | + error={store.error} |
| 92 | + /> |
| 93 | + <div class="flex gap-3 justify-end"> |
| 94 | + <Button variant="ghost" onClick={() => dialog.close()} disabled={store.loading}> |
| 95 | + Cancel |
| 96 | + </Button> |
| 97 | + <Button type="submit" disabled={store.loading}> |
| 98 | + {store.loading ? ( |
| 99 | + <span class="flex items-center gap-2"> |
| 100 | + <Spinner class="size-4" /> |
| 101 | + Creating... |
| 102 | + </span> |
| 103 | + ) : ( |
| 104 | + "Create Project" |
| 105 | + )} |
| 106 | + </Button> |
| 107 | + </div> |
| 108 | + </form> |
| 109 | + </Dialog> |
| 110 | + ) |
| 111 | +} |
0 commit comments