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.

2.9 KB
Raw Blame
import { type Accessor, createEffect, createSignal, onCleanup } from 'solid-js';

type ActivityDirection = 'start' | 'end';

export function useOffscreenActivity(options: {
  scrollRoot: Accessor<HTMLDivElement | undefined>;
  targetId: Accessor<string | undefined>;
}) {
  const [direction, setDirection] = createSignal<ActivityDirection>();
  let measuredTargetId: string | undefined;

  const update = (root: HTMLDivElement) => {
    const targetId = options.targetId();
    if (!targetId) {
      measuredTargetId = undefined;
      setDirection(undefined);
      return;
    }

    if (targetId !== measuredTargetId) {
      measuredTargetId = targetId;
      setDirection(undefined);
    }

    const target = document.getElementById(targetId);
    if (!target) {
      setDirection(undefined);
      return;
    }

    const rootBounds = root.getBoundingClientRect();
    const targetBounds = target.getBoundingClientRect();
    const targetIsVisible =
      targetBounds.bottom > rootBounds.top &&
      targetBounds.top < rootBounds.bottom;

    if (targetIsVisible || root.scrollHeight <= root.clientHeight + 1) {
      setDirection(undefined);
      return;
    }

    setDirection(targetBounds.bottom <= rootBounds.top ? 'start' : 'end');
  };

  createEffect(() => {
    const root = options.scrollRoot();
    if (!root) {
      setDirection(undefined);
      return;
    }

    measuredTargetId = undefined;
    const updateForRoot = () => update(root);
    root.addEventListener('scroll', updateForRoot, { passive: true });

    const resizeObserver = new ResizeObserver(updateForRoot);
    resizeObserver.observe(root);
    if (root.firstElementChild instanceof HTMLElement) {
      resizeObserver.observe(root.firstElementChild);
    }

    const mutationObserver = new MutationObserver(updateForRoot);
    mutationObserver.observe(root, { childList: true, subtree: true });

    queueMicrotask(updateForRoot);

    onCleanup(() => {
      root.removeEventListener('scroll', updateForRoot);
      resizeObserver.disconnect();
      mutationObserver.disconnect();
    });
  });

  createEffect(() => {
    options.targetId();
    const root = options.scrollRoot();
    if (!root) return;

    queueMicrotask(() => {
      if (root === options.scrollRoot()) update(root);
    });
  });

  const scrollToTarget = () => {
    const root = options.scrollRoot();
    const targetId = options.targetId();
    const target = targetId ? document.getElementById(targetId) : undefined;
    if (!root || !target) return;

    const rootBounds = root.getBoundingClientRect();
    const targetBounds = target.getBoundingClientRect();
    const rootCenter = rootBounds.top + rootBounds.height / 2;
    const targetCenter = targetBounds.top + targetBounds.height / 2;

    root.scrollTo({
      top: root.scrollTop + targetCenter - rootCenter,
      behavior: 'smooth',
    });
  };

  return { direction, scrollToTarget };
}