Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

1.4 KB
Raw Blame
'use client';

import { createContext, type ReactNode, use } from 'react';

import type { DocumentCommentAnchorsValue } from './useDocumentCommentAnchors';

const noop = () => {};
const nullValue = () => null;

/**
 * Keeps cards that render outside the list β€” the pinned deep-link thread
 * mounts before the list exists β€” from having to null-check the resolution.
 */
const FALLBACK: DocumentCommentAnchorsValue = {
  activeRootId: null,
  bodyElement: null,
  getAnchorMatch: nullValue,
  getAnchorRange: nullValue,
  getPendingAnchorMatch: nullValue,
  getPendingAnchorRange: nullValue,
  locateInBody: noop,
  orphanedRootIds: new Set(),
  pickTick: 0,
  resolvedAt: 0,
  selectedRootId: null,
  selectRoot: noop,
  setHoveredRootId: noop,
};

const DocumentCommentAnchorsContext = createContext<DocumentCommentAnchorsValue>(FALLBACK);

/**
 * Anchors are resolved once for the whole document β€” one DOM walk, one
 * repaint β€” and read by every card and by the gutter, so the resolution lives
 * in context rather than being recomputed per card.
 */
export const DocumentCommentAnchorsProvider = ({
  children,
  value,
}: {
  children: ReactNode;
  value: DocumentCommentAnchorsValue;
}) => <DocumentCommentAnchorsContext value={value}>{children}</DocumentCommentAnchorsContext>;

export const useCommentAnchors = (): DocumentCommentAnchorsValue =>
  use(DocumentCommentAnchorsContext);