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

main
ts 71 lines 2.99 KB
Raw
1 import { errorDisplayMessage } from "@t3tools/client-runtime/errors";
2 import { useAtomRefresh, useAtomValue } from "@effect/atom-react";
3 import {
4 descriptionDraftInForce,
5 settleDescriptionSave,
6 startDescriptionSave,
7 type DescriptionDraft,
8 } from "@t3tools/client-runtime/pull-request-editing";
9 import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
10 import type { PullRequestDetail } from "@t3tools/contracts";
11 import { setMarkdownTaskChecked } from "@t3tools/shared/markdownTaskList";
12 import { AsyncResult } from "effect/reactivity";
13 import { useGT } from "gt-react-native";
14 import { useRef, useState } from "react";
15 import * as Dialog from "../../components/ui/Dialog";
16 import { composerPullRequests } from "../../state/pull-requests";
17 import { useAtomCommand } from "../../state/use-atom-command";
18 import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff";
19 import { m } from "../../i18n/messages";
20
21 /** The description with checkbox ticks shown before the host confirms them. */
22 export function usePullRequestDescription(
23 params: PullRequestParams,
24 pr: PullRequestDetail | null,
25 canEdit: boolean,
26 ) {
27 const target = pullRequestTarget(params);
28 const gt = useGT();
29 const update = useAtomCommand(composerPullRequests.update, { reportFailure: false });
30 const refreshDetail = useAtomRefresh(composerPullRequests.detail(target));
31 const reading = useAtomValue(composerPullRequests.detailReadVersion(target));
32 const savesRunning = useRef(0);
33 const [draft, setDraft] = useState<DescriptionDraft | null>(null);
34 const kept = descriptionDraftInForce(draft, pr?.url ?? "", reading);
35 if (kept !== draft) setDraft(kept);
36 const body = kept?.body ?? pr?.body ?? "";
37 const save = async (detail: PullRequestDetail, next: string) => {
38 setDraft((current) =>
39 startDescriptionSave(current, { url: detail.url, observedAt: reading }, next),
40 );
41 savesRunning.current += 1;
42 const response = await update({ ...target, input: { ...target.input, body: next } });
43 savesRunning.current -= 1;
44 const saved = !AsyncResult.isFailure(response);
45 setDraft((current) => settleDescriptionSave(current, detail.url, saved));
46 if (AsyncResult.isFailure(response)) {
47 const cause = squashAtomCommandFailure(response);
48 Dialog.alert(
49 gt("Could not save the description"),
50 cause instanceof Error
51 ? (errorDisplayMessage(cause, m) ?? cause.message)
52 : gt("Try again."),
53 );
54 }
55 if (savesRunning.current === 0) refreshDetail();
56 };
57 return {
58 body,
59 /** The edit screen starts from the host's copy, so it waits for a reading with the ticks. */
60 pending: kept !== null,
61 toggleTask:
62 canEdit && pr !== null
63 ? ({ markerOffset, checked }: { markerOffset: number; checked: boolean }) => {
64 const next = setMarkdownTaskChecked(body, markerOffset, checked);
65 if (next === body) return false;
66 void save(pr, next);
67 return true;
68 }
69 : undefined,
70 };
71 }