@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 71 lines 2.59 KB
Raw
1 import { errorDisplayMessage } from "@t3tools/client-runtime/errors";
2 import { useAtomRefresh } from "@effect/atom-react";
3 import {
4 maintainerEditsChoiceInForce,
5 type MaintainerEditsChoice,
6 } from "@t3tools/client-runtime/pull-request-editing";
7 import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
8 import type { PullRequestDetail } from "@t3tools/contracts";
9 import { AsyncResult } from "effect/reactivity";
10 import { useGT } from "gt-react-native";
11 import { useRef, useState } from "react";
12 import * as Dialog from "../../components/ui/Dialog";
13 import { composerPullRequests } from "../../state/pull-requests";
14 import { useAtomCommand } from "../../state/use-atom-command";
15 import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff";
16 import { m } from "../../i18n/messages";
17
18 export function usePullRequestMaintainerEdits(
19 params: PullRequestParams,
20 pr: PullRequestDetail | null,
21 canEdit: boolean,
22 ) {
23 const target = pullRequestTarget(params);
24 const gt = useGT();
25 const update = useAtomCommand(composerPullRequests.update, { reportFailure: false });
26 const refreshDetail = useAtomRefresh(composerPullRequests.detail(target));
27 const pending = useRef(false);
28 const [saving, setSaving] = useState(false);
29 const [choice, setChoice] = useState<MaintainerEditsChoice | null>(null);
30 const kept = maintainerEditsChoiceInForce(choice, pr?.url ?? "", pr?.observedAt, saving);
31 if (kept !== choice) {
32 setChoice(kept);
33 }
34 const value = kept?.value ?? pr?.maintainerCanModify === true;
35 const save = async (next: boolean) => {
36 if (pending.current || pr === null || !canEdit) return;
37 pending.current = true;
38 const previousChoice = choice;
39 setSaving(true);
40 setChoice({ key: pr.url, observedAt: pr.observedAt, value: next });
41 const response = await update({
42 ...target,
43 input: { ...target.input, maintainerCanModify: next },
44 });
45 pending.current = false;
46 setSaving(false);
47 if (AsyncResult.isFailure(response)) {
48 setChoice(previousChoice);
49 const cause = squashAtomCommandFailure(response);
50 Dialog.alert(
51 gt("Could not save maintainer access"),
52 cause instanceof Error
53 ? (errorDisplayMessage(cause, m) ?? cause.message)
54 : gt("Try again."),
55 );
56 return;
57 }
58 refreshDetail();
59 };
60 return {
61 visible: canEdit,
62 value,
63 saving,
64 subtitle: saving
65 ? gt("Saving…")
66 : gt(
67 "Maintainers can push to this branch. If your fork has workflows, this can also give access to its secrets.",
68 ),
69 toggle: () => void save(!value),
70 };
71 }