Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

contractLabels.ts

287 lines10.0 KB

1/**
2 * Translatable copies of the English labels in `@t3tools/contracts` helpers. Same ids, same
3 * English; apps call these instead of the contracts helpers so the text follows the UI language.
4 */
5import {
6 type BrowserImportFailureReason,
7 isRuntimeModeAbove,
8 managedCapabilityDenial as contractsManagedCapabilityDenial,
9 managedUpdatesDenial as contractsManagedUpdatesDenial,
10 type ManagedPolicySummary,
11 type ProjectCloneSnapshot,
12 type ProjectCloneStage,
13 type RuntimeMode,
14 type WorktreeSetupStageId,
15 worktreeSetupStageLabel as contractsWorktreeSetupStageLabel,
16} from "@t3tools/contracts";
17import type { IdleCompactionMode } from "@t3tools/contracts/settings";
18import { mFallback, msg } from "gt-i18n";
19import type { MFunctionType } from "gt-i18n/types";
20​
21// Contracts decides each stage's English label; this table only makes that text translatable.
22const WORKTREE_SETUP_STAGE_LABELS: Readonly<Record<string, string>> = {
23 "Fetch base branch": msg("Fetch base branch"),
24 "Check out files": msg("Check out files"),
25 "Init submodules": msg("Init submodules"),
26 "Run setup script": msg("Run setup script"),
27 "Start agent": msg("Start agent"),
28};
29​
30/** @public */
31export function worktreeSetupStageLabel(
32 id: WorktreeSetupStageId,
33 resolve: MFunctionType = mFallback,
34): string {
35 const english = contractsWorktreeSetupStageLabel(id);
36 return resolve(WORKTREE_SETUP_STAGE_LABELS[english] ?? english);
38​
39const PROJECT_CLONE_STAGE_LABELS: Record<ProjectCloneStage, string> = {
40 connecting: msg("Connecting", { $context: "git clone stage" }),
41 counting: msg("Counting objects"),
42 receiving: msg("Receiving objects"),
43 resolving: msg("Resolving deltas"),
44 checkout: msg("Checking out files"),
45};
46​
47/** @public */
48export function projectCloneStageLabel(
49 stage: ProjectCloneStage,
50 resolve: MFunctionType = mFallback,
51): string {
52 return resolve(PROJECT_CLONE_STAGE_LABELS[stage]);
54​
55/** @public */
56export function projectCloneProgressSummary(
57 snapshot: Pick<ProjectCloneSnapshot, "stage" | "percent" | "detail" | "stalled">,
58 resolve: MFunctionType = mFallback,
59): string {
60 const parts = [projectCloneStageLabel(snapshot.stage, resolve)];
61 if (snapshot.percent !== null) parts.push(`${snapshot.percent}%`);
62 // The detail carries git's last transfer speed, which a stall makes a lie.
63 if (snapshot.stalled) parts.push(resolve(msg("Stalled", { $context: "git clone progress" })));
64 else if (snapshot.detail) parts.push(snapshot.detail);
65 return parts.join(" · ");
67​
68/** @public */
69export function managedUpdatesMessage(
70 organization: string,
71 resolve: MFunctionType = mFallback,
72): string {
73 return resolve(msg("{organization} manages T3 Code updates.", { organization }));
75​
76/** @public */
77export function managedNightlyDeniedMessage(
78 organization: string,
79 resolve: MFunctionType = mFallback,
80): string {
81 return resolve(
82 msg("{organization} manages T3 Code updates. Only stable releases can be installed.", {
83 organization,
84 }),
85 );
87​
88/** @public */
89export function managedUpdatesDenial(
90 summary: ManagedPolicySummary | undefined,
91 resolve: MFunctionType = mFallback,
92): string | undefined {
93 return summary !== undefined && contractsManagedUpdatesDenial(summary) !== undefined
94 ? managedUpdatesMessage(summary.organization, resolve)
95 : undefined;
97​
98/** @public */
99export function managedRuntimeModeCeilingMessage(
100 organization: string,
101 ceiling: RuntimeMode,
102 resolve: MFunctionType = mFallback,
103): string {
104 switch (ceiling) {
105 case "approval-required":
106 return resolve(msg("{organization} limits threads to Supervised.", { organization }));
107 case "auto-accept-edits":
108 return resolve(msg("{organization} limits threads to Auto-accept edits.", { organization }));
109 case "auto":
110 return resolve(msg("{organization} limits threads to Auto.", { organization }));
111 case "full-access":
112 return resolve(msg("{organization} limits threads to Full access.", { organization }));
113 }
115​
116/** @public */
117export function managedRuntimeModeDenial(
118 summary: ManagedPolicySummary | undefined,
119 mode: RuntimeMode,
120 resolve: MFunctionType = mFallback,
121): string | undefined {
122 const ceiling = summary?.limits?.maxRuntimeMode;
123 return summary !== undefined && ceiling !== undefined && isRuntimeModeAbove(mode, ceiling)
124 ? managedRuntimeModeCeilingMessage(summary.organization, ceiling, resolve)
125 : undefined;
127​
128export type ManagedCapability =
129 | "remoteClients"
130 | "connect"
131 | "webhookTasks"
132 | "browserCookieImport";
133​
134function managedCapabilityDeniedMessage(
135 organization: string,
136 capability: ManagedCapability,
137 resolve: MFunctionType,
138): string {
139 switch (capability) {
140 case "remoteClients":
141 return resolve(
142 msg("{organization}'s policy does not allow connecting from other devices.", {
143 organization,
144 }),
145 );
146 case "connect":
147 return resolve(msg("{organization}'s policy does not allow T3 Connect.", { organization }));
148 case "webhookTasks":
149 return resolve(
150 msg("{organization}'s policy does not allow webhook tasks.", { organization }),
151 );
152 case "browserCookieImport":
153 return resolve(
154 msg("{organization}'s policy does not allow importing browser cookies.", {
155 organization,
156 }),
157 );
158 }
160​
161/** @public */
162export function managedCapabilityDenial(
163 summary: ManagedPolicySummary | undefined,
164 capability: ManagedCapability,
165 resolve: MFunctionType = mFallback,
166): string | undefined {
167 return summary !== undefined &&
168 contractsManagedCapabilityDenial(summary, capability) !== undefined
169 ? managedCapabilityDeniedMessage(summary.organization, capability, resolve)
170 : undefined;
172​
173const IDLE_COMPACTION_MODE_LABELS: Record<IdleCompactionMode | "default", string> = {
174 default: msg("Use default"),
175 onReturn: msg("When you return"),
176 beforeCacheExpires: msg("Before the cache expires (beta)"),
177 off: msg("Never", { $context: "idle compaction mode" }),
178};
179​
180/** @public Labels for `IDLE_COMPACTION_MODES` values, plus the per-instance "default". */
181export function idleCompactionModeLabel(
182 mode: IdleCompactionMode | "default",
183 resolve: MFunctionType = mFallback,
184): string {
185 return resolve(IDLE_COMPACTION_MODE_LABELS[mode]);
187​
188const BROWSER_IMPORT_FAILURE_MESSAGES: Record<BrowserImportFailureReason, string> = {
189 notInstalled: msg("Not installed on this machine."),
190 needsKeychainApproval: msg("Needs Keychain access to read its cookies."),
191 keychainItemMissing: msg(
192 "No encryption key in your Keychain. Sign in to that browser once, then retry.",
193 ),
194 needsFullDiskAccess: msg(
195 "Give T3 Code Full Disk Access in System Settings → Privacy & Security, then retry.",
196 ),
197 browserRunning: msg("Quit the browser first so its cookie database can be read."),
198 unsupportedPlatform: msg("Importing from this browser isn't possible on this platform."),
199 keychainUnavailable: msg(
200 "The system keyring could not be accessed. Make sure your desktop keyring is running and unlocked, then retry.",
201 ),
202 unknownSource: msg("That browser is no longer available to import from."),
203 unknownSourceProfile: msg("That browser profile no longer exists."),
204 sessionUnavailable: msg("The target profile could not be opened."),
205 profileNotSaved: msg(
206 "The cookies were imported, but the new profile couldn't be saved. Try again.",
207 ),
208 profileLimitReached: msg(
209 "You've reached the profile limit. Delete a profile or import into an existing one.",
210 ),
211 readFailed: msg("The browser's cookie database could not be read."),
212};
213​
214/** @public `BROWSER_IMPORT_FAILURE_COPY[reason]`, translated. */
215export function browserImportFailureMessage(
216 reason: BrowserImportFailureReason,
217 resolve: MFunctionType = mFallback,
218): string {
219 return resolve(BROWSER_IMPORT_FAILURE_MESSAGES[reason]);
221​
222// The server's source-control discovery hints, in the same English; commands and URLs stay as is.
223const INSTALL_HINTS = [
224 msg("Install Git from {url} or with your package manager.", {
225 url: "https://git-scm.com/downloads",
226 }),
227 msg("Install Jujutsu with {command} or from {url}.", {
228 command: "`brew install jj`",
229 url: "https://github.com/jj-vcs/jj",
230 }),
231 msg(
232 "Install the GitHub command-line tool ({cli}) via {url} or your package manager (for example {command}).",
233 {
234 cli: "`gh`",
235 url: "https://cli.github.com/",
236 command: "`brew install gh`",
237 },
238 ),
239 msg(
240 "Install the GitLab command-line tool ({cli}) from {url} or your package manager (for example {command}).",
241 {
242 cli: "`glab`",
243 url: "https://gitlab.com/gitlab-org/cli",
244 command: "`brew install glab`",
245 },
246 ),
247 msg(
248 "Install the Azure command-line tools ({cli}), then enable Azure DevOps support with {command}.",
249 {
250 cli: "`az`",
251 command: "`az extension add --name azure-devops`",
252 },
253 ),
254 msg("Add a Bitbucket token in Settings → Source control & issues."),
255 msg("Install the Origin CLI with {install}, then run {login}.", {
256 install: "`curl -fsSL https://downloads.cursor.com/origin/install.sh | sh`",
257 login: "`origin auth login`",
258 }),
259 msg(
260 "Install {fj} 0.6 or later from {fjUrl} and run {fjLogin}, or install {tea} 0.16 or later from {teaUrl} and run {teaLogin} for each Forgejo or Gitea server.",
261 {
262 fj: "`fj`",
263 fjUrl: "https://codeberg.org/forgejo-contrib/forgejo-cli",
264 fjLogin: "`fj --host <server-url> auth add-token`",
265 tea: "`tea`",
266 teaUrl: "https://gitea.com/gitea/tea",
267 teaLogin: "`tea login add`",
268 },
269 ),
270 msg(
271 "Install the GitCafe CLI with {install}, then run {login}, save a token in Settings, or set {variable} on the server.",
272 {
273 install: "`bun install -g @gitcafe/cli`",
274 login: "`cafe auth login --host https://git.cafe/api`",
275 variable: "CAFE_TOKEN",
276 },
277 ),
278];
279​
280/** @public A source-control `installHint`, translated when it is one of the server's fixed hints. */
281export function sourceControlInstallHintText(
282 hint: string,
283 resolve: MFunctionType = mFallback,
284): string {
285 const known = INSTALL_HINTS.find((entry) => mFallback(entry) === hint);
286 return known === undefined ? hint : resolve(known);