2025-12-15 12:07:47 +00:00
|
|
|
import { atomWithQuery } from "jotai-tanstack-query";
|
|
|
|
|
import { userApiService } from "@/lib/apis/user-api.service";
|
2026-02-06 14:49:40 +02:00
|
|
|
import { getBearerToken, isPublicRoute } from "@/lib/auth-utils";
|
2025-12-15 12:07:47 +00:00
|
|
|
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
|
|
|
|
|
|
|
|
|
export const currentUserAtom = atomWithQuery(() => {
|
2026-02-06 14:49:40 +02:00
|
|
|
const pathname = typeof window !== "undefined" ? window.location.pathname : null;
|
2025-12-15 12:07:47 +00:00
|
|
|
return {
|
|
|
|
|
queryKey: cacheKeys.user.current(),
|
|
|
|
|
staleTime: 5 * 60 * 1000, // 5 minutes
|
2026-02-06 14:49:40 +02:00
|
|
|
enabled: !!getBearerToken() && pathname !== null && !isPublicRoute(pathname),
|
|
|
|
|
queryFn: async () => userApiService.getMe(),
|
2025-12-15 12:07:47 +00:00
|
|
|
};
|
|
|
|
|
});
|