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
@@ -6,7 +6,7 @@ import {
6
6
  type QueryClient,
7
7
  } from '@tanstack/react-query';
8
8
 
9
- import { git, github, mutate } from '../clients/clients.ts';
9
+ import type { Clients } from '../clients/clients.ts';
10
10
  import type { RepoPlace } from '../code/places.ts';
11
11
  import { rowOf, type CommitRow } from '../code/refs.ts';
12
12
  import type { MergeMethod } from '../code/repo.ts';
@@ -76,12 +76,16 @@ const mergeablePoll = 2_000;
76
76
  /** How often a commit's checks are read again while a page shows them. */
77
77
  export const checksPoll = 5_000;
78
78
 
79
+ /** Where the pull request `number` is read: its changes write and refresh it here. */
80
+ const pullKey = (place: RepoPlace, number: number) =>
81
+ ['pull', place.owner, place.repo, number] as const;
82
+
79
83
  /** The pull request `number`, or `null` when the viewer can read none there. */
80
- export const pullAt = (place: RepoPlace, number: number) =>
84
+ export const pullAt = (clients: Clients, place: RepoPlace, number: number) =>
81
85
  queryOptions({
82
- queryKey: ['pull', place.owner, place.repo, number],
86
+ queryKey: pullKey(place, number),
83
87
  queryFn: async (): Promise<PullDetail | null> => {
84
- const answer = await github.GET('/repos/{owner}/{repo}/pulls/{pull_number}', {
88
+ const answer = await clients.github.GET('/repos/{owner}/{repo}/pulls/{pull_number}', {
85
89
  params: { path: { ...place, pull_number: number } },
86
90
  });
87
91
  if (answer.response.status === 404) return null;
@@ -102,15 +106,19 @@ export const pullAt = (place: RepoPlace, number: number) =>
102
106
  */
103
107
  export const refreshPull = (client: QueryClient, place: RepoPlace, number: number) =>
104
108
  Promise.all([
105
- client.invalidateQueries({ queryKey: pullAt(place, number).queryKey }),
109
+ client.invalidateQueries({ queryKey: pullKey(place, number) }),
106
110
  client.invalidateQueries({ queryKey: ['issues', place.owner, place.repo] }),
107
111
  client.invalidateQueries({ queryKey: ['pulls', place.owner, place.repo] }),
108
112
  ]);
109
113
 
110
- export const reviewsOf = (place: RepoPlace, pull: number) =>
114
+ /** Where `pull`'s reviews are read: a review's changes refresh them here. */
115
+ export const reviewsKey = (place: RepoPlace, pull: number) =>
116
+ ['pull-reviews', place.owner, place.repo, pull] as const;
117
+
118
+ export const reviewsOf = (clients: Clients, place: RepoPlace, pull: number) =>
111
119
  queryOptions({
112
- queryKey: ['pull-reviews', place.owner, place.repo, pull],
113
- queryFn: async () => (await reviewsRead(place, pull)).rows,
120
+ queryKey: reviewsKey(place, pull),
121
+ queryFn: async () => (await reviewsRead(clients, place, pull)).rows,
114
122
  staleTime: 10_000,
115
123
  });
116
124
 
@@ -125,7 +133,7 @@ export const pullHolding = (client: QueryClient, place: RepoPlace, number: numbe
125
133
  return {
126
134
  confirm: (shown) => {
127
135
  issue.confirm(shown);
128
- client.setQueryData(pullAt(place, number).queryKey, (pull) =>
136
+ client.setQueryData<PullDetail | null>(pullKey(place, number), (pull) =>
129
137
  pull === null || pull === undefined ? pull : withShown(pull, shown),
130
138
  );
131
139
  },
@@ -142,16 +150,13 @@ export const pullHolding = (client: QueryClient, place: RepoPlace, number: numbe
142
150
  ),
143
151
  );
144
152
  },
145
- refreshed: [
146
- ...issue.refreshed,
147
- pullAt(place, number).queryKey,
148
- ['pulls', place.owner, place.repo],
149
- ],
153
+ refreshed: [...issue.refreshed, pullKey(place, number), ['pulls', place.owner, place.repo]],
150
154
  };
151
155
  };
152
156
 
153
157
  /** Asks for the reviews of `logins`, or takes the asks back, in one call. */
154
158
  const askReviews = async (
159
+ clients: Clients,
155
160
  place: RepoPlace,
156
161
  pull: number,
157
162
  logins: readonly string[],
@@ -162,7 +167,9 @@ const askReviews = async (
162
167
  body: { reviewers: [...logins] },
163
168
  };
164
169
  const path = '/repos/{owner}/{repo}/pulls/{pull_number}/requested_reviewers';
165
- const answer = asked ? await github.POST(path, at) : await github.DELETE(path, at);
170
+ const answer = asked
171
+ ? await clients.github.POST(path, at)
172
+ : await clients.github.DELETE(path, at);
166
173
  if (answer.error !== undefined) throw failure("The reviewers didn't change", answer);
167
174
  };
168
175
 
@@ -174,18 +181,23 @@ const listed = new Intl.ListFormat('en', { type: 'conjunction' });
174
181
  * succeeds and the other fails, the success stands: only the failed part rolls back, and the
175
182
  * toast names it.
176
183
  */
177
- export const reviewersChange = (changing: Changing, requested: readonly string[]): Change => ({
184
+ export const reviewersChange = (
185
+ clients: Clients,
186
+ changing: Changing,
187
+ requested: readonly string[],
188
+ ): Change => ({
178
189
  field: 'requested',
179
190
  value: requested,
180
191
  send: async () => {
181
192
  const { client, place, number } = changing;
182
- const confirmed = client.getQueryData(pullAt(place, number).queryKey)?.requested ?? [];
193
+ const confirmed =
194
+ client.getQueryData<PullDetail | null>(pullKey(place, number))?.requested ?? [];
183
195
  const added = requested.filter((login) => !confirmed.includes(login));
184
196
  const removed = confirmed.filter((login) => !requested.includes(login));
185
197
 
186
198
  const [asked, unasked] = await Promise.allSettled([
187
- added.length === 0 ? undefined : askReviews(place, number, added, true),
188
- removed.length === 0 ? undefined : askReviews(place, number, removed, false),
199
+ added.length === 0 ? undefined : askReviews(clients, place, number, added, true),
200
+ removed.length === 0 ? undefined : askReviews(clients, place, number, removed, false),
189
201
  ]);
190
202
  const kept =
191
203
  unasked.status === 'fulfilled'
@@ -212,14 +224,20 @@ export const reviewersChange = (changing: Changing, requested: readonly string[]
212
224
  });
213
225
 
214
226
  /** `pull`'s commits, oldest first, as many as GitHub lists: 250. */
215
- export const pullCommitsOf = (place: RepoPlace, pull: number) =>
227
+ export const pullCommitsOf = (clients: Clients, place: RepoPlace, pull: number) =>
216
228
  queryOptions({
217
229
  queryKey: ['pull-commits', place.owner, place.repo, pull],
218
230
  queryFn: async (): Promise<readonly CommitRow[]> => {
219
231
  const read = await everyPage(async (after) => {
220
- const answer = await github.GET('/repos/{owner}/{repo}/pulls/{pull_number}/commits', {
221
- params: { path: { ...place, pull_number: pull }, query: { ...{ per_page: 100, after } } },
222
- });
232
+ const answer = await clients.github.GET(
233
+ '/repos/{owner}/{repo}/pulls/{pull_number}/commits',
234
+ {
235
+ params: {
236
+ path: { ...place, pull_number: pull },
237
+ query: { ...{ per_page: 100, after } },
238
+ },
239
+ },
240
+ );
223
241
  if (answer.data === undefined) throw failure("The commits didn't load", answer);
224
242
 
225
243
  return { rows: answer.data.map(rowOf), link: answer.response.headers.get('link') };
@@ -269,7 +287,7 @@ const statusState = (state: string): Check['state'] => {
269
287
  };
270
288
 
271
289
  /** The checks on `sha`: its statuses' latest by context, and its check runs, every page, by name. */
272
- export const checksOf = (place: RepoPlace, sha: string) =>
290
+ export const checksOf = (clients: Clients, place: RepoPlace, sha: string) =>
273
291
  queryOptions({
274
292
  queryKey: ['checks', place.owner, place.repo, sha],
275
293
  queryFn: async (): Promise<readonly Check[]> => {
@@ -277,7 +295,7 @@ export const checksOf = (place: RepoPlace, sha: string) =>
277
295
  // `after` is the drop-in's cursor, which GitHub's types don't name: a spread passes it.
278
296
  const [fromStatuses, fromRuns] = await Promise.all([
279
297
  everyPage(async (after) => {
280
- const answer = await github.GET('/repos/{owner}/{repo}/commits/{ref}/status', {
298
+ const answer = await clients.github.GET('/repos/{owner}/{repo}/commits/{ref}/status', {
281
299
  params: { ...path, query: { ...{ per_page: 100, after } } },
282
300
  });
283
301
  if (answer.data === undefined) throw failure("The statuses didn't load", answer);
@@ -292,9 +310,12 @@ export const checksOf = (place: RepoPlace, sha: string) =>
292
310
  return { rows, link: answer.response.headers.get('link') };
293
311
  }),
294
312
  everyPage(async (after) => {
295
- const answer = await github.GET('/repos/{owner}/{repo}/commits/{ref}/check-runs', {
296
- params: { ...path, query: { ...{ per_page: 100, after } } },
297
- });
313
+ const answer = await clients.github.GET(
314
+ '/repos/{owner}/{repo}/commits/{ref}/check-runs',
315
+ {
316
+ params: { ...path, query: { ...{ per_page: 100, after } } },
317
+ },
318
+ );
298
319
  if (answer.data === undefined) throw failure("The check runs didn't load", answer);
299
320
 
300
321
  const rows = answer.data.check_runs.map((run): Check => ({
@@ -322,11 +343,11 @@ export const repoId = (place: RepoPlace) => `${place.owner}/${place.repo}`;
322
343
  export const branch = (name: string) => `refs/heads/${name}`;
323
344
 
324
345
  /** Git's protection on the branch `base`, which says how it takes changes. */
325
- export const protectionOf = (place: RepoPlace, base: string) =>
346
+ export const protectionOf = (clients: Clients, place: RepoPlace, base: string) =>
326
347
  queryOptions({
327
348
  queryKey: ['protection', place.owner, place.repo, base],
328
349
  queryFn: async (): Promise<Protection> => {
329
- const answer = await git.GET('/repos/{repo}/-/protection/{ref}', {
350
+ const answer = await clients.git.GET('/repos/{repo}/-/protection/{ref}', {
330
351
  params: { path: { repo: repoId(place), ref: branch(base) } },
331
352
  });
332
353
  if (answer.error !== undefined) throw failure("The branch's protection didn't load", answer);
@@ -349,17 +370,21 @@ export const queueKey = (pull: number) => `pr-${pull}`;
349
370
  /** How many pages of 100 the page reads through a queue: Git holds at most 1,000 entries. */
350
371
  const queuePages = 10;
351
372
 
373
+ /** Where pull request `pull`'s entry in `base`'s queue is read. */
374
+ export const queuedKey = (place: RepoPlace, base: string, pull: number) =>
375
+ ['queued', place.owner, place.repo, base, pull] as const;
376
+
352
377
  /** Pull request `pull`'s entry in `base`'s queue while it's open there, or `null`. */
353
- export const queuedOf = (place: RepoPlace, base: string, pull: number) =>
378
+ export const queuedOf = (clients: Clients, place: RepoPlace, base: string, pull: number) =>
354
379
  queryOptions({
355
- queryKey: ['queued', place.owner, place.repo, base, pull],
380
+ queryKey: queuedKey(place, base, pull),
356
381
  queryFn: async (): Promise<Queued | null> => {
357
382
  const first = { target: branch(base), limit: '100' };
358
383
  let after: string | undefined;
359
384
  let ahead = 0;
360
385
 
361
386
  for (let page = 0; page < queuePages; page += 1) {
362
- const answer = await git.GET('/repos/{repo}/-/queue/entries', {
387
+ const answer = await clients.git.GET('/repos/{repo}/-/queue/entries', {
363
388
  params: {
364
389
  path: { repo: repoId(place) },
365
390
  query: after === undefined ? first : { ...first, after },
@@ -400,11 +425,13 @@ export const lastEntryOf = (place: RepoPlace, pull: number) =>
400
425
  });
401
426
 
402
427
  /** Where queue entry `entry` stands: waiting or checking in its queue, or ended, merged or not. */
403
- export const entryStateOf = (entry: string) =>
428
+ export const entryStateOf = (clients: Clients, entry: string) =>
404
429
  queryOptions({
405
430
  queryKey: ['queue-entry', entry],
406
431
  queryFn: async () => {
407
- const answer = await git.GET('/queue/entries/{entry}', { params: { path: { entry } } });
432
+ const answer = await clients.git.GET('/queue/entries/{entry}', {
433
+ params: { path: { entry } },
434
+ });
408
435
  if (answer.data === undefined) throw failure("The queue's entry didn't load", answer);
409
436
 
410
437
  return answer.data.state;
@@ -413,11 +440,12 @@ export const entryStateOf = (entry: string) =>
413
440
 
414
441
  /** Merges `pull` by `method`, if its head is still `sha`. */
415
442
  export const mergePull = async (
443
+ clients: Clients,
416
444
  place: RepoPlace,
417
445
  pull: number,
418
446
  merging: { readonly method: MergeMethod; readonly sha: string },
419
447
  ) => {
420
- const answer = await github.PUT('/repos/{owner}/{repo}/pulls/{pull_number}/merge', {
448
+ const answer = await clients.github.PUT('/repos/{owner}/{repo}/pulls/{pull_number}/merge', {
421
449
  params: { path: { ...place, pull_number: pull } },
422
450
  body: { merge_method: merging.method, sha: merging.sha },
423
451
  });
@@ -455,14 +483,14 @@ export const queueOperations = {
455
483
  * its merge group, and closes the pull request once it merges (Q527). A head that moved since the
456
484
  * page read it is refused, and nothing joins.
457
485
  */
458
- export const enqueuePull = (pull: PullDetail) =>
459
- mutate("The pull request wasn't added to the queue", queueOperations.enqueue, {
486
+ export const enqueuePull = (clients: Clients, pull: PullDetail) =>
487
+ clients.mutate("The pull request wasn't added to the queue", queueOperations.enqueue, {
460
488
  pullRequestId: pull.nodeId,
461
489
  expectedHeadOid: pull.head.sha,
462
490
  });
463
491
 
464
492
  /** Takes `pull` out of its base's merge queue through the drop-in's `dequeuePullRequest` (Q527). */
465
- export const dequeuePull = (pull: Pick<PullDetail, 'nodeId'>) =>
466
- mutate("The pull request wasn't taken out of the queue", queueOperations.dequeue, {
493
+ export const dequeuePull = (clients: Clients, pull: Pick<PullDetail, 'nodeId'>) =>
494
+ clients.mutate("The pull request wasn't taken out of the queue", queueOperations.dequeue, {
467
495
  id: pull.nodeId,
468
496
  });
@@ -1,6 +1,6 @@
1
1
  import { queryOptions } from '@tanstack/react-query';
2
2
 
3
- import { github } from '../clients/clients.ts';
3
+ import type { Clients } from '../clients/clients.ts';
4
4
  import type { RepoPlace } from '../code/places.ts';
5
5
  import { rowOf, type CommitRow } from '../code/refs.ts';
6
6
  import { isText, type ChangedFile } from '../diffs/files.ts';
@@ -90,6 +90,10 @@ export const decisionOf = (
90
90
  /** A review: who gave it, what it decided, and when. */
91
91
  export interface Review {
92
92
  readonly id: number;
93
+ /** Its node ID, which GitHub's GraphQL adds a pending review's drafts by. */
94
+ readonly node: string;
95
+ /** The commit it's on: a pending review's drafts are placed on its lines. */
96
+ readonly commit: string | null;
93
97
  readonly login: string;
94
98
  readonly state: string;
95
99
  readonly body: string;
@@ -100,9 +104,9 @@ export interface Review {
100
104
  }
101
105
 
102
106
  /** `pull`'s reviews, oldest first, read from the drop-in a page of 100 at a time. */
103
- export const reviewsRead = (place: RepoPlace, pull: number) =>
107
+ export const reviewsRead = (clients: Clients, place: RepoPlace, pull: number) =>
104
108
  everyPage(async (after) => {
105
- const answer = await github.GET('/repos/{owner}/{repo}/pulls/{pull_number}/reviews', {
109
+ const answer = await clients.github.GET('/repos/{owner}/{repo}/pulls/{pull_number}/reviews', {
106
110
  // `after` is the drop-in's cursor, which GitHub's types don't name: a spread passes it.
107
111
  params: { path: { ...place, pull_number: pull }, query: { ...{ per_page: 100, after } } },
108
112
  });
@@ -110,6 +114,8 @@ export const reviewsRead = (place: RepoPlace, pull: number) =>
110
114
 
111
115
  const rows = answer.data.map((review): Review => ({
112
116
  id: Number(review.id),
117
+ node: review.node_id,
118
+ commit: review.commit_id,
113
119
  login: review.user?.login ?? 'ghost',
114
120
  state: review.state,
115
121
  body: review.body,
@@ -121,25 +127,30 @@ export const reviewsRead = (place: RepoPlace, pull: number) =>
121
127
  });
122
128
 
123
129
  /** `pull`'s reviews' decision. */
124
- const decided = async (place: RepoPlace, pull: number) =>
125
- decisionOf((await reviewsRead(place, pull)).rows);
130
+ const decided = async (clients: Clients, place: RepoPlace, pull: number) =>
131
+ decisionOf((await reviewsRead(clients, place, pull)).rows);
126
132
 
127
133
  /** Whether `login`'s review of `pull` is asked for. */
128
- const requestedOf = async (place: RepoPlace, pull: number, login: string) => {
129
- const answer = await github.GET('/repos/{owner}/{repo}/pulls/{pull_number}/requested_reviewers', {
130
- params: { path: { ...place, pull_number: pull } },
131
- });
134
+ const requestedOf = async (clients: Clients, place: RepoPlace, pull: number, login: string) => {
135
+ const answer = await clients.github.GET(
136
+ '/repos/{owner}/{repo}/pulls/{pull_number}/requested_reviewers',
137
+ {
138
+ params: { path: { ...place, pull_number: pull } },
139
+ },
140
+ );
132
141
  if (answer.data === undefined) throw failure(`#${pull}'s reviewers didn't load`, answer);
133
142
 
134
143
  return answer.data.users.some((user) => user.login === login);
135
144
  };
136
145
 
137
146
  /** Whether `pull` passes `filter`'s review qualifiers, which only its reviews can say. */
138
- const reviewedAs = async (place: RepoPlace, filter: PullFilter, pull: Pull) => {
147
+ const reviewedAs = async (clients: Clients, place: RepoPlace, filter: PullFilter, pull: Pull) => {
139
148
  const { review, reviewRequested } = filter;
140
149
  const [decision, requested] = await Promise.all([
141
- review === undefined ? undefined : decided(place, pull.number),
142
- reviewRequested === undefined ? true : requestedOf(place, pull.number, reviewRequested),
150
+ review === undefined ? undefined : decided(clients, place, pull.number),
151
+ reviewRequested === undefined
152
+ ? true
153
+ : requestedOf(clients, place, pull.number, reviewRequested),
143
154
  ]);
144
155
 
145
156
  return requested && (review === undefined || decision === review);
@@ -165,6 +176,7 @@ export interface PullPage {
165
176
  * read only when the box asks for them.
166
177
  */
167
178
  export const pullPage = async (
179
+ clients: Clients,
168
180
  place: RepoPlace,
169
181
  filter: PullFilter,
170
182
  vocabulary: Vocabulary,
@@ -173,7 +185,7 @@ export const pullPage = async (
173
185
  const state = listedState(filter);
174
186
  const query = state === undefined ? undefined : listQuery({ ...filter, state }, vocabulary);
175
187
  const searched = pullSearchQueryOf(place, filter);
176
- const lister = listerOf(place, filter, query, searched, 'The pull requests');
188
+ const lister = listerOf(clients, place, filter, query, searched, 'The pull requests');
177
189
  if (lister === undefined) return { pulls: [], next: undefined };
178
190
 
179
191
  const keep = async (listed: readonly Listed[]) => {
@@ -186,7 +198,7 @@ export const pullPage = async (
186
198
  });
187
199
  if (filter.review === undefined && filter.reviewRequested === undefined) return pulls;
188
200
 
189
- const passes = await Promise.all(pulls.map((pull) => reviewedAs(place, filter, pull)));
201
+ const passes = await Promise.all(pulls.map((pull) => reviewedAs(clients, place, filter, pull)));
190
202
 
191
203
  return pulls.filter((_, index) => passes[index]);
192
204
  };
@@ -206,10 +218,15 @@ const firstPage: string | undefined = undefined;
206
218
  export const pullLists = (place: RepoPlace) => ['pulls', place.owner, place.repo, 'list'] as const;
207
219
 
208
220
  /** The repo's pull requests under `filter`, a page at a time as the reader asks for more. */
209
- export const pullList = (place: RepoPlace, filter: PullFilter, vocabulary: Vocabulary) => ({
221
+ export const pullList = (
222
+ clients: Clients,
223
+ place: RepoPlace,
224
+ filter: PullFilter,
225
+ vocabulary: Vocabulary,
226
+ ) => ({
210
227
  queryKey: [...pullLists(place), filter] as const,
211
228
  queryFn: ({ pageParam }: { readonly pageParam: string | undefined }) =>
212
- pullPage(place, filter, vocabulary, pageParam),
229
+ pullPage(clients, place, filter, vocabulary, pageParam),
213
230
  initialPageParam: firstPage,
214
231
  getNextPageParam: (last: PullPage) => last.next,
215
232
  staleTime: 10_000,
@@ -252,11 +269,11 @@ const permalinkHead = (permalink: string) => {
252
269
  * The comparison of `basehead`, or `null` when there's none to make: a revision the repo hasn't
253
270
  * got, or two that share no history.
254
271
  */
255
- export const comparisonOf = (place: RepoPlace, basehead: Basehead) =>
272
+ export const comparisonOf = (clients: Clients, place: RepoPlace, basehead: Basehead) =>
256
273
  queryOptions({
257
274
  queryKey: ['comparison', place.owner, place.repo, basehead.base, basehead.head],
258
275
  queryFn: async (): Promise<Comparison | null> => {
259
- const answer = await github.GET('/repos/{owner}/{repo}/compare/{basehead}', {
276
+ const answer = await clients.github.GET('/repos/{owner}/{repo}/compare/{basehead}', {
260
277
  // No page asked for: the drop-in answers the first 250 commits and the files at once.
261
278
  params: { path: { ...place, basehead: baseheadOf(basehead) } },
262
279
  });
@@ -281,12 +298,12 @@ export const comparisonOf = (place: RepoPlace, basehead: Basehead) =>
281
298
  });
282
299
 
283
300
  /** The pull request `basehead` has open already, if it has one: GitHub opens one a pair. */
284
- export const openPullOf = (place: RepoPlace, basehead: Basehead) =>
301
+ export const openPullOf = (clients: Clients, place: RepoPlace, basehead: Basehead) =>
285
302
  queryOptions({
286
303
  // Under the list's key, so opening one, which invalidates the list, reads this again.
287
304
  queryKey: ['pulls', place.owner, place.repo, 'open', basehead.base, basehead.head],
288
305
  queryFn: async (): Promise<number | null> => {
289
- const answer = await github.GET('/repos/{owner}/{repo}/pulls', {
306
+ const answer = await clients.github.GET('/repos/{owner}/{repo}/pulls', {
290
307
  params: {
291
308
  path: place,
292
309
  query: { state: 'open', base: basehead.base, head: `${place.owner}:${basehead.head}` },
@@ -307,8 +324,8 @@ export interface Opening extends Basehead {
307
324
  }
308
325
 
309
326
  /** Opens a pull request, answering its number. */
310
- export const openPull = async (place: RepoPlace, opening: Opening) => {
311
- const answer = await github.POST('/repos/{owner}/{repo}/pulls', {
327
+ export const openPull = async (clients: Clients, place: RepoPlace, opening: Opening) => {
328
+ const answer = await clients.github.POST('/repos/{owner}/{repo}/pulls', {
312
329
  params: { path: place },
313
330
  body: { ...opening },
314
331
  });
@@ -0,0 +1,131 @@
1
+ import { hashKey, useQueryClient, type QueryClient } from '@tanstack/react-query';
2
+ import { useCallback, useSyncExternalStore } from 'react';
3
+
4
+ import type { RepoPlace } from '../code/places.ts';
5
+ import type { LinePlace } from './review.ts';
6
+
7
+ /** Lines picked from the gutter for a new comment, and the head whose diff they were picked from. */
8
+ export type Picked = LinePlace & { readonly head: string };
9
+
10
+ interface Held {
11
+ /**
12
+ * Each box's text, by its key: `reply:<root comment ID>`, `edit:<pending comment node ID>`, or
13
+ * `new`, the picked lines' comment.
14
+ */
15
+ readonly typed: ReadonlyMap<string, string>;
16
+ /** The lines the `new` comment is on, while its box is open. */
17
+ readonly picked?: Picked;
18
+ }
19
+
20
+ const heldRoot = ['pull-review-typed'] as const;
21
+
22
+ const heldKey = (place: RepoPlace, pull: number) =>
23
+ [...heldRoot, place.owner, place.repo, pull] as const;
24
+
25
+ const write = (client: QueryClient, key: readonly unknown[], change: (kept: Held) => Held) => {
26
+ client.setQueryDefaults(heldRoot, { gcTime: Infinity, staleTime: Infinity });
27
+ client.setQueryData<Held>(key, (kept) => change(kept ?? { typed: new Map() }));
28
+ };
29
+
30
+ /**
31
+ * One part of a pull request's held text, read from the holder on every draw, so a box shows what
32
+ * the holder has now: what it typed, or what a write made elsewhere left, as a send that lands
33
+ * after a push drew the box again. The query cache tells its listeners of a write as it's made, so
34
+ * a key stroke draws before the next click or hotkey, which then sends what was typed.
35
+ */
36
+ const useHeld = <A>(place: RepoPlace, pull: number, read: (held: Held | undefined) => A) => {
37
+ const client = useQueryClient();
38
+ const key = heldKey(place, pull);
39
+ const hash = hashKey(key);
40
+ const listen = useCallback(
41
+ (changed: () => void) =>
42
+ client.getQueryCache().subscribe(({ query }) => {
43
+ if (query.queryHash === hash) changed();
44
+ }),
45
+ [client, hash],
46
+ );
47
+ const snapshot = () => read(client.getQueryData<Held>(key));
48
+ // The server's client holds nothing typed, so its draw reads the same, empty holder.
49
+ const value = useSyncExternalStore(listen, snapshot, snapshot);
50
+
51
+ const set = (change: (kept: Held) => Held) => write(client, key, change);
52
+
53
+ return [value, set] as const;
54
+ };
55
+
56
+ /**
57
+ * What's typed and not yet sent in a pull request's review boxes, held above every box that shows
58
+ * it. A push draws the diff's rows anew, moves a thread to the outdated list, or puts a pending
59
+ * review behind, and each of those mounts its box afresh. A tab switch unmounts the whole tab. The
60
+ * box then starts from what's held here.
61
+ *
62
+ * It's held in the app's query client, never fetched, with no `gcTime` to collect it while no box
63
+ * reads it, as on another tab. A reload, or the sign-out's load of the home page, starts a new
64
+ * client, and with it a new map. The root's leave guard asks first while any of it is typed
65
+ * ({@link typedHeld}).
66
+ *
67
+ * `undefined` closes the box, as a send, a save or a cancel does, and lets its text go.
68
+ */
69
+ export const useTyped = (place: RepoPlace, pull: number, key: string) => {
70
+ const [text, setHeld] = useHeld(place, pull, (held) => held?.typed.get(key));
71
+
72
+ const set = (next: string | undefined) =>
73
+ setHeld((kept) => {
74
+ const changed = new Map(kept.typed);
75
+ if (next === undefined) changed.delete(key);
76
+ else changed.set(key, next);
77
+
78
+ return { ...kept, typed: changed };
79
+ });
80
+
81
+ return [text, set] as const;
82
+ };
83
+
84
+ /**
85
+ * The lines picked for a new comment, held with its text as {@link useTyped} holds it, so a return
86
+ * to the tab opens the box again on them. Picking lines, or closing the box with `undefined`, lets
87
+ * the `new` text go.
88
+ */
89
+ export const usePicked = (place: RepoPlace, pull: number) => {
90
+ const [picked, setHeld] = useHeld(place, pull, (held) => held?.picked);
91
+
92
+ const set = (next: Picked | undefined) =>
93
+ setHeld(({ typed }) => {
94
+ const changed = new Map(typed);
95
+ changed.delete('new');
96
+
97
+ return next === undefined ? { typed: changed } : { typed: changed, picked: next };
98
+ });
99
+
100
+ return [picked, set] as const;
101
+ };
102
+
103
+ /**
104
+ * Lets go of the pull request's held edits to pending comments not among `nodes`, the pending
105
+ * review's comments as the drop-in last answered: one submitted, discarded or deleted, here, in
106
+ * another tab or with gh, takes its edit with it, as no box is left to save or cancel it.
107
+ */
108
+ export const keepEdits = (
109
+ client: QueryClient,
110
+ place: RepoPlace,
111
+ pull: number,
112
+ nodes: readonly string[],
113
+ ) => {
114
+ const key = heldKey(place, pull);
115
+ const typed = client.getQueryData<Held>(key)?.typed ?? new Map<string, string>();
116
+ const gone = [...typed.keys()].filter(
117
+ (each) => each.startsWith('edit:') && !nodes.includes(each.slice('edit:'.length)),
118
+ );
119
+ if (gone.length === 0) return;
120
+
121
+ write(client, key, (kept) => ({
122
+ ...kept,
123
+ typed: new Map([...kept.typed].filter(([each]) => !gone.includes(each))),
124
+ }));
125
+ };
126
+
127
+ /** Whether any review box, on any pull request the page has open, holds text not yet sent. */
128
+ export const typedHeld = (client: QueryClient) =>
129
+ client
130
+ .getQueriesData<Held>({ queryKey: heldRoot })
131
+ .some(([, held]) => [...(held?.typed.values() ?? [])].some((text) => text.trim() !== ''));