Open History to see the last commit
1import type { IssueAssigneeCandidate, IssueDetail, IssueLabelCandidate } from "@t3tools/contracts";
2
3import { selectionChanges, type SelectionDraft } from "./pullRequestSelection.ts";
4
5type Labelled = Pick<IssueDetail, "labels">;
6type Assigned = Pick<IssueDetail, "assignees">;
7
8/**
9 * The loaded people a query matches by name or by the tracker's own search, which also reads
10 * emails, then the people only that search found.
11 */
12function mergeIssueAssigneeCandidates(
13 loaded: ReadonlyArray<IssueAssigneeCandidate>,
14 searched: ReadonlyArray<IssueAssigneeCandidate>,
15 query: string,
16): ReadonlyArray<IssueAssigneeCandidate> {
17 const needle = query.trim().toLowerCase();
18 const known = new Set(loaded.map((candidate) => candidate.id));
19 const matched = new Set(searched.map((candidate) => candidate.id));
20 return [
21 ...loaded.filter(
22 (candidate) =>
23 matched.has(candidate.id) ||
24 `${candidate.login} ${candidate.name ?? ""}`.toLowerCase().includes(needle),
25 ),
26 ...searched.filter((candidate) => !known.has(candidate.id)),
27 ];
28}
29
30/** An assignee picker's list while the tracker searches past what it loaded; a stale answer never shows. */
31export function issueAssigneeSearchView(input: {
32 readonly loaded: ReadonlyArray<IssueAssigneeCandidate>;
33 readonly searchable: boolean;
34 readonly query: string;
35 readonly debouncedQuery: string;
36 readonly search: {
37 readonly data: { readonly assignees: ReadonlyArray<IssueAssigneeCandidate> } | null;
38 readonly isPending: boolean;
39 readonly error: string | null;
40 };
41}) {
42 const searching = input.searchable && input.query.trim().length > 0;
43 const current = searching && input.query.trim() === input.debouncedQuery;
44 const candidates = mergeIssueAssigneeCandidates(
45 input.loaded,
46 current ? (input.search.data?.assignees ?? []) : [],
47 input.query,
48 );
49 return {
50 candidates,
51 pending: searching && (!current || input.search.isPending) && candidates.length === 0,
52 error: current && candidates.length === 0 ? input.search.error : null,
53 };
54}
55
56/**
57 * An issue write replaces the whole set, so a picker's picks become one write when it closes.
58 * Both return null when the picks leave the issue as the tracker already has it.
59 */
60export function issueLabelsAfterPicks(
61 detail: Labelled,
62 draft: SelectionDraft<IssueLabelCandidate>,
63): ReadonlyArray<string> | null {
64 const names = detail.labels.map((label) => label.name);
65 const { add, remove } = selectionChanges(draft, (name) => names.includes(name));
66 if (add.length + remove.length === 0) return null;
67 const removed = new Set(remove.map((label) => label.name));
68 return [...names.filter((name) => !removed.has(name)), ...add.map((label) => label.name)];
69}
70
71export const issueAssigneeHeld = (detail: Assigned, candidate: IssueAssigneeCandidate) =>
72 detail.assignees.some((actor) => actor.login.toLowerCase() === candidate.login.toLowerCase());
73
74/** People already assigned are written by login, the newly picked by the tracker's own id. */
75export function issueAssigneesAfterPicks(
76 detail: Assigned,
77 draft: SelectionDraft<IssueAssigneeCandidate>,
78): ReadonlyArray<string> | null {
79 const { add, remove } = selectionChanges(draft, (_, candidate) =>
80 issueAssigneeHeld(detail, candidate),
81 );
82 if (add.length + remove.length === 0) return null;
83 const removed = new Set(remove.map((candidate) => candidate.login.toLowerCase()));
84 return [
85 ...detail.assignees
86 .map((actor) => actor.login)
87 .filter((login) => !removed.has(login.toLowerCase())),
88 ...add.map((candidate) => candidate.id),
89 ];
90}
91
92/** A picker's picks, the read of the issue they were made against, and the set they last wrote. */
93export interface IssuePicks<T> {
94 readonly basis: object | null;
95 readonly draft: SelectionDraft<T>;
96 readonly written: ReadonlyArray<string> | null;
97}
98
99export function noIssuePicks<T>(): IssuePicks<T> {
100 return { basis: null, draft: new Map(), written: null };
101}
102
103/**
104 * The picks that count against `detail`. A write replaces the whole set and the detail only
105 * catches up once it is re-read, so picks outlive their write until then: a picker reopened in
106 * between builds on them instead of on the stale set. A newer read forgets them, except in an
107 * open picker, whose picks are diffed against that read when it closes.
108 */
109export function issuePicksFor<T>(
110 picks: IssuePicks<T>,
111 detail: object,
112 open: boolean,
113): IssuePicks<T> {
114 if (picks.basis === detail) return picks;
115 return { basis: detail, draft: open ? picks.draft : new Map(), written: null };
116}
117
118/**
119 * A write that failed is forgotten along with the picks it carried, unless the picker was picked
120 * in again since: those picks stay, and the next close writes them with the ones that failed.
121 */
122export function issuePicksAfterFailedWrite<T>(
123 latest: IssuePicks<T>,
124 sent: IssuePicks<T>,
125): IssuePicks<T> {
126 if (latest.written !== sent.written) return latest;
127 return latest.draft === sent.draft ? noIssuePicks() : { ...latest, written: null };
128}
129
130/** The set a closing picker writes: null when the tracker has it or was already sent it. */
131export function issueWriteAfterPicks(
132 after: ReadonlyArray<string> | null,
133 held: ReadonlyArray<string>,
134 written: ReadonlyArray<string> | null,
135): ReadonlyArray<string> | null {
136 const target = after ?? held;
137 const sent = written ?? held;
138 const same = target.length === sent.length && target.every((value) => sent.includes(value));
139 return same ? null : target;
140}