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): stop artifacts tip returning after every restart (#91806)

Charles Vien86f18c729945
diff --git a/products/desktop/apps/code/src/main/services/secure-store/service.ts b/products/desktop/apps/code/src/main/services/secure-store/service.ts
index 297b688e2d21..e13501288023 100644
--- a/products/desktop/apps/code/src/main/services/secure-store/service.ts
+++ b/products/desktop/apps/code/src/main/services/secure-store/service.ts
@@ -37,7 +37,13 @@ export class SecureStoreService {
if (!this.store.has(key)) {
return null;
}
- return decrypt(this.store.get(key) as string);
+ const plaintext = decrypt(this.store.get(key) as string);
+ if (plaintext === null) {
+ log.error("Stored value failed to decrypt; treating as missing", {
+ key,
+ });
+ }
+ return plaintext;
} catch (error) {
log.error("Failed to get item:", error);
return null;
diff --git a/products/desktop/packages/ui/src/features/settings/settingsStore.ts b/products/desktop/packages/ui/src/features/settings/settingsStore.ts
index dfdae5b56f52..10bff4fb436d 100644
--- a/products/desktop/packages/ui/src/features/settings/settingsStore.ts
+++ b/products/desktop/packages/ui/src/features/settings/settingsStore.ts
@@ -103,12 +103,13 @@ export const DEFAULT_HINT_MAX = 3;
* Whether a lesson has stopped offering itself: someone answered it, or it ran
* out of showings. Reset is what brings either back.
*/
-function isHintRetired(key: string, hint: HintState | undefined): boolean {
+export function isHintRetired(
+ key: string,
+ hint: HintState | undefined,
+): boolean {
if (!hint) return false;
if (hint.learned) return true;
- const showings = TIP_SHOWINGS[key as TipKey];
- if (showings?.kind === "answered-only") return false;
- return hint.count >= (showings?.max ?? DEFAULT_HINT_MAX);
+ return hint.count >= (TIP_SHOWINGS[key as TipKey]?.max ?? DEFAULT_HINT_MAX);
}
/** How many of a person's saved lessons have stopped offering themselves. */
diff --git a/products/desktop/packages/ui/src/features/settings/tipKeys.ts b/products/desktop/packages/ui/src/features/settings/tipKeys.ts
index bef920ea8518..09371ad9f661 100644
--- a/products/desktop/packages/ui/src/features/settings/tipKeys.ts
+++ b/products/desktop/packages/ui/src/features/settings/tipKeys.ts
@@ -23,12 +23,8 @@ export const TIP_KEYS = {
export type TipKey = (typeof TIP_KEYS)[keyof typeof TIP_KEYS];
-/**
- * How a lesson stops offering itself. The toast hints fade after a few
- * showings; the anchored tips never count showings at all, and only the person
- * answering ends them, so a limit would mean nothing to them.
- */
-type TipShowings = { kind: "counted"; max: number } | { kind: "answered-only" };
+/** How a lesson stops offering itself: answered, or out of showings. */
+type TipShowings = { max: number };
/**
* Every lesson's stopping rule, keyed the same way, so it is readable from
@@ -36,9 +32,9 @@ type TipShowings = { kind: "counted"; max: number } | { kind: "answered-only" };
* it to tell a tip that has stopped showing from one still waiting to be seen.
*/
export const TIP_SHOWINGS: Record<TipKey, TipShowings> = {
- [TIP_KEYS.sessionArtifactsLocation]: { kind: "answered-only" },
- [TIP_KEYS.pasteInline]: { kind: "counted", max: 3 },
- [TIP_KEYS.pasteAsFile]: { kind: "counted", max: 3 },
- [TIP_KEYS.recallMessageNav]: { kind: "counted", max: 3 },
- [TIP_KEYS.steerSafeBoundary]: { kind: "counted", max: 1 },
+ [TIP_KEYS.sessionArtifactsLocation]: { max: 3 },
+ [TIP_KEYS.pasteInline]: { max: 3 },
+ [TIP_KEYS.pasteAsFile]: { max: 3 },
+ [TIP_KEYS.recallMessageNav]: { max: 3 },
+ [TIP_KEYS.steerSafeBoundary]: { max: 1 },
};
diff --git a/products/desktop/packages/ui/src/primitives/TeachingTip.test.tsx b/products/desktop/packages/ui/src/primitives/TeachingTip.test.tsx
index 351ac93a96a6..9c2940a00358 100644
--- a/products/desktop/packages/ui/src/primitives/TeachingTip.test.tsx
+++ b/products/desktop/packages/ui/src/primitives/TeachingTip.test.tsx
@@ -1,4 +1,8 @@
-import { useSettingsStore } from "@posthog/ui/features/settings/settingsStore";
+import {
+ DEFAULT_HINT_MAX,
+ useSettingsStore,
+} from "@posthog/ui/features/settings/settingsStore";
+import { useRendererWindowFocusStore } from "@posthog/ui/shell/rendererWindowFocusStore";
import { Theme } from "@radix-ui/themes";
import {
act,
@@ -20,8 +24,14 @@ beforeEach(() => {
tipsEnabled: true,
_hasHydrated: true,
});
+ // Whether the test runner's document reports focus is beside the point of
+ // every test that is not about it.
+ useRendererWindowFocusStore.setState({ focused: true });
});
+const windowInFront = (focused: boolean) =>
+ act(() => useRendererWindowFocusStore.setState({ focused }));
+
function tip(id: string, open: boolean, moment?: number) {
return (
<Theme>
@@ -71,6 +81,74 @@ describe("TeachingTip", () => {
await expectNoTip();
});
+ // A lesson nobody answers must still end, or a recurring occasion (the
+ // artifacts mark returns on every restart) nags forever. What runs out is
+ // showings on screen, not occasions: one artifact a person ignores through
+ // several turns puts the tip back up on each of them, and each one counts.
+ it.each([
+ ["a new artifact ends each showing", "dismissed" as const],
+ ["one artifact outlasts several turns", "turn-restarted" as const],
+ ])(
+ "stops offering itself once it runs out of showings: %s",
+ async (_ending, howShowingsEnd) => {
+ // A turn starting takes the tip down and leaves `moment` where it was.
+ const momentFor = (showing: number) =>
+ howShowingsEnd === "dismissed" ? showing : 1;
+ const { rerender } = render(tip("tip-counted", true, momentFor(1)));
+
+ for (let showing = 1; showing <= DEFAULT_HINT_MAX; showing++) {
+ rerender(tip("tip-counted", true, momentFor(showing)));
+ await findTip();
+ if (howShowingsEnd === "dismissed") {
+ fireEvent.keyDown(document.body, { key: "Escape" });
+ } else {
+ rerender(tip("tip-counted", false, momentFor(showing)));
+ }
+ await expectNoTip();
+ }
+
+ rerender(tip("tip-counted", true, momentFor(DEFAULT_HINT_MAX + 1)));
+
+ await expectNoTip();
+ },
+ );
+
+ // Turns run long enough that people leave, and every one of them takes the
+ // tip down and puts it back. Counting those would run the lesson out behind
+ // another app's window, before anybody had a chance to read it.
+ it("spends no showings while the window is not in front", async () => {
+ windowInFront(false);
+ const { rerender } = render(tip("tip-away", true, 1));
+
+ for (let turn = 1; turn <= DEFAULT_HINT_MAX + 1; turn++) {
+ rerender(tip("tip-away", false, turn));
+ rerender(tip("tip-away", true, turn));
+ }
+ rerender(tip("tip-away", false, DEFAULT_HINT_MAX + 2));
+ await expectNoTip();
+ windowInFront(true);
+ rerender(tip("tip-away", true, DEFAULT_HINT_MAX + 2));
+
+ await findTip();
+ });
+
+ // Coming back to the window is not the tip appearing again; it is the same
+ // one, still up, still unread.
+ it("spends one showing however often the window loses focus", async () => {
+ const { rerender } = render(tip("tip-flapping", true, 1));
+ await findTip();
+
+ for (let flap = 0; flap < DEFAULT_HINT_MAX; flap++) {
+ windowInFront(false);
+ windowInFront(true);
+ }
+ rerender(tip("tip-flapping", false, 1));
+ await expectNoTip();
+ rerender(tip("tip-flapping", true, 2));
+
+ await findTip();
+ });
+
// "Hide" writes the same `hints` entry the toast hints answer, so a lesson
// learned from either surface ends for both.
it("stays away once learned from outside the tip", async () => {
diff --git a/products/desktop/packages/ui/src/primitives/TeachingTip.tsx b/products/desktop/packages/ui/src/primitives/TeachingTip.tsx
index 68c8ffb6fdfb..bb5d2ef0b8b2 100644
--- a/products/desktop/packages/ui/src/primitives/TeachingTip.tsx
+++ b/products/desktop/packages/ui/src/primitives/TeachingTip.tsx
@@ -6,9 +6,22 @@ import {
PopoverContent,
PopoverTrigger,
} from "@posthog/quill";
-import { useSettingsStore } from "@posthog/ui/features/settings/settingsStore";
+import {
+ type HintState,
+ isHintRetired,
+ useSettingsStore,
+} from "@posthog/ui/features/settings/settingsStore";
+import { useRendererWindowFocusStore } from "@posthog/ui/shell/rendererWindowFocusStore";
import type { ReactNode } from "react";
-import { useState } from "react";
+import { useEffect, useRef, useState } from "react";
+
+function discountOwnShowing(
+ hint: HintState | undefined,
+ recorded: boolean,
+): HintState | undefined {
+ if (!recorded || !hint || hint.learned) return hint;
+ return { ...hint, count: Math.max(0, hint.count - 1) };
+}
function ArrowGlyph() {
return (
@@ -53,7 +66,8 @@ export function TeachingTip({
className?: string;
children: ReactNode;
}) {
- const offerable = useSettingsStore((state) => state.shouldShowHint(id));
+ const hint = useSettingsStore((state) => state.hints[id]);
+ const tipsEnabled = useSettingsStore((state) => state.tipsEnabled);
// Wait for persisted answers, or a restart would flash already-hidden tips.
const hydrated = useSettingsStore((state) => state._hasHydrated);
const markLearned = useSettingsStore((state) => state.markHintLearned);
@@ -65,7 +79,25 @@ export function TeachingTip({
setOffered({ open, moment });
if (open) setHidden(false);
}
+ const inFront = useRendererWindowFocusStore((state) => state.focused);
+ const episode = useRef({ recorded: false });
+ const offerable =
+ tipsEnabled &&
+ !isHintRetired(id, discountOwnShowing(hint, episode.current.recorded));
const showing = open && offerable && hydrated && !hidden;
+ useEffect(() => {
+ if (!showing) {
+ episode.current.recorded = false;
+ return;
+ }
+ // A turn ending puts the tip back up whether or not anybody is watching,
+ // and turns run long enough that the window is often behind another app by
+ // then. Nothing is taught there, so spend the showing once the window comes
+ // back in front. Focus leaving and returning is still the same showing.
+ if (!inFront || episode.current.recorded) return;
+ episode.current.recorded = true;
+ useSettingsStore.getState().recordHintShown(id);
+ }, [showing, inFront, id]);
return (
<Popover