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.

14.7 KB
Raw Blame
import type { SoupApiItem } from '@service-storage/generated/schemas';
import type { InfiniteData, QueryKey } from '@tanstack/solid-query';
import { createSignal } from 'solid-js';

import { queryClient } from '../../client';
import {
  computeGroupKeysForItem,
  type ResolvedGroupMeta,
  resolveGroupMetaForKey,
} from '../grouped/api';
import type { GroupByField, GroupMeta } from '../grouped/types';
import type { SoupAstItemsGroupedPage } from '../items';
import { soupKeys } from '../keys';
import { getSoupQueryMeta } from './utils';

type GroupedGroupPage = {
  items: Record<string, SoupApiItem>;
  group: GroupMeta;
};

type GroupedGroupInfiniteData = InfiniteData<GroupedGroupPage, unknown>;

const [groupedCacheVersionSignal, setGroupedCacheVersion] = createSignal(0);

/**
 * Bumped whenever grouped soup caches are patched outside the query lifecycle
 * (optimistic updates, WS inserts/removals). Grouped views read group data
 * untracked behind a local version signal, so they subscribe to this to pick
 * up external patches.
 */
export const groupedCacheVersion = groupedCacheVersionSignal;

function bumpGroupedCacheVersion() {
  setGroupedCacheVersion((version) => version + 1);
}

/** Runtime guard for the normalized grouped parent page shape. */
function isGroupedPage(page: unknown): page is SoupAstItemsGroupedPage {
  if (!page || typeof page !== 'object') return false;
  const p = page as Record<string, unknown>;
  return (
    p.kind === 'grouped' &&
    Array.isArray(p.groups) &&
    p.items !== null &&
    typeof p.items === 'object' &&
    !Array.isArray(p.items)
  );
}

/** Builds a new optimistic group containing a single item. */
function makeGroup(meta: ResolvedGroupMeta, entityId: string): GroupMeta {
  return {
    ...meta,
    totalCount: 1,
    itemIds: [entityId],
    nextCursor: null,
  };
}

/** Moves an item id to the front of a group, incrementing count only if new. */
function prependId(group: GroupMeta, entityId: string): GroupMeta {
  const existing = group.itemIds.includes(entityId);

  return {
    ...group,
    itemIds: existing
      ? [entityId, ...group.itemIds.filter((id) => id !== entityId)]
      : [entityId, ...group.itemIds],
    totalCount: existing ? group.totalCount : group.totalCount + 1,
  };
}

/** Removes an item id from a group and decrements the total count safely. */
function removeId(group: GroupMeta, entityId: string): GroupMeta {
  if (!group.itemIds.includes(entityId)) return group;

  return {
    ...group,
    itemIds: group.itemIds.filter((id) => id !== entityId),
    totalCount: Math.max(0, group.totalCount - 1),
  };
}

/**
 * Recomputes an updated item's memberships across grouped parent AST queries.
 * Updates only resolvable grouped pages; unresolved grouping metadata is left
 * unchanged so callers can rely on later invalidation/refetch behavior.
 */
export function syncGroupedParents(entityId: string, entity: SoupApiItem) {
  const queries = queryClient.getQueryCache().findAll({
    queryKey: soupKeys.astItems._def,
  });

  for (const query of queries) {
    const prev = query.state.data as InfiniteData<unknown, unknown> | undefined;

    if (!prev || !Array.isArray(prev.pages) || prev.pages.length === 0) {
      continue;
    }

    const meta = getSoupQueryMeta(query.meta);

    if (!meta.groupBy) continue;

    // Empty memberships when the entity fails the query's item filter, so
    // `syncMembership` drops it instead of bucketing it into an unrelated
    // view. Mirrors the `meta.itemFilter` gate in `syncGroupQueries`.
    const filter = meta.itemFilter;
    const nextGroupKeys =
      filter && !filter(entity)
        ? []
        : computeGroupKeysForItem(entity, meta.groupBy);

    if (nextGroupKeys === undefined) {
      queryClient.invalidateQueries({ queryKey: query.queryKey });
      continue;
    }

    let changed = false;
    let needsInvalidation = false;

    const pages = [];
    for (const page of prev.pages) {
      if (!isGroupedPage(page)) {
        pages.push(page);
        continue;
      }

      // The insert gate only decides admission of rows the page doesn't hold
      // yet — rows already admitted keep the full membership sync.
      if (
        meta.insertFilter &&
        !meta.insertFilter(entity) &&
        !(entityId in page.items)
      ) {
        pages.push(page);
        continue;
      }

      const next = syncMembership(
        page,
        entityId,
        nextGroupKeys,
        entity,
        meta.groupBy
      );

      if (!next) {
        needsInvalidation = true;
        pages.push(page);
        continue;
      }

      if (next !== page) changed = true;
      pages.push(next);
    }

    if (needsInvalidation) {
      queryClient.invalidateQueries({ queryKey: query.queryKey });
      continue;
    }

    if (!changed) continue;

    query.setData({ ...prev, pages }, { manual: true });
    bumpGroupedCacheVersion();
  }
}

/**
 * Reconciles expanded single-group queries after an item changes.
 * Adds the item to matching first pages and removes it from groups it no
 * longer belongs to or no longer passes the query's item filter for.
 */
export function syncGroupQueries(entityId: string, entity: SoupApiItem) {
  const queries = queryClient.getQueryCache().findAll({
    queryKey: soupKeys.groupedGroup._def,
  });

  for (const query of queries) {
    const prev = query.state.data as GroupedGroupInfiniteData | undefined;
    if (!prev?.pages?.length) continue;

    const meta = getSoupQueryMeta(query.meta);
    if (!meta.groupBy || meta.groupKey == null) continue;

    // The insert gate only decides admission of rows the query doesn't hold
    // yet — rows already admitted keep the full membership sync.
    if (
      meta.insertFilter &&
      !meta.insertFilter(entity) &&
      !prev.pages.some((page) => page.group.itemIds.includes(entityId))
    ) {
      continue;
    }

    const filter = meta.itemFilter;
    const nextGroupKeys =
      filter && !filter(entity)
        ? []
        : computeGroupKeysForItem(entity, meta.groupBy);
    if (nextGroupKeys === undefined) {
      queryClient.invalidateQueries({ queryKey: query.queryKey });
      continue;
    }

    const existing = prev.pages.some((page) =>
      page.group.itemIds.includes(entityId)
    );
    const shouldHave = nextGroupKeys.includes(meta.groupKey);

    let changed = false;

    const pages = [];
    let index = 0;
    for (const page of prev.pages) {
      const hasEntity = page.group.itemIds.includes(entityId);

      if (shouldHave && index === 0 && !existing) {
        changed = true;
        pages.push({
          ...page,
          items: { ...page.items, [entityId]: entity },
          group: prependId(page.group, entityId),
        });
        index += 1;
        continue;
      }

      if (!shouldHave && hasEntity) {
        changed = true;
        const { [entityId]: _removed, ...items } = page.items;
        pages.push({
          ...page,
          items,
          group: removeId(page.group, entityId),
        });
        index += 1;
        continue;
      }

      // Membership unchanged: still refresh the item's data so in-place
      // edits (e.g. a property change that doesn't move it between groups)
      // reach the rendered rows.
      if (shouldHave && hasEntity && page.items[entityId] !== entity) {
        changed = true;
        pages.push({
          ...page,
          items: { ...page.items, [entityId]: entity },
        });
        index += 1;
        continue;
      }

      pages.push(page);
      index += 1;
    }

    if (changed) {
      query.setData({ ...prev, pages }, { manual: true });
      bumpGroupedCacheVersion();
    }
  }
}

/**
 * Inserts an item into a grouped parent page, creating optimistic group metadata
 * for any target groups that can be resolved locally.
 */
export function insertGroupedPage(
  page: SoupAstItemsGroupedPage,
  item: SoupApiItem,
  itemId: string,
  groupBy: GroupByField | undefined
) {
  const newKeys = computeGroupKeysForItem(item, groupBy);
  if (newKeys === undefined || newKeys.length === 0) return;

  const targetKeys = new Set(newKeys);
  const existingKeys = new Set(page.groups.map((g) => g.key));
  const groups: GroupMeta[] = [];

  for (const targetKey of targetKeys) {
    if (existingKeys.has(targetKey)) continue;

    const meta = resolveGroupMetaForKey(groupBy, targetKey, item);
    if (!meta) return;

    groups.push(makeGroup(meta, itemId));
  }

  for (const group of page.groups) {
    groups.push(targetKeys.has(group.key) ? prependId(group, itemId) : group);
  }

  return {
    ...page,
    items: { ...page.items, [itemId]: item },
    groups,
  };
}

/** Inserts an item into the first page of any expanded group query it matches. */
export function insertGroupQueries(
  item: SoupApiItem,
  itemId: string,
  keyFilter?: (key: QueryKey) => boolean
) {
  const queries = queryClient.getQueryCache().findAll({
    queryKey: soupKeys.groupedGroup._def,
  });

  for (const query of queries) {
    if (keyFilter && !keyFilter(query.queryKey)) continue;
    const prev = query.state.data as GroupedGroupInfiniteData | undefined;
    if (!prev?.pages?.length) continue;

    const meta = getSoupQueryMeta(query.meta);
    const filter = meta.itemFilter;
    if (filter && !filter(item)) continue;
    if (meta.insertFilter && !meta.insertFilter(item)) continue;

    if (!meta.groupBy || meta.groupKey == null) continue;

    const targetKeys = computeGroupKeysForItem(item, meta.groupBy);
    if (targetKeys === undefined) {
      queryClient.invalidateQueries({ queryKey: query.queryKey });
      continue;
    }
    if (!targetKeys.includes(meta.groupKey)) continue;

    // Continuation pages hold their own itemIds, so check them all — a
    // restore of an entity sitting on a later page must not duplicate it.
    if (prev.pages.some((page) => page.group.itemIds.includes(itemId)))
      continue;

    const firstPage = prev.pages[0];

    query.setData(
      {
        ...prev,
        pages: [
          {
            ...firstPage,
            items: { ...firstPage.items, [itemId]: item },
            group: prependId(firstPage.group, itemId),
          },
          ...prev.pages.slice(1),
        ],
      },
      { manual: true }
    );
    bumpGroupedCacheVersion();
  }
}

function matchingRemovalIds(
  entityIds: Set<string>,
  items: Record<string, SoupApiItem>,
  matches: ((item: SoupApiItem, id: string) => boolean) | undefined
): Set<string> {
  if (!matches) return entityIds;
  return new Set(
    [...entityIds].filter((id) => items[id] && matches(items[id], id))
  );
}
/** Removes items from a grouped parent page's item pool and group memberships. */
export function removeGroupedPage(
  page: SoupAstItemsGroupedPage,
  entityIds: Set<string>,
  matches?: (item: SoupApiItem, id: string) => boolean
) {
  const removedIds = matchingRemovalIds(entityIds, page.items, matches);
  const groups: GroupMeta[] = [];
  let changed = false;

  for (const group of page.groups) {
    const itemIds = group.itemIds.filter((id) => !removedIds.has(id));
    const removed = group.itemIds.length - itemIds.length;

    if (removed === 0) {
      groups.push(group);
      continue;
    }

    changed = true;
    groups.push({
      ...group,
      itemIds,
      totalCount: Math.max(0, group.totalCount - removed),
    });
  }

  const items: Record<string, SoupApiItem> = {};
  for (const [id, item] of Object.entries(page.items)) {
    if (removedIds.has(id)) {
      changed = true;
      continue;
    }

    items[id] = item;
  }

  return changed ? { ...page, items, groups } : page;
}

/** Removes items from all expanded group queries while preserving unaffected pages. */
export function removeGroupQueries(
  entityIds: Set<string>,
  keyFilter?: (key: QueryKey) => boolean,
  matches?: (item: SoupApiItem, id: string) => boolean
) {
  const queries = queryClient.getQueryCache().findAll({
    queryKey: soupKeys.groupedGroup._def,
  });

  for (const query of queries) {
    if (keyFilter && !keyFilter(query.queryKey)) continue;
    const prev = query.state.data as GroupedGroupInfiniteData | undefined;
    if (!prev?.pages?.length) continue;

    let changed = false;

    const pages = [];

    for (const page of prev.pages) {
      const removedIds = matchingRemovalIds(entityIds, page.items, matches);
      const itemIds = page.group.itemIds.filter((id) => !removedIds.has(id));
      const removed = page.group.itemIds.length - itemIds.length;
      let items = page.items;

      for (const id of removedIds) {
        if (id in items) {
          const { [id]: _, ...rest } = items;
          items = { ...rest };

          changed = true;
        }
      }

      if (removed === 0) {
        pages.push(items === page.items ? page : { ...page, items });
        continue;
      }

      changed = true;
      pages.push({
        ...page,
        items,
        group: {
          ...page.group,
          itemIds,
          totalCount: Math.max(0, page.group.totalCount - removed),
        },
      });
    }

    if (changed) {
      query.setData({ ...prev, pages }, { manual: true });
      bumpGroupedCacheVersion();
    }
  }
}

/** Reconcile group membership for an existing entity. Returns `undefined`
 * when a required new group cannot be created client-side and the caller should
 * invalidate instead of applying a partial optimistic move. */
function syncMembership(
  page: SoupAstItemsGroupedPage,
  entityId: string,
  nextGroupKeys: readonly string[],
  entity: SoupApiItem,
  groupBy: GroupByField | undefined
) {
  const nextGroups = new Set(nextGroupKeys);
  const currentGroups = new Set(page.groups.map((g) => g.key));

  let changed = false;

  const groups: GroupMeta[] = [];

  for (const group of nextGroups) {
    if (currentGroups.has(group)) continue;

    const meta = resolveGroupMetaForKey(groupBy, group, entity);

    if (!meta) return;

    changed = true;
    groups.push(makeGroup(meta, entityId));
  }

  for (const group of page.groups) {
    const hasEntity = group.itemIds.includes(entityId);
    const shouldInsert = nextGroups.has(group.key);

    if (hasEntity === shouldInsert) {
      groups.push(group);
      continue;
    }

    changed = true;
    groups.push(
      shouldInsert ? prependId(group, entityId) : removeId(group, entityId)
    );
  }

  // Data-only refresh: membership is unchanged but the cached item is stale
  // (e.g. a property edit that doesn't move it between groups).
  const dataChanged =
    nextGroups.size > 0 &&
    page.items[entityId] !== undefined &&
    page.items[entityId] !== entity;

  if (!changed && !dataChanged) return page;

  const newGroupsExist = nextGroups.size > 0;
  const nextItems: Record<string, SoupApiItem> = {};

  for (const [id, item] of Object.entries(page.items)) {
    if (id !== entityId || newGroupsExist) {
      nextItems[id] = item;
    }
  }

  if (newGroupsExist) {
    nextItems[entityId] = entity;
  }

  return { ...page, items: nextItems, groups };
}