92 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-10-21 13:57:45 -06:00
"use client"
2024-10-21 13:57:45 -06:00
import { deleteSandbox, updateSandbox } from "@/lib/actions"
import { Sandbox } from "@/lib/types"
2024-11-11 22:02:34 +01:00
import { useEffect, useMemo, useState } from "react"
2024-10-21 13:57:45 -06:00
import { toast } from "sonner"
import ProjectCard from "./projectCard"
2024-05-16 10:47:34 -07:00
2024-08-19 21:17:30 -04:00
const colors: { [key: string]: number[][] } = {
2024-05-16 10:47:34 -07:00
react: [
[71, 207, 237],
[30, 126, 148],
],
node: [
[86, 184, 72],
[59, 112, 52],
],
2024-10-21 13:57:45 -06:00
}
2024-04-18 14:42:47 -04:00
export default function DashboardProjects({
sandboxes,
2024-05-01 08:53:37 -04:00
q,
2024-04-18 14:42:47 -04:00
}: {
2024-10-21 13:57:45 -06:00
sandboxes: Sandbox[]
q: string | null
2024-04-18 14:42:47 -04:00
}) {
2024-10-21 13:57:45 -06:00
const [deletingId, setDeletingId] = useState<string>("")
2024-05-09 22:16:56 -07:00
2024-11-11 22:02:34 +01:00
const onVisibilityChange = useMemo(
() => async (sandbox: Pick<Sandbox, "id" | "name" | "visibility">) => {
const newVisibility =
sandbox.visibility === "public" ? "private" : "public"
toast(`Project ${sandbox.name} is now ${newVisibility}.`)
await updateSandbox({
id: sandbox.id,
visibility: newVisibility,
})
},
[]
)
const onDelete = useMemo(
() => async (sandbox: Pick<Sandbox, "id" | "name">) => {
setDeletingId(sandbox.id)
toast(`Project ${sandbox.name} deleted.`)
await deleteSandbox(sandbox.id)
},
[]
)
2024-05-16 21:45:19 -07:00
useEffect(() => {
if (deletingId) {
2024-10-21 13:57:45 -06:00
setDeletingId("")
2024-05-16 21:45:19 -07:00
}
2024-10-21 13:57:45 -06:00
}, [sandboxes])
2024-05-16 21:45:19 -07:00
2024-04-18 14:42:47 -04:00
return (
<div className="grow p-4 flex flex-col">
2024-05-01 08:53:37 -04:00
<div className="text-xl font-medium mb-8">
{q && q.length > 0 ? `Showing search results for: ${q}` : "My Projects"}
</div>
2024-04-27 16:22:35 -04:00
<div className="grow w-full ">
2024-05-07 21:19:32 -07:00
{sandboxes.length > 0 ? (
<div className="w-full grid lg:grid-cols-3 2xl:grid-cols-4 md:grid-cols-2 gap-4">
{sandboxes.map((sandbox) => {
if (q && q.length > 0) {
if (!sandbox.name.toLowerCase().includes(q.toLowerCase())) {
2024-10-21 13:57:45 -06:00
return null
2024-05-07 21:19:32 -07:00
}
2024-05-01 08:53:37 -04:00
}
2024-05-07 21:19:32 -07:00
return (
<ProjectCard
2024-05-07 21:19:32 -07:00
key={sandbox.id}
onVisibilityChange={onVisibilityChange}
onDelete={onDelete}
deletingId={deletingId}
isAuthenticated
{...sandbox}
/>
2024-10-21 13:57:45 -06:00
)
2024-05-07 21:19:32 -07:00
})}
</div>
) : (
<div className="text-muted-foreground text-sm">
You don't have any projects yet. Create one to get started!
</div>
)}
2024-04-18 14:42:47 -04:00
</div>
</div>
2024-10-21 13:57:45 -06:00
)
2024-04-18 14:42:47 -04:00
}