Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/react/hooks/use-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,40 @@ export interface UseMessagesResponse extends ChatStatusResponse {
*/
readonly getMessage: (serial: string) => Promise<Message>;

/**
* 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);
* }
* };
* ```
*/
Comment thread
coderabbitai[bot] marked this conversation as resolved.
readonly getVersions: (serial: string) => Promise<PaginatedResult<Message>>;

Comment on lines +102 to +135

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.

/**
* A shortcut to the {@link Messages.update} method.
*
Expand Down Expand Up @@ -511,6 +545,14 @@ export const useMessages = (params?: UseMessagesParams): UseMessagesResponse =>
[context],
);

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

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.


const deleteMessage = useCallback(
async (serial: string, details?: OperationDetails) => {
const room = await context.room;
Expand Down Expand Up @@ -655,6 +697,7 @@ export const useMessages = (params?: UseMessagesParams): UseMessagesResponse =>
roomError,
sendMessage,
getMessage,
getVersions,
updateMessage,
history,
deleteMessage,
Expand Down
6 changes: 6 additions & 0 deletions test/react/hooks/use-messages.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ describe('useMessages', () => {

const deleteSpy = vi.spyOn(mockRoom.messages, 'delete').mockResolvedValue({} as unknown as Message);

const getVersionsSpy = vi
.spyOn(mockRoom.messages, 'getVersions')
.mockResolvedValue({} as unknown as PaginatedResult<Message>);

const message = new DefaultMessage({
serial: '01719948956834-000@108TeGZDQBderu97202638',
clientId: 'client-1',
Expand All @@ -214,6 +218,7 @@ describe('useMessages', () => {
description: 'deleted',
metadata: { reason: 'test' },
});
await result.current.getVersions(message.serial);
});

expect(sendSpy).toHaveBeenCalledWith({ text: 'test message' });
Expand All @@ -222,6 +227,7 @@ describe('useMessages', () => {
description: 'deleted',
metadata: { reason: 'test' },
});
expect(getVersionsSpy).toHaveBeenCalledWith(message.serial);
});

it('should handle rerender if the room instance changes', async () => {
Expand Down
Loading