Skip to content

Commit

Permalink
feat: deploying project to s3 via ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Pulkitxm committed Oct 14, 2024
1 parent 6ddbd23 commit a534c11
Show file tree
Hide file tree
Showing 30 changed files with 5,835 additions and 262 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/node_modules
/node_modules
*test/
*dist/
*build/
54 changes: 54 additions & 0 deletions core/actions/db/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { prisma } from "@/db";
import { validateLogsZod } from "@/types/project";
import { getServerSession } from "next-auth";

export async function getUserIdByEmail(email: string) {
if (!email) return undefined;
Expand All @@ -17,3 +19,55 @@ export async function getUserIdByEmail(email: string) {
return undefined;
}
}

export async function getProjects() {
const session = await getServerSession();
if (!session) throw new Error("Session not found");

const userId = await getUserIdByEmail(session.user.email);
if (!userId) throw new Error("User not found");

const projects = await prisma.project.findMany({
where: {
userId,
},
select: {
id: true,
name: true,
repoName: true,
repoOwner: true,
branch: true,
updatedAt: true,
slug: true,
},
});

return projects;
}

export async function getProjectDetails(projectId: string) {
const session = await getServerSession();
if (!session) throw new Error("Session not found");

const userId = await getUserIdByEmail(session.user.email);
if (!userId) throw new Error("User not found");

const project = await prisma.project.findFirst({
where: {
userId,
id: projectId,
},
});
if (!project) return null;

const validateLogs = validateLogsZod.safeParse(project.logs);
if (validateLogs.success)
return {
...project,
logs: validateLogs.data,
};
return {
...project,
logs: [],
};
}
33 changes: 17 additions & 16 deletions core/actions/gh/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,6 @@ export async function importProject(importProject: ImportProjectType) {
const accessToken = await getAccessTokenByEmail(session.user.email);
const slug = generateSlug();

if (redis) {
console.log({
...importProject,
GITHUB_TOKEN: accessToken,
});
const res = await redis.lpush(
"project_import_queue",
JSON.stringify({
...importProject,
GITHUB_TOKEN: accessToken,
projectSlug: slug,
}),
);
console.log(res);
}

const user = await prisma.user.findUnique({
where: {
email: session.user.email,
Expand Down Expand Up @@ -78,7 +62,24 @@ export async function importProject(importProject: ImportProjectType) {
}),
);

if (redis) {
console.log({
...importProject,
GITHUB_TOKEN: accessToken,
});
await redis.lpush(
"project_import_queue",
JSON.stringify({
...importProject,
GITHUB_TOKEN: accessToken,
projectSlug: slug,
dbId: newProject.id,
}),
);
}

return {
success: true,
id: newProject.id,
};
}
43 changes: 0 additions & 43 deletions core/actions/user/project.ts

This file was deleted.

17 changes: 15 additions & 2 deletions core/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getProjects } from "@/actions/user/project";
import { getProjects } from "@/actions/db/user";
import New from "@/app/new/page";
import Projects from "@/components/Dashboard/Projects";
import ProtectRouteUI from "@/components/ProtectRoute";
Expand All @@ -12,7 +12,20 @@ export default async function Dashboard() {
<ProtectRouteUI />
</div>
);
const projects = await getProjects();

let projects: Awaited<ReturnType<typeof getProjects>> = [];
try {
projects = await getProjects();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
if (e.message === "User not found") {
return (
<div className="flex h-screen w-screen items-center justify-center">
<ProtectRouteUI />
</div>
);
}
}

if (projects.length === 0) return <New />;

Expand Down
24 changes: 24 additions & 0 deletions core/app/import/ImportButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use client";

import { Button } from "@/components/ui/button";
import { importProject } from "@/actions/gh/import";
import { ImportProjectType } from "@/types/project";
import { useRouter } from "next/navigation";

export default function ImportButton({
importProjectData,
}: {
importProjectData: ImportProjectType;
}) {
const router = useRouter();
async function handleImportProject() {
const { id } = await importProject(importProjectData);
if (id) router.push(`/project/${id}`);
}
return (
<Button className="w-full" onClick={handleImportProject}>
Deploy
</Button>
);
}

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { getProjectDetails } from "@/actions/user/project";
import { getProjectDetails } from "@/actions/db/user";
import ErroDiv from "@/components/ui/ErrorDiv";
import ProtectRouteUI from "@/components/ProtectRoute";
import { getServerSession } from "next-auth";
import ProjectDetails from "@/components/ProjectDetails";

export default async function ProjectDetailsPage({
projectName,
projectId,
}: {
projectName: string;
projectId: string;
}) {
const session = await getServerSession();
if (!session)
Expand All @@ -17,7 +17,18 @@ export default async function ProjectDetailsPage({
</div>
);

const project = await getProjectDetails(projectName);
let project: Awaited<ReturnType<typeof getProjectDetails>> = null;
try {
project = await getProjectDetails(projectId);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
console.log(e);
return (
<div className="flex h-screen w-screen items-center justify-center">
<ProtectRouteUI />
</div>
);
}

if (!project) {
return (
Expand Down
6 changes: 3 additions & 3 deletions core/components/Dashboard/Project.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "@/components/ui/card";
import Link from "next/link";
import { Button } from "@/components/ui/button";
import { getProjects } from "@/actions/user/project";
import { getProjects } from "@/actions/db/user";
import { formatTimeAgo } from "@/lib/time";
import { UpdateIcon } from "@radix-ui/react-icons";

Expand All @@ -26,7 +26,7 @@ export function Project({
<div className="flex items-center text-sm text-muted-foreground">
<GitFork className="mr-2 h-4 w-4" />
<Link
href={`https://github.com/${project.repoOwner}/${project.repoName}`}
href={`https://github.com/${project.repoOwner}/${project.name}`}
target="_blank"
rel="noopener noreferrer"
className="transition-colors hover:text-primary"
Expand Down Expand Up @@ -60,7 +60,7 @@ export function Project({
</CardContent>
<CardFooter>
<Button asChild variant="outline" className="w-full">
<Link href={`/project/${project.name}`}>View Project</Link>
<Link href={`/project/${project.id}`}>View Project</Link>
</Button>
</CardFooter>
</Card>
Expand Down
2 changes: 1 addition & 1 deletion core/components/Dashboard/Projects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Project } from "@/components/Dashboard/Project";
import { getProjects } from "@/actions/user/project";
import { getProjects } from "@/actions/db/user";
import { SORT_ORDER, VIEW_MODE } from "@/types/project";
import { localValues } from "../../lib/localValues";

Expand Down
5 changes: 0 additions & 5 deletions core/components/Dashboard/index.tsx

This file was deleted.

13 changes: 3 additions & 10 deletions core/components/ImportProject/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"use client";

import { Github } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import Link from "next/link";
import { ImportProjectType } from "@/types/project";
import ChooseDir from "./ChooseDir";
import BuilAndOutput from "./BuilAndOutput";
import EnvironmentVariable from "./EnvironmentVariable";
import ChooseBranch from "./ChooseBranch";
import { importProject } from "@/actions/gh/import";
import ImportButton from "@/app/import/ImportButton";

export default function ImportProject({
importProjectData,
Expand All @@ -18,11 +17,6 @@ export default function ImportProject({
importProjectData: ImportProjectType;
setImportProjectData: React.Dispatch<React.SetStateAction<ImportProjectType>>;
}) {
async function handleImportProject() {
importProject(importProjectData);
console.log(importProjectData);
}

return (
<div className="flex h-full w-full flex-col items-center overflow-y-auto overflow-x-hidden bg-background pt-40 text-foreground">
<main className="mx-auto max-w-6xl p-8 xl:w-[900px]">
Expand Down Expand Up @@ -105,9 +99,8 @@ export default function ImportProject({
importProjectData={importProjectData}
setImportProjectData={setImportProjectData}
/>
<Button className="w-full" onClick={handleImportProject}>
Deploy
</Button>

<ImportButton importProjectData={importProjectData} />
</div>
</div>
</div>
Expand Down
Loading

0 comments on commit a534c11

Please sign in to comment.