Files
wehub-resource-sync 6ede33ccdb
Build and Push Docker Images / create_manifest (web, surfsense-web, , cpu) (push) Has been cancelled
Build and Push Docker Images / finalize_release (push) Has been cancelled
Obsidian Plugin Lint / lint (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_web, cpu, ./surfsense_web/Dockerfile, web, surfsense-web, ubuntu-24.04-arm, linux/arm64, arm64, , runner, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_web, cpu, ./surfsense_web/Dockerfile, web, surfsense-web, ubuntu-latest, linux/amd64, amd64, , runner, false, cpu) (push) Has been cancelled
Build and Push Docker Images / compute_version (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cpu, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, , production, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cpu, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, , production, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu126, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, -cuda126, production, true, cuda126) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu126, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, -cuda126, production, true, cuda126) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu128, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, -cuda, production, true, cuda) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu128, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, -cuda, production, true, cuda) (push) Has been cancelled
Build and Push Docker Images / verify_digests (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, , cpu) (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, -cuda, cuda) (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, -cuda126, cuda126) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:33:44 +08:00

85 lines
2.1 KiB
TypeScript

"use client";
import { useAtomValue } from "jotai";
import { usePathname } from "next/navigation";
import { useEffect, useRef } from "react";
import { currentUserAtom } from "@/atoms/user/user-query.atoms";
import { useSession } from "@/hooks/use-session";
import { isPublicRoute } from "@/lib/auth-utils";
import { identifyUser, resetUser } from "@/lib/posthog/events";
/**
* Component that handles PostHog user identification.
* - Identifies users when they're logged in (user data is available)
* - Resets the PostHog identity when user logs out
*
* This should be rendered inside the PostHogProvider.
*/
function PostHogReset() {
useEffect(() => {
resetUser();
}, []);
return null;
}
function PostHogUserIdentify() {
const { data: user, isSuccess, isError } = useAtomValue(currentUserAtom);
const previousUserIdRef = useRef<string | null>(null);
useEffect(() => {
// Only run on client side
if (typeof window === "undefined") return;
// User is logged in and we have their data
if (isSuccess && user?.id) {
const userId = String(user.id);
// Only identify if this is a new user or different from previous
if (previousUserIdRef.current !== userId) {
identifyUser(userId, {
email: user.email,
name: user.display_name,
is_superuser: user.is_superuser,
is_verified: user.is_verified,
});
previousUserIdRef.current = userId;
}
}
// User is not logged in (query failed due to auth error)
// and we previously had a user identified
if (isError && previousUserIdRef.current !== null) {
resetUser();
previousUserIdRef.current = null;
}
}, [user, isSuccess, isError]);
// This component doesn't render anything
return null;
}
function SessionGatedPostHogIdentify() {
const session = useSession();
if (session.status === "loading") {
return null;
}
if (session.status === "unauthenticated") {
return <PostHogReset />;
}
return <PostHogUserIdentify />;
}
export function PostHogIdentify() {
const pathname = usePathname();
if (isPublicRoute(pathname)) {
return <PostHogReset />;
}
return <SessionGatedPostHogIdentify />;
}