Refactor cache functions to use explicit loaders

The commit refactors the previous implementation of using cache functions across several modules. They are now explicitly defined as loaders to improve readability and maintain a consistent style. This prevents the cache function calls from getting too nested and difficult to understand, especially in asynchronous cases. Additionally, the user session related hooks are deleted which were not used anymore.
This commit is contained in:
giancarlo
2024-06-04 01:03:57 +07:00
parent b1c3f12721
commit bf7e3185de
11 changed files with 48 additions and 63 deletions

View File

@@ -11,9 +11,11 @@ import { createAdminDashboardService } from '../services/admin-dashboard.service
* @description Load the admin dashboard data.
* @param params
*/
export const loadAdminDashboard = cache(() => {
export const loadAdminDashboard = cache(adminDashboardLoader);
function adminDashboardLoader() {
const client = getSupabaseServerComponentClient({ admin: true });
const service = createAdminDashboardService(client);
return service.getDashboardData();
});
}

View File

@@ -7,7 +7,6 @@ import { usePathname, useRouter } from 'next/navigation';
import { useQueryClient } from '@tanstack/react-query';
import { useSupabase } from './use-supabase';
import { useRevalidateUserSession } from './use-user-session';
/**
* @name PRIVATE_PATH_PREFIXES
@@ -30,7 +29,6 @@ export function useAuthChangeListener({
const client = useSupabase();
const pathName = usePathname();
const router = useRouter();
const revalidateUserSession = useRevalidateUserSession();
const queryClient = useQueryClient();
useEffect(() => {
@@ -61,7 +59,6 @@ export function useAuthChangeListener({
}, [
client.auth,
router,
revalidateUserSession,
pathName,
appHomePath,
privatePathPrefixes,

View File

@@ -1,41 +0,0 @@
import { useCallback } from 'react';
import type { Session } from '@supabase/supabase-js';
import { useQueryClient, useSuspenseQuery } from '@tanstack/react-query';
import { useSupabase } from './use-supabase';
const queryKey = ['supabase:session'];
export function useUserSession(initialSession?: Session | null) {
const supabase = useSupabase();
const queryFn = async () => {
const { data, error } = await supabase.auth.getSession();
if (error) {
throw error;
}
return data.session;
};
return useSuspenseQuery({
queryKey,
queryFn,
initialData: initialSession,
});
}
export function useRevalidateUserSession() {
const client = useQueryClient();
return useCallback(
() =>
client.invalidateQueries({
queryKey,
}),
[client],
);
}