Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

UI: sidebar team select

Aashish-Upadhyay-1013850881059e0
diff --git a/components/Sidebar.tsx b/components/Sidebar.tsx
index 05ed91d7..2b08414e 100644
--- a/components/Sidebar.tsx
+++ b/components/Sidebar.tsx
@@ -17,12 +17,17 @@ import { CustomUser } from "@/lib/types";
import LoadingSpinner from "./ui/loading-spinner";
import Banner from "./banner";
import SelectTeam from "./teams/select-team";
+import { TeamContextType, initialState, useTeam } from "@/context/team-context";
+import { AddTeamModal } from "./teams/add-team-modal";
export default function Sidebar() {
const { data: session, status } = useSession();
const [sidebarOpen, setSidebarOpen] = useState<boolean>(false);
const router = useRouter();
+ const { currentTeam, teams, isLoading }: TeamContextType =
+ useTeam() || initialState;
+
const navigation = [
// {
// name: "Overview",
@@ -155,7 +160,11 @@ export default function Sidebar() {
</p>
</div>
<nav className="flex flex-1 flex-col">
- <SelectTeam />
+ <SelectTeam
+ currentTeam={currentTeam}
+ teams={teams}
+ isLoading={isLoading}
+ />
<ul role="list" className="flex flex-1 flex-col gap-y-7">
<li>
<ul role="list" className="-mx-2 space-y-1">
diff --git a/components/teams/add-team-modal.tsx b/components/teams/add-team-modal.tsx
new file mode 100644
index 00000000..330e57b6
--- /dev/null
+++ b/components/teams/add-team-modal.tsx
@@ -0,0 +1,97 @@
+import { usePlausible } from "next-plausible";
+import { useRouter } from "next/router";
+import { useState } from "react";
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+ DialogTrigger,
+} from "@/components/ui/dialog";
+import { Button } from "../ui/button";
+import { Input } from "../ui/input";
+import { Label } from "../ui/label";
+import { toast } from "sonner";
+
+export function AddTeamModal({ children }: { children: React.ReactNode }) {
+ const router = useRouter();
+ const plausible = usePlausible();
+ const [teamName, setTeamName] = useState<string>("");
+ const [loading, setLoading] = useState<boolean>(false);
+
+ const handleCreateTeam = async (event: React.FormEvent<HTMLFormElement>) => {
+ event.preventDefault();
+ event.stopPropagation();
+
+ if (!teamName) return;
+
+ setLoading(true);
+ const response = await fetch("/api/teams", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ team: teamName,
+ }),
+ });
+
+ if (!response.ok) {
+ const error = await response.json();
+ setLoading(false);
+ toast.error(error);
+ return;
+ }
+
+ const team = await response.json();
+ toast.success("Team created successfully!");
+ router.push(`/teams/${team?.id}`);
+ setLoading(false);
+ };
+
+ return (
+ <Dialog>
+ <DialogTrigger asChild>{children}</DialogTrigger>
+ <DialogContent
+ className="text-foreground bg-background"
+ onClick={(e) => e.stopPropagation()}>
+ <DialogHeader>
+ <DialogTitle>Create a Team</DialogTitle>
+ <DialogDescription className="py-2 mb-1 text-sm text-muted-foreground">
+ Start by naming your new team and inviting team members.
+ </DialogDescription>
+ </DialogHeader>
+ <form
+ onSubmit={handleCreateTeam}
+ className="flex flex-col gap-8 mt-4"
+ onClick={(e) => e.stopPropagation()}>
+ <div className="flex flex-col gap-3">
+ <div>
+ <Label htmlFor="team">Team name</Label>
+ </div>
+ <div>
+ <Input
+ id="team"
+ placeholder="Enter team's name"
+ onChange={(e) => setTeamName(e.target.value)}
+ />
+ </div>
+ </div>
+ <DialogFooter className="flex justify-center">
+ <Button
+ type="submit"
+ className="w-full lg:w-1/2"
+ loading={loading}
+ // disabled={uploading || !currentFile}
+ >
+ {loading ? "Creating..." : "Create"}
+ {/* {uploading ? "Uploading..." : "Upload Document"} */}
+ </Button>
+ </DialogFooter>
+ </form>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/components/teams/select-team.tsx b/components/teams/select-team.tsx
index ca60359d..4bc666e1 100644
--- a/components/teams/select-team.tsx
+++ b/components/teams/select-team.tsx
@@ -1,4 +1,4 @@
-import React from "react";
+import React, { Fragment, useEffect, useRef, useState } from "react";
import {
Select,
SelectContent,
@@ -6,26 +6,118 @@ import {
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
-import { PlusIcon } from "lucide-react";
+import { Check, CheckIcon, Loader, PlusIcon } from "lucide-react";
+import { TeamContextType } from "@/context/team-context";
+import { AddTeamModal } from "./add-team-modal";
+import { Listbox, Transition } from "@headlessui/react";
+import { ChevronUpDownIcon } from "@heroicons/react/20/solid";
+
+const SelectTeam = ({ teams, currentTeam, isLoading }: TeamContextType) => {
+ const [selectTeamOpen, setSelectTeamOpen] = useState<boolean>(false);
+ const ref = useRef<HTMLDivElement>(null);
+
+ useEffect(() => {
+ function handleDocumentClick(event: MouseEvent) {
+ if (ref.current && !ref.current.contains(event?.target as Node)) {
+ setSelectTeamOpen(false);
+ }
+ }
+
+ document.addEventListener("click", handleDocumentClick);
+
+ return () => {
+ document.removeEventListener("click", handleDocumentClick);
+ };
+ }, []);
-const SelectTeam = () => {
return (
- <Select defaultValue="aashish">
- <SelectTrigger className="w-full mb-6">
- <SelectValue placeholder="Select project" />
- </SelectTrigger>
- <SelectContent>
- <div className="max-h-48 overflow-y-scroll ">
- <SelectItem value="demo">Demo project</SelectItem>
- <SelectItem value="aashish">Aashish's Team</SelectItem>
- <SelectItem value="papermark">College Engineering Group</SelectItem>
- </div>
- <div className="flex gap-2 items-center border-t p-2 text-sm hover:bg-gray-800 duration-100 hover:cursor-pointer">
- <PlusIcon className="h-5 w-5" />
- Create Team
- </div>
- </SelectContent>
- </Select>
+ <>
+ {isLoading ? (
+ <h2 className="flex items-center gap-2 mb-6 text-sm">
+ <Loader className="animate-spin h-5 w-5" /> Loading teams...
+ </h2>
+ ) : (
+ <>
+ <div
+ className="relative h-10 mb-6 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background select-none"
+ ref={ref}>
+ <div
+ className="flex items-center justify-between cursor-pointer"
+ onClick={() => setSelectTeamOpen(!selectTeamOpen)}>
+ {currentTeam?.name}
+ <ChevronUpDownIcon className="h-4 w-4" />
+ </div>
+
+ {selectTeamOpen && (
+ <div className="z-50 bg-popover text-popover-foreground shadow-md absolute top-12 left-0 max-h-72 w-full space-y-0.5 border overflow-auto rounded-md text-base sm:w-60 sm:text-sm sm:shadow-lg">
+ <div className="py-2 px-4 text-sm text-gray-400">
+ Custom Projects
+ </div>
+ {teams.map((team) => (
+ <div
+ key={team.id}
+ className={`relative flex w-full items-center space-x-2 rounded-md px-4 py-2 hover:bg-gray-800 duration-100 cursor-pointer ${
+ team.id === currentTeam?.id ? "font-medium" : ""
+ } transition-all duration-75`}
+ onClick={() => setSelectTeamOpen(false)}>
+ <span
+ className={`block truncate text-sm ${
+ team.id === currentTeam?.id
+ ? "font-medium"
+ : "font-normal"
+ }`}>
+ {team.name}
+ </span>
+ {team.id === currentTeam?.id ? (
+ <span className="absolute inset-y-0 right-0 flex items-center pr-3 text-black">
+ <Check
+ className="h-5 w-5 text-white"
+ aria-hidden="true"
+ />
+ </span>
+ ) : null}
+ </div>
+ ))}
+ <AddTeamModal>
+ <div className="flex mt-1 gap-2 items-center border-t p-3 text-sm hover:bg-gray-800 duration-100 hover:cursor-pointer">
+ <PlusIcon className="h-5 w-5" />
+ Create Team
+ </div>
+ </AddTeamModal>
+ </div>
+ )}
+ </div>
+ </>
+ // <>
+ // <Select
+ // defaultValue={currentTeam?.name}
+ // onOpenChange={() => setVisible(!visible)}>
+ // <SelectTrigger className="w-full mb-6">
+ // <SelectValue placeholder="Select project" />
+ // </SelectTrigger>
+ // <SelectContent>
+ // <div className="max-h-48 overflow-y-scroll">
+ // {teams.map((team) => (
+ // <SelectItem key={team.id} value={team.name}>
+ // {team.name}
+ // </SelectItem>
+ // ))}
+ // </div>
+ // </SelectContent>
+ // {visible && (
+ // <div className="absolute">
+ // <AddTeamModal>
+ // <div className="flex gap-2 items-center border-t p-2 text-sm hover:bg-gray-800 duration-100 hover:cursor-pointer">
+ // <PlusIcon className="h-5 w-5" />
+ // Create Team
+ // </div>
+ // </AddTeamModal>
+ // </div>
+ // )}
+ // </Select>
+ // </>
+ )}
+ </>
);
};
diff --git a/context/team-context.tsx b/context/team-context.tsx
index 5030ed19..05e0b82c 100644
--- a/context/team-context.tsx
+++ b/context/team-context.tsx
@@ -6,13 +6,13 @@ interface TeamContextProps {
children: React.ReactNode;
}
-type TeamContextType = {
+export type TeamContextType = {
teams: Team[];
currentTeam: Team | null;
isLoading: boolean;
};
-const initialState = {
+export const initialState = {
teams: [],
currentTeam: null,
isLoading: false,
@@ -23,7 +23,10 @@ const TeamContext = createContext<TeamContextType | null>(initialState);
export const TeamProvider = ({ children }: TeamContextProps): JSX.Element => {
const { teams, loading } = useTeams();
- const currentTeamId = localStorage.getItem("currentTeamId");
+ const currentTeamId =
+ typeof localStorage !== "undefined"
+ ? localStorage.getItem("currentTeamId")
+ : null;
const value = useMemo(
() => ({
diff --git a/pages/_app.tsx b/pages/_app.tsx
index 1e86707d..70cff992 100644
--- a/pages/_app.tsx
+++ b/pages/_app.tsx
@@ -8,6 +8,7 @@ import Head from "next/head";
import { Analytics } from "@vercel/analytics/react";
import PlausibleProvider from "next-plausible";
import { ThemeProvider } from "@/components/theme-provider";
+import { TeamProvider } from "@/context/team-context";
const inter = Inter({ subsets: ["latin"] });
@@ -24,11 +25,12 @@ export default function App({
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
<PlausibleProvider
domain="papermark.io"
- enabled={process.env.NEXT_PUBLIC_VERCEL_ENV === "production"}
- >
+ enabled={process.env.NEXT_PUBLIC_VERCEL_ENV === "production"}>
<main className={inter.className}>
<Toaster closeButton richColors theme={"system"} />
- <Component {...pageProps} />
+ <TeamProvider>
+ <Component {...pageProps} />
+ </TeamProvider>
</main>
</PlausibleProvider>
</ThemeProvider>
diff --git a/pages/api/teams/[teamId]/documents/[id]/index.ts b/pages/api/teams/[teamId]/documents/[id]/index.ts
index 7daeb176..db93fde8 100644
--- a/pages/api/teams/[teamId]/documents/[id]/index.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/index.ts
@@ -76,7 +76,7 @@ export default async function handle(
});
// delete the document from vercel blob
- await del(document.file);
+ await del(document!.file);
// delete the document from database
await prisma.document.delete({
where: {