| ... | ... | @@ -1,4 +1,5 @@ |
| 1 | 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 2 | 3 | import { |
| 3 | 4 | createLinkedPullRequestSummaryAtomFamily, |
| 4 | 5 | createPullRequestEnvironmentAtoms, |
| ... | ... | @@ -6,12 +7,18 @@ import { |
| 6 | 7 | } from "@t3tools/client-runtime/state/pull-requests"; |
| 7 | 8 | import type { |
| 8 | 9 | EnvironmentId, |
| 10 | ProjectId, |
| 9 | 11 | PullRequestListInput, |
| 10 | 12 | PullRequestListStatsInput, |
| 11 | 13 | PullRequestListEntry, |
| 12 | 14 | PullRequestRef, |
| 13 | 15 | PullRequestSummary, |
| 16 | ThreadPullRequestLink, |
| 14 | 17 | } from "@t3tools/contracts"; |
| 18 | import { |
| 19 | threadPullRequestKeyOf, |
| 20 | visibleThreadPullRequests, |
| 21 | } from "@t3tools/shared/threadPullRequests"; |
| 15 | 22 | import * as Option from "effect/Option"; |
| 16 | 23 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 17 | 24 | import { useCallback, useLayoutEffect, useMemo } from "react"; |
| ... | ... | @@ -24,6 +31,7 @@ import { |
| 24 | 31 | type MergedPullRequestList, |
| 25 | 32 | } from "../components/pullRequest/pullRequestList.logic"; |
| 26 | 33 | import { formatEnvironmentQueryError } from "./query"; |
| 34 | import { environmentThreadShells } from "./threads"; |
| 27 | 35 | |
| 28 | 36 | export const pullRequestEnvironment = createPullRequestEnvironmentAtoms(connectionAtomRuntime); |
| 29 | 37 | export const linkedPullRequestDetailAtom = createLinkedPullRequestSummaryAtomFamily( |
| ... | ... | @@ -149,6 +157,58 @@ function observePullRequestSummary( |
| 149 | 157 | }); |
| 150 | 158 | } |
| 151 | 159 | |
| 160 | export function threadPullRequestLinkToSummary( |
| 161 | link: ThreadPullRequestLink, |
| 162 | projectId: ProjectId, |
| 163 | ): PullRequestSummary | null { |
| 164 | // Without the host update time a snapshot cannot be ordered against a fresh read, so it sits out. |
| 165 | const updatedAt = link.snapshot?.updatedAt; |
| 166 | if (link.snapshot === null || updatedAt == null) return null; |
| 167 | return { |
| 168 | ...link.snapshot, |
| 169 | provider: ( |
| 170 | sourceControlClients.findByChangeRequestUrl(link.url) ?? sourceControlClients.get(undefined) |
| 171 | ).kind, |
| 172 | projectId, |
| 173 | repository: link.repository, |
| 174 | number: link.number, |
| 175 | url: link.url, |
| 176 | updatedAt, |
| 177 | }; |
| 178 | } |
| 179 | |
| 180 | // Saved thread snapshots by environment and host-qualified link, indexed once per shell update |
| 181 | // rather than per control. The host keeps a link to another forge from matching the project's PR. |
| 182 | function savedPullRequestKey( |
| 183 | environmentId: EnvironmentId, |
| 184 | reference: Pick<PullRequestRef, "host" | "repository" | "number">, |
| 185 | ): string { |
| 186 | return reference.host === undefined |
| 187 | ? "" |
| 188 | : `${environmentId}:${threadPullRequestKeyOf({ ...reference, host: reference.host })}`; |
| 189 | } |
| 190 | |
| 191 | const savedPullRequestSummariesAtom = Atom.make((get) => { |
| 192 | const summaries = new Map<string, PullRequestSummary>(); |
| 193 | for (const thread of get(environmentThreadShells.threadShellsAtom)) { |
| 194 | for (const link of visibleThreadPullRequests(thread.pullRequests)) { |
| 195 | const summary = threadPullRequestLinkToSummary(link, thread.projectId); |
| 196 | if (summary === null) continue; |
| 197 | const key = savedPullRequestKey(thread.environmentId, link); |
| 198 | summaries.set(key, newestPullRequestSummary(summaries.get(key) ?? null, summary) ?? summary); |
| 199 | } |
| 200 | } |
| 201 | return summaries; |
| 202 | }); |
| 203 | |
| 204 | const savedPullRequestSummaryAtom = Atom.family((key: string) => |
| 205 | Atom.make((get) => get(savedPullRequestSummariesAtom).get(key) ?? null).pipe( |
| 206 | // Shell updates also resend unchanged links; keep their subscribers quiet. |
| 207 | Atom.withEquality((previous, next) => JSON.stringify(previous) === JSON.stringify(next)), |
| 208 | Atom.setIdleTTL(5 * 60_000), |
| 209 | ), |
| 210 | ); |
| 211 | |
| 152 | 212 | export function useSharedPullRequestSummary( |
| 153 | 213 | environmentId: EnvironmentId | null, |
| 154 | 214 | reference: PullRequestRef | null, |
| ... | ... | @@ -161,17 +221,24 @@ export function useSharedPullRequestSummary( |
| 161 | 221 | : pullRequestSummaryKey(environmentId, reference); |
| 162 | 222 | const atom = observedPullRequestSummaryAtom(key); |
| 163 | 223 | const observed = useAtomValue(atom); |
| 224 | const saved = useAtomValue( |
| 225 | savedPullRequestSummaryAtom( |
| 226 | environmentId === null || reference === null |
| 227 | ? "" |
| 228 | : savedPullRequestKey(environmentId, reference), |
| 229 | ), |
| 230 | ); |
| 164 | 231 | useLayoutEffect(() => { |
| 165 | 232 | if (environmentId === null || reference === null || current === null || observedAt === null) |
| 166 | 233 | return; |
| 167 | 234 | observePullRequestSummary(environmentId, reference, current, observedAt); |
| 168 | 235 | }, [current, environmentId, reference, observedAt]); |
| 169 | | return ( |
| 236 | const summary = |
| 170 | 237 | newestPullRequestObservation( |
| 171 | 238 | observed, |
| 172 | 239 | current === null || observedAt === null ? null : { summary: current, observedAt }, |
| 173 | | )?.summary ?? current |
| 174 | | ); |
| 240 | )?.summary ?? current; |
| 241 | return newestPullRequestSummary(summary, saved); |
| 175 | 242 | } |
| 176 | 243 | export const pullRequestStackAtom = createPullRequestStackAtomFamily( |
| 177 | 244 | connectionAtomRuntime, |