TypeScript
fix(web): settled shelf no longer crashes with several projects filtered (#27231)
Jagrate2ad07abad1d
diff --git a/apps/mobile/src/state/olderSettledShelf.ts b/apps/mobile/src/state/olderSettledShelf.tsindex 1d5088b2b7d..e55bf927b4b 100644--- a/apps/mobile/src/state/olderSettledShelf.ts+++ b/apps/mobile/src/state/olderSettledShelf.ts@@ -3,6 +3,7 @@ import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/modelimport {isListedSettledThread,settledShelfFrontier,+ sortOlderSettledScopes,unloadedOlderSettledCount,wantedOlderSettledPages,type OlderSettled,@@ -10,7 +11,7 @@ import {} from "@t3tools/client-runtime/state/threads";import type { EnvironmentId, ProjectId } from "@t3tools/contracts";import { Atom } from "effect/reactivity";-import { useEffect, useState } from "react";+import { useEffect, useMemo, useState } from "react";import { appAtomRegistry } from "./atom-registry";import { olderSettledThreads, threadFamilies } from "./threads";@@ -30,10 +31,11 @@ export function useOlderSettledShelf(input: {/** Rows the shelf shows; pages are read until it lists that many. */readonly visibleCount: number;}) {- const scopeKey = JSON.stringify(input.scopes);+ const scopes = useMemo(() => sortOlderSettledScopes(input.scopes), [input.scopes]);+ const scopeKey = JSON.stringify(scopes);const [requested, setRequested] = useState({ scopeKey, pages: NO_PAGES });const pages = requested.scopeKey === scopeKey && input.expanded ? requested.pages : NO_PAGES;- const shelfScopes = input.scopes.map((scope) => ({+ const shelfScopes = scopes.map((scope) => ({scope,pages: pages.get(scope.environmentId) ?? 0,}));@@ -70,7 +72,7 @@ export function useOlderSettledShelf(input: {isListed: (thread: EnvironmentThreadShell) => isListedSettledThread(thread.source, frontier),/** Older settled threads in scope that are not local; add it to the local Settled rows. */unloadedCount: (threads: ReadonlyArray<EnvironmentThreadShell>) =>- unloadedOlderSettledCount({ olderSettled: olderOf, scopes: input.scopes, threads }),+ unloadedOlderSettledCount({ olderSettled: olderOf, scopes, threads }),loading: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "pending"),failed: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "failed"),retry: () => {diff --git a/apps/web/src/state/olderSettledShelf.browser.test.tsx b/apps/web/src/state/olderSettledShelf.browser.test.tsxnew file mode 100644index 00000000000..a99ba97e480--- /dev/null+++ b/apps/web/src/state/olderSettledShelf.browser.test.tsx@@ -0,0 +1,39 @@+import type { OlderSettledScope } from "@t3tools/client-runtime/state/threads";+import { EnvironmentId, ProjectId } from "@t3tools/contracts";+import { describe, expect, it } from "vite-plus/test";+import { userEvent } from "vite-plus/test/browser";++import { render } from "../../test/render";+import { useOlderSettledShelf } from "./olderSettledShelf";++const environmentId = EnvironmentId.make("env-a");+const first = ProjectId.make("project-a");+const second = ProjectId.make("project-b");++function Shelf(props: { readonly scopes: ReadonlyArray<OlderSettledScope> }) {+ const shelf = useOlderSettledShelf({ scopes: props.scopes, expanded: true, visibleCount: 0 });+ return (+ <>+ <output>{shelf.pages.get(environmentId) ?? 0}</output>+ <button type="button" onClick={() => shelf.grow(new Map([[environmentId, 1]]))}>+ Grow+ </button>+ </>+ );+}++describe("useOlderSettledShelf", () => {+ it("keeps requested pages when the scope's projects reorder", async () => {+ await using screen = await render(+ <Shelf scopes={[{ environmentId, projectIds: [first, second] }]} />,+ // A bare hook host; nothing here links or navigates.+ { router: false },+ );+ await userEvent.click(screen.getByRole("button", { name: "Grow" }));+ await expect.element(screen.getByRole("status")).toHaveTextContent("1");++ // Loading a page can reorder the sidebar's projects; the shelf must not drop its pages.+ await screen.rerender(<Shelf scopes={[{ environmentId, projectIds: [second, first] }]} />);+ await expect.element(screen.getByRole("status")).toHaveTextContent("1");+ });+});diff --git a/apps/web/src/state/olderSettledShelf.ts b/apps/web/src/state/olderSettledShelf.tsindex a903fd6d99b..e313c805dd3 100644--- a/apps/web/src/state/olderSettledShelf.ts+++ b/apps/web/src/state/olderSettledShelf.ts@@ -3,6 +3,7 @@ import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/modelimport {isListedSettledThread,settledShelfFrontier,+ sortOlderSettledScopes,unloadedOlderSettledCount,wantedOlderSettledPages,type OlderSettledScope,@@ -27,10 +28,11 @@ export function useOlderSettledShelf(input: {/** Rows the shelf shows; pages are read until it lists that many. */readonly visibleCount: number;}) {- const scopeKey = JSON.stringify(input.scopes);+ const scopes = sortOlderSettledScopes(input.scopes);+ const scopeKey = JSON.stringify(scopes);const [requested, setRequested] = useState({ scopeKey, pages: NO_PAGES });const pages = requested.scopeKey === scopeKey && input.expanded ? requested.pages : NO_PAGES;- const shelfScopes = input.scopes.map((scope) => ({+ const shelfScopes = scopes.map((scope) => ({scope,pages: pages.get(scope.environmentId) ?? 0,}));@@ -67,7 +69,7 @@ export function useOlderSettledShelf(input: {isListed: (thread: EnvironmentThreadShell) => isListedSettledThread(thread.source, frontier),/** Older settled threads in scope that are not local; add it to the local Settled rows. */unloadedCount: (threads: ReadonlyArray<EnvironmentThreadShell>) =>- unloadedOlderSettledCount({ olderSettled: olderOf, scopes: input.scopes, threads }),+ unloadedOlderSettledCount({ olderSettled: olderOf, scopes, threads }),loading: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "pending"),failed: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "failed"),retry: () => {diff --git a/packages/client-runtime/src/state/olderSettledThreads.ts b/packages/client-runtime/src/state/olderSettledThreads.tsindex c4d7e2f6652..47779f110ef 100644--- a/packages/client-runtime/src/state/olderSettledThreads.ts+++ b/packages/client-runtime/src/state/olderSettledThreads.ts@@ -63,6 +63,21 @@ export interface OlderSettledShelfEnvironment {readonly pages: OlderSettledPages;}+/**+ * Scopes in one order, so a shelf keyed on them keeps its pages when the caller's project order+ * changes. Loading a page can reorder the sidebar's projects, which would otherwise drop the page.+ */+export function sortOlderSettledScopes(+ scopes: ReadonlyArray<OlderSettledScope>,+): ReadonlyArray<OlderSettledScope> {+ return scopes+ .map((scope) => ({+ environmentId: scope.environmentId,+ projectIds: scope.projectIds === null ? null : [...scope.projectIds].sort(),+ }))+ .sort((left, right) => left.environmentId.localeCompare(right.environmentId));+}+/*** Changes whenever the older count can: any removal (by largest sequence, so a repeat counts), an* older row that arrives or changes, or an old row arriving outside the set (unsettled). Threads