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

pullRequestReviewStore.ts

116 lines4.7 KB

1import { type PullRequestRef, PullRequestReviewCommentDraft } from "@t3tools/contracts";
2import * as Schema from "effect/Schema";
3import { create } from "zustand";
4import { createJSONStorage, persist } from "zustand/middleware";
5​
6import { resolveStorage } from "../../lib/storage";
7import { randomUUID } from "../../lib/utils";
8​
9export type PendingReviewComment = PullRequestReviewCommentDraft & { readonly id: string };
10​
11// Content-derived ids can collide across comments, and a counter restarts below persisted ids.
12export function nextPendingReviewCommentId(): string {
13 return `pending-review-comment-${randomUUID()}`;
15​
16export function pullRequestReviewKey(reference: PullRequestRef): string {
17 return JSON.stringify([
18 reference.projectId,
19 reference.host?.toLowerCase() ?? null,
20 reference.repository.toLowerCase(),
21 reference.number,
22 ]);
24​
25interface PullRequestReviewStoreState {
26 readonly drafts: Readonly<Record<string, ReadonlyArray<PendingReviewComment>>>;
27 readonly summaries: Readonly<Record<string, string>>;
28 readonly addComment: (key: string, comment: PendingReviewComment) => void;
29 readonly removeComment: (key: string, commentId: string) => void;
30 readonly removeComments: (key: string, commentIds: ReadonlyArray<string>) => void;
31 readonly clear: (key: string) => void;
32 readonly setSummary: (key: string, body: string) => void;
33 readonly clearSummary: (key: string, submittedBody: string) => void;
35​
36const EMPTY: ReadonlyArray<PendingReviewComment> = [];
37​
38const isCommentDraft = Schema.is(PullRequestReviewCommentDraft);
39const isPendingComment = (entry: unknown): entry is PendingReviewComment =>
40 isCommentDraft(entry) && typeof (entry as { id?: unknown }).id === "string";
41​
42type PersistedReviewDrafts = Pick<PullRequestReviewStoreState, "drafts" | "summaries">;
43​
44function restoreReviewDrafts(persisted: unknown): PersistedReviewDrafts {
45 const candidate = persisted as Partial<Record<keyof PersistedReviewDrafts, unknown>> | null;
46 const drafts: Record<string, ReadonlyArray<PendingReviewComment>> = {};
47 for (const [key, comments] of Object.entries(candidate?.drafts ?? {})) {
48 const valid = Array.isArray(comments) ? comments.filter(isPendingComment) : [];
49 if (valid.length > 0) drafts[key] = valid;
50 }
51 const summaries: Record<string, string> = {};
52 for (const [key, body] of Object.entries(candidate?.summaries ?? {})) {
53 if (typeof body === "string") summaries[key] = body;
54 }
55 return { drafts, summaries };
57​
58export const usePullRequestReviewStore = create<PullRequestReviewStoreState>()(
59 persist(
60 (set) => ({
61 drafts: {},
62 summaries: {},
63 addComment: (key, comment) =>
64 set((state) => ({
65 drafts: { ...state.drafts, [key]: [...(state.drafts[key] ?? EMPTY), comment] },
66 })),
67 removeComment: (key, commentId) =>
68 set((state) => {
69 const remaining = (state.drafts[key] ?? EMPTY).filter((entry) => entry.id !== commentId);
70 if (remaining.length > 0) return { drafts: { ...state.drafts, [key]: remaining } };
71 const { [key]: _removed, ...rest } = state.drafts;
72 return { drafts: rest };
73 }),
74 removeComments: (key, commentIds) =>
75 set((state) => {
76 const submitted = new Set(commentIds);
77 const remaining = (state.drafts[key] ?? EMPTY).filter(
78 (entry) => !submitted.has(entry.id),
79 );
80 if (remaining.length > 0) return { drafts: { ...state.drafts, [key]: remaining } };
81 const { [key]: _removed, ...rest } = state.drafts;
82 return { drafts: rest };
83 }),
84 clear: (key) =>
85 set((state) => {
86 const { [key]: _removed, ...rest } = state.drafts;
87 return { drafts: rest };
88 }),
89 setSummary: (key, body) =>
90 set((state) => ({ summaries: { ...state.summaries, [key]: body } })),
91 clearSummary: (key, submittedBody) =>
92 set((state) => {
93 // Keep summary edits made during submission; the textarea stays editable.
94 if (state.summaries[key] !== submittedBody) return state;
95 const { [key]: _removed, ...rest } = state.summaries;
96 return { summaries: rest };
97 }),
98 }),
99 {
100 name: "t3code:pull-request-review-drafts:v1",
101 storage: createJSONStorage(() =>
102 resolveStorage(typeof localStorage !== "undefined" ? localStorage : undefined),
103 ),
104 partialize: ({ drafts, summaries }) => ({ drafts, summaries }),
105 merge: (persisted, current) => ({ ...current, ...restoreReviewDrafts(persisted) }),
106 },
107 ),
108);
109​
110export function usePendingReviewComments(
111 reference: PullRequestRef,
112): ReadonlyArray<PendingReviewComment> {
113 return usePullRequestReviewStore(
114 (store) => store.drafts[pullRequestReviewKey(reference)] ?? EMPTY,
115 );