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// Content-derived ids can collide across comments; use a counter.
7let pendingCommentSequence = 0;
8
9export function nextPendingReviewCommentId(): string {
10 pendingCommentSequence += 1;
11 return `pending-review-comment-${pendingCommentSequence}`;
12}
13
14export function pullRequestReviewKey(reference: PullRequestRef): string {
15 return JSON.stringify([
16 reference.projectId,
17 reference.host?.toLowerCase() ?? null,
18 reference.repository.toLowerCase(),
19 reference.number,
20 ]);
21}
22
23interface PullRequestReviewStoreState {
24 readonly drafts: Readonly<Record<string, ReadonlyArray<PendingReviewComment>>>;
25 readonly summaries: Readonly<Record<string, string>>;
26 readonly addComment: (key: string, comment: PendingReviewComment) => void;
27 readonly removeComment: (key: string, commentId: string) => void;
28 readonly removeComments: (key: string, commentIds: ReadonlyArray<string>) => void;
29 readonly clear: (key: string) => void;
30 readonly setSummary: (key: string, body: string) => void;
31 readonly clearSummary: (key: string, submittedBody: string) => void;
32}
33
34const EMPTY: ReadonlyArray<PendingReviewComment> = [];
35
36export const usePullRequestReviewStore = create<PullRequestReviewStoreState>()((set) => ({
37 drafts: {},
38 summaries: {},
39 addComment: (key, comment) =>
40 set((state) => ({
41 drafts: { ...state.drafts, [key]: [...(state.drafts[key] ?? EMPTY), comment] },
42 })),
43 removeComment: (key, commentId) =>
44 set((state) => {
45 const remaining = (state.drafts[key] ?? EMPTY).filter((entry) => entry.id !== commentId);
46 if (remaining.length > 0) return { drafts: { ...state.drafts, [key]: remaining } };
47 const { [key]: _removed, ...rest } = state.drafts;
48 return { drafts: rest };
49 }),
50 removeComments: (key, commentIds) =>
51 set((state) => {
52 const submitted = new Set(commentIds);
53 const remaining = (state.drafts[key] ?? EMPTY).filter((entry) => !submitted.has(entry.id));
54 if (remaining.length > 0) return { drafts: { ...state.drafts, [key]: remaining } };
55 const { [key]: _removed, ...rest } = state.drafts;
56 return { drafts: rest };
57 }),
58 clear: (key) =>
59 set((state) => {
60 const { [key]: _removed, ...rest } = state.drafts;
61 return { drafts: rest };
62 }),
63 setSummary: (key, body) => set((state) => ({ summaries: { ...state.summaries, [key]: body } })),
64 clearSummary: (key, submittedBody) =>
65 set((state) => {
66 // Keep summary edits made during submission; the textarea stays editable.
67 if (state.summaries[key] !== submittedBody) return state;
68 const { [key]: _removed, ...rest } = state.summaries;
69 return { summaries: rest };
70 }),
71}));
72
73export function usePendingReviewComments(
74 reference: PullRequestRef,
75): ReadonlyArray<PendingReviewComment> {
76 return usePullRequestReviewStore(
77 (store) => store.drafts[pullRequestReviewKey(reference)] ?? EMPTY,
78 );
79}