UNPKG

@atlaskit/renderer

Version:
69 lines 2.49 kB
import { useContext, useEffect } from 'react'; import { AnnotationUpdateEvent } from '@atlaskit/editor-common/types'; import { ProvidersContext } from '../context'; import { RendererContext as ActionsContext } from '../../RendererActionsContext'; import { useAnnotationManagerDispatch } from '../contexts/AnnotationManagerContext'; export const useLoadAnnotations = ({ adfDocument, isNestedRender, onLoadComplete }) => { const actions = useContext(ActionsContext); const providers = useContext(ProvidersContext); const { annotationManager, dispatch } = useAnnotationManagerDispatch(); const isAnnotationManagerEnabled = !!annotationManager; useEffect(() => { if (!providers) { return; } const { inlineComment: { getState: inlineCommentGetState, updateSubscriber: updateSubscriberInlineComment } } = providers; const annotations = actions.getAnnotationMarks(); // we don't want to request integrators for state with an empty list of ids. if (!annotations.length) { if (!isNestedRender) { // inlineCommentGetState handles empty lists gracefully. It has a side-effect of clearing state, which is why this call is needed inlineCommentGetState([], isNestedRender); } onLoadComplete && onLoadComplete({ numberOfUnresolvedInlineComments: 0 }); return; } const ids = annotations.map(mark => mark.attrs.id); const cb = data => { if (!updateSubscriberInlineComment) { return; } const payload = data.reduce((acc, value) => ({ ...acc, [value.id]: value }), {}); if (isAnnotationManagerEnabled) { dispatch({ type: 'loadAnnotation', data: Object.keys(payload).map(id => { var _payload$id$state; return { id, markState: (_payload$id$state = payload[id].state) !== null && _payload$id$state !== void 0 ? _payload$id$state : undefined }; }) }); } else { updateSubscriberInlineComment.emit(AnnotationUpdateEvent.SET_ANNOTATION_STATE, payload); } onLoadComplete && onLoadComplete({ numberOfUnresolvedInlineComments: data.filter(data => data.state === 'active').length }); }; inlineCommentGetState(ids, isNestedRender).then(cb); }, [actions, providers, adfDocument, isNestedRender, onLoadComplete, dispatch, isAnnotationManagerEnabled]); };