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.

5.1 KB
Raw Blame
import type { AcceptanceStatus } from '@lobechat/types';

import type { AcceptanceListItem } from '@/services/verify';

import { acceptanceListPath } from '../Viewer/routes';
import { type AcceptanceStatusAction, getAcceptanceStatusActions } from '../Viewer/statusActions';

export type AcceptanceSelectAllState = 'all' | 'none' | 'partial';

export const toggleAcceptanceSelection = (selected: string[], id: string): string[] =>
  selected.includes(id) ? selected.filter((entry) => entry !== id) : [...selected, id];

/**
 * Shift-click: every row between the anchor and the target in VISUAL order,
 * collapsed groups included β€” the batch bar count shows what was swept. An
 * anchor that has scrolled out of the visible order degrades to a single pick.
 */
export const rangeAcceptanceSelection = (
  orderedIds: string[],
  anchor: string | null,
  target: string,
  selected: string[],
): string[] => {
  const from = anchor ? orderedIds.indexOf(anchor) : -1;
  const to = orderedIds.indexOf(target);
  const range =
    from === -1 || to === -1
      ? [target]
      : orderedIds.slice(Math.min(from, to), Math.max(from, to) + 1);

  const merged = new Set(selected);
  for (const id of range) merged.add(id);
  return [...merged];
};

/**
 * The selection as the user can currently SEE it.
 *
 * Selection is remembered raw, but every read narrows it to the rows the active
 * filter/search is showing: a sweep must never touch a row that scrolled out of
 * the list behind a filter change, and the "3 selected" count must match the
 * three ticked boxes on screen. Clearing the filter brings the earlier picks
 * back, which is what a user who filtered mid-selection expects.
 */
export const visibleAcceptanceSelection = (
  selected: string[],
  visibleItems: AcceptanceListItem[],
): string[] => {
  const visible = new Set(visibleItems.map((item) => item.id));
  return selected.filter((id) => visible.has(id));
};

export const acceptanceSelectAllState = (
  visibleCount: number,
  selectedCount: number,
): AcceptanceSelectAllState => {
  if (visibleCount === 0 || selectedCount === 0) return 'none';
  return selectedCount >= visibleCount ? 'all' : 'partial';
};

/** Select-all toggles to "everything visible" unless everything already is. */
export const nextAcceptanceSelectAll = (
  selected: string[],
  visibleItems: AcceptanceListItem[],
): string[] => {
  const visibleIds = visibleItems.map((item) => item.id);
  const state = acceptanceSelectAllState(
    visibleIds.length,
    visibleAcceptanceSelection(selected, visibleItems).length,
  );
  if (state === 'all') return selected.filter((id) => !visibleIds.includes(id));

  const merged = new Set(selected);
  for (const id of visibleIds) merged.add(id);
  return [...merged];
};

/**
 * The subset of a selection a status action can actually move.
 *
 * A sweep mixes states β€” an already-accepted delivery cannot be accepted again,
 * and a still-verifying one is not decidable at all. Filtering here (rather than
 * letting the server reject row by row) is what lets the bar disable an action
 * that would do nothing and report an honest count for one that does.
 */
export const acceptanceBatchTargets = (
  items: AcceptanceListItem[],
  selected: string[],
  action: AcceptanceStatusAction,
): string[] => {
  const selectedSet = new Set(selected);
  return items
    .filter(
      (item) =>
        selectedSet.has(item.id) &&
        getAcceptanceStatusActions(item.status as AcceptanceStatus, item.subjectType).includes(
          action,
        ),
    )
    .map((item) => item.id);
};

/**
 * The subset of a selection a project move can actually change.
 *
 * A row already filed under the target project (or, for `null`, one that is in
 * no project at all) has nowhere to move β€” leaving it out keeps the reported
 * count honest, the same rule the status sweep applies.
 */
export const acceptanceProjectTargets = (
  items: AcceptanceListItem[],
  selected: string[],
  projectId: string | null,
): string[] => {
  const selectedSet = new Set(selected);
  return items
    .filter((item) => selectedSet.has(item.id) && (item.project?.id ?? null) !== projectId)
    .map((item) => item.id);
};

/**
 * The server caps one sweep at 200 ids, so a select-all after enough scrolling
 * would be rejected wholesale β€” accept, close and delete all failing before
 * anything changed. Split the work instead: a sweep is a background chore, and
 * a partial result the caller can report beats a hard refusal.
 */
export const ACCEPTANCE_BATCH_CHUNK = 200;

export const chunkAcceptanceBatch = (ids: string[], size = ACCEPTANCE_BATCH_CHUNK): string[][] => {
  if (ids.length === 0) return [];
  if (ids.length <= size) return [ids];

  const chunks: string[][] = [];
  for (let index = 0; index < ids.length; index += size) {
    chunks.push(ids.slice(index, index + size));
  }
  return chunks;
};

export const acceptanceRedirectAfterDelete = (
  openId: string | undefined,
  targets: string[],
  failedIds: string[],
  routeBase?: string,
): string | undefined =>
  openId && targets.includes(openId) && !failedIds.includes(openId)
    ? acceptanceListPath(routeBase)
    : undefined;