|
| 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 | +} |
0 commit comments