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

IssueAssigneesScreen.tsx

123 lines5.5 KB

1import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
2import { useAtom } from "@effect/atom-react";
3import {
4 issueAssigneeHeld,
5 issueAssigneeSearchView,
6 issueAssigneesAfterPicks,
7 issuePicksAfterFailedWrite,
8 issuePicksFor,
9 issueWriteAfterPicks,
10 noIssuePicks,
11} from "@t3tools/client-runtime/issue-selection";
12import { isSelected, toggleSelection } from "@t3tools/client-runtime/pull-request-selection";
13import type { IssueAssigneeCandidate } from "@t3tools/contracts";
14import { msg, useGT } from "gt-react-native";
15import { useEffect, useState } from "react";
16​
17import { m } from "../../i18n/messages";
18import * as Dialog from "../../components/ui/Dialog";
19import { issueEnvironment, refreshIssueLists } from "../../state/issues";
20import { useEnvironmentQuery } from "../../state/query";
21import { useDebouncedValue } from "../../state/queries";
22import { useAtomCommand } from "../../state/use-atom-command";
23import { PullRequestAvatar } from "../pull-requests/PullRequestPresentation";
24import { IssueCandidatesSheet } from "./IssueCandidatesSheet";
25import { issueParamsKey, issueTarget, type IssueParams } from "./issueParams";
26import { issueAssigneePicksFamily } from "./issuePicks";
27import { issueCommandError, useIssueAccess } from "./issuePresentation";
28​
29export function IssueAssigneesScreen({ route }: StaticScreenProps<IssueParams>) {
30 const gt = useGT();
31 const navigation = useNavigation();
32 const target = issueTarget(route.params);
33 const detail = useEnvironmentQuery(issueEnvironment.detail(target)).data;
34 const access = useIssueAccess(route.params, detail);
35 const result = useEnvironmentQuery(issueEnvironment.candidates(target));
36 const update = useAtomCommand(issueEnvironment.update, { reportFailure: false });
37 const [query, setQuery] = useState("");
38 const debouncedQuery = useDebouncedValue(query.trim(), 250);
39 const searchable = result.data?.assigneeSearch === true;
40 const hostQuery = searchable && debouncedQuery.length > 0 ? debouncedQuery : null;
41 const search = useEnvironmentQuery(
42 hostQuery === null
43 ? null
44 : issueEnvironment.candidates({ ...target, input: { ...target.input, query: hostQuery } }),
45 );
46 const view = issueAssigneeSearchView({
47 loaded: result.data?.assignees ?? [],
48 searchable,
49 query,
50 debouncedQuery,
51 search,
52 });
53 // The atom carries a closed sheet's picks to the next one; this sheet's own live here.
54 const [stored, setStored] = useAtom(issueAssigneePicksFamily(issueParamsKey(route.params)));
55 // Picks against an older read are dropped once, when the sheet opens.
56 const [picks, setPicks] = useState(() =>
57 detail ? issuePicksFor(stored, detail, false) : noIssuePicks<IssueAssigneeCandidate>(),
58 );
59 const own = detail ? issuePicksFor(picks, detail, true) : noIssuePicks<IssueAssigneeCandidate>();
60 // What was last sent is read from the atom, so a write that fails while this sheet is open is
61 // sent again when it closes.
62 const current = { ...own, written: stored.basis === detail ? stored.written : null };
63 const draft = current.draft;
64 const allowed = access?.assignees === true;
65 const held = (candidate: IssueAssigneeCandidate) =>
66 detail ? issueAssigneeHeld(detail, candidate) : false;
67 const candidates = view.candidates;
68 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- picks are sent when the sheet leaves the navigation stack, however it is dismissed
69 useEffect(
70 () =>
71 navigation.addListener("beforeRemove", () => {
72 if (!detail) return;
73 const assignees = issueWriteAfterPicks(
74 issueAssigneesAfterPicks(detail, draft),
75 detail.assignees.map((actor) => actor.login),
76 current.written,
77 );
78 if (assignees === null) return;
79 const sent = { ...current, written: assignees };
80 setStored(sent);
81 void update({ ...target, input: { ...target.input, assignees } }).then((response) => {
82 const failure = issueCommandError(response);
83 if (failure === null) return refreshIssueLists(target.environmentId);
84 // Picks made after this write was sent are kept.
85 setStored((latest) => issuePicksAfterFailedWrite(latest, sent));
86 Dialog.alert(m(msg("Couldn't change assignees")), failure);
87 });
88 }),
89 [navigation, detail, draft, current, setStored, update, target],
90 );
91 return (
92 <IssueCandidatesSheet
93 title={gt`Assignees`}
94 noun="people"
95 query={query}
96 onQueryChange={setQuery}
97 candidates={candidates.map((candidate) => ({
98 key: candidate.id,
99 title: candidate.login,
100 ...(candidate.name ? { subtitle: candidate.name } : {}),
101 leading: <PullRequestAvatar actor={candidate} size={24} />,
102 // Sent when the sheet closes; a failure puts the tracker's answer back.
103 selected: isSelected(draft, candidate.id, held(candidate)),
104 }))}
105 allowed={allowed}
106 notice={
107 detail && !allowed
108 ? (access?.blocked ?? gt`Changing assignees needs triage access on this tracker.`)
109 : null
110 }
111 pending={(result.isPending && !result.data) || view.pending}
112 loadError={result.data ? view.error : result.error}
113 onRetry={hostQuery === null ? result.refresh : search.refresh}
114 truncated={result.data?.truncated === true}
115 onToggle={(id) => {
116 const candidate = candidates.find((entry) => entry.id === id);
117 if (candidate) {
118 setPicks({ ...current, draft: toggleSelection(draft, id, candidate, held(candidate)) });
119 }
120 }}
121 />
122 );