ref0-git 0.3.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (210) hide show
  1. package/app/apps/github/.dev.vars.example +2 -1
  2. package/app/apps/github/generated/git-rest.ts +191 -25
  3. package/app/apps/github/generated/routeTree.gen.ts +21 -0
  4. package/app/apps/github/package.json +1 -1
  5. package/app/apps/github/src/branches/branch-list.tsx +8 -4
  6. package/app/apps/github/src/branches/branches.ts +10 -5
  7. package/app/apps/github/src/camo/addresses.ts +120 -0
  8. package/app/apps/github/src/camo/client.ts +125 -0
  9. package/app/apps/github/src/camo/fetch.ts +201 -0
  10. package/app/apps/github/src/camo/lookup.ts +23 -0
  11. package/app/apps/github/src/camo/node.ts +50 -0
  12. package/app/apps/github/src/camo/proxy.ts +116 -0
  13. package/app/apps/github/src/camo/sign-images.ts +307 -0
  14. package/app/apps/github/src/camo/signing.ts +80 -0
  15. package/app/apps/github/src/clients/allowed.ts +111 -18
  16. package/app/apps/github/src/clients/clients.ts +76 -61
  17. package/app/apps/github/src/clients/transport.ts +13 -0
  18. package/app/apps/github/src/code/code-tree.tsx +3 -1
  19. package/app/apps/github/src/code/file-view.tsx +15 -6
  20. package/app/apps/github/src/code/files.ts +26 -19
  21. package/app/apps/github/src/code/go-to-file.tsx +2 -1
  22. package/app/apps/github/src/code/load.ts +41 -23
  23. package/app/apps/github/src/code/markdown-files.ts +13 -2
  24. package/app/apps/github/src/code/ref-pages.ts +14 -5
  25. package/app/apps/github/src/code/ref-switcher.tsx +4 -3
  26. package/app/apps/github/src/code/refs.ts +42 -8
  27. package/app/apps/github/src/code/repo-home.tsx +6 -2
  28. package/app/apps/github/src/code/repo.ts +12 -6
  29. package/app/apps/github/src/code/saved.ts +5 -3
  30. package/app/apps/github/src/code/trees.ts +11 -9
  31. package/app/apps/github/src/components/markdown/markdown.css +6 -0
  32. package/app/apps/github/src/components/markdown/markdown.tsx +95 -10
  33. package/app/apps/github/src/components/owner-card.tsx +3 -1
  34. package/app/apps/github/src/components/repos/your-repos.tsx +3 -1
  35. package/app/apps/github/src/components/shell/owner-repos.tsx +7 -12
  36. package/app/apps/github/src/components/shell/repo-switcher.tsx +10 -16
  37. package/app/apps/github/src/components/signin/device-card.tsx +9 -2
  38. package/app/apps/github/src/components/user-avatar.tsx +2 -1
  39. package/app/apps/github/src/context/context.ts +13 -0
  40. package/app/apps/github/src/diffs/blobs.ts +37 -14
  41. package/app/apps/github/src/diffs/diff-job.ts +53 -0
  42. package/app/apps/github/src/diffs/diff-worker.ts +6 -0
  43. package/app/apps/github/src/diffs/diffing.ts +136 -0
  44. package/app/apps/github/src/diffs/files.ts +4 -3
  45. package/app/apps/github/src/diffs/hunks.tsx +101 -53
  46. package/app/apps/github/src/diffs/patch.ts +20 -11
  47. package/app/apps/github/src/history/commit-diff.ts +10 -5
  48. package/app/apps/github/src/history/commit-list.tsx +3 -1
  49. package/app/apps/github/src/history/commit-page.tsx +4 -2
  50. package/app/apps/github/src/history/history.ts +12 -10
  51. package/app/apps/github/src/hooks/allowed.ts +9 -9
  52. package/app/apps/github/src/hooks/avatars.ts +3 -3
  53. package/app/apps/github/src/hooks/open-counts.ts +5 -4
  54. package/app/apps/github/src/hooks/profiles.ts +26 -12
  55. package/app/apps/github/src/hooks/repos.ts +78 -25
  56. package/app/apps/github/src/inbox/count.ts +30 -29
  57. package/app/apps/github/src/inbox/inbox-item.tsx +3 -1
  58. package/app/apps/github/src/inbox/inbox-page.tsx +5 -2
  59. package/app/apps/github/src/inbox/marks.ts +26 -15
  60. package/app/apps/github/src/inbox/release-tags.ts +7 -6
  61. package/app/apps/github/src/inbox/subjects.ts +5 -4
  62. package/app/apps/github/src/inbox/subscription-controls.tsx +14 -5
  63. package/app/apps/github/src/inbox/subscriptions.ts +95 -35
  64. package/app/apps/github/src/inbox/threads.ts +6 -6
  65. package/app/apps/github/src/issues/changes.ts +40 -16
  66. package/app/apps/github/src/issues/counts.ts +8 -5
  67. package/app/apps/github/src/issues/issue-list.tsx +20 -6
  68. package/app/apps/github/src/issues/issue-page.tsx +30 -14
  69. package/app/apps/github/src/issues/issues.ts +195 -46
  70. package/app/apps/github/src/issues/label-dialog.tsx +8 -11
  71. package/app/apps/github/src/issues/labels-page.tsx +35 -3
  72. package/app/apps/github/src/issues/milestone-dialog.tsx +197 -0
  73. package/app/apps/github/src/issues/milestones-page.tsx +269 -0
  74. package/app/apps/github/src/issues/new-issue.tsx +3 -1
  75. package/app/apps/github/src/issues/sidebar.tsx +8 -6
  76. package/app/apps/github/src/markdown/README.md +22 -17
  77. package/app/apps/github/src/markdown/context.ts +83 -10
  78. package/app/apps/github/src/markdown/images.ts +3 -2
  79. package/app/apps/github/src/markdown/lookup.ts +9 -8
  80. package/app/apps/github/src/markdown/page-budget.ts +50 -0
  81. package/app/apps/github/src/markdown/patches.ts +74 -32
  82. package/app/apps/github/src/markdown/query.ts +7 -2
  83. package/app/apps/github/src/markdown/remote.ts +106 -0
  84. package/app/apps/github/src/markdown/render.ts +101 -24
  85. package/app/apps/github/src/markdown/texts.ts +43 -0
  86. package/app/apps/github/src/markdown/words-module.d.ts +5 -0
  87. package/app/apps/github/src/markdown/words-plugin.ts +135 -0
  88. package/app/apps/github/src/names/repos.ts +1 -1
  89. package/app/apps/github/src/palette/palette.tsx +7 -4
  90. package/app/apps/github/src/palette/rows.tsx +54 -33
  91. package/app/apps/github/src/palette/server.ts +5 -5
  92. package/app/apps/github/src/palette/targets.ts +1 -0
  93. package/app/apps/github/src/policy.ts +165 -66
  94. package/app/apps/github/src/private-repos.ts +2 -2
  95. package/app/apps/github/src/pulls/compare-page.tsx +6 -3
  96. package/app/apps/github/src/pulls/files-tab.tsx +480 -214
  97. package/app/apps/github/src/pulls/held-summary.ts +178 -0
  98. package/app/apps/github/src/pulls/live.ts +6 -4
  99. package/app/apps/github/src/pulls/merge-box.tsx +17 -10
  100. package/app/apps/github/src/pulls/pull-list.tsx +6 -3
  101. package/app/apps/github/src/pulls/pull-page.tsx +19 -10
  102. package/app/apps/github/src/pulls/pull.ts +68 -40
  103. package/app/apps/github/src/pulls/pulls.ts +39 -22
  104. package/app/apps/github/src/pulls/review-drafts.ts +131 -0
  105. package/app/apps/github/src/pulls/review.ts +374 -99
  106. package/app/apps/github/src/pulls/threads.tsx +152 -0
  107. package/app/apps/github/src/queue/queue-page.tsx +15 -8
  108. package/app/apps/github/src/queue/queue.ts +35 -20
  109. package/app/apps/github/src/releases/latest-release.tsx +3 -1
  110. package/app/apps/github/src/releases/overview.tsx +4 -2
  111. package/app/apps/github/src/releases/release-form.tsx +8 -5
  112. package/app/apps/github/src/releases/release-list.tsx +8 -5
  113. package/app/apps/github/src/releases/release-view.tsx +1 -0
  114. package/app/apps/github/src/releases/releases.ts +33 -23
  115. package/app/apps/github/src/releases/tag-list.tsx +3 -1
  116. package/app/apps/github/src/router.tsx +5 -2
  117. package/app/apps/github/src/routes/$owner/$repo/_code/blob/$.tsx +7 -1
  118. package/app/apps/github/src/routes/$owner/$repo/_code/tree/$.tsx +6 -1
  119. package/app/apps/github/src/routes/$owner/$repo/_releases/releases/index.tsx +2 -1
  120. package/app/apps/github/src/routes/$owner/$repo/_releases/tags.tsx +1 -1
  121. package/app/apps/github/src/routes/$owner/$repo/branches.tsx +5 -1
  122. package/app/apps/github/src/routes/$owner/$repo/commit/$sha.tsx +1 -1
  123. package/app/apps/github/src/routes/$owner/$repo/commits/$.tsx +6 -1
  124. package/app/apps/github/src/routes/$owner/$repo/index.tsx +1 -1
  125. package/app/apps/github/src/routes/$owner/$repo/issues/$number.tsx +4 -2
  126. package/app/apps/github/src/routes/$owner/$repo/issues/index.tsx +2 -2
  127. package/app/apps/github/src/routes/$owner/$repo/issues/new.tsx +5 -2
  128. package/app/apps/github/src/routes/$owner/$repo/labels.tsx +2 -1
  129. package/app/apps/github/src/routes/$owner/$repo/milestones.tsx +36 -0
  130. package/app/apps/github/src/routes/$owner/$repo/pull/$number/route.tsx +4 -2
  131. package/app/apps/github/src/routes/$owner/$repo/pulls/index.tsx +2 -2
  132. package/app/apps/github/src/routes/$owner/$repo/queue.tsx +2 -1
  133. package/app/apps/github/src/routes/$owner/$repo/releases/edit/$.tsx +5 -2
  134. package/app/apps/github/src/routes/$owner/$repo/releases/new.tsx +2 -1
  135. package/app/apps/github/src/routes/$owner/$repo/releases/tag/$.tsx +4 -2
  136. package/app/apps/github/src/routes/$owner/$repo/route.tsx +9 -5
  137. package/app/apps/github/src/routes/$owner/$repo/settings/hooks/index.tsx +3 -1
  138. package/app/apps/github/src/routes/$owner/$repo/settings/index.tsx +3 -1
  139. package/app/apps/github/src/routes/-nav.tsx +10 -2
  140. package/app/apps/github/src/routes/-root-testing.ts +29 -6
  141. package/app/apps/github/src/routes/__root.tsx +9 -4
  142. package/app/apps/github/src/routes/index.tsx +3 -1
  143. package/app/apps/github/src/routes/orgs/$owner/route.tsx +4 -1
  144. package/app/apps/github/src/routes/orgs/$owner/settings.tsx +4 -1
  145. package/app/apps/github/src/seed/acme.ts +1 -1
  146. package/app/apps/github/src/seed/seed.ts +199 -110
  147. package/app/apps/github/src/server.ts +24 -15
  148. package/app/apps/github/src/settings/account-avatar.tsx +4 -1
  149. package/app/apps/github/src/settings/general-settings.tsx +3 -1
  150. package/app/apps/github/src/settings/general.ts +4 -4
  151. package/app/apps/github/src/settings/hook-form.tsx +3 -1
  152. package/app/apps/github/src/settings/hook-list.tsx +5 -2
  153. package/app/apps/github/src/settings/hooks.ts +7 -7
  154. package/app/apps/github/src/settings/org-settings.tsx +3 -1
  155. package/app/apps/github/src/settings/orgs.ts +21 -11
  156. package/app/apps/github/src/settings/settings-layout.tsx +3 -1
  157. package/app/apps/github/src/signin/approval.ts +4 -3
  158. package/app/apps/github/src/signin/auth.ts +9 -2
  159. package/app/apps/github/src/signin/device-code.ts +8 -3
  160. package/app/apps/github/src/signin/device-prompt.ts +28 -3
  161. package/app/apps/github/src/signin/revoke.ts +36 -0
  162. package/app/apps/github/tsconfig.json +4 -1
  163. package/app/apps/github/vite.config.ts +8 -0
  164. package/app/pnpm-workspace.yaml +12 -5
  165. package/app/tsconfig.base.json +1 -0
  166. package/dist/cli/child.mjs +1 -1
  167. package/dist/cli/credentials.mjs +4 -4
  168. package/dist/cli/documents.mjs +32 -22
  169. package/dist/cli/events.mjs +1 -1
  170. package/dist/cli/failure.mjs +20 -7
  171. package/dist/cli/folders.mjs +2 -2
  172. package/dist/cli/git-remote-ref0.mjs +47 -22
  173. package/dist/cli/git.mjs +1 -1
  174. package/dist/cli/helpers.mjs +2 -2
  175. package/dist/cli/hosts.mjs +3 -3
  176. package/dist/cli/output.mjs +2 -2
  177. package/dist/cli/prompt.mjs +1 -1
  178. package/dist/cli/queue.mjs +7 -7
  179. package/dist/cli/ref0-git.mjs +2 -2
  180. package/dist/cli/repos.mjs +35 -18
  181. package/dist/cli/resources.mjs +5 -5
  182. package/dist/cli/retry-after.mjs +8 -0
  183. package/dist/cli/setup.mjs +172 -66
  184. package/dist/cli/sign-in.mjs +65 -23
  185. package/dist/cli/sign-ins.mjs +4 -4
  186. package/dist/cli/sqlite.mjs +2 -2
  187. package/dist/cli/tokens.mjs +3 -3
  188. package/dist/cli/vendor.mjs +1 -1
  189. package/dist/init/app.mjs +20 -21
  190. package/dist/init/choices.mjs +1 -1
  191. package/dist/init/cloudflare.mjs +15 -19
  192. package/dist/init/create.mjs +3 -2
  193. package/dist/init/detect.mjs +4 -4
  194. package/dist/init/files.mjs +120 -13
  195. package/dist/init/frameworks.mjs +16 -0
  196. package/dist/init/git-module.mjs +22 -7
  197. package/dist/init/jsonc.mjs +9 -7
  198. package/dist/init/metadata.mjs +9 -3
  199. package/dist/init/mounts.mjs +13 -10
  200. package/dist/init/next.mjs +2 -2
  201. package/dist/init/node-app.mjs +3 -3
  202. package/dist/init/node.mjs +42 -45
  203. package/dist/init/nuxt.mjs +153 -27
  204. package/dist/init/stores.mjs +14 -9
  205. package/dist/init/sveltekit.mjs +4 -2
  206. package/dist/init/template.mjs +9 -6
  207. package/dist/init/templates.mjs +63 -47
  208. package/dist/init/vercel.mjs +37 -37
  209. package/dist/init/versions.mjs +3 -3
  210. package/package.json +6 -5
@@ -1,11 +1,13 @@
1
- import { queryOptions, type QueryClient } from '@tanstack/react-query';
2
- import { useEffect } from 'react';
1
+ import { keepPreviousData, queryOptions, type QueryClient } from '@tanstack/react-query';
2
+ import * as Schema from 'effect/Schema';
3
+ import { useEffect, useId } from 'react';
3
4
 
4
- import { git, github } from '../clients/clients.ts';
5
+ import type { Clients } from '../clients/clients.ts';
5
6
  import type { RepoPlace } from '../code/places.ts';
6
7
  import { everyPage, failure } from '../hooks/answers.ts';
7
8
  import { type Pictures, picturesOf } from '../hooks/avatars.ts';
8
- import { refreshPull, reviewsOf, type PullDetail } from './pull.ts';
9
+ import { refreshPull, reviewsKey, type PullDetail } from './pull.ts';
10
+ import { typedHeld } from './review-drafts.ts';
9
11
 
10
12
  /**
11
13
  * What a review comment's two shapes in GitHub's REST share: a pull request's comments, and a
@@ -13,9 +15,11 @@ import { refreshPull, reviewsOf, type PullDetail } from './pull.ts';
13
15
  */
14
16
  interface RestComment {
15
17
  readonly id: number | bigint;
18
+ readonly node_id: string;
16
19
  readonly user: { readonly login: string; readonly avatar_url: string } | null;
17
20
  readonly body: string;
18
21
  readonly created_at: string;
22
+ readonly commit_id: string;
19
23
  readonly original_commit_id: string;
20
24
  readonly in_reply_to_id?: number;
21
25
  readonly path: string;
@@ -42,28 +46,27 @@ export interface LinePlace {
42
46
  /** A review comment on a line of the diff, saved or a pending review's draft. */
43
47
  export interface LineComment extends LinePlace {
44
48
  readonly id: number;
49
+ /** Its node ID, which GitHub's GraphQL names a pending review's draft by. */
50
+ readonly node: string;
45
51
  readonly login: string;
46
52
  readonly body: string;
47
53
  readonly created: string;
48
54
  /**
49
- * The commit it was made on, whose lines it's on: the drop-in doesn't move them when the head
50
- * moves (Q508), and GitHub's `original_commit_id` keeps it where GitHub's does.
55
+ * The commit whose lines it's on: the head, where the drop-in moved it when the lines above it
56
+ * changed, or the commit it was made on, once it's outdated.
51
57
  */
52
58
  readonly commit: string;
59
+ /**
60
+ * Whether its lines changed at the head, as the drop-in works out (Q508, Q891): then it's on the
61
+ * lines it was made on, which the head's diff no longer shows.
62
+ */
63
+ readonly outdated: boolean;
53
64
  /** The comment whose thread it replies in, or none when it starts one. */
54
65
  readonly replyTo: number | undefined;
55
66
  /** Its author's picture, as the answer named it. */
56
67
  readonly pictures: Pictures;
57
68
  }
58
69
 
59
- /** A comment the page holds for the review the viewer is writing, until they submit it. */
60
- export interface Draft extends LinePlace {
61
- readonly key: string;
62
- readonly body: string;
63
- /** The head it was written on, whose diff its lines are numbered in. */
64
- readonly head: string;
65
- }
66
-
67
70
  /** What submitting a review says of the pull request, as GitHub's review box offers it. */
68
71
  export type ReviewEvent = 'COMMENT' | 'APPROVE' | 'REQUEST_CHANGES';
69
72
 
@@ -71,15 +74,17 @@ const sideOf = (side: string | undefined): LineSide | undefined =>
71
74
  side === 'LEFT' || side === 'RIGHT' ? side : undefined;
72
75
 
73
76
  /**
74
- * A comment on lines, where it was made, or `undefined` for one on a whole file. Its `original_*`
75
- * fields keep where it was made, which GitHub's `line` doesn't once a push moves it.
77
+ * A comment on lines, or `undefined` for one on a whole file: on the head's lines, as REST's `line`
78
+ * says them, or, once it's outdated and REST gives no `line`, where it was made, as GitHub's
79
+ * `original_*` fields keep it.
76
80
  */
77
81
  const lineCommentOf = (comment: RestComment): LineComment | undefined => {
78
82
  const side = sideOf(comment.side);
79
- const line = comment.original_line ?? comment.line ?? undefined;
83
+ const outdated = comment.line === undefined || comment.line === null;
84
+ const line = (outdated ? comment.original_line : comment.line) ?? undefined;
80
85
  if (line === undefined || side === undefined) return undefined;
81
86
 
82
- const start = comment.original_start_line ?? comment.start_line ?? undefined;
87
+ const start = (outdated ? comment.original_start_line : comment.start_line) ?? undefined;
83
88
  const range =
84
89
  start === undefined || start === line
85
90
  ? {}
@@ -87,10 +92,12 @@ const lineCommentOf = (comment: RestComment): LineComment | undefined => {
87
92
 
88
93
  return {
89
94
  id: Number(comment.id),
95
+ node: comment.node_id,
90
96
  login: comment.user?.login ?? 'ghost',
91
97
  body: comment.body,
92
98
  created: comment.created_at,
93
- commit: comment.original_commit_id,
99
+ commit: outdated ? comment.original_commit_id : comment.commit_id,
100
+ outdated,
94
101
  replyTo: comment.in_reply_to_id,
95
102
  pictures: picturesOf([comment.user]),
96
103
  path: comment.path,
@@ -103,28 +110,25 @@ const lineCommentOf = (comment: RestComment): LineComment | undefined => {
103
110
  const linesOf = (comments: readonly RestComment[]) =>
104
111
  comments.flatMap((comment) => lineCommentOf(comment) ?? []);
105
112
 
106
- /** Whether a thread begun with `first` is outdated: made on a commit that isn't `head`. */
107
- export const isOutdated = (first: LineComment, head: string) => first.commit !== head;
108
-
109
113
  /**
110
- * The comments on `head`'s diff: threads begun on it, with their replies. A thread begun on an
111
- * earlier head is outdated, and its lines may hold other code now, so it doesn't show there.
114
+ * The comments on the head's diff: threads whose first comment isn't outdated, with their replies.
115
+ * An outdated thread's lines hold other code at the head, so it doesn't show there.
112
116
  */
113
- export const onHead = (comments: readonly LineComment[], head: string) => {
117
+ export const onHead = (comments: readonly LineComment[]) => {
114
118
  const begun = new Set(
115
119
  comments.flatMap((comment) =>
116
- comment.replyTo === undefined && !isOutdated(comment, head) ? [comment.id] : [],
120
+ comment.replyTo === undefined && !comment.outdated ? [comment.id] : [],
117
121
  ),
118
122
  );
119
123
 
120
124
  return comments.filter((comment) => begun.has(comment.replyTo ?? comment.id));
121
125
  };
122
126
 
123
- /** The threads begun on an earlier head than `head`, oldest first, each with its replies. */
124
- export const outdatedThreads = (comments: readonly LineComment[], head: string) => {
127
+ /** The threads whose first comment is outdated, oldest first, each with its replies. */
128
+ export const outdatedThreads = (comments: readonly LineComment[]) => {
125
129
  const threads = new Map<number, LineComment[]>(
126
130
  comments.flatMap((comment) =>
127
- comment.replyTo === undefined && isOutdated(comment, head) ? [[comment.id, []]] : [],
131
+ comment.replyTo === undefined && comment.outdated ? [[comment.id, []]] : [],
128
132
  ),
129
133
  );
130
134
 
@@ -133,16 +137,25 @@ export const outdatedThreads = (comments: readonly LineComment[], head: string)
133
137
  return [...threads.values()];
134
138
  };
135
139
 
136
- /** `pull`'s saved review comments on lines of its diff as it stands, oldest first. */
137
- export const lineCommentsOf = (place: RepoPlace, pull: number) =>
140
+ /**
141
+ * `pull`'s saved review comments on lines of its diff at `head`, oldest first. Where each is drawn,
142
+ * and whether it's outdated, is the drop-in's answer at a head, so a push reads them again.
143
+ */
144
+ export const lineCommentsOf = (clients: Clients, place: RepoPlace, pull: number, head: string) =>
138
145
  queryOptions({
139
- queryKey: ['pull-line-comments', place.owner, place.repo, pull],
146
+ queryKey: ['pull-line-comments', place.owner, place.repo, pull, head],
140
147
  queryFn: async () => {
141
148
  const read = await everyPage(async (after) => {
142
- const answer = await github.GET('/repos/{owner}/{repo}/pulls/{pull_number}/comments', {
143
- // `after` is the drop-in's cursor, which GitHub's types don't name: a spread passes it.
144
- params: { path: { ...place, pull_number: pull }, query: { ...{ per_page: 100, after } } },
145
- });
149
+ const answer = await clients.github.GET(
150
+ '/repos/{owner}/{repo}/pulls/{pull_number}/comments',
151
+ {
152
+ // `after` is the drop-in's cursor, which GitHub's types don't name: a spread passes it.
153
+ params: {
154
+ path: { ...place, pull_number: pull },
155
+ query: { ...{ per_page: 100, after } },
156
+ },
157
+ },
158
+ );
146
159
  if (answer.data === undefined) throw failure("The review comments didn't load", answer);
147
160
 
148
161
  return { rows: linesOf(answer.data), link: answer.response.headers.get('link') };
@@ -151,18 +164,181 @@ export const lineCommentsOf = (place: RepoPlace, pull: number) =>
151
164
  return read.rows;
152
165
  },
153
166
  staleTime: 10_000,
167
+ // While a new head's comments load, the last head's stay drawn, so a thread's open reply
168
+ // isn't unmounted with its text; the new answer then moves or folds each thread.
169
+ placeholderData: keepPreviousData,
170
+ });
171
+
172
+ /** A review thread as the drop-in says it stands, by its first comment's REST `id`. */
173
+ export interface ThreadState {
174
+ /** Its GraphQL node ID, which its resolve and unresolve name. */
175
+ readonly id: string;
176
+ readonly root: number;
177
+ readonly resolved: boolean;
178
+ /** Who resolved it, while it's resolved and they're still known. */
179
+ readonly resolvedBy: string | undefined;
180
+ /** Whether the viewer may resolve it, or unresolve it, as the drop-in decides for each. */
181
+ readonly canResolve: boolean;
182
+ readonly canUnresolve: boolean;
183
+ }
184
+
185
+ const threadFields = `id isResolved viewerCanResolve viewerCanUnresolve
186
+ resolvedBy { login } comments(first: 1) { nodes { databaseId } }`;
187
+
188
+ const ThreadNode = Schema.Struct({
189
+ id: Schema.String,
190
+ isResolved: Schema.Boolean,
191
+ viewerCanResolve: Schema.Boolean,
192
+ viewerCanUnresolve: Schema.Boolean,
193
+ resolvedBy: Schema.NullOr(Schema.Struct({ login: Schema.String })),
194
+ comments: Schema.Struct({
195
+ nodes: Schema.Array(Schema.NullOr(Schema.Struct({ databaseId: Schema.NullOr(Schema.Int) }))),
196
+ }),
197
+ });
198
+
199
+ /** A thread's state from its node, or `undefined` for one whose first comment has no REST ID. */
200
+ const threadStateOf = (node: typeof ThreadNode.Type): ThreadState | undefined => {
201
+ const root = node.comments.nodes[0]?.databaseId ?? undefined;
202
+ if (root === undefined) return undefined;
203
+
204
+ return {
205
+ id: node.id,
206
+ root,
207
+ resolved: node.isResolved,
208
+ resolvedBy: node.resolvedBy?.login,
209
+ canResolve: node.viewerCanResolve,
210
+ canUnresolve: node.viewerCanUnresolve,
211
+ };
212
+ };
213
+
214
+ const threadsOperation = `query ($owner: String!, $name: String!, $number: Int!, $after: String) {
215
+ repository(owner: $owner, name: $name) {
216
+ pullRequest(number: $number) {
217
+ reviewThreads(first: 100, after: $after) {
218
+ nodes { ${threadFields} }
219
+ pageInfo { hasNextPage endCursor }
220
+ }
221
+ }
222
+ }
223
+ }`;
224
+
225
+ const ThreadsAnswer = Schema.Struct({
226
+ repository: Schema.NullOr(
227
+ Schema.Struct({
228
+ pullRequest: Schema.NullOr(
229
+ Schema.Struct({
230
+ reviewThreads: Schema.Struct({
231
+ nodes: Schema.Array(Schema.NullOr(ThreadNode)),
232
+ pageInfo: Schema.Struct({
233
+ hasNextPage: Schema.Boolean,
234
+ endCursor: Schema.NullOr(Schema.String),
235
+ }),
236
+ }),
237
+ }),
238
+ ),
239
+ }),
240
+ ),
241
+ });
242
+
243
+ /** A page of `pull`'s review threads after `after`, and the next page's cursor, if any. */
244
+ const threadsPage = async (
245
+ clients: Clients,
246
+ place: RepoPlace,
247
+ pull: number,
248
+ after: string | undefined,
249
+ ) => {
250
+ const variables = { owner: place.owner, name: place.repo, number: pull, after: after ?? null };
251
+ const answer = await clients.query(
252
+ "The review threads didn't load",
253
+ threadsOperation,
254
+ variables,
255
+ ThreadsAnswer,
256
+ );
257
+ const read = answer.repository?.pullRequest?.reviewThreads;
258
+ if (read === undefined) throw new Error("The review threads didn't load");
259
+
260
+ const states = read.nodes.flatMap((node) => (node === null ? [] : (threadStateOf(node) ?? [])));
261
+ const next = read.pageInfo.hasNextPage ? (read.pageInfo.endCursor ?? undefined) : undefined;
262
+
263
+ return { states, next };
264
+ };
265
+
266
+ /**
267
+ * `pull`'s review threads as the drop-in says they stand, by their first comment's REST `id`: the
268
+ * comments themselves are REST's, which this joins on.
269
+ */
270
+ /** Where `pull`'s review threads are read: a review's changes refresh them here. */
271
+ const threadsKey = (place: RepoPlace, pull: number) =>
272
+ ['pull-review-threads', place.owner, place.repo, pull] as const;
273
+
274
+ export const threadsOf = (clients: Clients, place: RepoPlace, pull: number) =>
275
+ queryOptions({
276
+ queryKey: threadsKey(place, pull),
277
+ queryFn: async (): Promise<ReadonlyMap<number, ThreadState>> => {
278
+ const threads = new Map<number, ThreadState>();
279
+ let after: string | undefined;
280
+
281
+ do {
282
+ const page = await threadsPage(clients, place, pull, after);
283
+
284
+ for (const state of page.states) threads.set(state.root, state);
285
+ after = page.next;
286
+ } while (after !== undefined);
287
+
288
+ return threads;
289
+ },
290
+ staleTime: 10_000,
154
291
  });
155
292
 
156
- /** The drafts of the viewer's pending review `review`, started elsewhere, such as with `gh`. */
157
- export const pendingCommentsOf = (place: RepoPlace, pull: number, review: number | undefined) =>
293
+ const changeOperation = (resolve: boolean) => {
294
+ const name = resolve ? 'resolveReviewThread' : 'unresolveReviewThread';
295
+
296
+ return `mutation ($threadId: ID!) { ${name}(input: { threadId: $threadId }) { thread { ${threadFields} } } }`;
297
+ };
298
+
299
+ const changeAnswer = (resolve: boolean) =>
300
+ Schema.Struct({
301
+ [resolve ? 'resolveReviewThread' : 'unresolveReviewThread']: Schema.NullOr(
302
+ Schema.Struct({ thread: Schema.NullOr(ThreadNode) }),
303
+ ),
304
+ });
305
+
306
+ /**
307
+ * Resolves `thread`, or unresolves it, and says how it stands after: the drop-in's answer, so two
308
+ * people resolving it at once each see the state it holds.
309
+ */
310
+ export const resolveThread = async (clients: Clients, thread: ThreadState, resolve: boolean) => {
311
+ const what = resolve ? "The thread wasn't resolved" : "The thread wasn't unresolved";
312
+ const answer = await clients.query(
313
+ what,
314
+ changeOperation(resolve),
315
+ { threadId: thread.id },
316
+ changeAnswer(resolve),
317
+ );
318
+ const node = Object.values(answer)[0]?.thread;
319
+ const state = node === null || node === undefined ? undefined : threadStateOf(node);
320
+ if (state === undefined) throw new Error(what);
321
+
322
+ return state;
323
+ };
324
+
325
+ /**
326
+ * The drafts of the viewer's pending review `review`, as the drop-in keeps them: written here, in
327
+ * another tab, or with `gh`, so a reload shows each. Every page, since bytes, not a count, bound them.
328
+ */
329
+ export const pendingCommentsOf = (
330
+ clients: Clients,
331
+ place: RepoPlace,
332
+ pull: number,
333
+ review: number | undefined,
334
+ ) =>
158
335
  queryOptions({
159
336
  queryKey: ['pull-pending-comments', place.owner, place.repo, pull, review],
160
337
  queryFn: async () => {
161
338
  if (review === undefined) return [];
162
339
 
163
- // A pending review holds at most 300 drafts (Q513): three pages of 100.
164
340
  const read = await everyPage(async (after) => {
165
- const answer = await github.GET(
341
+ const answer = await clients.github.GET(
166
342
  '/repos/{owner}/{repo}/pulls/{pull_number}/reviews/{review_id}/comments',
167
343
  {
168
344
  params: {
@@ -181,50 +357,34 @@ export const pendingCommentsOf = (place: RepoPlace, pull: number, review: number
181
357
  staleTime: 10_000,
182
358
  });
183
359
 
184
- /** The drafts the page holds for `pull`, kept across its tabs until the review is submitted. */
185
- export const draftsOf = (place: RepoPlace, pull: number) =>
186
- queryOptions({
187
- queryKey: ['pull-review-drafts', place.owner, place.repo, pull],
188
- queryFn: (): readonly Draft[] => [],
189
- staleTime: Infinity,
190
- gcTime: Infinity,
191
- });
360
+ /**
361
+ * Each review write in flight or refused, by the form it's in: the page asks before it unloads
362
+ * while any is, since that text isn't on the server. A draft once saved is, and a reload shows it.
363
+ */
364
+ const unsavedKey = ['pull-review-unsaved'] as const;
192
365
 
193
- /** What **Review changes** holds before it's submitted: its summary, and what the review says. */
194
- export interface ReviewDraft {
195
- readonly body: string;
196
- readonly event: ReviewEvent;
197
- }
366
+ /** Marks the form calling it unsaved while `unsaved` holds, and saved once it's gone. */
367
+ export const useUnsaved = (client: QueryClient, unsaved: boolean) => {
368
+ const id = useId();
198
369
 
199
- export const noReviewDraft: ReviewDraft = { body: '', event: 'COMMENT' };
370
+ useEffect(() => {
371
+ const key = [...unsavedKey, id];
372
+ client.setQueryData(key, unsaved);
200
373
 
201
- /**
202
- * The review `pull`'s **Review changes** holds, beside its line drafts and kept as they are: across
203
- * closing it, the pull request's tabs and a push, until it's submitted or discarded. It's under the
204
- * pull request's own key, so one pull request's submit leaves another's draft alone.
205
- */
206
- export const reviewDraftOf = (place: RepoPlace, pull: number) =>
207
- queryOptions({
208
- queryKey: ['pull-review-summary', place.owner, place.repo, pull],
209
- queryFn: (): ReviewDraft => noReviewDraft,
210
- staleTime: Infinity,
211
- gcTime: Infinity,
212
- });
374
+ return () => client.removeQueries({ queryKey: key, exact: true });
375
+ }, [client, id, unsaved]);
376
+ };
213
377
 
214
378
  /**
215
- * Asks before the page unloads, by a reload or a link out of the app, while any pull request's
216
- * review drafts or summary are unsent: they live only in the page, since the drop-in adds none to
217
- * a pending review (Q529). A link inside the app keeps them.
379
+ * Asks before the page unloads, by a reload or a link out of the app, while a review's write is
380
+ * in flight or was refused, or a reply or an edit is typed and not sent. Saved drafts and the saved
381
+ * summary are the drop-in's, and come back.
218
382
  */
219
383
  export const useAskBeforeLosingDrafts = (client: QueryClient) =>
220
384
  useEffect(() => {
221
385
  const ask = (event: BeforeUnloadEvent) => {
222
- const held = client.getQueriesData<readonly Draft[]>({ queryKey: ['pull-review-drafts'] });
223
- const summaries = client.getQueriesData<ReviewDraft>({ queryKey: ['pull-review-summary'] });
224
- const unsent =
225
- held.some(([, drafts]) => (drafts?.length ?? 0) > 0) ||
226
- summaries.some(([, draft]) => (draft?.body.trim() ?? '') !== '');
227
- if (unsent) event.preventDefault();
386
+ const held = client.getQueriesData<boolean>({ queryKey: unsavedKey });
387
+ if (held.some(([, unsaved]) => unsaved === true) || typedHeld(client)) event.preventDefault();
228
388
  };
229
389
 
230
390
  addEventListener('beforeunload', ask);
@@ -233,11 +393,11 @@ export const useAskBeforeLosingDrafts = (client: QueryClient) =>
233
393
  }, [client]);
234
394
 
235
395
  /** The commit `pull`'s diff is from: where its base and head last met, as Git finds it. */
236
- export const mergeBaseOf = (place: RepoPlace, pull: PullDetail) =>
396
+ export const mergeBaseOf = (clients: Clients, place: RepoPlace, pull: PullDetail) =>
237
397
  queryOptions({
238
398
  queryKey: ['pull-merge-base', place.owner, place.repo, pull.base.sha, pull.head.sha],
239
399
  queryFn: async () => {
240
- const answer = await git.POST('/repos/{repo}/-/commits/merge-base', {
400
+ const answer = await clients.git.POST('/repos/{repo}/-/commits/merge-base', {
241
401
  params: { path: { repo: `${place.owner}/${place.repo}` } },
242
402
  body: { commits: [pull.base.sha, pull.head.sha] },
243
403
  });
@@ -258,14 +418,15 @@ const placeFields = (place: LinePlace) => {
258
418
 
259
419
  /**
260
420
  * Posts one comment, a review of its own as GitHub's does, on lines of the head whose diff they
261
- * were picked from, which a push since leaves outdated.
421
+ * were picked from. A push since leaves it outdated only if it changed those lines.
262
422
  */
263
423
  export const commentOnLines = async (
424
+ clients: Clients,
264
425
  place: RepoPlace,
265
426
  pull: number,
266
427
  draft: LinePlace & { readonly body: string; readonly head: string },
267
428
  ) => {
268
- const answer = await github.POST('/repos/{owner}/{repo}/pulls/{pull_number}/comments', {
429
+ const answer = await clients.github.POST('/repos/{owner}/{repo}/pulls/{pull_number}/comments', {
269
430
  params: { path: { ...place, pull_number: pull } },
270
431
  body: { body: draft.body, commit_id: draft.head, ...placeFields(draft) },
271
432
  });
@@ -277,12 +438,13 @@ export const commentOnLines = async (
277
438
  * but its body: the place GitHub's types ask for is the thread's.
278
439
  */
279
440
  export const replyTo = async (
441
+ clients: Clients,
280
442
  place: RepoPlace,
281
443
  pull: PullDetail,
282
444
  thread: LineComment,
283
445
  body: string,
284
446
  ) => {
285
- const answer = await github.POST('/repos/{owner}/{repo}/pulls/{pull_number}/comments', {
447
+ const answer = await clients.github.POST('/repos/{owner}/{repo}/pulls/{pull_number}/comments', {
286
448
  params: { path: { ...place, pull_number: pull.number } },
287
449
  body: { body, in_reply_to: thread.id, commit_id: pull.head.sha, path: thread.path },
288
450
  });
@@ -290,39 +452,137 @@ export const replyTo = async (
290
452
  };
291
453
 
292
454
  /**
293
- * Submits a review of `review.head` with `drafts` as its comments, in one call: the head they were
294
- * written on, so each lands on the lines it was written on.
455
+ * The viewer's pending review on `head`, started with its first draft or its summary: GitHub's
456
+ * `POST …/reviews` with no event, which keeps it on the server until it's submitted or discarded.
295
457
  */
296
- export const submitReview = async (
458
+ export const startReview = async (
459
+ clients: Clients,
297
460
  place: RepoPlace,
298
461
  pull: number,
299
- review: {
300
- readonly event: ReviewEvent;
301
- readonly body: string;
462
+ start: {
302
463
  readonly head: string;
303
- readonly drafts: readonly Draft[];
464
+ readonly draft?: LinePlace & { readonly body: string };
465
+ readonly body?: string;
304
466
  },
305
467
  ) => {
306
- const answer = await github.POST('/repos/{owner}/{repo}/pulls/{pull_number}/reviews', {
468
+ const { draft } = start;
469
+ const answer = await clients.github.POST('/repos/{owner}/{repo}/pulls/{pull_number}/reviews', {
307
470
  params: { path: { ...place, pull_number: pull } },
308
471
  body: {
309
- commit_id: review.head,
310
- event: review.event,
311
- body: review.body,
312
- comments: review.drafts.map((draft) => ({ body: draft.body, ...placeFields(draft) })),
472
+ commit_id: start.head,
473
+ body: start.body ?? '',
474
+ comments: draft === undefined ? [] : [{ body: draft.body, ...placeFields(draft) }],
313
475
  },
314
476
  });
477
+ if (answer.error !== undefined) throw failure("The review wasn't started", answer);
478
+ };
479
+
480
+ /**
481
+ * A review with nothing pending, submitted at once, such as an approval with only a summary: on
482
+ * `head`, the commit the viewer read.
483
+ */
484
+ export const submitNew = async (
485
+ clients: Clients,
486
+ place: RepoPlace,
487
+ pull: number,
488
+ review: { readonly event: ReviewEvent; readonly body: string; readonly head: string },
489
+ ) => {
490
+ const answer = await clients.github.POST('/repos/{owner}/{repo}/pulls/{pull_number}/reviews', {
491
+ params: { path: { ...place, pull_number: pull } },
492
+ body: { commit_id: review.head, event: review.event, body: review.body },
493
+ });
315
494
  if (answer.error !== undefined) throw failure("The review wasn't submitted", answer);
316
495
  };
317
496
 
318
- /** Submits the viewer's pending review `review`, started elsewhere, with its drafts. */
497
+ /**
498
+ * The GraphQL operations a pending review's drafts take, one at a time, as `packages/github`'s
499
+ * `graphql/fixtures/app/` holds them, so the drop-in's served schema is held to them.
500
+ */
501
+ export const draftOperations = {
502
+ add: `mutation AddPullRequestReviewThread($review: ID!, $path: String!, $line: Int, $side: DiffSide, $startLine: Int, $startSide: DiffSide, $body: String!) {
503
+ addPullRequestReviewThread(input: { pullRequestReviewId: $review, path: $path, line: $line, side: $side, startLine: $startLine, startSide: $startSide, body: $body }) {
504
+ clientMutationId
505
+ }
506
+ }
507
+ `,
508
+ edit: `mutation UpdatePullRequestReviewComment($id: ID!, $body: String!) {
509
+ updatePullRequestReviewComment(input: { pullRequestReviewCommentId: $id, body: $body }) {
510
+ clientMutationId
511
+ }
512
+ }
513
+ `,
514
+ remove: `mutation DeletePullRequestReviewComment($id: ID!) {
515
+ deletePullRequestReviewComment(input: { id: $id }) {
516
+ clientMutationId
517
+ }
518
+ }
519
+ `,
520
+ reply: `mutation AddPullRequestReviewThreadReply($review: ID!, $thread: ID!, $body: String!) {
521
+ addPullRequestReviewThreadReply(input: { pullRequestReviewId: $review, pullRequestReviewThreadId: $thread, body: $body }) {
522
+ clientMutationId
523
+ }
524
+ }
525
+ `,
526
+ };
527
+
528
+ /** A new draft on `draft`'s lines in the viewer's pending review, its node ID `review`. */
529
+ export const addDraft = (
530
+ clients: Clients,
531
+ review: string,
532
+ draft: LinePlace & { readonly body: string },
533
+ ) =>
534
+ clients.mutate("The comment wasn't saved", draftOperations.add, {
535
+ review,
536
+ path: draft.path,
537
+ line: draft.line,
538
+ side: draft.side,
539
+ startLine: draft.startLine ?? null,
540
+ startSide: draft.startLine === undefined ? null : (draft.startSide ?? draft.side),
541
+ body: draft.body,
542
+ });
543
+
544
+ /** A draft's new body, by its node ID. */
545
+ export const editDraft = (clients: Clients, draft: string, body: string) =>
546
+ clients.mutate("The comment wasn't saved", draftOperations.edit, { id: draft, body });
547
+
548
+ /** A draft out of the viewer's pending review, by its node ID: the review stays pending. */
549
+ export const deleteDraft = (clients: Clients, draft: string) =>
550
+ clients.mutate("The comment wasn't removed", draftOperations.remove, { id: draft });
551
+
552
+ /**
553
+ * A reply in the thread `thread`, by its node ID, kept in the viewer's pending review `review` as
554
+ * a draft in that thread until the review is submitted.
555
+ */
556
+ export const replyDraft = (clients: Clients, review: string, thread: string, body: string) =>
557
+ clients.mutate("The reply wasn't saved", draftOperations.reply, { review, thread, body });
558
+
559
+ /** The summary of the viewer's pending review `review`, saved as its body. */
560
+ export const saveSummary = async (
561
+ clients: Clients,
562
+ place: RepoPlace,
563
+ pull: number,
564
+ review: number,
565
+ body: string,
566
+ ) => {
567
+ const answer = await clients.github.PUT(
568
+ '/repos/{owner}/{repo}/pulls/{pull_number}/reviews/{review_id}',
569
+ {
570
+ params: { path: { ...place, pull_number: pull, review_id: review } },
571
+ body: { body },
572
+ },
573
+ );
574
+ if (answer.error !== undefined) throw failure("The summary wasn't saved", answer);
575
+ };
576
+
577
+ /** Submits the viewer's pending review `review`, with its drafts. */
319
578
  export const submitPending = async (
579
+ clients: Clients,
320
580
  place: RepoPlace,
321
581
  pull: number,
322
582
  review: number,
323
583
  submit: { readonly event: ReviewEvent; readonly body: string },
324
584
  ) => {
325
- const answer = await github.POST(
585
+ const answer = await clients.github.POST(
326
586
  '/repos/{owner}/{repo}/pulls/{pull_number}/reviews/{review_id}/events',
327
587
  { params: { path: { ...place, pull_number: pull, review_id: review } }, body: submit },
328
588
  );
@@ -330,21 +590,36 @@ export const submitPending = async (
330
590
  };
331
591
 
332
592
  /** Deletes the viewer's pending review `review` and its drafts. */
333
- export const discardPending = async (place: RepoPlace, pull: number, review: number) => {
334
- const answer = await github.DELETE(
593
+ export const discardPending = async (
594
+ clients: Clients,
595
+ place: RepoPlace,
596
+ pull: number,
597
+ review: number,
598
+ ) => {
599
+ const answer = await clients.github.DELETE(
335
600
  '/repos/{owner}/{repo}/pulls/{pull_number}/reviews/{review_id}',
336
601
  { params: { path: { ...place, pull_number: pull, review_id: review } } },
337
602
  );
338
603
  if (answer.error !== undefined) throw failure("The pending review wasn't discarded", answer);
339
604
  };
340
605
 
341
- /** Reads `pull`'s review comments, its reviews and the pull request again, after a review. */
606
+ /** Reads the viewer's pending review on `pull` again, and its drafts, after one of its writes. */
607
+ export const refreshPending = (client: QueryClient, place: RepoPlace, pull: number) =>
608
+ Promise.all([
609
+ client.invalidateQueries({
610
+ queryKey: ['pull-pending-comments', place.owner, place.repo, pull],
611
+ }),
612
+ client.invalidateQueries({ queryKey: reviewsKey(place, pull) }),
613
+ ]);
614
+
615
+ /** Reads `pull`'s review comments, threads and reviews, and the pull request, after a review. */
342
616
  export const refreshReviews = (client: QueryClient, place: RepoPlace, pull: number) =>
343
617
  Promise.all([
344
- client.invalidateQueries({ queryKey: lineCommentsOf(place, pull).queryKey }),
618
+ client.invalidateQueries({ queryKey: ['pull-line-comments', place.owner, place.repo, pull] }),
345
619
  client.invalidateQueries({
346
620
  queryKey: ['pull-pending-comments', place.owner, place.repo, pull],
347
621
  }),
348
- client.invalidateQueries({ queryKey: reviewsOf(place, pull).queryKey }),
622
+ client.invalidateQueries({ queryKey: reviewsKey(place, pull) }),
623
+ client.invalidateQueries({ queryKey: threadsKey(place, pull) }),
349
624
  refreshPull(client, place, pull),
350
625
  ]);