Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { resolveEnvironmentMachineKind } from "@t3tools/contracts";
3import {
4 pullRequestRoutingConnectionKey,
5 pullRequestRoutingPermissionFor,
6 type PullRequestRoutingPermission,
7} from "@t3tools/client-runtime/connection";
8import { msg, T, useGT, useMessages, type Msg } from "gt-react";
9import { useState } from "react";
10
11import { environmentCatalog } from "~/connection/catalog";
12import type { MessageResolver } from "~/i18n/messages";
13import type { EnvironmentPresentation } from "~/state/environments";
14import { useAtomCommand } from "~/state/use-atom-command";
15import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
16import { toastManager } from "../ui/toast";
17import { EnvironmentRow, EnvironmentTransportLabel } from "./EnvironmentRow";
18import { FoldedSettingsSection } from "./FoldedSettingsSection";
19import { useSearchableSetting } from "./settingsSearch";
20
21const options: ReadonlyArray<{ value: PullRequestRoutingPermission; label: string | Msg }> = [
22 { value: "off", label: msg({ context: "pull request routing permission" })`Off` },
23 { value: "read", label: msg("Read PRs") },
24 { value: "read-write", label: msg("Read and act") },
25];
26
27const summaryLabels = {
28 "read-write": ({ machines }: { machines: string }) => msg`${{ machines }} read and act`,
29 read: ({ machines }: { machines: string }) => msg`${{ machines }} read PRs`,
30} as const;
31
32function summarizePullRequestRouting(
33 entries: ReadonlyArray<{
34 readonly label: string;
35 readonly permission: PullRequestRoutingPermission;
36 }>,
37 resolve: MessageResolver,
38): string | null {
39 const groups = (["read-write", "read"] as const).flatMap((permission) => {
40 const labels = entries.filter((entry) => entry.permission === permission);
41 return labels.length === 0
42 ? []
43 : [
44 resolve(
45 summaryLabels[permission]({
46 machines: labels.map((entry) => entry.label).join(", "),
47 }),
48 ),
49 ];
50 });
51 return groups.length === 0 ? null : groups.join(" · ");
52}
53
54export function PullRequestRoutingSettings({
55 environments,
56}: {
57 readonly environments: ReadonlyArray<EnvironmentPresentation>;
58}) {
59 const searchableSetting = useSearchableSetting();
60 const permissions = useAtomValue(environmentCatalog.pullRequestRoutingPermissionsValueAtom);
61 const catalog = useAtomValue(environmentCatalog.catalogValueAtom);
62 const update = useAtomCommand(environmentCatalog.setPullRequestRoutingPermission);
63 const [saving, setSaving] = useState(false);
64 const gt = useGT();
65 const m = useMessages();
66 const items = options.map((option) => ({ ...option, label: m(option.label) }));
67
68 if (environments.length < 2) return null;
69
70 const { id, title } = searchableSetting("pull-request-routing");
71 return (
72 <FoldedSettingsSection
73 id={id}
74 title={title}
75 summary={
76 summarizePullRequestRouting(
77 environments.map((environment) => ({
78 label: environment.label,
79 permission: pullRequestRoutingPermissionFor(environment.entry, permissions),
80 })),
81 m,
82 ) ?? gt({ context: "pull request routing permission" })`Off`
83 }
84 >
85 <p className="px-3 py-2.5 text-xs text-muted-foreground sm:px-4">
86 <T>
87 Machines you trust here can read PR data through each other's source control access.
88 Enable both machines. Read and act may use broader permissions than the machine that owns
89 them. This applies only to this device.
90 </T>
91 </p>
92 {environments.map((environment) => (
93 <EnvironmentRow
94 key={environment.environmentId}
95 kind={resolveEnvironmentMachineKind(environment.serverConfig)}
96 label={environment.label}
97 subtitle={<EnvironmentTransportLabel environment={environment} />}
98 >
99 <Select
100 items={items}
101 value={pullRequestRoutingPermissionFor(environment.entry, permissions)}
102 disabled={
103 !catalog.isReady ||
104 saving ||
105 pullRequestRoutingConnectionKey(environment.entry) === null
106 }
107 onValueChange={(permission) => {
108 if (permission === null) return;
109 setSaving(true);
110 void update({ environmentId: environment.environmentId, permission }).then(
111 (result) => {
112 setSaving(false);
113 if (result._tag === "Failure")
114 toastManager.add({
115 type: "error",
116 title: gt`Could not save pull request routing permission`,
117 });
118 },
119 );
120 }}
121 >
122 <SelectTrigger
123 size="xs"
124 className="w-32"
125 aria-label={gt`${{ environment: environment.label }} pull request routing`}
126 >
127 <SelectValue />
128 </SelectTrigger>
129 <SelectPopup align="end" alignItemWithTrigger={false}>
130 {items.map(({ value, label }) => (
131 <SelectItem key={value} value={value}>
132 {label}
133 </SelectItem>
134 ))}
135 </SelectPopup>
136 </Select>
137 </EnvironmentRow>
138 ))}
139 </FoldedSettingsSection>
140 );
141}