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

pullRequestReviewStore.ts

92 lines4.0 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, PullRequestRef, PullRequestReviewCommentDraft } from "@t3tools/contracts";
10import { create } from "zustand";
11​
12export type PendingReviewComment = PullRequestReviewCommentDraft & { readonly id: string };
13​
14/**
15 * A counter rather than anything derived from the comment: two remarks on one line can be the
16 * same length, and an id built from the draft's own contents would collide with a comment that
17 * was already removed — which shares a React key with it and, worse, makes discarding one card
18 * delete both.
19 */
20let pendingCommentSequence = 0;
21​
22export function nextPendingReviewCommentId(): string {
23 pendingCommentSequence += 1;
24 return `pending-review-comment-${pendingCommentSequence}`;
26​
27/** One pull request's draft, scoped by project as well as repository: a repository can be checked out twice. */
28export function pullRequestReviewKey(reference: PullRequestRef): string {
29 return `${reference.projectId}/${reference.repository}#${reference.number}`;
31​
32interface PullRequestReviewStoreState {
33 readonly drafts: Readonly<Record<string, ReadonlyArray<PendingReviewComment>>>;
34 readonly summaries: Readonly<Record<string, string>>;
35 readonly addComment: (key: string, comment: PendingReviewComment) => void;
36 readonly removeComment: (key: string, commentId: string) => void;
37 readonly removeComments: (key: string, commentIds: ReadonlyArray<string>) => void;
38 readonly clear: (key: string) => void;
39 readonly setSummary: (key: string, body: string) => void;
40 readonly clearSummary: (key: string, submittedBody: string) => void;
42​
43const EMPTY: ReadonlyArray<PendingReviewComment> = [];
44​
45export const usePullRequestReviewStore = create<PullRequestReviewStoreState>()((set) => ({
46 drafts: {},
47 summaries: {},
48 addComment: (key, comment) =>
49 set((state) => ({
50 drafts: { ...state.drafts, [key]: [...(state.drafts[key] ?? EMPTY), comment] },
51 })),
52 removeComment: (key, commentId) =>
53 set((state) => {
54 const remaining = (state.drafts[key] ?? EMPTY).filter((entry) => entry.id !== commentId);
55 if (remaining.length > 0) return { drafts: { ...state.drafts, [key]: remaining } };
56 const { [key]: _removed, ...rest } = state.drafts;
57 return { drafts: rest };
58 }),
59 removeComments: (key, commentIds) =>
60 set((state) => {
61 const submitted = new Set(commentIds);
62 const remaining = (state.drafts[key] ?? EMPTY).filter((entry) => !submitted.has(entry.id));
63 if (remaining.length > 0) return { drafts: { ...state.drafts, [key]: remaining } };
64 const { [key]: _removed, ...rest } = state.drafts;
65 return { drafts: rest };
66 }),
67 clear: (key) =>
68 set((state) => {
69 const { [key]: _removed, ...rest } = state.drafts;
70 return { drafts: rest };
71 }),
72 setSummary: (key, body) => set((state) => ({ summaries: { ...state.summaries, [key]: body } })),
73 clearSummary: (key, submittedBody) =>
74 set((state) => {
75 // The textarea stays editable while the request is in flight. Only remove the exact draft
76 // the host accepted; a revised summary for the same pull request is new work.
77 if (state.summaries[key] !== submittedBody) return state;
78 const { [key]: _removed, ...rest } = state.summaries;
79 return { summaries: rest };
80 }),
81}));
82​
83/** The comments a pull request's draft holds, stable across renders while it is empty. */
84export function usePendingReviewComments(reference: {
85 readonly projectId: ProjectId;
86 readonly repository: string;
87 readonly number: number;
88}): ReadonlyArray<PendingReviewComment> {
89 return usePullRequestReviewStore(
90 (store) => store.drafts[pullRequestReviewKey(reference)] ?? EMPTY,
91 );