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

issueTrackers.ts

117 lines4.7 KB

1import {
2 WS_METHODS,
3 type IssueTrackerConnection,
4 type IssueTrackerConnectionList,
5} from "@t3tools/contracts";
6import * as Effect from "effect/Effect";
7import { Atom } from "effect/reactivity";
8import { mFallback, msg } from "gt-i18n";
9import type { MFunctionType } from "gt-i18n/types";
10​
11import {
12 createAtomCommandScheduler,
13 createEnvironmentRpcCommand,
14 createEnvironmentRpcQueryAtomFamily,
15} from "./runtime.ts";
16import type { EnvironmentRegistry } from "../connection/registry.ts";
17import * as Persistence from "../platform/persistence.ts";
18​
19/**
20 * Standalone issue trackers (Linear, Jira) per environment: which are connected, as whom, which
21 * scopes they reach, and which project is bound to which scope. One query carries all of it, and
22 * every mutation refreshes that query, so the settings page never shows a stale status.
23 */
24export function createIssueTrackerEnvironmentAtoms<R, E>(
25 runtime: Atom.AtomRuntime<EnvironmentRegistry | Persistence.EnvironmentCacheStore | R, E>,
26) {
27 const commandScheduler = createAtomCommandScheduler();
28 const connections = createEnvironmentRpcQueryAtomFamily(runtime, {
29 label: "environment-data:issue-trackers:connections",
30 tag: WS_METHODS.issueTrackersList,
31 });
32 const refresh = (environmentId: Parameters<typeof connections>[0]["environmentId"]) =>
33 connections({ environmentId, input: {} });
34 // Connect, disconnect and bind all edit one environment's tracker state; serialized so a
35 // disconnect cannot race the bind of a project to the tracker being removed.
36 const serialPerEnvironment = {
37 mode: "serial" as const,
38 key: ({ environmentId }: { readonly environmentId: string }) => environmentId,
39 };
40 return {
41 connections,
42 /** Saves a credential the tracker accepted, or removes the stored one with a null token. */
43 connect: createEnvironmentRpcCommand(runtime, {
44 label: "environment-data:issue-trackers:connect",
45 tag: WS_METHODS.issueTrackersConnect,
46 scheduler: commandScheduler,
47 concurrency: serialPerEnvironment,
48 onSettled: (target, registry) =>
49 Effect.sync(() => registry.refresh(refresh(target.environmentId))),
50 }),
51 bindProject: createEnvironmentRpcCommand(runtime, {
52 label: "environment-data:issue-trackers:bind-project",
53 tag: WS_METHODS.issueTrackersBindProject,
54 scheduler: commandScheduler,
55 concurrency: serialPerEnvironment,
56 onSettled: (target, registry) =>
57 Effect.sync(() => registry.refresh(refresh(target.environmentId))),
58 }),
59 };
61​
62// Tracker packages name their credential and scope in English, so a sentence is translated only
63// when it matches a known tracker's whole sentence; any other tracker keeps the English one.
64function knownTrackerSentence(
65 english: string,
66 candidates: ReadonlyArray<string>,
67 resolve: MFunctionType,
68): string {
69 const known = candidates.find((candidate) => mFallback(candidate) === english);
70 return known === undefined ? english : resolve(known);
72​
73const SERVER_TRACKER_DETAILS = [msg("Issues cannot be browsed for this host yet.")];
74​
75/** A tracker's `detail` from the issue list, translated when it is a fixed server line. */
76export function issueTrackerDetailText(detail: string, resolve: MFunctionType = mFallback): string {
77 return knownTrackerSentence(detail, SERVER_TRACKER_DETAILS, resolve);
79​
80/**
81 * The line under a tracker's name. A connected tracker lists nothing until a project is linked
82 * to one of its scopes, so the line says how many are, and what to do when none is.
83 */
84export function issueTrackerSummary(
85 tracker: IssueTrackerConnection,
86 bindings: IssueTrackerConnectionList["bindings"],
87 resolve: MFunctionType = mFallback,
88): string {
89 if (!tracker.connected)
90 return knownTrackerSentence(
91 `Not connected. Add a ${tracker.credential.label.toLowerCase()} to list its issues.`,
92 [msg("Not connected. Add a personal api key to list its issues.")],
93 resolve,
94 );
95 if (tracker.account === null)
96 return tracker.detail ?? resolve(msg("The saved credential was rejected."));
97 const linked = Object.values(bindings).filter(
98 (binding) => binding.tracker === tracker.tracker,
99 ).length;
100 return linked === 0
101 ? knownTrackerSentence(
102 `Connected as ${tracker.account} · no projects linked yet. Pick a ${tracker.scopeNoun} for a project to list its issues.`,
103 [
104 msg(
105 "Connected as {account} · no projects linked yet. Pick a team for a project to list its issues.",
106 { account: tracker.account },
107 ),
108 ],
109 resolve,
110 )
111 : resolve(
112 msg(
113 "{count, plural, one {Connected as {account} · {count} project linked} other {Connected as {account} · {count} projects linked}}",
114 { account: tracker.account, count: linked },
115 ),
116 );