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.0 KB
Raw Blame
import { isTouchDevice } from '@core/mobile/isTouchDevice';
import { type Accessor, createEffect, on } from 'solid-js';

type Anchor = { id: string; top: number };

/** A row past the first one keeps the anchor when the first is the one removed. */
const ANCHOR_CANDIDATES = 3;
/**
 * A touch scroll still moving is left alone: setting `scrollTop` stops iOS
 * momentum scrolling dead. Scroll events arrive every frame while it moves.
 */
const TOUCH_SCROLL_SETTLE_MS = 150;

// Threads only: a pinned date header stays put whatever moves under it.
const rowElements = (scroller: HTMLElement) => [
  ...scroller.querySelectorAll<HTMLElement>('[data-soup-entity][id]'),
];

/**
 * Keeps the rows on screen where they are when rows are added or removed
 * above them: new mail arriving, or a thread marked done from elsewhere.
 * Without it the list's content shifts under the user while the scroll
 * offset stays put. At the top of the list nothing is anchored, so new mail
 * shows up there.
 *
 * Rows are found within the scroller by their DOM `id`, which is their row id.
 */
export function createScrollAnchor(options: {
  scroller: Accessor<HTMLElement | undefined>;
  rows: Accessor<readonly { id: string }[]>;
}) {
  let anchors: Anchor[] = [];
  let lastScrollAt = Number.NEGATIVE_INFINITY;

  const measure = () => {
    const scroller = options.scroller();
    anchors = [];
    if (!scroller || scroller.scrollTop <= 0) return;
    const viewTop = scroller.getBoundingClientRect().top;
    // Virtualized rows are positioned, so DOM order is not screen order.
    anchors = rowElements(scroller)
      .map((element) => ({
        id: element.id,
        top: element.getBoundingClientRect().top,
      }))
      .filter((anchor) => anchor.top >= viewTop)
      .sort((left, right) => left.top - right.top)
      .slice(0, ANCHOR_CANDIDATES);
  };
  /** Records where the first rows on screen are; call on every scroll. */
  const capture = () => {
    lastScrollAt = performance.now();
    measure();
  };
  const touchScrolling = () =>
    isTouchDevice() &&
    performance.now() - lastScrollAt < TOUCH_SCROLL_SETTLE_MS;

  // The virtualizer positions the new rows in the same flush, so the anchor's
  // move is measurable here, before the user sees a frame of it.
  createEffect(
    on(
      options.rows,
      () => {
        const scroller = options.scroller();
        if (!scroller) return;
        if (touchScrolling()) {
          measure();
          return;
        }
        // Another split can show the same rows under the same ids.
        const rows = new Map(
          rowElements(scroller).map((element) => [element.id, element])
        );
        for (const anchor of anchors) {
          const element = rows.get(anchor.id);
          if (!element) continue;
          const moved = element.getBoundingClientRect().top - anchor.top;
          if (Math.abs(moved) >= 1) scroller.scrollTop += moved;
          break;
        }
        measure();
      },
      { defer: true }
    )
  );

  return { capture };
}