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.8 KB
Raw Blame
/**
 * Read side of an in-flight option selection, kept free of any transport
 * import. List rows read this on every render, and pulling the mutation module
 * in would drag the REST and GraphQL clients (and the Soup websocket) into
 * every consumer that only wants to display a pending tag.
 */

import type { Property, PropertyDefinitionDomain } from '@property/types';
// The concrete module, not the `@property/utils` barrel, which pulls UI and
// side-effecting imports along with it.
import { isInstantiatedProperty } from '@property/utils/typeGuards';
import type { EntityType } from '@service-properties/generated/schemas/entityType';
import { useMutationState } from '@tanstack/solid-query';

/** One tag-picker selection, as submitted to the option-update mutation. */
export type BulkUpdateEntityPropertyOptionsParams = {
  entityId: string;
  entityType: EntityType;
  properties: Array<{
    property: Property | PropertyDefinitionDomain;
    /** Preserve assignment identity without losing the definition's TAG data type. */
    assignmentId?: string;
    currentOptionIds: string[];
    nextOptionIds: string[];
  }>;
};

/**
 * Mutation-cache key for an entity's bulk option updates. Used both as the
 * mutation's serialization scope and to read its in-flight variables for
 * optimistic display.
 */
export function bulkEntityPropertyOptionsKey(entityId: string) {
  return ['bulkEntityPropertyOptions', entityId] as const;
}

function propertyDefinitionIdOf(
  property: Property | PropertyDefinitionDomain
): string {
  return isInstantiatedProperty(property)
    ? property.propertyDefinitionId
    : property.id;
}

/**
 * Optimistic overlay for sources outside the mutation's normalized cache and
 * edits waiting for their turn in the per-entity mutation scope. Returns
 * the option ids an in-flight update is applying, or `undefined` when nothing is
 * in flight for the property, so callers fall back to the persisted value. On
 * settle the mutation leaves `pending` and the overlay disappears — no manual
 * rollback.
 */
export function useInFlightEntityPropertyOptions(entityId: string) {
  const inFlight = useMutationState(() => ({
    filters: {
      mutationKey: bulkEntityPropertyOptionsKey(entityId),
      status: 'pending' as const,
    },
    select: (mutation) =>
      mutation.state.variables as
        | BulkUpdateEntityPropertyOptionsParams
        | undefined,
  }));

  return (propertyDefinitionId: string): string[] | undefined => {
    const pending = inFlight();
    // Latest in-flight update targeting this property wins.
    for (let index = pending.length - 1; index >= 0; index--) {
      const match = pending[index]?.properties.find(
        (update) =>
          propertyDefinitionIdOf(update.property) === propertyDefinitionId
      );
      if (match) return match.nextOptionIds;
    }
    return undefined;
  };
}