Skip to content

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

hotfix: update teamProvider

Ashish Viradiyaade7decd0773
diff --git a/context/team-context.tsx b/context/team-context.tsx
index 0b69048b..9ca87c66 100644
--- a/context/team-context.tsx
+++ b/context/team-context.tsx
@@ -1,14 +1,14 @@
-import {
- createContext,
- useCallback,
- useContext,
- useMemo,
- useState,
-} from "react";
+import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
+
+
import { useTeams } from "@/lib/swr/use-teams";
import { Team } from "@/lib/types";
+
+
+
+
interface TeamContextProps {
children: React.ReactNode;
}
@@ -35,30 +35,51 @@ export const TeamProvider = ({ children }: TeamContextProps): JSX.Element => {
const { teams, loading } = useTeams();
const [currentTeam, setCurrentTeamState] = useState<Team | null>(null);
+ useEffect(() => {
+ if (!teams || teams.length === 0 || currentTeam) return;
+
+ const savedTeamId =
+ typeof localStorage !== "undefined"
+ ? localStorage.getItem("currentTeamId")
+ : null;
+
+ let teamToSet: Team | null = null;
+
+ if (savedTeamId) {
+ teamToSet = teams.find((team) => team.id === savedTeamId) || null;
+ }
+
+ if (!teamToSet && teams.length > 0) {
+ teamToSet = teams[0];
+ }
+
+ if (teamToSet) {
+ setCurrentTeamState(teamToSet);
+ if (typeof localStorage !== "undefined") {
+ localStorage.setItem("currentTeamId", teamToSet.id);
+ }
+ }
+ }, [teams, currentTeam]);
+
const setCurrentTeam = useCallback((team: Team) => {
setCurrentTeamState(team);
+ if (typeof localStorage !== "undefined") {
+ localStorage.setItem("currentTeamId", team.id);
+ }
}, []);
- const currentTeamId = currentTeam
- ? currentTeam.id
- : typeof localStorage !== "undefined"
- ? localStorage.getItem("currentTeamId")
- : null;
-
const value = useMemo(
() => ({
teams: teams || [],
- currentTeam:
- (teams || []).find((team) => team.id === currentTeamId) ||
- (teams || [])[0],
- currentTeamId,
+ currentTeam,
+ currentTeamId: currentTeam?.id || null,
isLoading: loading,
setCurrentTeam,
}),
- [teams, currentTeam, currentTeamId, loading],
+ [teams, currentTeam, loading, setCurrentTeam],
);
return <TeamContext.Provider value={value}>{children}</TeamContext.Provider>;
};
-export const useTeam = () => useContext(TeamContext);
+export const useTeam = () => useContext(TeamContext);
\ No newline at end of file