The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { PullRequestRef, PullRequestReviewCommentDraft } from "@t3tools/contracts"; |
| 2 | import { create } from "zustand"; |
| 3 | |
| 4 | export type PendingReviewComment = PullRequestReviewCommentDraft & { readonly id: string }; |
| 5 | |
| 6 | // Content-derived ids can collide across comments; use a counter. |
| 7 | let pendingCommentSequence = 0; |
| 8 | |
| 9 | export function nextPendingReviewCommentId(): string { |
| 10 | pendingCommentSequence += 1; |
| 11 | return `pending-review-comment-${pendingCommentSequence}`; |
| 12 | } |
| 13 | |
| 14 | export 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 | |
| 23 | interface 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 | |
| 34 | const EMPTY: ReadonlyArray<PendingReviewComment> = []; |
| 35 | |
| 36 | export 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 | |
| 73 | export function usePendingReviewComments( |
| 74 | reference: PullRequestRef, |
| 75 | ): ReadonlyArray<PendingReviewComment> { |
| 76 | return usePullRequestReviewStore( |
| 77 | (store) => store.drafts[pullRequestReviewKey(reference)] ?? EMPTY, |
| 78 | ); |
| 79 | } |