shared with me page logic

This commit is contained in:
Ishaan Dey 2024-05-01 02:49:25 -04:00
parent d3698b9a50
commit 1066638e92
4 changed files with 62 additions and 16 deletions

View File

@ -93,7 +93,30 @@ export default {
return methodNotAllowed; return methodNotAllowed;
} }
} else if (path === "/api/sandbox/share") { } else if (path === "/api/sandbox/share") {
if (method === "POST") { if (method === "GET") {
const params = url.searchParams;
if (params.has("id")) {
const id = params.get("id") as string;
const res = await db.query.usersToSandboxes.findMany({
where: (uts, { eq }) => eq(uts.userId, id),
});
const owners = await Promise.all(
res.map(async (r) => {
const sb = await db.query.sandbox.findFirst({
where: (sandbox, { eq }) => eq(sandbox.id, r.sandboxId),
with: {
author: true,
},
});
if (!sb) return;
return { id: sb.id, name: sb.name, type: sb.type, author: sb.author.name, sharedOn: Date.now() };
})
);
return json(owners ?? {});
} else return invalidRequest;
} else if (method === "POST") {
const shareSchema = z.object({ const shareSchema = z.object({
sandboxId: z.string(), sandboxId: z.string(),
email: z.string(), email: z.string(),

View File

@ -14,10 +14,21 @@ export default async function DashboardPage() {
const userRes = await fetch(`http://localhost:8787/api/user?id=${user.id}`) const userRes = await fetch(`http://localhost:8787/api/user?id=${user.id}`)
const userData = (await userRes.json()) as User const userData = (await userRes.json()) as User
const sharedRes = await fetch(
`http://localhost:8787/api/sandbox/share?id=${user.id}`
)
const shared = (await sharedRes.json()) as {
id: string
name: string
type: "react" | "node"
author: string
sharedOn: Date
}[]
return ( return (
<div className="w-screen h-screen flex flex-col overflow-hidden overscroll-none"> <div className="w-screen h-screen flex flex-col overflow-hidden overscroll-none">
<Navbar userData={userData} /> <Navbar userData={userData} />
<Dashboard sandboxes={userData.sandbox} /> <Dashboard sandboxes={userData.sandbox} shared={shared} />
</div> </div>
) )
} }

View File

@ -3,28 +3,34 @@
import CustomButton from "@/components/ui/customButton" import CustomButton from "@/components/ui/customButton"
import { Button } from "@/components/ui/button" import { Button } from "@/components/ui/button"
import { import {
Clock,
Code2, Code2,
Ellipsis,
FolderDot, FolderDot,
Globe,
HelpCircle, HelpCircle,
Plus, Plus,
Settings, Settings,
Users, Users,
} from "lucide-react" } from "lucide-react"
import { useState } from "react" import { useState } from "react"
import ProjectCard from "./projectCard"
import { Sandbox } from "@/lib/types" import { Sandbox } from "@/lib/types"
import Image from "next/image"
import ProjectCardDropdown from "./projectCard/dropdown"
import DashboardProjects from "./projects" import DashboardProjects from "./projects"
import DashboardSharedWithMe from "./shared" import DashboardSharedWithMe from "./shared"
import NewProjectModal from "./newProject" import NewProjectModal from "./newProject"
type TScreen = "projects" | "shared" | "settings" | "search" type TScreen = "projects" | "shared" | "settings" | "search"
export default function Dashboard({ sandboxes }: { sandboxes: Sandbox[] }) { export default function Dashboard({
sandboxes,
shared,
}: {
sandboxes: Sandbox[]
shared: {
id: string
name: string
type: "react" | "node"
author: string
sharedOn: Date
}[]
}) {
const [screen, setScreen] = useState<TScreen>("projects") const [screen, setScreen] = useState<TScreen>("projects")
const [newProjectModalOpen, setNewProjectModalOpen] = useState(false) const [newProjectModalOpen, setNewProjectModalOpen] = useState(false)
@ -95,7 +101,7 @@ export default function Dashboard({ sandboxes }: { sandboxes: Sandbox[] }) {
{screen === "projects" ? ( {screen === "projects" ? (
<DashboardProjects sandboxes={sandboxes} /> <DashboardProjects sandboxes={sandboxes} />
) : screen === "shared" ? ( ) : screen === "shared" ? (
<DashboardSharedWithMe sandboxes={sandboxes} /> <DashboardSharedWithMe shared={shared} />
) : screen === "settings" ? null : null} ) : screen === "settings" ? null : null}
</div> </div>
</> </>

View File

@ -14,9 +14,15 @@ import { ChevronRight } from "lucide-react"
import Avatar from "../ui/avatar" import Avatar from "../ui/avatar"
export default function DashboardSharedWithMe({ export default function DashboardSharedWithMe({
sandboxes, shared,
}: { }: {
sandboxes: Sandbox[] shared: {
id: string
name: string
type: "react" | "node"
author: string
sharedOn: Date
}[]
}) { }) {
return ( return (
<div className="grow p-4 flex flex-col"> <div className="grow p-4 flex flex-col">
@ -32,7 +38,7 @@ export default function DashboardSharedWithMe({
</TableRow> </TableRow>
</TableHeader> </TableHeader>
<TableBody> <TableBody>
{sandboxes.map((sandbox) => ( {shared.map((sandbox) => (
<TableRow> <TableRow>
<TableCell> <TableCell>
<div className="font-medium flex items-center"> <div className="font-medium flex items-center">
@ -52,11 +58,11 @@ export default function DashboardSharedWithMe({
</TableCell> </TableCell>
<TableCell> <TableCell>
<div className="flex items-center"> <div className="flex items-center">
<Avatar name="Ishaan Dey" className="mr-2" /> <Avatar name={sandbox.author} className="mr-2" />
Ishaan Dey {sandbox.author}
</div> </div>
</TableCell> </TableCell>
<TableCell>{new Date().toLocaleDateString()}</TableCell> <TableCell>{sandbox.sharedOn.toLocaleDateString()}</TableCell>
<TableCell className="text-right"> <TableCell className="text-right">
<Button> <Button>
Open <ChevronRight className="w-4 h-4 ml-2" /> Open <ChevronRight className="w-4 h-4 ml-2" />