add search projects functionality

This commit is contained in:
Ishaan Dey 2024-05-01 08:53:37 -04:00
parent a084ecd6c7
commit e6e5247a3d
2 changed files with 77 additions and 59 deletions

View File

@ -15,6 +15,8 @@ import { Sandbox } from "@/lib/types"
import DashboardProjects from "./projects" import DashboardProjects from "./projects"
import DashboardSharedWithMe from "./shared" import DashboardSharedWithMe from "./shared"
import NewProjectModal from "./newProject" import NewProjectModal from "./newProject"
import Link from "next/link"
import { useSearchParams } from "next/navigation"
type TScreen = "projects" | "shared" | "settings" | "search" type TScreen = "projects" | "shared" | "settings" | "search"
@ -40,6 +42,9 @@ export default function Dashboard({
else return "justify-start font-normal text-muted-foreground" else return "justify-start font-normal text-muted-foreground"
} }
const searchParams = useSearchParams()
const q = searchParams.get("q")
return ( return (
<> <>
<NewProjectModal <NewProjectModal
@ -72,16 +77,17 @@ export default function Dashboard({
<Users className="w-4 h-4 mr-2" /> <Users className="w-4 h-4 mr-2" />
Shared With Me Shared With Me
</Button> </Button>
<Button {/* <Button
variant="ghost" variant="ghost"
onClick={() => setScreen("settings")} onClick={() => setScreen("settings")}
className={activeScreen("settings")} className={activeScreen("settings")}
> >
<Settings className="w-4 h-4 mr-2" /> <Settings className="w-4 h-4 mr-2" />
Settings Settings
</Button> </Button> */}
</div> </div>
<div className="flex flex-col"> <div className="flex flex-col">
<Link href="https://github.com/ishaan1013/sandbox">
<Button <Button
variant="ghost" variant="ghost"
className="justify-start font-normal text-muted-foreground" className="justify-start font-normal text-muted-foreground"
@ -89,6 +95,7 @@ export default function Dashboard({
<Code2 className="w-4 h-4 mr-2" /> <Code2 className="w-4 h-4 mr-2" />
GitHub Repository GitHub Repository
</Button> </Button>
</Link>
<Button <Button
variant="ghost" variant="ghost"
className="justify-start font-normal text-muted-foreground" className="justify-start font-normal text-muted-foreground"
@ -99,7 +106,7 @@ export default function Dashboard({
</div> </div>
</div> </div>
{screen === "projects" ? ( {screen === "projects" ? (
<DashboardProjects sandboxes={sandboxes} /> <DashboardProjects sandboxes={sandboxes} q={q} />
) : screen === "shared" ? ( ) : screen === "shared" ? (
<DashboardSharedWithMe shared={shared} /> <DashboardSharedWithMe shared={shared} />
) : screen === "settings" ? null : null} ) : screen === "settings" ? null : null}

View File

@ -12,18 +12,20 @@ import { toast } from "sonner"
export default function DashboardProjects({ export default function DashboardProjects({
sandboxes, sandboxes,
q,
}: { }: {
sandboxes: Sandbox[] sandboxes: Sandbox[]
q: string | null
}) { }) {
const onDelete = async (sandbox: Sandbox) => { const onDelete = async (sandbox: Sandbox) => {
toast(`Project ${sandbox.name} deleted.`) toast(`Project ${sandbox.name} deleted.`)
const res = await deleteSandbox(sandbox.id) await deleteSandbox(sandbox.id)
} }
const onVisibilityChange = async (sandbox: Sandbox) => { const onVisibilityChange = async (sandbox: Sandbox) => {
const newVisibility = sandbox.visibility === "public" ? "private" : "public" const newVisibility = sandbox.visibility === "public" ? "private" : "public"
toast(`Project ${sandbox.name} is now ${newVisibility}.`) toast(`Project ${sandbox.name} is now ${newVisibility}.`)
const res = await updateSandbox({ await updateSandbox({
id: sandbox.id, id: sandbox.id,
visibility: newVisibility, visibility: newVisibility,
}) })
@ -31,10 +33,18 @@ export default function DashboardProjects({
return ( return (
<div className="grow p-4 flex flex-col"> <div className="grow p-4 flex flex-col">
<div className="text-xl font-medium mb-8">My Projects</div> <div className="text-xl font-medium mb-8">
{q && q.length > 0 ? `Showing search results for: ${q}` : "My Projects"}
</div>
<div className="grow w-full "> <div className="grow w-full ">
<div className="w-full grid lg:grid-cols-3 2xl:grid-cols-4 md:grid-cols-2 gap-4"> <div className="w-full grid lg:grid-cols-3 2xl:grid-cols-4 md:grid-cols-2 gap-4">
{sandboxes.map((sandbox) => ( {sandboxes.map((sandbox) => {
if (q && q.length > 0) {
if (!sandbox.name.toLowerCase().includes(q.toLowerCase())) {
return null
}
}
return (
<Link <Link
key={sandbox.id} key={sandbox.id}
href={`/code/${sandbox.id}`} href={`/code/${sandbox.id}`}
@ -81,7 +91,8 @@ export default function DashboardProjects({
{/* </ProjectCard> */} {/* </ProjectCard> */}
</Card> </Card>
</Link> </Link>
))} )
})}
</div> </div>
</div> </div>
</div> </div>