set user in query data

This commit is contained in:
Nate Kelley 2025-02-17 17:30:05 -07:00
parent 3d71e084e3
commit 5fbcfd87c4
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
7 changed files with 42 additions and 13 deletions

View File

@ -3,10 +3,17 @@ import {
useCreateReactMutation,
useCreateReactQuery
} from '@/api/createReactQuery';
import { getUser, getUser_server, updateOrganizationUser, getMyUserInfo } from './requests';
import {
getUser,
getUser_server,
updateOrganizationUser,
getMyUserInfo,
getMyUserInfo_server
} from './requests';
import { useMemoizedFn } from 'ahooks';
import { QueryClient, useQueryClient } from '@tanstack/react-query';
import { queryKeys } from '@/api/query_keys';
import { BusterUserResponse } from '@/api/asset_interfaces';
export const useGetMyUserInfo = () => {
const queryFn = useMemoizedFn(async () => getMyUserInfo());
@ -18,12 +25,17 @@ export const useGetMyUserInfo = () => {
});
};
export const prefetchGetMyUserInfo = async (queryClientProp?: QueryClient) => {
export const prefetchGetMyUserInfo = async (
params: Parameters<typeof getMyUserInfo_server>[0],
queryClientProp?: QueryClient
) => {
const queryClient = queryClientProp || new QueryClient();
const initialData = await getMyUserInfo_server(params);
await queryClient.prefetchQuery({
...queryKeys['/users/response:getUserMyself'],
queryFn: () => getMyUserInfo()
initialData
});
return { queryClient, initialData };
};
export const useGetUser = (params: Parameters<typeof getUser>[0]) => {

View File

@ -28,6 +28,9 @@ export const getMyUserInfo_server = async ({
}
})
.then((response) => {
if (!response.ok) {
return undefined;
}
return response.json();
})
.catch((error) => {

View File

@ -1,8 +1,13 @@
import { prefetchGetDatasets } from '@/api/buster_rest/datasets';
import { DatasetsPageContent } from './DatasetsPageContent';
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
export default async function DashboardPage() {
await prefetchGetDatasets();
const queryClient = await prefetchGetDatasets();
return <DatasetsPageContent />;
return (
<HydrationBoundary state={dehydrate(queryClient)}>
<DatasetsPageContent />
</HydrationBoundary>
);
}

View File

@ -2,7 +2,6 @@
import { useSupabaseServerContext } from '@/context/Supabase/useSupabaseContext';
import React from 'react';
import { getMyUserInfo_server } from '@/api/buster_rest/users/requests';
import { getAppSplitterLayout } from '@/components/layout/AppSplitter';
import { useBusterSupabaseAuthMethods } from '@/hooks/useBusterSupabaseAuthMethods';
import { createBusterRoute } from '@/routes';
@ -10,6 +9,8 @@ import { BusterAppRoutes } from '@/routes/busterRoutes/busterAppRoutes';
import { headers, cookies } from 'next/headers';
import { ClientRedirect } from '../../components/layout/ClientRedirect';
import { AppLayoutClient } from './layoutClient';
import { prefetchGetMyUserInfo } from '@/api/buster_rest';
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
export default async function Layout({
children
@ -18,7 +19,10 @@ export default async function Layout({
}>) {
const headersList = headers();
const supabaseContext = await useSupabaseServerContext();
const userInfo = await getMyUserInfo_server({ jwtToken: supabaseContext.accessToken });
const { initialData: userInfo, queryClient } = await prefetchGetMyUserInfo({
jwtToken: supabaseContext.accessToken
});
const defaultLayout = await getAppSplitterLayout('app-layout', ['230px', 'auto']);
const { signOut } = useBusterSupabaseAuthMethods();
const pathname = headersList.get('x-next-pathname') as string;
@ -29,7 +33,7 @@ export default async function Layout({
(!userInfo?.organizations?.[0]?.id || !userInfo?.user?.name) &&
!cookiePathname?.includes(newUserRoute) &&
pathname !== newUserRoute &&
supabaseContext.accessToken //added to avoid bug with anon user
!!supabaseContext.accessToken //added to avoid bug with anon user
) {
return <ClientRedirect to={newUserRoute} />;
}
@ -40,7 +44,7 @@ export default async function Layout({
supabaseContext={supabaseContext}
defaultLayout={defaultLayout}
signOut={signOut}>
{children}
<HydrationBoundary state={dehydrate(queryClient)}>{children}</HydrationBoundary>
</AppLayoutClient>
);
}

View File

@ -6,26 +6,30 @@ import { AppLayout } from './_controllers/AppLayout';
import type { BusterUserResponse } from '@/api/asset_interfaces';
import { useSupabaseServerContext } from '@/context/Supabase/useSupabaseContext';
import { GlobalErrorComponent } from '../../components/error';
import { dehydrate, HydrationBoundary, QueryClient } from '@tanstack/react-query';
export const AppLayoutClient = ({
children,
userInfo,
supabaseContext,
defaultLayout,
signOut
}: {
children: React.ReactNode;
userInfo: BusterUserResponse | undefined;
supabaseContext: Awaited<ReturnType<typeof useSupabaseServerContext>>;
defaultLayout: [string, string];
signOut: () => void;
signOut: () => void; //DO I really need this here?
}) => {
return (
<GlobalErrorComponent>
<AppProviders userInfo={userInfo} supabaseContext={supabaseContext}>
{/* <HydrationBoundary state={dehydrate(queryClient)}> */}
<AppLayout defaultLayout={defaultLayout} signOut={signOut}>
{children}
</AppLayout>
{/* </HydrationBoundary> */}
</AppProviders>
</GlobalErrorComponent>
);

View File

@ -25,6 +25,7 @@ import { BusterChatProvider } from './Chats';
import { RoutePrefetcher } from './RoutePrefetcher';
import { BusterMetricsProvider } from './Metrics';
import type { BusterUserResponse } from '@/api/asset_interfaces';
import { QueryClient } from '@tanstack/react-query';
// scan({
// enabled: true,

View File

@ -1,9 +1,9 @@
'use client';
import type { BusterUserResponse } from '@/api/asset_interfaces';
import React, { PropsWithChildren, useState } from 'react';
import React, { PropsWithChildren } from 'react';
import { useFavoriteProvider } from './useFavoriteProvider';
import { getMyUserInfo_server, useGetMyUserInfo } from '@/api/buster_rest/users';
import { useGetMyUserInfo } from '@/api/buster_rest/users';
import { useSupabaseContext } from '../Supabase';
import {
ContextSelector,
@ -17,7 +17,7 @@ import { useInviteUser } from './useInviteUser';
export const useUserConfigProvider = ({ userInfo }: { userInfo: BusterUserResponse | null }) => {
const isAnonymousUser = useSupabaseContext((state) => state.isAnonymousUser);
const { data: userResponse, refetch: refetchUserResponse } = useGetMyUserInfo();
const { data: userResponse, refetch: refetchUserResponse, isFetched } = useGetMyUserInfo();
const favoriteConfig = useFavoriteProvider();