-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.tsx
More file actions
59 lines (47 loc) · 1.45 KB
/
page.tsx
File metadata and controls
59 lines (47 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { eq } from "drizzle-orm";
import { z } from "zod";
import { db } from "~/server/db";
import {
files as fileSchema,
folders as folderSchema,
} from "~/server/db/schema";
import DriveContents from "../../drive-contents";
async function getAllParents(folderId: number) {
const parents = [];
let currentId: number | null = folderId;
while (currentId != null) {
const folders = await db
.selectDistinct()
.from(folderSchema)
.where(eq(folderSchema.id, currentId));
if (!folders[0]) throw new Error("parent folder not found");
parents.unshift(folders[0]);
currentId = folders[0]?.parent; // parent can be null
}
return parents;
}
export default async function GoogleDriveClone(props: {
params: Promise<{ folderId: number }>;
}) {
const params = await props.params;
const { data, success } = z
.object({ folderId: z.coerce.number() })
.safeParse(params);
if (!success) return <div>Invalid Folder ID</div>;
const folderId = data.folderId;
const parentsPromise = getAllParents(folderId);
const foldersPromise = db
.select()
.from(folderSchema)
.where(eq(folderSchema.parent, folderId));
const filesPromise = db
.select()
.from(fileSchema)
.where(eq(fileSchema.parent, folderId));
const [folders, files, parents] = await Promise.all([
foldersPromise,
filesPromise,
parentsPromise,
]);
return <DriveContents folders={folders} files={files} parents={parents} />;
}