@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
tsx 109 lines 5.04 KB
Raw
1 import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
2 import { useAtom } from "@effect/atom-react";
3 import {
4 issueLabelsAfterPicks,
5 issuePicksAfterFailedWrite,
6 issuePicksFor,
7 issueWriteAfterPicks,
8 noIssuePicks,
9 } from "@t3tools/client-runtime/issue-selection";
10 import { isSelected, toggleSelection } from "@t3tools/client-runtime/pull-request-selection";
11 import type { IssueLabelCandidate } from "@t3tools/contracts";
12 import { msg, useGT } from "gt-react-native";
13 import { useEffect, useState } from "react";
14
15 import { m } from "../../i18n/messages";
16 import * as Dialog from "../../components/ui/Dialog";
17 import { issueEnvironment, refreshIssueLists } from "../../state/issues";
18 import { useEnvironmentQuery } from "../../state/query";
19 import { useAtomCommand } from "../../state/use-atom-command";
20 import { PullRequestLabelDot } from "../pull-requests/PullRequestLabelDot";
21 import { pullRequestLabelColor } from "../pull-requests/pullRequestList";
22 import { IssueCandidatesSheet } from "./IssueCandidatesSheet";
23 import { issueParamsKey, issueTarget, type IssueParams } from "./issueParams";
24 import { issueLabelPicksFamily } from "./issuePicks";
25 import { issueCommandError, useIssueAccess } from "./issuePresentation";
26
27 export function IssueLabelsScreen({ route }: StaticScreenProps<IssueParams>) {
28 const gt = useGT();
29 const navigation = useNavigation();
30 const target = issueTarget(route.params);
31 const detail = useEnvironmentQuery(issueEnvironment.detail(target)).data;
32 const access = useIssueAccess(route.params, detail);
33 const result = useEnvironmentQuery(issueEnvironment.candidates(target));
34 const update = useAtomCommand(issueEnvironment.update, { reportFailure: false });
35 const [query, setQuery] = useState("");
36 // The atom carries a closed sheet's picks to the next one; this sheet's own live here.
37 const [stored, setStored] = useAtom(issueLabelPicksFamily(issueParamsKey(route.params)));
38 // Picks against an older read are dropped once, when the sheet opens.
39 const [picks, setPicks] = useState(() =>
40 detail ? issuePicksFor(stored, detail, false) : noIssuePicks<IssueLabelCandidate>(),
41 );
42 const own = detail ? issuePicksFor(picks, detail, true) : noIssuePicks<IssueLabelCandidate>();
43 // What was last sent is read from the atom, so a write that fails while this sheet is open is
44 // sent again when it closes.
45 const current = { ...own, written: stored.basis === detail ? stored.written : null };
46 const draft = current.draft;
47 const allowed = access?.labels === true;
48 const applied = new Set(detail?.labels.map((label) => label.name));
49 const needle = query.trim().toLowerCase();
50 // Host order, not applied-first: re-sorting on toggle would move the row out from under a tap.
51 const candidates = (result.data?.labels ?? []).filter((label) =>
52 `${label.name} ${label.description ?? ""}`.toLowerCase().includes(needle),
53 );
54 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- picks are sent when the sheet leaves the navigation stack, however it is dismissed
55 useEffect(
56 () =>
57 navigation.addListener("beforeRemove", () => {
58 if (!detail) return;
59 const labels = issueWriteAfterPicks(
60 issueLabelsAfterPicks(detail, draft),
61 detail.labels.map((label) => label.name),
62 current.written,
63 );
64 if (labels === null) return;
65 const sent = { ...current, written: labels };
66 setStored(sent);
67 void update({ ...target, input: { ...target.input, labels } }).then((response) => {
68 const failure = issueCommandError(response);
69 if (failure === null) return refreshIssueLists(target.environmentId);
70 // Picks made after this write was sent are kept.
71 setStored((latest) => issuePicksAfterFailedWrite(latest, sent));
72 Dialog.alert(m(msg("Couldn't change labels")), failure);
73 });
74 }),
75 [navigation, detail, draft, current, setStored, update, target],
76 );
77 return (
78 <IssueCandidatesSheet
79 title={gt("Labels")}
80 noun="labels"
81 query={query}
82 onQueryChange={setQuery}
83 candidates={candidates.map((label) => ({
84 key: label.name,
85 title: label.name,
86 ...(label.description ? { subtitle: label.description } : {}),
87 leading: <PullRequestLabelDot color={pullRequestLabelColor(label.color)} />,
88 // Sent when the sheet closes; a failure puts the tracker's answer back.
89 selected: isSelected(draft, label.name, applied.has(label.name)),
90 }))}
91 allowed={allowed}
92 notice={
93 detail && !allowed
94 ? (access?.blocked ?? gt("Changing labels needs triage access on this tracker."))
95 : null
96 }
97 pending={result.isPending && !result.data}
98 loadError={result.data ? null : result.error}
99 onRetry={result.refresh}
100 truncated={result.data?.truncated === true}
101 onToggle={(name) => {
102 const label = candidates.find((entry) => entry.name === name);
103 if (label) {
104 setPicks({ ...current, draft: toggleSelection(draft, name, label, applied.has(name)) });
105 }
106 }}
107 />
108 );
109 }