From 3ed50df2144b58e1c436ec97270ac126c446211b Mon Sep 17 00:00:00 2001 From: Weves Date: Sat, 26 Oct 2024 12:29:11 -0700 Subject: [PATCH 1/4] Move user fetching to SS --- .../admin/connectors/[connector]/Sidebar.tsx | 5 +-- web/src/app/chat/page.tsx | 1 - web/src/app/layout.tsx | 12 +++++-- web/src/components/context/AppProvider.tsx | 5 ++- web/src/components/user/UserProvider.tsx | 35 ++++++++++--------- 5 files changed, 35 insertions(+), 23 deletions(-) diff --git a/web/src/app/admin/connectors/[connector]/Sidebar.tsx b/web/src/app/admin/connectors/[connector]/Sidebar.tsx index c31e3930c24..310bc19de4c 100644 --- a/web/src/app/admin/connectors/[connector]/Sidebar.tsx +++ b/web/src/app/admin/connectors/[connector]/Sidebar.tsx @@ -8,12 +8,13 @@ import { credentialTemplates } from "@/lib/connectors/credentials"; import Link from "next/link"; import { useUser } from "@/components/user/UserProvider"; import { useContext } from "react"; +import { UserRole } from "@/lib/types"; export default function Sidebar() { const { formStep, setFormStep, connector, allowAdvanced, allowCreate } = useFormContext(); const combinedSettings = useContext(SettingsContext); - const { isLoadingUser, isAdmin } = useUser(); + const { user } = useUser(); if (!combinedSettings) { return null; } @@ -62,7 +63,7 @@ export default function Sidebar() { >

- {isAdmin ? "Admin Page" : "Curator Page"} + {user?.role === UserRole.ADMIN ? "Admin Page" : "Curator Page"}

diff --git a/web/src/app/chat/page.tsx b/web/src/app/chat/page.tsx index 4f91699b48c..05207ea9282 100644 --- a/web/src/app/chat/page.tsx +++ b/web/src/app/chat/page.tsx @@ -5,7 +5,6 @@ import { WelcomeModal } from "@/components/initialSetup/welcome/WelcomeModalWrap import { ChatProvider } from "@/components/context/ChatContext"; import { fetchChatData } from "@/lib/chat/fetchChatData"; import WrappedChat from "./WrappedChat"; -import { AssistantsProvider } from "@/components/context/AssistantsContext"; export default async function Page({ searchParams, diff --git a/web/src/app/layout.tsx b/web/src/app/layout.tsx index e7e3c2c416d..6fd2799ab85 100644 --- a/web/src/app/layout.tsx +++ b/web/src/app/layout.tsx @@ -19,6 +19,7 @@ import { fetchAssistantData } from "@/lib/chat/fetchAssistantdata"; import { AppProvider } from "@/components/context/AppProvider"; import { PHProvider } from "./providers"; import { default as dynamicImport } from "next/dynamic"; +import { getCurrentUserSS } from "@/lib/userSS"; const PostHogPageView = dynamicImport(() => import("./PostHogPageView"), { ssr: false, @@ -57,7 +58,11 @@ export default async function RootLayout({ }: { children: React.ReactNode; }) { - const combinedSettings = await fetchSettingsSS(); + const [combinedSettings, assistantsData, user] = await Promise.all([ + fetchSettingsSS(), + fetchAssistantData(), + getCurrentUserSS(), + ]); const productGating = combinedSettings?.settings.product_gating ?? GatingType.NONE; @@ -165,11 +170,12 @@ export default async function RootLayout({ ); } - const data = await fetchAssistantData(); - const { assistants, hasAnyConnectors, hasImageCompatibleModel } = data; + const { assistants, hasAnyConnectors, hasImageCompatibleModel } = + assistantsData; return getPageContent( { return ( - + (undefined); -export function UserProvider({ children }: { children: React.ReactNode }) { - const [user, setUser] = useState(null); - const [isLoadingUser, setIsLoadingUser] = useState(true); - const [isAdmin, setIsAdmin] = useState(false); - const [isCurator, setIsCurator] = useState(false); +export function UserProvider({ + children, + user, +}: { + children: React.ReactNode; + user: User | null; +}) { + const [upToDateUser, setUpToDateUser] = useState(user); + const [isLoadingUser, setIsLoadingUser] = useState(false); const fetchUser = async () => { try { - const user = await getCurrentUser(); - setUser(user); - setIsAdmin(user?.role === UserRole.ADMIN); - setIsCurator( - user?.role === UserRole.CURATOR || user?.role == UserRole.GLOBAL_CURATOR - ); + setIsLoadingUser(true); + const currentUser = await getCurrentUser(); + setUpToDateUser(currentUser); } catch (error) { console.error("Error fetching current user:", error); } finally { @@ -35,17 +36,19 @@ export function UserProvider({ children }: { children: React.ReactNode }) { } }; - useEffect(() => { - fetchUser(); - }, []); - const refreshUser = async () => { await fetchUser(); }; return ( {children} From a837cfa6f6d70b77119b6ac723dc4a16ca5c9cd3 Mon Sep 17 00:00:00 2001 From: Weves Date: Sat, 26 Oct 2024 12:33:06 -0700 Subject: [PATCH 2/4] Cleanup --- .../admin/connectors/[connector]/Sidebar.tsx | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/web/src/app/admin/connectors/[connector]/Sidebar.tsx b/web/src/app/admin/connectors/[connector]/Sidebar.tsx index 310bc19de4c..ebf09f14f04 100644 --- a/web/src/app/admin/connectors/[connector]/Sidebar.tsx +++ b/web/src/app/admin/connectors/[connector]/Sidebar.tsx @@ -10,11 +10,39 @@ import { useUser } from "@/components/user/UserProvider"; import { useContext } from "react"; import { UserRole } from "@/lib/types"; +function BackButton({ + isAdmin, + isCurator, +}: { + isAdmin: boolean; + isCurator: boolean; +}) { + const buttonText = isAdmin ? "Admin Page" : "Curator Page"; + + if (!isAdmin && !isCurator) { + console.error( + "User is neither admin nor curator, defaulting to curator view" + ); + } + + return ( +
+ + +

{buttonText}

+ +
+ ); +} + export default function Sidebar() { const { formStep, setFormStep, connector, allowAdvanced, allowCreate } = useFormContext(); const combinedSettings = useContext(SettingsContext); - const { user } = useUser(); + const { isCurator, isAdmin } = useUser(); if (!combinedSettings) { return null; } @@ -56,17 +84,7 @@ export default function Sidebar() { -
- - -

- {user?.role === UserRole.ADMIN ? "Admin Page" : "Curator Page"} -

- -
+
From 5eb48df5fc179795fabbbf8da55ed76d23197971 Mon Sep 17 00:00:00 2001 From: Weves Date: Sat, 26 Oct 2024 12:40:53 -0700 Subject: [PATCH 3/4] Add more logging --- web/src/app/admin/connectors/[connector]/Sidebar.tsx | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/web/src/app/admin/connectors/[connector]/Sidebar.tsx b/web/src/app/admin/connectors/[connector]/Sidebar.tsx index ebf09f14f04..56682f9e00a 100644 --- a/web/src/app/admin/connectors/[connector]/Sidebar.tsx +++ b/web/src/app/admin/connectors/[connector]/Sidebar.tsx @@ -13,15 +13,21 @@ import { UserRole } from "@/lib/types"; function BackButton({ isAdmin, isCurator, + user, }: { isAdmin: boolean; isCurator: boolean; + user: User | null; }) { const buttonText = isAdmin ? "Admin Page" : "Curator Page"; if (!isAdmin && !isCurator) { console.error( - "User is neither admin nor curator, defaulting to curator view" + `User is neither admin nor curator, defaulting to curator view. Found user:\n ${JSON.stringify( + user, + null, + 2 + )}` ); } @@ -42,7 +48,7 @@ export default function Sidebar() { const { formStep, setFormStep, connector, allowAdvanced, allowCreate } = useFormContext(); const combinedSettings = useContext(SettingsContext); - const { isCurator, isAdmin } = useUser(); + const { isCurator, isAdmin, user } = useUser(); if (!combinedSettings) { return null; } @@ -84,7 +90,7 @@ export default function Sidebar() {
- +
From 7331ef757312b0205f5669dbaf3c26f92c08b1a6 Mon Sep 17 00:00:00 2001 From: Weves Date: Sat, 26 Oct 2024 19:37:02 -0700 Subject: [PATCH 4/4] Small cleanup --- web/src/app/admin/connectors/[connector]/Sidebar.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web/src/app/admin/connectors/[connector]/Sidebar.tsx b/web/src/app/admin/connectors/[connector]/Sidebar.tsx index 56682f9e00a..e227159ed9d 100644 --- a/web/src/app/admin/connectors/[connector]/Sidebar.tsx +++ b/web/src/app/admin/connectors/[connector]/Sidebar.tsx @@ -8,7 +8,7 @@ import { credentialTemplates } from "@/lib/connectors/credentials"; import Link from "next/link"; import { useUser } from "@/components/user/UserProvider"; import { useContext } from "react"; -import { UserRole } from "@/lib/types"; +import { User } from "@/lib/types"; function BackButton({ isAdmin, @@ -32,7 +32,7 @@ function BackButton({ } return ( -
+