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.

3.5 KB
Raw Blame
import type { ChannelDetail, ChannelJoinableItem, ChannelListItem } from '@lobechat/types';

import {
  arrayEntity,
  cacheScope,
  createReplicaSlice,
  linkReplicaEntity,
  recordLens,
} from '@/libs/replica';
import type { StoreSetter } from '@/store/types';
import { isTrpcErrorCode } from '@/utils/trpcError';

import { settleOutbox } from './outbox';
import {
  channelDetailEntity,
  channelDetailResource,
  channelJoinableResource,
  channelListResource,
  channelThreadEntityId,
} from './projection';
import type { ChannelState } from './store';

export class ChannelPrivateAction {
  // Scope → thread ID → entity ID. A pre-delete head response may arrive after
  // persistence has settled; unlike hydration, it would lift replica entity guards.
  // Only a recycle-bin restore, which keeps the thread ID, lifts the marker.
  private readonly deletedThreads = new Map<string, Map<string, string>>();
  resetThreadRemovals = () => this.deletedThreads.clear();
  forgetThreadRemovals = (threadIds: Iterable<string>) => {
    for (const threadId of threadIds)
      for (const ids of this.deletedThreads.values()) ids.delete(threadId);
  };
  removeThread = (channelId: string, threadId: string, scope: string) => {
    const id = channelThreadEntityId(channelId, threadId);
    const ids = this.deletedThreads.get(scope) ?? new Map<string, string>();
    ids.set(threadId, id);
    this.deletedThreads.set(scope, ids);
    this.detail.removeEntity(id, { scope });
  };
  readonly detail;
  readonly entity;
  readonly joinable;
  readonly list;
  constructor(set: StoreSetter<ChannelState>, get: () => ChannelState) {
    this.list = createReplicaSlice(channelListResource, {
      actionPrefix: 'channel/list',
      entity: arrayEntity<ChannelListItem>((channel) => channel.id),
      get,
      set,
      stateKey: 'listReplica',
      view: recordLens<ChannelState, ChannelListItem[]>('listMap'),
    });
    this.joinable = createReplicaSlice(channelJoinableResource, {
      actionPrefix: 'channel/joinable',
      get,
      set,
      stateKey: 'joinableReplica',
      view: recordLens<ChannelState, ChannelJoinableItem[]>('joinableMap'),
    });
    this.detail = createReplicaSlice(channelDetailResource, {
      actionPrefix: 'channel/detail',
      effects: {
        onApplied: (data, params, { scope }) => {
          const { outbox } = get();
          const next = settleOutbox(outbox, params, data, scope);
          if (next !== outbox) set({ outbox: next }, false, 'channel/outbox/settle');
        },
        onError: (error, params, { scope }) => {
          if (params.threadId && isTrpcErrorCode(error, 'NOT_FOUND')) {
            this.removeThread(params.channelId, params.threadId, scope);
            return;
          }
          if (
            isTrpcErrorCode(error, 'NOT_FOUND') ||
            isTrpcErrorCode(error, 'FORBIDDEN') ||
            isTrpcErrorCode(error, 'UNAUTHORIZED')
          )
            this.entity.remove(params.channelId, { scope });
        },
      },
      entity: channelDetailEntity,
      prepareHead: (incoming) => {
        let data: ChannelDetail | undefined = incoming;
        for (const id of this.deletedThreads.get(cacheScope.get())?.values() ?? []) {
          if (!data) break;
          data = channelDetailEntity.map(data, id, () => undefined);
        }
        return data;
      },
      get,
      set,
      stateKey: 'detailReplica',
      view: recordLens<ChannelState, ChannelDetail>('detailMap'),
    });
    this.entity = linkReplicaEntity<ChannelListItem>([this.list, this.detail]);
  }
}