Skip to content

Add useHistoryWindow so long chats load without jumping #100

Closed

Description

@pratikbuilds

Keep long conversations fast by rendering a window of recent items and loading older ones as the reader scrolls up, without moving the text they are reading. Write down how big a conversation this is built for.

Part of #94.

Depends on #96, #99.

Why

Long chats get slow if every message is on the page. A window of recent items keeps them fast, but loading older items above the reader must not make the text jump. Hosts often page history from the server, so the hook has to work with in-memory history and with asynchronous paging alike.

What

  • src/hooks/use-history-window.ts: useHistoryWindow({ items, windowSize, step, hasEarlier, onLoadEarlier }). It starts at the latest windowSize items, grows by step near the top, freezes while the reader is scrolled up and trims back when they return to the bottom. The "load more near the top" trigger (e.g. reachStart) is part of its public return value. Its doc comment records the windowing ceiling.
  • src/ui/conversation-earlier.tsx: ConversationEarlier, the loading marker at the top.
  • stories/chat/history-window.stories.tsx: the long sample chat from Add sample conversations for the chat stories #96, and a paged variant with a delayed loader.
  • e2e/fixtures.tsx: a history-window scenario, with e2e/history-window.spec.ts.
  • src/hooks/use-history-window.test.ts; exports in src/index.ts.

A reasonable starting point: keep 120 items and load 60 more near the top.

Requirements

  • R25: window of the latest items; loads more near the top; freezes while scrolled up; trims on return; the item in view moves by no more than 1px when older items load.
  • R26: works with host paging (hasEarlier, onLoadEarlier); ConversationEarlier shows the loading marker.
  • R27: the ceiling written beside the hook: why windowing rather than virtualization (native scrolling, find-in-page and focus within the window, variable heights with nothing to measure), the conversation size it is designed for, and when a host should page from the server instead.
  • R5–R12: the shared component rules.

Constraints

  • Use captureAnchor / restoreAnchor from useConversation (Add Conversation and its scrolling #99) around each load; don't reimplement anchoring.
  • Never remove items while the reader is scrolled up.
  • No wall-clock budgets in tests. Assert the ceiling deterministically (render counts, items mounted, number of trims).
  • Tests use only the hook's public API.
  • The shared rules in Replace the chat components with a stateless Conversation family #94 apply: no new dependency or peer, no optional-peer import, token-only styling, no change to existing modules, additions only to src/index.ts and e2e/fixtures.tsx.

Done when

  • Unit tests cover:
    • Window: starts at the latest windowSize; the top trigger grows it by step; returning to the bottom trims it back.
    • Paged: when the local slice is exhausted and hasEarlier is true, onLoadEarlier is called once until it resolves.
    • Ceiling: over 10,000 in-memory items, the hook's work per update stays within the deterministic bound stated in its doc comment.
  • Browser spec: three loads in a row move the item in view by no more than 1px.
  • No serious or critical axe violation in the new stories, in light or dark.
  • bun run check, bun run build, bun run test:e2e and bun run dep-guard pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions