|
1 | 1 | import { RegistryContext, useAtomValue } from "@effect/atom-react"; |
2 | 2 | import { executeAtomQuery, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
3 | | -import type { EnvironmentId, PullRequestListInput } from "@t3tools/contracts"; |
| 3 | +import type { EnvironmentId, PullRequestDiffStat, PullRequestListInput } from "@t3tools/contracts"; |
4 | 4 | import * as Cause from "effect/Cause"; |
5 | 5 | import * as Effect from "effect/Effect"; |
6 | 6 | import * as Option from "effect/Option"; |
@@ -252,16 +252,13 @@ export function usePullRequestList(targets: readonly ListTarget[]) { |
252 | 252 | }, [statsKey]); |
253 | 253 | const counts = useAtomValue(statsAtom); |
254 | 254 | // Server answers land in pullRequestListStatsFamily; these cover a cached answer, which is |
255 | | - // all an offline environment has. |
| 255 | + // all an offline environment has. Scrolling makes a new stats query, which has no answer until |
| 256 | + // its cached rows load, so each environment keeps the answer it showed until the new one lands. |
| 257 | + const [shown, setShown] = useState({ counts, answers: answeredStats(counts, []) }); |
| 258 | + if (shown.counts !== counts) setShown({ counts, answers: answeredStats(counts, shown.answers) }); |
256 | 259 | const entries = useMemo( |
257 | | - () => |
258 | | - withShownPullRequestStats( |
259 | | - data.entries, |
260 | | - counts.flatMap(({ environmentId, result }) => |
261 | | - AsyncResult.isSuccess(result) ? [{ environmentId, stats: result.value.stats }] : [], |
262 | | - ), |
263 | | - ), |
264 | | - [data.entries, counts], |
| 260 | + () => withShownPullRequestStats(data.entries, shown.answers), |
| 261 | + [data.entries, shown.answers], |
265 | 262 | ); |
266 | 263 |
|
267 | 264 | const updateEntry = useCallback( |
@@ -335,3 +332,32 @@ function requestErrors(results: readonly PromiseSettledResult<unknown>[]) { |
335 | 332 | .join("\n") || null |
336 | 333 | ); |
337 | 334 | } |
| 335 | + |
| 336 | +type StatsAnswer = { |
| 337 | + readonly environmentId: EnvironmentId; |
| 338 | + readonly stats: readonly PullRequestDiffStat[]; |
| 339 | +}; |
| 340 | + |
| 341 | +/** Each environment's newest stats answer, or the one it had while its query has none. */ |
| 342 | +function answeredStats( |
| 343 | + counts: readonly { |
| 344 | + readonly environmentId: EnvironmentId; |
| 345 | + readonly result: AsyncResult.AsyncResult< |
| 346 | + { readonly stats: readonly PullRequestDiffStat[] }, |
| 347 | + unknown |
| 348 | + >; |
| 349 | + }[], |
| 350 | + previous: readonly StatsAnswer[], |
| 351 | +): readonly StatsAnswer[] { |
| 352 | + const next = counts.flatMap(({ environmentId, result }) => { |
| 353 | + const kept = previous.find((answer) => answer.environmentId === environmentId); |
| 354 | + if (!AsyncResult.isSuccess(result)) return kept ? [kept] : []; |
| 355 | + return kept?.stats === result.value.stats |
| 356 | + ? [kept] |
| 357 | + : [{ environmentId, stats: result.value.stats }]; |
| 358 | + }); |
| 359 | + return next.length === previous.length && |
| 360 | + next.every((answer, index) => answer === previous[index]) |
| 361 | + ? previous |
| 362 | + : next; |
| 363 | +} |
0 commit comments