Skip to content

The open alternative to Salesforce, designed for AI.

3.5 KB
Raw Blame
import { useStore } from 'jotai';
import { getAgentChatMentionsResolvedAt } from 'twenty-shared/ai';
import { isDefined } from 'twenty-shared/utils';

import { AGENT_CHAT_THREAD_PARTICIPANT_UNSAVED_UPDATED_AT } from '@/ai/constants/AgentChatThreadParticipantUnsavedUpdatedAt';
import { useApplyAgentChatThreadUpdate } from '@/ai/hooks/useApplyAgentChatThreadUpdate';
import { agentChatThreadParticipantsState } from '@/ai/states/agentChatThreadParticipantsState';
import { agentChatThreadVisitState } from '@/ai/states/agentChatThreadVisitState';
import { agentChatThreadRecordFamilySelector } from '@/ai/states/selectors/agentChatThreadRecordFamilySelector';

// Mirrors what the server records when the member sends a message: the
// thread leaves the trash, moves to the top of their inbox and is caught up,
// so its unread line goes
export const useOptimisticallyRestoreOnSend = () => {
  const { applyAgentChatThreadUpdate } = useApplyAgentChatThreadUpdate();
  const store = useStore();

  const applyOptimisticRestore = ({
    threadId,
    optimisticUpdatedAt,
  }: {
    threadId: string;
    optimisticUpdatedAt: string;
  }): (() => void) => {
    const thread = store.get(
      agentChatThreadRecordFamilySelector.selectorFamily(threadId),
    );
    const previousParticipant = store.get(
      agentChatThreadParticipantsState.atom,
    )?.[threadId];
    const previousVisit = store.get(agentChatThreadVisitState.atom);

    applyAgentChatThreadUpdate({
      id: threadId,
      lastActivityAt: optimisticUpdatedAt,
      ...(isDefined(thread?.deletedAt) && {
        deletedAt: null,
        updatedAt: optimisticUpdatedAt,
      }),
    });
    store.set(agentChatThreadParticipantsState.atom, (participants) =>
      isDefined(participants)
        ? {
            ...participants,
            [threadId]: {
              ...participants[threadId],
              threadId,
              lastReadAt: optimisticUpdatedAt,
              mentionsResolvedAt: getAgentChatMentionsResolvedAt(
                previousParticipant ?? {},
              ),
              archivedAt: null,
              snoozedUntil: null,
              isSubscribed: true,
              updatedAt:
                previousParticipant?.updatedAt ??
                AGENT_CHAT_THREAD_PARTICIPANT_UNSAVED_UPDATED_AT,
            },
          }
        : participants,
    );
    store.set(agentChatThreadVisitState.atom, (visit) =>
      visit?.threadId === threadId
        ? { ...visit, isUnread: false, isKeptUnread: false }
        : visit,
    );
    const optimisticParticipant = store.get(
      agentChatThreadParticipantsState.atom,
    )?.[threadId];

    return () => {
      applyAgentChatThreadUpdate({
        id: threadId,
        ...(isDefined(thread) && {
          lastActivityAt: thread.lastActivityAt,
          updatedAt: thread.updatedAt,
        }),
        ...(isDefined(thread?.deletedAt) && { deletedAt: thread.deletedAt }),
      });
      store.set(agentChatThreadParticipantsState.atom, (participants) => {
        if (
          !isDefined(participants) ||
          participants[threadId] !== optimisticParticipant
        ) {
          return participants;
        }

        const { [threadId]: _sentParticipant, ...otherParticipants } =
          participants;

        return isDefined(previousParticipant)
          ? { ...otherParticipants, [threadId]: previousParticipant }
          : otherParticipants;
      });
      store.set(agentChatThreadVisitState.atom, (visit) =>
        visit?.threadId === threadId ? previousVisit : visit,
      );
    };
  };

  return { applyOptimisticRestore };
};