mirror of
https://github.com/multica-ai/multica.git
synced 2026-08-01 01:16:17 +02:00
Replace mixpanel-browser with posthog-js for frontend analytics. PostHog provides CLI + SQL query support (HogQL) which enables agents to query analytics data directly. Key changes: - Replace mixpanel-browser dep with posthog-js - Rewrite analytics module: mixpanel.ts → posthog.ts - mixpanel.track() → posthog.capture() - mixpanel.identify() → posthog.identify() - mixpanel.register() → posthog.register() - mixpanel.reset() → posthog.reset() - Remove incrementUserProperty (PostHog counts events natively via HogQL) - Env vars: NEXT_PUBLIC_MIXPANEL_TOKEN → NEXT_PUBLIC_POSTHOG_KEY + NEXT_PUBLIC_POSTHOG_HOST - All existing tracking points preserved, same event names and properties
58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, type ReactNode } from "react";
|
|
import { useAuthStore } from "./store";
|
|
import { useWorkspaceStore } from "@/features/workspace";
|
|
import { api } from "@/shared/api";
|
|
import { createLogger } from "@/shared/logger";
|
|
import { identifyUser, resetUser } from "@/features/analytics";
|
|
import { setLoggedInCookie, clearLoggedInCookie } from "./auth-cookie";
|
|
|
|
const logger = createLogger("auth");
|
|
|
|
/**
|
|
* Initializes auth + workspace state from localStorage on mount.
|
|
* Fires getMe() and listWorkspaces() in parallel when a cached token exists.
|
|
*/
|
|
export function AuthInitializer({ children }: { children: ReactNode }) {
|
|
useEffect(() => {
|
|
const token = localStorage.getItem("multica_token");
|
|
if (!token) {
|
|
clearLoggedInCookie();
|
|
useAuthStore.setState({ isLoading: false });
|
|
return;
|
|
}
|
|
|
|
api.setToken(token);
|
|
const wsId = localStorage.getItem("multica_workspace_id");
|
|
|
|
// Fire getMe and listWorkspaces in parallel
|
|
const mePromise = api.getMe();
|
|
const wsPromise = api.listWorkspaces();
|
|
|
|
Promise.all([mePromise, wsPromise])
|
|
.then(([user, wsList]) => {
|
|
setLoggedInCookie();
|
|
identifyUser(user.id, {
|
|
name: user.name,
|
|
email: user.email,
|
|
created_at: user.created_at,
|
|
});
|
|
useAuthStore.setState({ user, isLoading: false });
|
|
useWorkspaceStore.getState().hydrateWorkspace(wsList, wsId);
|
|
})
|
|
.catch((err) => {
|
|
logger.error("auth init failed", err);
|
|
api.setToken(null);
|
|
api.setWorkspaceId(null);
|
|
localStorage.removeItem("multica_token");
|
|
localStorage.removeItem("multica_workspace_id");
|
|
clearLoggedInCookie();
|
|
resetUser();
|
|
useAuthStore.setState({ user: null, isLoading: false });
|
|
});
|
|
}, []);
|
|
|
|
return <>{children}</>;
|
|
}
|