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.

3.5 KB
Raw Blame
import {
  GUTTER_MARGIN,
  MIN_RIGHT_COLUMN_WIDTH,
  THREAD_WIDTH,
} from '@block-pdf/signal/viewerThreeColumnLayout';
import { usePageCommentLayout } from '@block-pdf/store/comments/commentLayout';
import { useCreateMessageComment } from '@block-pdf/store/comments/commentOperations';
import type { ThreadId } from '@core/comments/commentType';
import {
  baseCommentTheme,
  CommentsContext,
  type CommentsContextType,
  Thread,
} from '@core/comments/Thread';
import { Key } from '@solid-primitives/keyed';
import { createSelector } from 'solid-js';
import { usePdfComments } from '../context/pdf-comments-context';
import { usePdfDocument } from '../context/pdf-document-context';

const rightMarginStyle = {
  minWidth: `${MIN_RIGHT_COLUMN_WIDTH}px`,
  width: `${THREAD_WIDTH - GUTTER_MARGIN * 2}px`,
  right: `${-THREAD_WIDTH + GUTTER_MARGIN}px`,
};

export function RightMarginLayout(props: { pageIndex: number }) {
  return (
    <div
      class="rightMargin absolute top-0 z-pdf-comments pointer-events-auto [transition: width 0.05s linear, right 0.05s linear]"
      style={rightMarginStyle}
    >
      <CommentsAndSuggestions pageIndex={props.pageIndex} />
    </div>
  );
}

const useCommentsContext = (
  setThreadHeight: CommentsContextType['setThreadHeight']
): CommentsContextType => {
  const pdf = usePdfDocument();
  const comments = usePdfComments();
  const setActiveThread = (threadId: ThreadId | null) => {
    if (threadId == null) {
      comments.clearActiveThread();
    } else {
      comments.activateThread(threadId);
    }
  };

  const createComment = useCreateMessageComment();

  const commentsContext: CommentsContextType = {
    setActiveThread,
    setThreadHeight,
    canComment: () => !pdf.isNested() && pdf.permissions.canComment(),
    isDocumentOwner: pdf.permissions.isOwner,
    documentId: pdf.documentId(),
    documentType: 'pdf',
    messageOperations: { createComment },
    highlightedCommentId: () => null,
  };

  return commentsContext;
};

function CommentsAndSuggestions(props: { pageIndex: number }) {
  const comments = usePdfComments();
  const { threads, setThreadHeight } = usePageCommentLayout(
    () => props.pageIndex
  );

  const isActiveThreadSelector = createSelector(comments.activeThreadId);

  const isSelectingThreadSelector = createSelector(comments.selectedThreadId);

  const commentTheme = (threadId: ThreadId | null) => {
    const isSelecting = isSelectingThreadSelector(threadId);
    let theme = {
      ...baseCommentTheme,
      text: {
        ...baseCommentTheme.text,
        base: isSelecting ? 'select-text!' : 'select-none',
      },
    };
    return theme;
  };

  const handleThreadMouseDown = (threadId: ThreadId) => (e: MouseEvent) => {
    e.stopPropagation();
    comments.selectThread(threadId);

    const handleMouseUp = (e: MouseEvent) => {
      e.stopPropagation();
      comments.activateThread(threadId);
      document.removeEventListener('mouseup', handleMouseUp, true);
    };
    document.addEventListener('mouseup', handleMouseUp, true);
  };

  const commentsContext = useCommentsContext(setThreadHeight);

  return (
    <CommentsContext.Provider value={commentsContext}>
      <Key each={threads()} by="threadId">
        {(root) => (
          <Thread
            comment={root()}
            layout={root().layout}
            isActive={isActiveThreadSelector(root().threadId)}
            theme={commentTheme(root().threadId)}
            handleMouseDown={handleThreadMouseDown(root().threadId)}
          />
        )}
      </Key>
    </CommentsContext.Provider>
  );
}