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