Skip to content

Commit e2ad07a

Browse files
jagrat7maria-rcks
andauthored
fix(web): settled shelf no longer crashes with several projects filtered (#27231)
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent ff46c6b commit e2ad07a

4 files changed

Lines changed: 65 additions & 7 deletions

File tree

‎apps/mobile/src/state/olderSettledShelf.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/model
33
import {
44
isListedSettledThread,
55
settledShelfFrontier,
6+
sortOlderSettledScopes,
67
unloadedOlderSettledCount,
78
wantedOlderSettledPages,
89
type OlderSettled,
910
type OlderSettledScope,
1011
} from "@t3tools/client-runtime/state/threads";
1112
import type { EnvironmentId, ProjectId } from "@t3tools/contracts";
1213
import { Atom } from "effect/reactivity";
13-
import { useEffect, useState } from "react";
14+
import { useEffect, useMemo, useState } from "react";
1415

1516
import { appAtomRegistry } from "./atom-registry";
1617
import { olderSettledThreads, threadFamilies } from "./threads";
@@ -30,10 +31,11 @@ export function useOlderSettledShelf(input: {
3031
/** Rows the shelf shows; pages are read until it lists that many. */
3132
readonly visibleCount: number;
3233
}) {
33-
const scopeKey = JSON.stringify(input.scopes);
34+
const scopes = useMemo(() => sortOlderSettledScopes(input.scopes), [input.scopes]);
35+
const scopeKey = JSON.stringify(scopes);
3436
const [requested, setRequested] = useState({ scopeKey, pages: NO_PAGES });
3537
const pages = requested.scopeKey === scopeKey && input.expanded ? requested.pages : NO_PAGES;
36-
const shelfScopes = input.scopes.map((scope) => ({
38+
const shelfScopes = scopes.map((scope) => ({
3739
scope,
3840
pages: pages.get(scope.environmentId) ?? 0,
3941
}));
@@ -70,7 +72,7 @@ export function useOlderSettledShelf(input: {
7072
isListed: (thread: EnvironmentThreadShell) => isListedSettledThread(thread.source, frontier),
7173
/** Older settled threads in scope that are not local; add it to the local Settled rows. */
7274
unloadedCount: (threads: ReadonlyArray<EnvironmentThreadShell>) =>
73-
unloadedOlderSettledCount({ olderSettled: olderOf, scopes: input.scopes, threads }),
75+
unloadedOlderSettledCount({ olderSettled: olderOf, scopes, threads }),
7476
loading: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "pending"),
7577
failed: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "failed"),
7678
retry: () => {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { OlderSettledScope } from "@t3tools/client-runtime/state/threads";
2+
import { EnvironmentId, ProjectId } from "@t3tools/contracts";
3+
import { describe, expect, it } from "vite-plus/test";
4+
import { userEvent } from "vite-plus/test/browser";
5+
6+
import { render } from "../../test/render";
7+
import { useOlderSettledShelf } from "./olderSettledShelf";
8+
9+
const environmentId = EnvironmentId.make("env-a");
10+
const first = ProjectId.make("project-a");
11+
const second = ProjectId.make("project-b");
12+
13+
function Shelf(props: { readonly scopes: ReadonlyArray<OlderSettledScope> }) {
14+
const shelf = useOlderSettledShelf({ scopes: props.scopes, expanded: true, visibleCount: 0 });
15+
return (
16+
<>
17+
<output>{shelf.pages.get(environmentId) ?? 0}</output>
18+
<button type="button" onClick={() => shelf.grow(new Map([[environmentId, 1]]))}>
19+
Grow
20+
</button>
21+
</>
22+
);
23+
}
24+
25+
describe("useOlderSettledShelf", () => {
26+
it("keeps requested pages when the scope's projects reorder", async () => {
27+
await using screen = await render(
28+
<Shelf scopes={[{ environmentId, projectIds: [first, second] }]} />,
29+
// A bare hook host; nothing here links or navigates.
30+
{ router: false },
31+
);
32+
await userEvent.click(screen.getByRole("button", { name: "Grow" }));
33+
await expect.element(screen.getByRole("status")).toHaveTextContent("1");
34+
35+
// Loading a page can reorder the sidebar's projects; the shelf must not drop its pages.
36+
await screen.rerender(<Shelf scopes={[{ environmentId, projectIds: [second, first] }]} />);
37+
await expect.element(screen.getByRole("status")).toHaveTextContent("1");
38+
});
39+
});

‎apps/web/src/state/olderSettledShelf.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/model
33
import {
44
isListedSettledThread,
55
settledShelfFrontier,
6+
sortOlderSettledScopes,
67
unloadedOlderSettledCount,
78
wantedOlderSettledPages,
89
type OlderSettledScope,
@@ -27,10 +28,11 @@ export function useOlderSettledShelf(input: {
2728
/** Rows the shelf shows; pages are read until it lists that many. */
2829
readonly visibleCount: number;
2930
}) {
30-
const scopeKey = JSON.stringify(input.scopes);
31+
const scopes = sortOlderSettledScopes(input.scopes);
32+
const scopeKey = JSON.stringify(scopes);
3133
const [requested, setRequested] = useState({ scopeKey, pages: NO_PAGES });
3234
const pages = requested.scopeKey === scopeKey && input.expanded ? requested.pages : NO_PAGES;
33-
const shelfScopes = input.scopes.map((scope) => ({
35+
const shelfScopes = scopes.map((scope) => ({
3436
scope,
3537
pages: pages.get(scope.environmentId) ?? 0,
3638
}));
@@ -67,7 +69,7 @@ export function useOlderSettledShelf(input: {
6769
isListed: (thread: EnvironmentThreadShell) => isListedSettledThread(thread.source, frontier),
6870
/** Older settled threads in scope that are not local; add it to the local Settled rows. */
6971
unloadedCount: (threads: ReadonlyArray<EnvironmentThreadShell>) =>
70-
unloadedOlderSettledCount({ olderSettled: olderOf, scopes: input.scopes, threads }),
72+
unloadedOlderSettledCount({ olderSettled: olderOf, scopes, threads }),
7173
loading: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "pending"),
7274
failed: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "failed"),
7375
retry: () => {

‎packages/client-runtime/src/state/olderSettledThreads.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ export interface OlderSettledShelfEnvironment {
6363
readonly pages: OlderSettledPages;
6464
}
6565

66+
/**
67+
* Scopes in one order, so a shelf keyed on them keeps its pages when the caller's project order
68+
* changes. Loading a page can reorder the sidebar's projects, which would otherwise drop the page.
69+
*/
70+
export function sortOlderSettledScopes(
71+
scopes: ReadonlyArray<OlderSettledScope>,
72+
): ReadonlyArray<OlderSettledScope> {
73+
return scopes
74+
.map((scope) => ({
75+
environmentId: scope.environmentId,
76+
projectIds: scope.projectIds === null ? null : [...scope.projectIds].sort(),
77+
}))
78+
.sort((left, right) => left.environmentId.localeCompare(right.environmentId));
79+
}
80+
6681
/**
6782
* Changes whenever the older count can: any removal (by largest sequence, so a repeat counts), an
6883
* older row that arrives or changes, or an old row arriving outside the set (unsettled). Threads

0 commit comments

Comments
 (0)