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

GitHubRoutingSettings.tsx

117 lines4.4 KB

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