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

pullRequestReviewStore.ts

99 lines4.2 KB

1/**
2 * A review being written, held until it is sent.
3 *
4 * Nothing here reaches the host: a review is one request carrying every line comment and the
5 * verdict together, so a half-written one is invisible to everyone else — including on the
6 * hosts that have no pending review of their own. That also means a draft lives only as long
7 * as the tab does, which is why this is deliberately not persisted.
8 */
9import type { ProjectId, PullRequestDiffSide, PullRequestRef } from "@t3tools/contracts";
10import { create } from "zustand";
11​
12export interface PendingReviewComment {
13 readonly id: string;
14 readonly path: string;
15 /** The line in the file the comment's side names: the new file on the right, the old on the left. */
16 readonly line: number;
17 readonly side: PullRequestDiffSide;
18 readonly body: string;
20​
21/**
22 * A counter rather than anything derived from the comment: two remarks on one line can be the
23 * same length, and an id built from the draft's own contents would collide with a comment that
24 * was already removed — which shares a React key with it and, worse, makes discarding one card
25 * delete both.
26 */
27let pendingCommentSequence = 0;
28​
29export function nextPendingReviewCommentId(): string {
30 pendingCommentSequence += 1;
31 return `pending-review-comment-${pendingCommentSequence}`;
33​
34/** One pull request's draft, scoped by project as well as repository: a repository can be checked out twice. */
35export function pullRequestReviewKey(reference: PullRequestRef): string {
36 return `${reference.projectId}/${reference.repository}#${reference.number}`;
38​
39interface PullRequestReviewStoreState {
40 readonly drafts: Readonly<Record<string, ReadonlyArray<PendingReviewComment>>>;
41 readonly summaries: Readonly<Record<string, string>>;
42 readonly addComment: (key: string, comment: PendingReviewComment) => void;
43 readonly removeComment: (key: string, commentId: string) => void;
44 readonly removeComments: (key: string, commentIds: ReadonlyArray<string>) => void;
45 readonly clear: (key: string) => void;
46 readonly setSummary: (key: string, body: string) => void;
47 readonly clearSummary: (key: string, submittedBody: string) => void;
49​
50const EMPTY: ReadonlyArray<PendingReviewComment> = [];
51​
52export const usePullRequestReviewStore = create<PullRequestReviewStoreState>()((set) => ({
53 drafts: {},
54 summaries: {},
55 addComment: (key, comment) =>
56 set((state) => ({
57 drafts: { ...state.drafts, [key]: [...(state.drafts[key] ?? EMPTY), comment] },
58 })),
59 removeComment: (key, commentId) =>
60 set((state) => {
61 const remaining = (state.drafts[key] ?? EMPTY).filter((entry) => entry.id !== commentId);
62 if (remaining.length > 0) return { drafts: { ...state.drafts, [key]: remaining } };
63 const { [key]: _removed, ...rest } = state.drafts;
64 return { drafts: rest };
65 }),
66 removeComments: (key, commentIds) =>
67 set((state) => {
68 const submitted = new Set(commentIds);
69 const remaining = (state.drafts[key] ?? EMPTY).filter((entry) => !submitted.has(entry.id));
70 if (remaining.length > 0) return { drafts: { ...state.drafts, [key]: remaining } };
71 const { [key]: _removed, ...rest } = state.drafts;
72 return { drafts: rest };
73 }),
74 clear: (key) =>
75 set((state) => {
76 const { [key]: _removed, ...rest } = state.drafts;
77 return { drafts: rest };
78 }),
79 setSummary: (key, body) => set((state) => ({ summaries: { ...state.summaries, [key]: body } })),
80 clearSummary: (key, submittedBody) =>
81 set((state) => {
82 // The textarea stays editable while the request is in flight. Only remove the exact draft
83 // the host accepted; a revised summary for the same pull request is new work.
84 if (state.summaries[key] !== submittedBody) return state;
85 const { [key]: _removed, ...rest } = state.summaries;
86 return { summaries: rest };
87 }),
88}));
89​
90/** The comments a pull request's draft holds, stable across renders while it is empty. */
91export function usePendingReviewComments(reference: {
92 readonly projectId: ProjectId;
93 readonly repository: string;
94 readonly number: number;
95}): ReadonlyArray<PendingReviewComment> {
96 return usePullRequestReviewStore(
97 (store) => store.drafts[pullRequestReviewKey(reference)] ?? EMPTY,
98 );