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

GitHubRoutingSettings.tsx

93 lines3.6 KB

1import { useAtomValue } from "@effect/atom-react";
2import type { EnvironmentId } 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 { SettingsRow, SettingsSection } from "./settingsLayout";
16import { searchableSetting } from "./settingsSearch";
17​
18const options: ReadonlyArray<{ value: GitHubRoutingPermission; label: string }> = [
19 { value: "off", label: "Off" },
20 { value: "read", label: "Read PRs" },
21 { value: "read-write", label: "Read and act" },
22];
23​
24export function GitHubRoutingSettings({
25 environments,
26 selectedEnvironmentId,
27}: {
28 readonly environments: ReadonlyArray<EnvironmentPresentation>;
29 readonly selectedEnvironmentId: EnvironmentId;
30}) {
31 const permissions = useAtomValue(environmentCatalog.githubRoutingPermissionsValueAtom);
32 const catalog = useAtomValue(environmentCatalog.catalogValueAtom);
33 const update = useAtomCommand(environmentCatalog.setGitHubRoutingPermission);
34 const [saving, setSaving] = useState(false);
35 const selectedEnvironments = environments.filter(
36 (environment) => environment.environmentId === selectedEnvironmentId,
37 );
38​
39 return (
40 <SettingsSection {...searchableSetting("github-routing")}>
41 <SettingsRow
42 title="Share GitHub access"
43 className="sm:[&>div]:grid-cols-1 [&_p]:max-w-none"
44 description="Choose environments you trust to share PR data and use each other's GitHub access. Enable both environments. Read and act may use broader permissions than the original environment. This applies only to this client."
45 />
46 {selectedEnvironments.map((environment) => (
47 <SettingsRow
48 key={environment.environmentId}
49 title="GitHub access"
50 description={environment.displayUrl ?? "T3 Connect"}
51 control={
52 <Select
53 items={options}
54 value={gitHubRoutingPermissionFor(environment.entry, permissions)}
55 disabled={
56 !catalog.isReady || saving || gitHubRoutingConnectionKey(environment.entry) === null
57 }
58 onValueChange={(permission) => {
59 if (permission === null) return;
60 setSaving(true);
61 void update({ environmentId: environment.environmentId, permission }).then(
62 (result) => {
63 setSaving(false);
64 if (result._tag === "Failure")
65 toastManager.add({
66 type: "error",
67 title: "Could not save GitHub routing permission",
68 });
69 },
70 );
71 }}
72 >
73 <SelectTrigger
74 size="sm"
75 className="w-full sm:w-40"
76 aria-label={`${environment.label} GitHub routing`}
77 >
78 <SelectValue />
79 </SelectTrigger>
80 <SelectPopup align="end" alignItemWithTrigger={false}>
81 {options.map(({ value, label }) => (
82 <SelectItem key={value} value={value}>
83 {label}
84 </SelectItem>
85 ))}
86 </SelectPopup>
87 </Select>
88 }
89 />
90 ))}
91 </SettingsSection>
92 );