Open History to see the last commit
1import { type EnvironmentId, SourceControlProviderKind } from "@t3tools/contracts";
2import { runAtomCommand } from "@t3tools/client-runtime/state/runtime";
3import * as Schema from "effect/Schema";
4import { StarIcon } from "lucide-react";
5
6import { getLocalStorageItem, setLocalStorageItem } from "../hooks/useLocalStorage";
7import { readLocalApi } from "../localApi";
8import { appAtomRegistry } from "../rpc/atomRegistry";
9import { sourceControlEnvironment } from "../state/sourceControl";
10import { toastManager } from "./ui/toast";
11
12// Separate keys so a count written by another tab can never undo a dismissal.
13const SENT_KEY = "t3code:github-star-prompt:sent";
14const DISMISSED_KEY = "t3code:github-star-prompt:dismissed";
15const REPOSITORY = "pingdotgg/t3code";
16const PROMPTS_BEFORE_ASKING = 5;
17
18let shownThisSession = false;
19
20function 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.
27function 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.
53async 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 : undefined;
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 */
74export 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 const starred = await alreadyStarred(environmentId);
87 if (starred === undefined) return;
88 if (starred) return dismiss();
89 try {
90 // Another tab can dismiss while the check runs.
91 if (getLocalStorageItem(DISMISSED_KEY, Schema.Boolean)) return;
92 } catch {
93 return;
94 }
95 showStarPrompt();
96}