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.

11.8 KB
Raw Blame
import type { TrashCountByType, TrashItem } from '@lobechat/types';

import { getActiveWorkspaceId } from '@/business/client/hooks/useActiveWorkspaceId';
import {
  cacheScope,
  createReplicaSlice,
  recordLens,
  type ReplicaPageResult,
  type ReplicaSyncResult,
} from '@/libs/replica';
import { mutate } from '@/libs/swr';
import { type TrashCountFilter, trashService, type TrashViewFilter } from '@/services/trash';
import { channelActions } from '@/store/channel';
import type { StoreSetter } from '@/store/types';
import { setNamespace } from '@/utils/storeDebug';

import type { TrashListData } from './initialState';
import {
  trashCountKey,
  trashCountResource,
  trashListKey,
  type TrashListParams,
  trashListResource,
} from './projection';
import { trashSelectors } from './selectors';
import type { TrashStore } from './store';

const n = setNamespace('trash');

type Setter = StoreSetter<TrashStore>;

/**
 * SWR key roots whose lists can regain rows after a restore. Lists painted from
 * a local copy are replicas and revalidate through their `replica:sync` keys;
 * the rest are the in-memory SWR keys still registered in `@/libs/swr/keys`.
 */
const RESTORE_AFFECTED_KEY_PREFIXES = [
  'agent:',
  'image:',
  // Replicas (agent sidebar, topic list, documents, tasks, recents, …) sync
  // through `replica:sync` keys (`local-replica`).
  'replica:',
  // conversation transcripts — a restored message must reappear in its thread
  'message:',
  'page',
  'project',
  'topic:',
  'video:',
];

/**
 * "Empty trash" stopped because the user switched to another scope (account /
 * workspace) while it was running. Every batch already sent ran in the scope
 * the sweep started in; nothing was purged anywhere else.
 */
export class TrashEmptyScopeChangedError extends Error {
  constructor() {
    super('The active scope changed while emptying the trash');
    this.name = 'TrashEmptyScopeChangedError';
  }
}

export const trashSlice = (set: Setter, get: () => TrashStore, _api?: unknown) =>
  new TrashActionImpl(set, get, _api);

/**
 * Recycle-bin store. The list and the per-type counts are local-first replicas:
 * each filter keeps its own persisted page (first frame paints from storage,
 * the network confirms), and a restore / purge drops the row from every loaded
 * filter before the counts revalidate.
 */
export class TrashActionImpl {
  readonly #get: () => TrashStore;
  readonly #set: Setter;
  readonly #trashCount;
  readonly #trashList;

  constructor(set: Setter, get: () => TrashStore, _api?: unknown) {
    void _api;
    this.#set = set;
    this.#get = get;
    this.#trashList = createReplicaSlice(trashListResource, {
      actionPrefix: n('trashList'),
      fetcher: this.#fetchPage,
      get,
      set,
      stateKey: 'trashListReplica',
      view: recordLens<TrashStore, TrashListData>('trashListMap'),
    });
    this.#trashCount = createReplicaSlice(trashCountResource, {
      actionPrefix: n('trashCount'),
      fetcher: (filter) => trashService.countByType(filter),
      get,
      set,
      stateKey: 'trashCountReplica',
      view: recordLens<TrashStore, TrashCountByType>('trashCountMap'),
    });
  }

  /** One recycle-bin page: `cursor` undefined = the head (newest first). */
  #fetchPage = async (
    filter: TrashListParams,
    cursor?: string,
  ): Promise<ReplicaPageResult<TrashItem, string>> => {
    const { items, nextCursor } = await trashService.list(cursor ? { ...filter, cursor } : filter);
    return { items, nextCursor };
  };

  /**
   * Switch the view. Each view is its own keyed entry, so a late response for
   * the previous one lands in that entry and never repaints this one; the
   * entry switched to starts again from its head page instead of the pages
   * loaded on an earlier visit. `scope` binds the project to the scope it was
   * chosen in (see `trashSelectors.activeProjectId`).
   */
  setFilter = (filter: TrashViewFilter, scope: string) => {
    const { projectId, resourceType, visibility } = filter;
    this.#set(
      {
        activeType: resourceType,
        // A personal view has no side; keep the workspace side for the next visit.
        ...(visibility ? { activeVisibility: visibility } : {}),
        projectSelection: { projectId, scope },
      },
      false,
      n('setFilter'),
    );
    this.#trashList.collapse(trashListKey(filter));
  };

  /** Re-fetch the list(s) and the counts of the active identity. */
  refresh = async () => {
    await Promise.all([this.#trashList.revalidate(), this.#trashCount.revalidate()]);
  };

  /**
   * A restore brings rows back into lists owned by other stores (sidebar
   * agents, topics, pages, files, tasks …). Revalidate every mounted SWR key in
   * those namespaces so a user returning from the recycle bin sees the row
   * without a reload. `mutate` with a filter only re-fetches keys that have a
   * mounted subscriber, so this is cheap.
   */
  revalidateRestoredScopes = async () => {
    await mutate(
      (key: unknown) =>
        Array.isArray(key) &&
        typeof key[0] === 'string' &&
        RESTORE_AFFECTED_KEY_PREFIXES.some((prefix) => (key[0] as string).startsWith(prefix)),
    );
  };

  /** Next page of one view, using its loaded head page's params. */
  loadMore = async (filter: TrashViewFilter) => {
    await this.#trashList.loadMore(trashListKey(filter), filter);
  };

  /** Drop a row from every loaded filter (the engine also patches persisted rows). */
  #dropRows = (ids: Iterable<string>) => {
    for (const id of ids) this.#trashList.updateEntity(id, () => undefined);
  };

  /**
   * After a write, the counts of every view but the one on screen are stale,
   * and so are their rows after a sweep (it reaches past the loaded pages).
   * Drop them, memory and persisted copy, so a later visit loads them instead
   * of painting numbers or rows that no longer exist. The view on screen is
   * revalidated in place by `refresh`.
   */
  #dropOtherViews = ({ rows }: { rows: boolean }) => {
    const state = this.#get();
    const { activeType, trashCountMap, trashListMap } = state;
    const projectId = trashSelectors.activeProjectId(cacheScope.get())(state);
    const visibility = trashSelectors.activeVisibility(!!getActiveWorkspaceId())(state);
    const countKey = trashCountKey({ projectId, visibility });
    for (const key of Object.keys(trashCountMap)) {
      if (key !== countKey) this.#trashCount.remove(key);
    }
    if (!rows) return;
    const listKey = trashListKey({ projectId, resourceType: activeType, visibility });
    for (const key of Object.keys(trashListMap)) {
      if (key !== listKey) this.#trashList.remove(key);
    }
  };

  #withLoading = async (
    ids: string[],
    run: () => Promise<void>,
    { sweep = false }: { sweep?: boolean } = {},
  ) => {
    const scope = cacheScope.get();
    this.#set({ loadingIds: [...this.#get().loadingIds, ...ids] }, false, n('loading/start'));
    try {
      await run();
    } finally {
      const done = new Set(ids);
      this.#set(
        { loadingIds: this.#get().loadingIds.filter((id) => !done.has(id)) },
        false,
        n('loading/end'),
      );
      // Views of another scope are not ours to drop (a switch resets them anyway).
      if (cacheScope.get() === scope) this.#dropOtherViews({ rows: sweep });
      await this.refresh();
    }
  };

  /**
   * Restore roots. Returns the server outcome so the caller can toast the
   * partial failures (a topic whose agent is still in the bin, …). Only the
   * successfully restored rows leave the list.
   */
  restore = async (ids: string[]) => {
    let outcome: Awaited<ReturnType<typeof trashService.restore>> = { failed: [], restored: [] };
    await this.#withLoading(ids, async () => {
      outcome = await trashService.restore(ids);
      const gone = new Set(outcome.restored.map((item) => item.id));
      // `notFound` rows were dropped from the registry server-side.
      for (const failure of outcome.failed) if (failure.code === 'notFound') gone.add(failure.id);
      this.#dropRows(gone);
    });
    const threadIds = outcome.restored
      .filter((item) => item.resourceType === 'channelThread')
      .map((item) => item.resourceId);
    // The Channel store keeps hiding deleted threads; a restored one keeps its ID.
    if (threadIds.length > 0) channelActions.forgetThreadRemovals(threadIds);
    if (outcome.restored.length > 0) void this.revalidateRestoredScopes();
    return outcome;
  };

  /**
   * Purge roots. Returns the server outcome: a root its owner cannot delete
   * yet (a channel still stopping its replies) is `blocked` and stays listed.
   */
  purge = async (ids: string[]) => {
    let outcome: Awaited<ReturnType<typeof trashService.purge>> = { blocked: [], purged: 0 };
    await this.#withLoading(ids, async () => {
      outcome = await trashService.purge(ids);
      const blocked = new Set(outcome.blocked);
      this.#dropRows(ids.filter((id) => !blocked.has(id)));
    });
    return outcome;
  };

  /**
   * Purge every root `filter` covers, across all pages, in the scope the call
   * starts in. The filter and scope are fixed for the whole sweep: switching
   * the view (or the workspace) while batches run never widens what is
   * purged — a scope switch stops the sweep with `TrashEmptyScopeChangedError`.
   * Returns how many roots had to stay in the bin for now.
   */
  emptyTrash = async (filter: TrashViewFilter) => {
    const scope = cacheScope.get();
    const workspaceId = getActiveWorkspaceId();
    const key = trashListKey(filter);
    const ids = this.#get().trashListMap[key]?.items.map((item) => item.id) ?? [];
    // A blocked root stays listed, so later batches report it again: the largest
    // batch count is the number left behind, not the sum.
    let blocked = 0;
    await this.#withLoading(
      ids,
      async () => {
        // The server purges one bounded batch per call so no single request runs
        // away on a large bin; keep going until it reports nothing left.
        for (;;) {
          if (cacheScope.get() !== scope) throw new TrashEmptyScopeChangedError();
          const batch = await trashService.emptyTrash(filter, workspaceId);
          blocked = Math.max(blocked, batch.blocked);
          if (!batch.hasMore) break;
        }
        // The view lives in the scope the sweep ran in; do not touch another's.
        // Blocked roots are still in the bin: leave the rows to the refresh that
        // follows instead of collapsing the view to empty.
        if (cacheScope.get() !== scope || blocked > 0) return;
        // Its rows are gone from every other loaded view too (the same root can
        // sit under "All", its type and its project).
        this.#dropRows(ids);
        // The view is swept server-side across every page, so it collapses to an
        // empty head page rather than dropping only loaded rows.
        this.#trashList.update(key, (data) =>
          data
            ? {
                ...data,
                currentPage: 0,
                hasMore: false,
                items: [],
                nextCursor: null,
                pages: [{ count: 0, next: null }],
                total: 0,
              }
            : data,
        );
      },
      { sweep: true },
    );
    return { blocked };
  };

  /**
   * Fetch orchestration for one view's recycle-bin page. Hydrates the
   * persisted projection, then revalidates; rows land in `trashListMap` — read
   * them through `trashSelectors`, never from this hook.
   */
  useFetchTrash = (enabled: boolean, filter: TrashViewFilter): ReplicaSyncResult =>
    this.#trashList.useSync(enabled ? filter : null);

  /** Fetch orchestration for one side and project filter's per-type counts; read them via `trashSelectors`. */
  useFetchTrashCount = (enabled: boolean, filter: TrashCountFilter): ReplicaSyncResult =>
    this.#trashCount.useSync(enabled ? filter : null);
}

export type TrashAction = Pick<TrashActionImpl, keyof TrashActionImpl>;
export type { TrashItem };