Skip to content

Commit 6f1a24d

Browse files
committed
added create root folder if user has no drive
1 parent 14867ce commit 6f1a24d

7 files changed

Lines changed: 88 additions & 37 deletions

File tree

src/app/(home)/drive/page.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { auth } from "@clerk/nextjs/server"
2+
import { MUTATIONS, QUERIES } from "~/server/db/queries"
3+
import { redirect } from "next/navigation"
4+
import { Button } from "~/components/ui/button"
5+
import { root } from ".eslintrc.cjs"
6+
7+
export default async function DrivePage() {
8+
9+
const session = await auth()
10+
11+
if (!session.userId) {
12+
return redirect("/sign-in")
13+
}
14+
15+
const rootFolder = await QUERIES.getRootFolderForUser(session.userId)
16+
17+
if (!rootFolder) {
18+
return (
19+
<form action={async ()=>{
20+
"use server";
21+
const session = await auth()
22+
23+
if (!session.userId) {
24+
return redirect("/sign-in")
25+
}
26+
27+
const rootFolderId = await MUTATIONS.onboardUser(session.userId)
28+
29+
return redirect(`/f/${rootFolderId}`)
30+
}}>
31+
<Button>Create New Drive</Button>
32+
</form>
33+
)
34+
}
35+
36+
return redirect(`/f/${rootFolder.id}`)
37+
}

src/app/(home)/layout.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function HomePage(props: {children: React.ReactNode}) {
2+
return (
3+
<div className="flex min-h-screen flex-col items-center justify-center bg-gradient-to-br from-black via-neutral-900 to-neutral-800 p-4 text-white">
4+
<main className="text-center">{props.children}</main>
5+
</div>
6+
);
7+
}

src/app/(home)/page.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { Button } from "~/components/ui/button";
44

55
export default function HomePage() {
66
return (
7-
<div className="flex min-h-screen flex-col items-center justify-center bg-gradient-to-br from-black via-neutral-900 to-neutral-800 p-4 text-white">
8-
<main className="text-center">
7+
<>
98
<h1 className="mb-4 bg-gradient-to-r from-neutral-200 to-neutral-400 bg-clip-text text-5xl font-bold text-transparent md:text-6xl">
109
DriveV2
1110
</h1>
@@ -30,10 +29,10 @@ export default function HomePage() {
3029
Get Started
3130
</Button>
3231
</form>
33-
</main>
32+
3433
<footer className="mt-16 text-sm text-neutral-500">
3534
© {new Date().getFullYear()} DriveV2. All rights reserved.
3635
</footer>
37-
</div>
36+
</>
3837
);
3938
}

src/app/(home)/sign-in/page.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { SignInButton } from "@clerk/nextjs";
2+
export default function HomePage() {
3+
return (
4+
<>
5+
<SignInButton forceRedirectUrl={"/drive"} />
6+
<footer className="mt-16 text-sm text-neutral-500">
7+
© {new Date().getFullYear()} T3 Drive. All rights reserved.
8+
</footer>
9+
</>
10+
);
11+
}

src/app/drive/page.tsx

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/app/sign-in/page.tsx

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/server/db/queries.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import "server-only"
33
import { db } from "~/server/db"
44
import { files_table as filesSchema, folders_table as foldersSchema } from "~/server/db/schema"
55
import { eq, and, isNull } from "drizzle-orm"
6+
import { root } from ".eslintrc.cjs"
67

78
export const QUERIES = {
89
getFiles : function(folderId: number) {
@@ -75,6 +76,35 @@ export const MUTATIONS = {
7576
ownerId: input.userId,
7677
})
7778

79+
},
80+
81+
onboardUser : async function(userId : string) {
82+
const rootFolder = await db.insert(foldersSchema).values({
83+
name: "Root",
84+
parent: null,
85+
ownerId: userId,
86+
}).$returningId()
87+
88+
const rootFolderId = rootFolder[0]!.id
89+
90+
await db.insert(foldersSchema).values([{
91+
name: "Trash",
92+
parent: rootFolderId,
93+
ownerId: userId
94+
},
95+
{
96+
name: "Shared",
97+
parent: rootFolderId,
98+
ownerId: userId,
99+
},
100+
{
101+
name: "Documents",
102+
parent: rootFolderId,
103+
ownerId: userId,
104+
}
105+
])
106+
107+
return rootFolderId
78108
}
79109
}
80110

0 commit comments

Comments
 (0)