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