Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

1.7 KB
Raw Blame
import { usePdfCommentRealtimeBehavior } from '@block-pdf/store/commentsResource';
import { isPdfDraftThreadId } from '@block-pdf/type/comments';
import { fireAndForget } from '@core/util/fireAndForget';
import { createEffect, createMemo } from 'solid-js';
import { usePdfComments } from '../../context/pdf-comments-context';
import {
  useDeleteNewComments,
  useScrollToCommentThread,
} from './commentOperations';

const useDeleteNewCommentEffect = () => {
  const deleteNewComments = useDeleteNewComments();
  const activeCommentThreadId = usePdfComments().activeThreadId;

  createEffect(() => {
    const activeThreadId = activeCommentThreadId();
    if (!isPdfDraftThreadId(activeThreadId)) {
      deleteNewComments();
    }
  });
};

const useScrollToActiveThreadEffect = () => {
  const scrollToCommentThread = useScrollToCommentThread();
  const commentsContext = usePdfComments();
  const comments = commentsContext.all;
  const activeCommentThreadId = commentsContext.activeThreadId;
  const activeThreadScrollingSuppressed = commentsContext.scrollingSuppressed;
  const hasActiveThread = createMemo(() => {
    const activeThreadId = activeCommentThreadId();
    return (
      activeThreadId != null &&
      comments().some((comment) => comment.threadId === activeThreadId)
    );
  });

  createEffect(() => {
    if (activeThreadScrollingSuppressed()) return;

    const activeThreadId = activeCommentThreadId();
    if (activeThreadId == null) return;
    if (!hasActiveThread()) return;

    if (!isPdfDraftThreadId(activeThreadId))
      fireAndForget(scrollToCommentThread(activeThreadId), 'pdf-comments');
  });
};

export const usePdfCommentEffects = () => {
  usePdfCommentRealtimeBehavior();
  useDeleteNewCommentEffect();
  useScrollToActiveThreadEffect();
};