Skip to content

feat: expose getVersions() via the useMessages React hook#732

Merged
splindsay-92 merged 1 commit into
mainfrom
feat/use-messages-get-versions
Jun 5, 2026
Merged

feat: expose getVersions() via the useMessages React hook#732
splindsay-92 merged 1 commit into
mainfrom
feat/use-messages-get-versions

Conversation

@splindsay-92

@splindsay-92 splindsay-92 commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes the React parity gap for the getVersions() method shipped in v1.4.0 (#730). The core SDK exposes room.messages.getVersions(serial), but it was never surfaced through the useMessages hook.

What changed

  • UseMessagesResponse.getVersions — new shortcut field with JSDoc, mirroring the existing getMessage shortcut
  • useMessagesgetVersions useCallback delegating to room.messages.getVersions(serial), added to the returned object
  • Stable reference across renders for the same room, consistent with the other method shortcuts

Tests

  • Extended the "should correctly call the methods exposed by the hook" test to spy on getVersions and assert it delegates with the correct serial

Validation

  • npm run test:typescript
  • npm run test:react-unit ✅ (146 passed)
  • npm run docs:lint
  • ESLint clean on changed files

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Message version history is now available via a paginated endpoint, letting users retrieve and browse previous message versions in chronological order.
  • Tests
    • Hook behavior updated with tests to verify the new version-retrieval method is exposed and invoked correctly.

@coderabbitai

coderabbitai Bot commented Jun 1, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

Adds a stable getVersions(serial) method to the useMessages hook API that delegates to room.messages.getVersions(serial) and returns a Promise<PaginatedResult<Message>> (oldest-first). The hook, its response interface, and tests are updated to expose and verify this method.

Changes

getVersions Hook Integration

Layer / File(s) Summary
getVersions interface and implementation
src/react/hooks/use-messages.ts
UseMessagesResponse adds readonly getVersions(serial) returning paginated message versions (oldest-first). Hook implements getVersions as a useCallback delegating to room.messages.getVersions(serial) and includes it in the returned object.
Test coverage for getVersions wiring
test/react/hooks/use-messages.test.tsx
Test mocks room.messages.getVersions and verifies result.current.getVersions(message.serial) is callable and invokes the underlying method with the expected serial parameter.

Sequence Diagram(s)

sequenceDiagram
  participant ReactComponent as React component
  participant useMessages as useMessages hook
  participant Room as Room.messages
  participant REST as REST API
  ReactComponent->>useMessages: calls getVersions(serial)
  useMessages->>Room: room.messages.getVersions(serial)
  Room->>REST: HTTP GET /messages/:serial/versions
  REST-->>Room: PaginatedResult<Message> (oldest-first)
  Room-->>useMessages: PaginatedResult<Message>
  useMessages-->>ReactComponent: Promise<PaginatedResult<Message>>
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • ably/ably-chat-js#730: Both PRs add the same getVersions message-version-history API end-to-end (core API plus hook wiring and tests).

Suggested reviewers

  • AndyTWF
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately describes the main change: exposing the getVersions() method via the useMessages React hook.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/use-messages-get-versions

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions Bot temporarily deployed to staging/pull/732/typedoc June 1, 2026 14:11 Inactive
@github-actions

github-actions Bot commented Jun 1, 2026

Copy link
Copy Markdown

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 95.58% (🎯 95%) 4026 / 4212
🔵 Statements 95.58% (🎯 95%) 4026 / 4212
🔵 Functions 98.99% (🎯 98%) 296 / 299
🔵 Branches 97.12% (🎯 97%) 979 / 1008
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/react/hooks/use-messages.ts 93.82% 94.59% 100% 93.82% 542-544, 616-617, 624-629
Generated in workflow #3206 for commit e74e8f3 by the Vitest Coverage Report Action

@splindsay-92 splindsay-92 requested a review from AndyTWF June 1, 2026 16:43
Adds a getVersions shortcut to useMessages, mirroring getMessage, that
delegates to room.messages.getVersions(serial) and returns the full
message version history as a PaginatedResult<Message>.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
test/react/hooks/use-messages.test.tsx (1)

185-231: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Annotate this test change with the feature spec point.

Line 185-Line 231 extends coverage for new hook behavior but is missing a feature marker comment (for example // CHA-M10a) to keep spec-to-test traceability.

As per coding guidelines, "test/**/*.test.{ts,tsx}: Annotate tests with feature spec points like // CHA-M10a"

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/react/hooks/use-messages.test.tsx` around lines 185 - 231, This test
block for useMessages() (the it('should correctly call the methods exposed by
the hook', ...) case) needs the feature spec marker comment added (e.g. //
CHA-M10a) to maintain spec-to-test traceability; add the marker comment
immediately above the test declaration (or at the top of the test block) so the
test file test/react/hooks/use-messages.test.tsx is annotated with the
appropriate spec point for the new hook behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/react/hooks/use-messages.ts`:
- Around line 102-135: The public API surface changed (the exported hook method
getVersions in use-messages.ts), so update the public docs and the Ably CLI
bundles: add a review note “Public API changes detected. Please update the
documentation and the Ably CLI.” and update any public docs/README and the CLI
spec that reference Messages.getVersions/getVersions to reflect the new
signature/behavior; ensure changelog/typed API exports and any generated CLI
schema are regenerated to include the new getVersions entry.
- Around line 548-554: Add the feature-spec traceability annotation comment
above the new hook method; specifically, insert a line like "// `@CHA-M10a`"
immediately above the getVersions useCallback declaration (the function named
getVersions in use-messages.ts) following the existing project pattern for
src/**/*.{ts,tsx} so the new behavior is annotated for spec traceability.
- Around line 102-133: Update the JSDoc for the public getVersions function in
use-messages.ts to include explicit `@throws` tags: add two `@throws` entries
referencing {`@link` Ably.ErrorInfo} — one describing the error thrown when the
serial is null, undefined, or empty, and one describing errors from the Ably
Chat REST API (network/authorization/request failures). Ensure the new `@throws`
lines are added to the same JSDoc block that documents getVersions (the
hook-exported getVersions shortcut to Messages.getVersions) and follow the
existing style with {`@link` Type} references.

---

Outside diff comments:
In `@test/react/hooks/use-messages.test.tsx`:
- Around line 185-231: This test block for useMessages() (the it('should
correctly call the methods exposed by the hook', ...) case) needs the feature
spec marker comment added (e.g. // CHA-M10a) to maintain spec-to-test
traceability; add the marker comment immediately above the test declaration (or
at the top of the test block) so the test file
test/react/hooks/use-messages.test.tsx is annotated with the appropriate spec
point for the new hook behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: edec3dfc-a7cf-452b-9c49-7aa890c757d9

📥 Commits

Reviewing files that changed from the base of the PR and between 561b453 and e74e8f3.

📒 Files selected for processing (2)
  • src/react/hooks/use-messages.ts
  • test/react/hooks/use-messages.test.tsx

Comment thread src/react/hooks/use-messages.ts
Comment on lines +102 to +135
/**
* A shortcut to the {@link Messages.getVersions} method.
*
* Get all versions of a message by its serial, in oldest-first order.
*
* Returns the original create event followed by any subsequent update and delete events.
*
* **NOTE**: This method uses the Ably Chat REST API and so does not require the room
* to be attached to be called.
*
* This is a stable reference and will not be changed between renders for the same room.
* @param serial - The unique serial identifier of the message.
* @returns A Promise that resolves to a {@link PaginatedResult} of {@link Message} objects representing each version,
* or rejects with:
* - {@link Ably.ErrorInfo} when the serial is null, undefined, or empty
* - {@link Ably.ErrorInfo} when the Ably Chat REST API request fails due to network or authorization errors
* @example
* ```tsx
* const { getVersions } = useMessages();
*
* const handleGetVersions = async (messageSerial: string) => {
* try {
* const versions = await getVersions(messageSerial);
* for (const version of versions.items) {
* console.log(version.action, version.text);
* }
* } catch (error) {
* console.error('Failed to get message versions:', error);
* }
* };
* ```
*/
readonly getVersions: (serial: string) => Promise<PaginatedResult<Message>>;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Public API change follow-up is required.

Public API changes detected. Please update the documentation and the Ably CLI.

As per coding guidelines, "src/**/*.{ts,tsx}: If there are any changes to the public-facing API, add a review comment: 'Public API changes detected. Please update the documentation and the Ably CLI.'"

Also applies to: 700-700

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/react/hooks/use-messages.ts` around lines 102 - 135, The public API
surface changed (the exported hook method getVersions in use-messages.ts), so
update the public docs and the Ably CLI bundles: add a review note “Public API
changes detected. Please update the documentation and the Ably CLI.” and update
any public docs/README and the CLI spec that reference
Messages.getVersions/getVersions to reflect the new signature/behavior; ensure
changelog/typed API exports and any generated CLI schema are regenerated to
include the new getVersions entry.

Comment on lines +548 to +554
const getVersions = useCallback(
async (serial: string) => {
const room = await context.room;
return room.messages.getVersions(serial);
},
[context],
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Add feature-spec traceability annotation for the new hook method.

Line 548 introduces new behavior but lacks a feature tag comment (for example // @CHA-M10a``) used for spec traceability.

As per coding guidelines, "src/**/*.{ts,tsx}: Annotate code with feature spec points like // @CHA-M10a``"

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/react/hooks/use-messages.ts` around lines 548 - 554, Add the feature-spec
traceability annotation comment above the new hook method; specifically, insert
a line like "// `@CHA-M10a`" immediately above the getVersions useCallback
declaration (the function named getVersions in use-messages.ts) following the
existing project pattern for src/**/*.{ts,tsx} so the new behavior is annotated
for spec traceability.

@splindsay-92 splindsay-92 merged commit d20e241 into main Jun 5, 2026
21 of 22 checks passed
@splindsay-92 splindsay-92 deleted the feat/use-messages-get-versions branch June 5, 2026 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants