Skip to content

Commit f0acbc3

Browse files
t3dotggincognitojam
authored andcommitted
feat(web): ask for a GitHub star once after 5 prompts (pingdotgg#27832)
Co-authored-by: Claude Opus 5.5 <[email protected]> Fork adaptation: The prompt names styal and links to, and checks stars on, the fork's repository, incognitojam/styal, instead of upstream's. The GitHub driver test keeps upstream's repository name, which is only an example there. The prompt's storage keys use a styal prefix, because its count and dismissal belong to a different repository from upstream's and localhost origins can hold both apps' data. (cherry picked from commit d92c437) Upstream-PR: 27832 Fork-Feature: fork-distribution-identity, fork-runtime-storage-isolation
1 parent 6bbf52e commit f0acbc3

14 files changed

Lines changed: 285 additions & 1 deletion

File tree

‎.github/fork-features.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2218,6 +2218,7 @@ features:
22182218
- apps/server/vite.config.ts
22192219
- apps/web/src/branding.logic.ts
22202220
- apps/web/src/branding.ts
2221+
- apps/web/src/components/GitHubStarPrompt.tsx
22212222
- apps/web/src/components/SidebarStageBackdrop.tsx
22222223
- apps/web/src/components/StyalToolIcon.tsx
22232224
- apps/web/src/components/StyalWordmark.tsx

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export const RPC_REQUIRED_SCOPES = {
156156
[WS_METHODS.sourceControlGetIssue]: AuthOrchestrationReadScope,
157157
[WS_METHODS.sourceControlResolveReferences]: AuthOrchestrationReadScope,
158158
[WS_METHODS.sourceControlAppAccess]: AuthOrchestrationReadScope,
159+
[WS_METHODS.sourceControlStarred]: AuthOrchestrationReadScope,
159160
[WS_METHODS.subscribeProjectClones]: AuthOrchestrationReadScope,
160161
[WS_METHODS.projectsListEntries]: AuthFilesystemReadScope,
161162
[WS_METHODS.projectsListIgnoredFiles]: AuthFilesystemReadScope,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ const RPC_AGGREGATES = {
156156
[WS_METHODS.sourceControlSignInWait]: "source-control",
157157
[WS_METHODS.sourceControlSignInCancel]: "source-control",
158158
[WS_METHODS.sourceControlAppAccess]: "source-control",
159+
[WS_METHODS.sourceControlStarred]: "source-control",
159160
[WS_METHODS.projectCloneStart]: "source-control",
160161
[WS_METHODS.projectCloneCancel]: "source-control",
161162
[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
>()("@styal/cli/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
@@ -2698,6 +2698,7 @@ export const layerWsRpc = (
26982698
[WS_METHODS.sourceControlSignInWait]: (input) => sourceControlSignIn.wait(input),
26992699
[WS_METHODS.sourceControlSignInCancel]: (input) => sourceControlSignIn.cancel(input),
27002700
[WS_METHODS.sourceControlAppAccess]: (input) => sourceControlSignIn.access(input),
2701+
[WS_METHODS.sourceControlStarred]: (input) => sourceControlSignIn.starred(input),
27012702
[WS_METHODS.projectCloneStart]: (input) =>
27022703
projectCloneTracker.start(input, {
27032704
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([["styal: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("styal: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 = "styal:github-star-prompt:sent";
14+
const DISMISSED_KEY = "styal:github-star-prompt:dismissed";
15+
const REPOSITORY = "incognitojam/styal";
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 styal?",
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
@@ -62,6 +62,7 @@ import {
6262
} from "../../composer-logic";
6363
import { resolvePlanFollowUpSubmission } from "../../proposedPlan";
6464
import { type ChatMessage } from "../../types";
65+
import { recordPromptSent } from "../GitHubStarPrompt";
6566
import { stackedThreadToast, toastManager } from "../ui/toast";
6667
import { newMessageId, newThreadId } from "~/lib/utils";
6768
import { useNewThreadHandler } from "../../hooks/useHandleNewThread";
@@ -1418,6 +1419,7 @@ export function useComposerSend(input: ComposerSendInput) {
14181419
) {
14191420
handleNewThreadInActiveProject();
14201421
}
1422+
void recordPromptSent(environmentId);
14211423
}
14221424
}
14231425

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export function createSourceControlEnvironmentAtoms<R, E>(
3535
label: "environment-data:source-control:check-app-access",
3636
tag: WS_METHODS.sourceControlAppAccess,
3737
}),
38+
checkStarred: createEnvironmentRpcCommand(runtime, {
39+
label: "environment-data:source-control:check-starred",
40+
tag: WS_METHODS.sourceControlStarred,
41+
}),
3842
repository: createEnvironmentRpcQueryAtomFamily(runtime, {
3943
label: "environment-data:source-control:repository",
4044
tag: WS_METHODS.sourceControlLookupRepository,

‎packages/contracts/src/rpc.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ import {
435435
SourceControlSetDefaultRepositoryInput,
436436
SourceControlAppAccess,
437437
SourceControlAppAccessInput,
438+
SourceControlStarredInput,
438439
SourceControlSignInError,
439440
SourceControlSignInInput,
440441
SourceControlSignInPrompt,
@@ -663,6 +664,7 @@ export const WS_METHODS = {
663664
sourceControlSignInWait: "sourceControl.signInWait",
664665
sourceControlSignInCancel: "sourceControl.signInCancel",
665666
sourceControlAppAccess: "sourceControl.appAccess",
667+
sourceControlStarred: "sourceControl.starred",
666668
projectCloneStart: "projectClone.start",
667669
projectCloneCancel: "projectClone.cancel",
668670
projectCloneRetry: "projectClone.retry",
@@ -1358,6 +1360,13 @@ const WsSourceControlAppAccessRpc = Rpc.make(WS_METHODS.sourceControlAppAccess,
13581360
error: EnvironmentAuthorizationError,
13591361
});
13601362

1363+
// False when the server's forge account has not starred the repository or the forge cannot say.
1364+
const WsSourceControlStarredRpc = Rpc.make(WS_METHODS.sourceControlStarred, {
1365+
payload: SourceControlStarredInput,
1366+
success: Schema.Boolean,
1367+
error: EnvironmentAuthorizationError,
1368+
});
1369+
13611370
// Returns when the project exists and cloning starts; progress uses the subscription.
13621371
const WsProjectCloneStartRpc = Rpc.make(WS_METHODS.projectCloneStart, {
13631372
payload: ProjectCloneStartInput,
@@ -2315,6 +2324,7 @@ export const WsRpcGroup = RpcGroup.make(
23152324
WsSourceControlSignInWaitRpc,
23162325
WsSourceControlSignInCancelRpc,
23172326
WsSourceControlAppAccessRpc,
2327+
WsSourceControlStarredRpc,
23182328
WsProjectCloneStartRpc,
23192329
WsProjectCloneCancelRpc,
23202330
WsProjectCloneRetryRpc,

0 commit comments

Comments
 (0)