Skip to content

Commit d92c437

Browse files
t3dotggclaude
andauthored
feat(web): ask for a GitHub star once after 5 prompts (#27832)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent be5a887 commit d92c437

13 files changed

Lines changed: 284 additions & 1 deletion

File tree

‎apps/server/src/auth/RpcAuthorization.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export const RPC_REQUIRED_SCOPES = {
141141
[WS_METHODS.pullRequestsLabelCandidates]: AuthOrchestrationReadScope,
142142
[WS_METHODS.sourceControlLookupRepository]: AuthOrchestrationReadScope,
143143
[WS_METHODS.sourceControlAppAccess]: AuthOrchestrationReadScope,
144+
[WS_METHODS.sourceControlStarred]: AuthOrchestrationReadScope,
144145
[WS_METHODS.subscribeProjectClones]: AuthOrchestrationReadScope,
145146
[WS_METHODS.projectsListEntries]: AuthFilesystemReadScope,
146147
[WS_METHODS.projectsListIgnoredFiles]: AuthFilesystemReadScope,

‎apps/server/src/observability/RpcInstrumentation.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ const RPC_AGGREGATES = {
140140
[WS_METHODS.sourceControlSignInWait]: "source-control",
141141
[WS_METHODS.sourceControlSignInCancel]: "source-control",
142142
[WS_METHODS.sourceControlAppAccess]: "source-control",
143+
[WS_METHODS.sourceControlStarred]: "source-control",
143144
[WS_METHODS.projectCloneStart]: "source-control",
144145
[WS_METHODS.projectCloneCancel]: "source-control",
145146
[WS_METHODS.projectCloneRetry]: "source-control",

‎apps/server/src/sourceControl/SourceControlSignIn.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import {
55
type SourceControlSignInInput,
66
type SourceControlSignInPrompt,
77
type SourceControlSignInResult,
8+
type SourceControlStarredInput,
89
} from "@t3tools/contracts";
910
import * as Context from "effect/Context";
1011
import * as Effect from "effect/Effect";
1112
import * as Layer from "effect/Layer";
1213

1314
import * as SourceControlBuiltInDrivers from "./builtInDrivers.ts";
1415

15-
/** Signs the server in to a forge through the forge's own app, for hosts whose driver offers it. */
16+
/** Signs the server in to a forge through the forge's own app, and reads what its account did. */
1617
export class SourceControlSignIn extends Context.Service<
1718
SourceControlSignIn,
1819
{
@@ -26,6 +27,7 @@ export class SourceControlSignIn extends Context.Service<
2627
input: SourceControlSignInInput,
2728
) => Effect.Effect<void, SourceControlSignInError>;
2829
readonly access: (input: SourceControlAppAccessInput) => Effect.Effect<SourceControlAppAccess>;
30+
readonly starred: (input: SourceControlStarredInput) => Effect.Effect<boolean>;
2931
}
3032
>()("t3/sourceControl/SourceControlSignIn") {}
3133

@@ -63,6 +65,11 @@ const make = Effect.gen(function* () {
6365
signIn === undefined ? "signed-out" : yield* signIn.access(input.host, input.repository),
6466
} satisfies SourceControlAppAccess;
6567
}),
68+
starred: Effect.fn("SourceControlSignIn.starred")(function* (input) {
69+
const isStarred = drivers.find(({ driver }) => driver.kind === input.provider)?.instance
70+
.isRepositoryStarred;
71+
return isStarred === undefined ? false : yield* isStarred(input.host, input.repository);
72+
}),
6673
});
6774
});
6875

‎apps/server/src/ws.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2681,6 +2681,7 @@ export const layerWsRpc = (
26812681
[WS_METHODS.sourceControlSignInWait]: (input) => sourceControlSignIn.wait(input),
26822682
[WS_METHODS.sourceControlSignInCancel]: (input) => sourceControlSignIn.cancel(input),
26832683
[WS_METHODS.sourceControlAppAccess]: (input) => sourceControlSignIn.access(input),
2684+
[WS_METHODS.sourceControlStarred]: (input) => sourceControlSignIn.starred(input),
26842685
[WS_METHODS.projectCloneStart]: (input) =>
26852686
projectCloneTracker.start(input, {
26862687
createProject: (project) =>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { EnvironmentId } from "@t3tools/contracts";
2+
import { describe, expect, it, vi } from "vite-plus/test";
3+
4+
import type { toastManager } from "./ui/toast";
5+
6+
const { add, starred } = vi.hoisted(() => ({
7+
add: vi.fn<typeof toastManager.add>(),
8+
starred: { value: false, duringCheck: () => {} },
9+
}));
10+
vi.mock("./ui/toast", () => ({ toastManager: { add, close: vi.fn() } }));
11+
vi.mock("../rpc/atomRegistry", () => ({ appAtomRegistry: {} }));
12+
vi.mock("../state/sourceControl", () => ({ sourceControlEnvironment: { checkStarred: {} } }));
13+
vi.mock("@t3tools/client-runtime/state/runtime", () => ({
14+
runAtomCommand: async () => {
15+
starred.duringCheck();
16+
return { _tag: "Success", value: starred.value };
17+
},
18+
}));
19+
20+
const environmentId = EnvironmentId.make("environment-1");
21+
22+
function setup({ sent = 0, writable = true, alreadyStarred = false } = {}) {
23+
add.mockReset();
24+
starred.value = alreadyStarred;
25+
starred.duringCheck = () => {};
26+
const store = new Map([["t3code:github-star-prompt:sent", String(sent)]]);
27+
vi.stubGlobal("window", {
28+
localStorage: {
29+
getItem: (key: string) => store.get(key) ?? null,
30+
setItem: (key: string, value: string) => {
31+
if (!writable) throw new Error("QuotaExceededError");
32+
store.set(key, value);
33+
},
34+
},
35+
});
36+
return store;
37+
}
38+
39+
// A fresh module import stands in for an app reload; storage survives it.
40+
async function startSession() {
41+
vi.resetModules();
42+
const session = await import("./GitHubStarPrompt");
43+
return { send: () => session.recordPromptSent(environmentId) };
44+
}
45+
46+
describe("recordPromptSent", () => {
47+
it("asks after five prompts, once per session, and never again once closed", async () => {
48+
setup();
49+
let session = await startSession();
50+
for (let sent = 0; sent < 5; sent++) await session.send();
51+
expect(add).not.toHaveBeenCalled();
52+
53+
await session.send();
54+
await session.send();
55+
expect(add).toHaveBeenCalledOnce();
56+
57+
session = await startSession();
58+
await session.send();
59+
expect(add).toHaveBeenCalledTimes(2);
60+
61+
add.mock.lastCall?.[0].onClose?.();
62+
session = await startSession();
63+
for (let sent = 0; sent < 10; sent++) await session.send();
64+
expect(add).toHaveBeenCalledTimes(2);
65+
vi.unstubAllGlobals();
66+
});
67+
68+
it("never asks when a dismissal could not be saved", async () => {
69+
setup({ sent: 5, writable: false });
70+
await (await startSession()).send();
71+
expect(add).not.toHaveBeenCalled();
72+
vi.unstubAllGlobals();
73+
});
74+
75+
it("never asks once the environment's GitHub account starred the repo", async () => {
76+
setup({ sent: 5, alreadyStarred: true });
77+
await (await startSession()).send();
78+
starred.value = false;
79+
await (await startSession()).send();
80+
expect(add).not.toHaveBeenCalled();
81+
vi.unstubAllGlobals();
82+
});
83+
84+
it("never asks when another tab dismisses it during the star check", async () => {
85+
const store = setup({ sent: 5 });
86+
starred.duringCheck = () => store.set("t3code:github-star-prompt:dismissed", "true");
87+
await (await startSession()).send();
88+
expect(add).not.toHaveBeenCalled();
89+
vi.unstubAllGlobals();
90+
});
91+
});
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { type EnvironmentId, SourceControlProviderKind } from "@t3tools/contracts";
2+
import { runAtomCommand } from "@t3tools/client-runtime/state/runtime";
3+
import * as Schema from "effect/Schema";
4+
import { StarIcon } from "lucide-react";
5+
6+
import { getLocalStorageItem, setLocalStorageItem } from "../hooks/useLocalStorage";
7+
import { readLocalApi } from "../localApi";
8+
import { appAtomRegistry } from "../rpc/atomRegistry";
9+
import { sourceControlEnvironment } from "../state/sourceControl";
10+
import { toastManager } from "./ui/toast";
11+
12+
// Separate keys so a count written by another tab can never undo a dismissal.
13+
const SENT_KEY = "t3code:github-star-prompt:sent";
14+
const DISMISSED_KEY = "t3code:github-star-prompt:dismissed";
15+
const REPOSITORY = "pingdotgg/t3code";
16+
const PROMPTS_BEFORE_ASKING = 5;
17+
18+
let shownThisSession = false;
19+
20+
function dismiss() {
21+
try {
22+
setLocalStorageItem(DISMISSED_KEY, true, Schema.Boolean);
23+
} catch {}
24+
}
25+
26+
// Every close path, including both buttons, runs `onClose`, which saves the dismissal.
27+
function showStarPrompt() {
28+
const close = () => toastManager.close(id);
29+
const id = toastManager.add({
30+
title: "Enjoying T3 Code?",
31+
description: "Give us a star on GitHub to let us know!",
32+
timeout: 0,
33+
onClose: dismiss,
34+
actionProps: {
35+
children: "Star on GitHub",
36+
onClick: () => {
37+
close();
38+
void readLocalApi()
39+
?.shell.openExternal(`https://github.com/${REPOSITORY}`)
40+
.catch(() => undefined);
41+
},
42+
},
43+
data: {
44+
actionLayout: "stacked-end",
45+
leadingIcon: <StarIcon aria-hidden className="size-4 text-warning-foreground" />,
46+
secondaryActionProps: { children: "Dismiss", onClick: close },
47+
secondaryActionVariant: "ghost",
48+
},
49+
});
50+
}
51+
52+
// The server's GitHub account stands in for the user's; it may not be theirs on a shared server.
53+
async function alreadyStarred(environmentId: EnvironmentId) {
54+
const result = await runAtomCommand(
55+
appAtomRegistry,
56+
sourceControlEnvironment.checkStarred,
57+
{
58+
environmentId,
59+
input: {
60+
provider: SourceControlProviderKind.make("github"),
61+
host: "github.com",
62+
repository: REPOSITORY,
63+
},
64+
},
65+
{ label: "check GitHub star", reportFailure: false, reportDefect: false },
66+
);
67+
return result._tag === "Success" && result.value;
68+
}
69+
70+
/**
71+
* Counts a sent prompt. After more than five, asks once per session for a star until dismissed,
72+
* unless the environment's GitHub account already starred the repo.
73+
*/
74+
export async function recordPromptSent(environmentId: EnvironmentId): Promise<void> {
75+
if (shownThisSession) return;
76+
try {
77+
if (getLocalStorageItem(DISMISSED_KEY, Schema.Boolean)) return;
78+
const sent = (getLocalStorageItem(SENT_KEY, Schema.Number) ?? 0) + 1;
79+
setLocalStorageItem(SENT_KEY, sent, Schema.Number);
80+
if (sent <= PROMPTS_BEFORE_ASKING) return;
81+
} catch {
82+
// Storage that cannot be read or written could not keep a dismissal, so never ask.
83+
return;
84+
}
85+
shownThisSession = true;
86+
if (await alreadyStarred(environmentId)) return dismiss();
87+
try {
88+
// Another tab can dismiss while the check runs.
89+
if (getLocalStorageItem(DISMISSED_KEY, Schema.Boolean)) return;
90+
} catch {
91+
return;
92+
}
93+
showStarPrompt();
94+
}

‎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+
void recordPromptSent(environmentId);
13711373
}
13721374
}
13731375

‎packages/client-runtime/src/state/sourceControl.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export function createSourceControlEnvironmentAtoms<R, E>(
3030
label: "environment-data:source-control:check-app-access",
3131
tag: WS_METHODS.sourceControlAppAccess,
3232
}),
33+
checkStarred: createEnvironmentRpcCommand(runtime, {
34+
label: "environment-data:source-control:check-starred",
35+
tag: WS_METHODS.sourceControlStarred,
36+
}),
3337
repository: createEnvironmentRpcQueryAtomFamily(runtime, {
3438
label: "environment-data:source-control:repository",
3539
tag: WS_METHODS.sourceControlLookupRepository,

‎packages/contracts/src/rpc.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ import {
395395
SourceControlRepositoryLookupInput,
396396
SourceControlAppAccess,
397397
SourceControlAppAccessInput,
398+
SourceControlStarredInput,
398399
SourceControlSignInError,
399400
SourceControlSignInInput,
400401
SourceControlSignInPrompt,
@@ -592,6 +593,7 @@ export const WS_METHODS = {
592593
sourceControlSignInWait: "sourceControl.signInWait",
593594
sourceControlSignInCancel: "sourceControl.signInCancel",
594595
sourceControlAppAccess: "sourceControl.appAccess",
596+
sourceControlStarred: "sourceControl.starred",
595597
projectCloneStart: "projectClone.start",
596598
projectCloneCancel: "projectClone.cancel",
597599
projectCloneRetry: "projectClone.retry",
@@ -1242,6 +1244,13 @@ const WsSourceControlAppAccessRpc = Rpc.make(WS_METHODS.sourceControlAppAccess,
12421244
error: EnvironmentAuthorizationError,
12431245
});
12441246

1247+
// False when the server's forge account has not starred the repository or the forge cannot say.
1248+
const WsSourceControlStarredRpc = Rpc.make(WS_METHODS.sourceControlStarred, {
1249+
payload: SourceControlStarredInput,
1250+
success: Schema.Boolean,
1251+
error: EnvironmentAuthorizationError,
1252+
});
1253+
12451254
// Returns when the project exists and cloning starts; progress uses the subscription.
12461255
const WsProjectCloneStartRpc = Rpc.make(WS_METHODS.projectCloneStart, {
12471256
payload: ProjectCloneStartInput,
@@ -2070,6 +2079,7 @@ export const WsRpcGroup = RpcGroup.make(
20702079
WsSourceControlSignInWaitRpc,
20712080
WsSourceControlSignInCancelRpc,
20722081
WsSourceControlAppAccessRpc,
2082+
WsSourceControlStarredRpc,
20732083
WsProjectCloneStartRpc,
20742084
WsProjectCloneCancelRpc,
20752085
WsProjectCloneRetryRpc,

‎packages/contracts/src/sourceControl.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,13 @@ export const SourceControlAppAccess = Schema.Struct({
226226
});
227227
export type SourceControlAppAccess = typeof SourceControlAppAccess.Type;
228228

229+
export const SourceControlStarredInput = Schema.Struct({
230+
provider: SourceControlProviderKind,
231+
host: TrimmedNonEmptyString,
232+
repository: TrimmedNonEmptyString,
233+
});
234+
export type SourceControlStarredInput = typeof SourceControlStarredInput.Type;
235+
229236
export class SourceControlSignInError extends Schema.TaggedError<SourceControlSignInError>()(
230237
"SourceControlSignInError",
231238
{

0 commit comments

Comments
 (0)