Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(desktop): report main app drops back to the loading screen (#88857)

Adam Leith149df31eb06e
diff --git a/products/desktop/packages/ui/src/shell/App.tsx b/products/desktop/packages/ui/src/shell/App.tsx
index cf1dfafb6dbc..e5fac98a2a98 100644
--- a/products/desktop/packages/ui/src/shell/App.tsx
+++ b/products/desktop/packages/ui/src/shell/App.tsx
@@ -39,6 +39,7 @@ import {
rememberStartupLocation,
resolveStartupLocation,
} from "@posthog/ui/shell/startupLocation";
+import { useAppLoadingGateTelemetry } from "@posthog/ui/shell/useAppLoadingGateTelemetry";
import { useAppVisibilityWatchdog } from "@posthog/ui/shell/useAppVisibilityWatchdog";
import { RouterProvider } from "@tanstack/react-router";
import { AnimatePresence, motion } from "framer-motion";
@@ -182,6 +183,16 @@ function App({ devToolbar }: AppProps) {
// Mirrors the "main" branch of renderContent() below; keep the two in sync.
const showingMainApp = readyForMainApp && initialRouteLoaded;
useAppVisibilityWatchdog(mainRef, showingMainApp);
+ useAppLoadingGateTelemetry(showingMainApp, {
+ isBootstrapped,
+ isCheckingAccess,
+ readyForMainApp,
+ initialRouteLoaded,
+ authStatus: authState.status,
+ desktopAccessStatus: desktopAccess.status,
+ desktopAccessIsCurrent,
+ consentStatus: consent.status,
+ });
// Single gate for every state where the whole app is still loading.
if (
diff --git a/products/desktop/packages/ui/src/shell/useAppLoadingGateTelemetry.test.ts b/products/desktop/packages/ui/src/shell/useAppLoadingGateTelemetry.test.ts
new file mode 100644
index 000000000000..ee178391f610
--- /dev/null
+++ b/products/desktop/packages/ui/src/shell/useAppLoadingGateTelemetry.test.ts
@@ -0,0 +1,75 @@
+import { renderHook } from "@testing-library/react";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+vi.mock("@posthog/ui/shell/analytics", () => ({
+ captureException: vi.fn(),
+}));
+
+vi.mock("@posthog/ui/shell/logger", () => ({
+ logger: {
+ scope: () => ({
+ error: vi.fn(),
+ warn: vi.fn(),
+ info: vi.fn(),
+ debug: vi.fn(),
+ }),
+ },
+}));
+
+import { captureException } from "@posthog/ui/shell/analytics";
+import {
+ type AppLoadingGateState,
+ useAppLoadingGateTelemetry,
+} from "./useAppLoadingGateTelemetry";
+
+const baseState: AppLoadingGateState = {
+ isBootstrapped: true,
+ isCheckingAccess: false,
+ readyForMainApp: true,
+ initialRouteLoaded: true,
+ authStatus: "authenticated",
+ desktopAccessStatus: "allowed",
+ desktopAccessIsCurrent: true,
+ consentStatus: "resolved",
+};
+
+describe("useAppLoadingGateTelemetry", () => {
+ beforeEach(() => {
+ vi.mocked(captureException).mockClear();
+ });
+
+ it("reports only when the main app drops back to the loading screen", () => {
+ const { rerender } = renderHook(
+ ({ showing, state }) => useAppLoadingGateTelemetry(showing, state),
+ { initialProps: { showing: false, state: baseState } },
+ );
+ expect(captureException).not.toHaveBeenCalled();
+
+ rerender({ showing: true, state: baseState });
+ rerender({
+ showing: true,
+ state: { ...baseState, consentStatus: "loading" },
+ });
+ expect(captureException).not.toHaveBeenCalled();
+
+ const hidden = {
+ ...baseState,
+ isCheckingAccess: true,
+ desktopAccessStatus: "checking",
+ };
+ rerender({ showing: false, state: hidden });
+ rerender({
+ showing: false,
+ state: { ...hidden, consentStatus: "loading" },
+ });
+ expect(captureException).toHaveBeenCalledTimes(1);
+ expect(captureException).toHaveBeenCalledWith(
+ expect.any(Error),
+ expect.objectContaining({
+ isCheckingAccess: true,
+ desktopAccessStatus: "checking",
+ source: "app-loading-gate",
+ }),
+ );
+ });
+});
diff --git a/products/desktop/packages/ui/src/shell/useAppLoadingGateTelemetry.ts b/products/desktop/packages/ui/src/shell/useAppLoadingGateTelemetry.ts
new file mode 100644
index 000000000000..aa7a00f226df
--- /dev/null
+++ b/products/desktop/packages/ui/src/shell/useAppLoadingGateTelemetry.ts
@@ -0,0 +1,45 @@
+import { captureException } from "@posthog/ui/shell/analytics";
+import { logger } from "@posthog/ui/shell/logger";
+import { useEffect, useRef } from "react";
+
+const log = logger.scope("app-loading-gate");
+
+export interface AppLoadingGateState {
+ isBootstrapped: boolean;
+ isCheckingAccess: boolean;
+ readyForMainApp: boolean;
+ initialRouteLoaded: boolean;
+ authStatus: string;
+ desktopAccessStatus: string;
+ desktopAccessIsCurrent: boolean;
+ consentStatus: string;
+}
+
+// Reports the main app dropping back to the full-window loading screen after
+// it was already showing, with the gate inputs that caused it. Users see this
+// as a brief logo flash, and the inputs are the only way to tell which one
+// flipped.
+export function useAppLoadingGateTelemetry(
+ showingMainApp: boolean,
+ state: AppLoadingGateState,
+): void {
+ const wasShowingMainApp = useRef(false);
+ // Read through a ref so the effect fires on the transition only, not on
+ // every input change while the app stays hidden.
+ const stateRef = useRef(state);
+ stateRef.current = state;
+ useEffect(() => {
+ if (showingMainApp) {
+ wasShowingMainApp.current = true;
+ return;
+ }
+ if (!wasShowingMainApp.current) return;
+ wasShowingMainApp.current = false;
+ const detail = { ...stateRef.current, route: window.location.hash };
+ log.warn("Main app returned to loading screen", detail);
+ captureException(new Error("Main app returned to loading screen"), {
+ ...detail,
+ source: "app-loading-gate",
+ });
+ }, [showingMainApp]);
+}