Skip to content

Commit

Permalink
feat: project selection
Browse files Browse the repository at this point in the history
  • Loading branch information
yongenaelf committed Sep 9, 2024
1 parent c5bdde5 commit 5651a98
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
9 changes: 5 additions & 4 deletions components/github/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import { useState } from "react";
import { RepoUrlForm } from "./repo-url-form";
import { RepoSelectFiles } from "./repo-select-files";
import { X } from "lucide-react";
import { RepoBranchSelection } from "./repo-branch-selection";
import { RepoWorkspaceName } from "./repo-workspace-name";
import ProjectSelection from "./project-selection";

export default function GitHub() {
const [repo, setRepo] = useState<string>();
Expand Down Expand Up @@ -58,10 +58,10 @@ export default function GitHub() {
/>
)}
{branch && selected.length === 0 ? (
<RepoSelectFiles
<ProjectSelection
repo={repo}
branch={branch}
onSubmit={async (paths) => {
onSelect={async (paths) => {
setSelected(paths);
}}
/>
Expand All @@ -70,7 +70,8 @@ export default function GitHub() {
) : null}
{selected.length > 0 && repo && branch ? (
<div>
{selected.length} files or folders selected.{" "}
You have selected <b>{selected.find((i) => i.endsWith(".csproj"))}</b>
.
<button
className="text-red-600 hover:font-bold"
onClick={() => {
Expand Down
54 changes: 54 additions & 0 deletions components/github/project-selection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React, { useEffect, useMemo } from "react";
import { useRepoTree } from "./use-octokit";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";

function ProjectSelection({
repo,
branch,
onSelect,
}: {
repo?: string;
branch?: string;
onSelect?: (val: string[]) => void;
}) {
const { data: repoData, isLoading } = useRepoTree(repo, branch);

const data = useMemo(() => {
if (!!repoData) {
return repoData.filter(
(i) => i.path?.endsWith(".csproj") && !i.path.endsWith(".Tests.csproj")
);
}

return undefined;
}, [repoData]);

const _onSelect = (val: string) => {
const parentFolder = val.split("/").slice(0, -2).join("/");

if (!repoData) return;

onSelect?.(repoData.filter(i => parentFolder ? i.path?.startsWith(parentFolder + "/") : true).map(i => i.path!));
}

if (isLoading || !data) return <div>Loading...</div>;

return (
<Select onValueChange={e => _onSelect(e)}>
<SelectTrigger>
<SelectValue placeholder="Select project" />
</SelectTrigger>
<SelectContent>
{data.map(i => <SelectItem key={i.path} value={i.path || ''}>{i.path}</SelectItem>)}
</SelectContent>
</Select>
);
}

export default ProjectSelection;
14 changes: 12 additions & 2 deletions components/github/repo-url-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { Input } from "@/components/ui/input";
import { Loader2 } from "lucide-react";
import gh from "parse-github-url";
import { getRepoInfoSchema } from "./schema";

const FormSchema = z.object({
url: z.string().refine((arg) => {
Expand All @@ -39,8 +40,17 @@ export function RepoUrlForm({
async function onSubmit(data: z.infer<typeof FormSchema>) {
form.clearErrors();
if (!!_onSubmit) {
const { repo, branch } = gh(data.url) || {};
await _onSubmit({ repo: repo || undefined, branch: branch || undefined });
const { owner, repo } = gh(data.url) || {};

const [o, r] = repo?.split("/") || [];

const res = await fetch(`/api/get-repo-info?owner=${owner}&repo=${r}`);

const _data = await res.json();

const {default_branch} = getRepoInfoSchema.parse(_data);

await _onSubmit({ repo: repo || undefined, branch: default_branch || undefined });
}
}

Expand Down
6 changes: 4 additions & 2 deletions components/github/repo-workspace-name.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function RepoWorkspaceName({
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
defaultValues: {
name: "",
name: paths.find(i => i.endsWith(".csproj"))?.split("/").pop()?.replace(".csproj", ""),
template: `github.com/${repo}/tree/${branch}`,
},
});
Expand Down Expand Up @@ -79,9 +79,11 @@ export function RepoWorkspaceName({

const parsedData = getRepoBlobsSchema.parse(fileData);

const rootPath = parsedData.find(i => i.path.endsWith(".csproj"))?.path.split("/").slice(0, -2).join("/")

await db.files.bulkAdd(
parsedData.map(({ path, contents }) => ({
path: `/workspace/${data.name}/${encodeURIComponent(path)}`,
path: `/workspace/${data.name}/${encodeURIComponent(rootPath ? path.replace(rootPath + "/", "") : path)}`,
contents,
}))
);
Expand Down

0 comments on commit 5651a98

Please sign in to comment.