Skip to content

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

frontend: team context

Aashish-Upadhyay-1012563a55ed240
diff --git a/components/Sidebar.tsx b/components/Sidebar.tsx
index a4836825..05ed91d7 100644
--- a/components/Sidebar.tsx
+++ b/components/Sidebar.tsx
@@ -16,14 +16,7 @@ import { Button } from "./ui/button";
import { CustomUser } from "@/lib/types";
import LoadingSpinner from "./ui/loading-spinner";
import Banner from "./banner";
-import {
- Select,
- SelectContent,
- SelectItem,
- SelectTrigger,
- SelectValue,
-} from "@/components/ui/select";
-import { PlusIcon } from "lucide-react";
+import SelectTeam from "./teams/select-team";
export default function Sidebar() {
const { data: session, status } = useSession();
@@ -162,25 +155,7 @@ export default function Sidebar() {
</p>
</div>
<nav className="flex flex-1 flex-col">
- <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>
-
+ <SelectTeam />
<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/select-team.tsx b/components/teams/select-team.tsx
new file mode 100644
index 00000000..ca60359d
--- /dev/null
+++ b/components/teams/select-team.tsx
@@ -0,0 +1,32 @@
+import React from "react";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select";
+import { PlusIcon } from "lucide-react";
+
+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>
+ );
+};
+
+export default SelectTeam;
diff --git a/context/team-context.tsx b/context/team-context.tsx
new file mode 100644
index 00000000..fce9d862
--- /dev/null
+++ b/context/team-context.tsx
@@ -0,0 +1,44 @@
+import { useTeams } from "@/lib/swr/use-teams";
+import { Team } from "@/lib/types";
+import { createContext, useContext, useMemo } from "react";
+
+interface TeamContextProps {
+ children: React.ReactNode;
+}
+
+type TeamContextType = {
+ teams: Team[];
+ currentTeam: Team | null;
+ isLoading: boolean;
+};
+
+const initialState = {
+ teams: [],
+ currentTeam: null,
+ isLoading: false,
+};
+
+const TeamContext = createContext<TeamContextType | null>(initialState);
+
+export const TeamProvider = ({ children }: TeamContextProps): JSX.Element => {
+ //TODO: check if there is team, if not, create a new default one with User's name
+
+ const { teams, loading } = useTeams();
+
+ const currentTeamId = localStorage.getItem("currentTeamId");
+
+ const value = useMemo(
+ () => ({
+ teams: teams || [],
+ currentTeam:
+ (teams || []).find((team) => team.id === currentTeamId) ||
+ (teams || [])[0],
+ isLoading: loading,
+ }),
+ [teams, currentTeamId, loading]
+ );
+
+ return <TeamContext.Provider value={value}>{children}</TeamContext.Provider>;
+};
+
+export const useTeam = () => useContext(TeamContext);
diff --git a/lib/swr/use-teams.ts b/lib/swr/use-teams.ts
new file mode 100644
index 00000000..b51b7049
--- /dev/null
+++ b/lib/swr/use-teams.ts
@@ -0,0 +1,23 @@
+import { useRouter } from "next/router";
+import useSWR from "swr";
+import { fetcher } from "@/lib/utils";
+import { Team } from "@/lib/types";
+
+export function useTeams() {
+ const router = useRouter();
+
+ const { data: teams, isValidating } = useSWR<Team[]>(
+ router.isReady && "/api/teams",
+ fetcher,
+ {
+ revalidateOnFocus: false,
+ dedupingInterval: 30000,
+ }
+ );
+
+ return {
+ teams,
+ loading: teams ? false : true,
+ isValidating,
+ };
+}
diff --git a/lib/types.ts b/lib/types.ts
index 6edd4476..9511ee00 100644
--- a/lib/types.ts
+++ b/lib/types.ts
@@ -1,5 +1,11 @@
import { User as NextAuthUser } from "next-auth";
-import { Document, Link, View, User as PrismaUser, DocumentVersion } from "@prisma/client";
+import {
+ Document,
+ Link,
+ View,
+ User as PrismaUser,
+ DocumentVersion,
+} from "@prisma/client";
export type CustomUser = NextAuthUser & PrismaUser;
@@ -50,7 +56,7 @@ export type DomainVerificationStatusProps =
| "Pending Verification"
| "Domain Not Found"
| "Unknown Error";
-
+
// From https://vercel.com/docs/rest-api/endpoints#get-a-project-domain
export interface DomainResponse {
name: string;
@@ -122,7 +128,7 @@ export type AnalyticsEvents =
documentId: string;
customDomain: string | null | undefined;
}
- | { event: "User Upgraded"; email: string | null | undefined; }
+ | { event: "User Upgraded"; email: string | null | undefined }
| {
event: "User Signed In";
email: string | null | undefined;
@@ -141,4 +147,9 @@ export type AnalyticsEvents =
| {
event: "Domain Verified";
slug: string;
- };
\ No newline at end of file
+ };
+
+export interface Team {
+ id: string;
+ name: string;
+}
diff --git a/pages/api/teams/index.ts b/pages/api/teams/index.ts
index e2318c86..6c45b15b 100644
--- a/pages/api/teams/index.ts
+++ b/pages/api/teams/index.ts
@@ -24,18 +24,9 @@ export default async function handle(
},
include: {
team: {
- include: {
- users: {
- select: {
- userId: true,
- role: true,
- },
- },
- documents: {
- select: {
- id: true,
- },
- },
+ select: {
+ id: true,
+ name: true,
},
},
},