Skip to content

Commit 9751257

Browse files
t3dotggclaude
andcommitted
feat(web): ask for a GitHub star once after 5 prompts
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c6ef3b4 commit 9751257

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { describe, expect, it, vi } from "vite-plus/test";
2+
3+
import type { toastManager } from "./ui/toast";
4+
5+
const { add } = vi.hoisted(() => ({ add: vi.fn<typeof toastManager.add>() }));
6+
vi.mock("./ui/toast", () => ({ toastManager: { add, close: vi.fn() } }));
7+
8+
function setup({ sent = 0, writable = true } = {}) {
9+
add.mockReset();
10+
const store = new Map([["t3code:github-star-prompt:sent", String(sent)]]);
11+
vi.stubGlobal("window", {
12+
localStorage: {
13+
getItem: (key: string) => store.get(key) ?? null,
14+
setItem: (key: string, value: string) => {
15+
if (!writable) throw new Error("QuotaExceededError");
16+
store.set(key, value);
17+
},
18+
},
19+
});
20+
}
21+
22+
// A fresh module import stands in for an app reload; storage survives it.
23+
async function startSession() {
24+
vi.resetModules();
25+
return import("./GitHubStarPrompt");
26+
}
27+
28+
describe("recordPromptSent", () => {
29+
it("asks after five prompts, once per session, and never again once closed", async () => {
30+
setup();
31+
let session = await startSession();
32+
for (let sent = 0; sent < 5; sent++) session.recordPromptSent();
33+
expect(add).not.toHaveBeenCalled();
34+
35+
session.recordPromptSent();
36+
session.recordPromptSent();
37+
expect(add).toHaveBeenCalledOnce();
38+
39+
session = await startSession();
40+
session.recordPromptSent();
41+
expect(add).toHaveBeenCalledTimes(2);
42+
43+
add.mock.lastCall?.[0].onClose?.();
44+
session = await startSession();
45+
for (let sent = 0; sent < 10; sent++) session.recordPromptSent();
46+
expect(add).toHaveBeenCalledTimes(2);
47+
vi.unstubAllGlobals();
48+
});
49+
50+
it("never asks when a dismissal could not be saved", async () => {
51+
setup({ sent: 5, writable: false });
52+
const session = await startSession();
53+
session.recordPromptSent();
54+
expect(add).not.toHaveBeenCalled();
55+
vi.unstubAllGlobals();
56+
});
57+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import * as Schema from "effect/Schema";
2+
import { StarIcon } from "lucide-react";
3+
4+
import { getLocalStorageItem, setLocalStorageItem } from "../hooks/useLocalStorage";
5+
import { readLocalApi } from "../localApi";
6+
import { toastManager } from "./ui/toast";
7+
8+
// Separate keys so a count written by another tab can never undo a dismissal.
9+
const SENT_KEY = "t3code:github-star-prompt:sent";
10+
const DISMISSED_KEY = "t3code:github-star-prompt:dismissed";
11+
const REPO_URL = "https://github.com/pingdotgg/t3code";
12+
const PROMPTS_BEFORE_ASKING = 5;
13+
14+
let shownThisSession = false;
15+
16+
function dismiss() {
17+
try {
18+
setLocalStorageItem(DISMISSED_KEY, true, Schema.Boolean);
19+
} catch {}
20+
}
21+
22+
// Every close path, including both buttons, runs `onClose`, which saves the dismissal.
23+
function showStarPrompt() {
24+
const close = () => toastManager.close(id);
25+
const id = toastManager.add({
26+
title: "Enjoying T3 Code?",
27+
description: "Give us a star on GitHub to let us know!",
28+
timeout: 0,
29+
onClose: dismiss,
30+
actionProps: {
31+
children: "Star on GitHub",
32+
onClick: () => {
33+
close();
34+
void readLocalApi()
35+
?.shell.openExternal(REPO_URL)
36+
.catch(() => undefined);
37+
},
38+
},
39+
data: {
40+
actionLayout: "stacked-end",
41+
leadingIcon: <StarIcon aria-hidden className="size-4 text-warning-foreground" />,
42+
secondaryActionProps: { children: "Dismiss", onClick: close },
43+
secondaryActionVariant: "ghost",
44+
},
45+
});
46+
}
47+
48+
/** Counts a sent prompt. After more than five, asks once per session for a star until dismissed. */
49+
export function recordPromptSent(): void {
50+
if (shownThisSession) return;
51+
try {
52+
if (getLocalStorageItem(DISMISSED_KEY, Schema.Boolean)) return;
53+
const sent = (getLocalStorageItem(SENT_KEY, Schema.Number) ?? 0) + 1;
54+
setLocalStorageItem(SENT_KEY, sent, Schema.Number);
55+
if (sent <= PROMPTS_BEFORE_ASKING) return;
56+
} catch {
57+
// Storage that cannot be read or written could not keep a dismissal, so never ask.
58+
return;
59+
}
60+
shownThisSession = true;
61+
showStarPrompt();
62+
}

‎apps/web/src/components/chat/useComposerSend.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import {
5858
} from "../../composer-logic";
5959
import { resolvePlanFollowUpSubmission } from "../../proposedPlan";
6060
import { type ChatMessage } from "../../types";
61+
import { recordPromptSent } from "../GitHubStarPrompt";
6162
import { stackedThreadToast, toastManager } from "../ui/toast";
6263
import { newMessageId, newThreadId } from "~/lib/utils";
6364
import { useNewThreadHandler } from "../../hooks/useHandleNewThread";
@@ -1368,6 +1369,7 @@ export function useComposerSend(input: ComposerSendInput) {
13681369
) {
13691370
handleNewThreadInActiveProject();
13701371
}
1372+
recordPromptSent();
13711373
}
13721374
}
13731375

0 commit comments

Comments
 (0)