1. Take action when a comment is added

Using Props:

<VeltComments onCommentAdd={(event) => yourMethod(event)} />

const yourMethod = (event) => {
  event?.addContext({ customKey: 'customValue' });
}

Using Hooks:

import { useCommentAddHandler} from '@veltdev/react';

export default function YourDocument() {

  const commentAddEvent = useCommentAddHandler();

  useEffect(() => {
    console.log('commentAddEvent', commentAddEvent);
  }, [commentAddEvent]);

  return (
    <div></div>
  )
}

Using API:

const commentElement = client.getCommentElement();

commentElement.onCommentAdd().subscribe((event) => {
  console.log('commentAddEvent', event);
});

onCommentAdd Event Data Schema

Field NameTypeDescription
addContextFunctionUse this to set custom data on the comment
annotationCommentAnnotationThe annotation that is associated with the comment that was updated
documentIdstringThe document ID where the comment was added
locationObjectThe location where the comment was added
targetAnnotationIdstringThe id of the target annotation

2. Take action when comment is updated

Using Props:

<VeltComments onCommentUpdate={(event) => yourMethod(event)} />

const yourMethod = (event) => {
  console.log('commentUpdateEvent', event);
}

Using Hooks:

import { useCommentUpdateHandler} from '@veltdev/react';

export default function YourDocument() {

  const commentUpdateEvent = useCommentUpdateHandler();
  useEffect(() => {
    console.log('commentUpdateEvent', commentUpdateEvent);
  }, [commentUpdateEvent]);

  return (
    <div></div>
  )
}

Using API:

const commentElement = client.getCommentElement();

commentElement.onCommentUpdate().subscribe((event) => {
  console.log('commentUpdateEvent', event);
});

onCommentUpdate Event Data Schema

Field NameTypeDescription
annotationCommentAnnotationThe annotation that is associated with the comment that was updated
typestringThe type of comment that was updated
targetAnnotationIdstringThe ID of the target annotation that contains the comment that was updated
targetCommentIdnumberThe ID of the target comment that was updated

3. Listen to changes in comment mode

The comment mode is toggled on and off when you click on the Comment Tool.

The useCommentModeState() hook can be used to get the Comment mode without having to subscribe to changes. When the Comment mode changes, the hook return value will update.

The subscription is automatically unsubscribed when the component dismounts.

import { useCommentModeState } from '@veltdev/react';

export default function YourDocument() {

  let commentModeState = useCommentModeState()

  return (
    <div>
       Comment Mode is turned on: {commentModeState}
    </div>
  )
}

4. Listen to Comment Selection changes

The useCommentSelectionChangeHandler hook can be used to subscribe to Comment selection changes.

import React, { useEffect } from 'react';
import { useCommentSelectionChangeHandler } from '@veltdev/react';

function YourComponent() {
    const commentSelectionChange = useCommentSelectionChangeHandler();

    useEffect(() => {
        console.log('commentSelectionChange', commentSelectionChange);
    }, [commentSelectionChange]);

    return (
        <>
            Selected Comment: {commentSelectionChange.annotation.id}
        </>
    );
}

You can add a callback method for when a comment link is copied. This can be useful for tracking when a comment link is copied or showing a confirmation toast to the user.

Using Hooks:

  const commentLink = useCommentCopyLinkHandler();
  useEffect(() => {
    console.log('commentLink', commentLink);
  }, [commentLink]);

Using API:

const commentElement = client.getCommentElement();
commentElement.onCopyLink().subscribe((commentLink) => {
  console.log(commentLink);
});