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.0 KB
Raw Blame
import { AGENT_CHAT_TOPIC_URL, GROUP_CHAT_TOPIC_URL } from '@lobechat/const';
import { toast } from '@lobehub/ui';
import { useTranslation } from 'react-i18next';

import { openRenameModal } from '@/components/RenameModal';
import { confirmRemoveTopic } from '@/features/DeleteTopicConfirm';
import { type PortalMoreMenuConfig } from '@/features/Portal/components/PortalMoreMenu/types';
import { usePermission } from '@/hooks/usePermission';
import { useChatStore } from '@/store/chat';
import { chatPortalSelectors, topicSelectors } from '@/store/chat/selectors';
import { isForbiddenError } from '@/utils/forbiddenError';

import { usePortalShareUrl } from '../components/PortalMoreMenu/shareUrl';

export const useTopicMoreMenu = (): PortalMoreMenuConfig | undefined => {
  const { t } = useTranslation(['topic', 'common']);
  const { allowed: canEdit } = usePermission('edit_own_content');
  // The side-by-side topic belongs to the conversation of the main column: an
  // agent, or a group — where `activeAgentId` is only the supervisor.
  const [
    agentId,
    groupId,
    topicId,
    title,
    updateTopicTitle,
    removeTopic,
    refreshMessages,
    closeTopicPortal,
  ] = useChatStore((s) => {
    const id = chatPortalSelectors.portalTopicId(s);
    return [
      s.activeAgentId,
      s.activeGroupId,
      id,
      id ? topicSelectors.getTopicById(id)(s)?.title : undefined,
      s.updateTopicTitle,
      s.removeTopic,
      s.refreshMessages,
      s.closeTopicPortal,
    ] as const;
  });
  const shareUrl = usePortalShareUrl(
    !topicId
      ? undefined
      : groupId
        ? GROUP_CHAT_TOPIC_URL(groupId, topicId)
        : agentId
          ? AGENT_CHAT_TOPIC_URL(agentId, topicId)
          : undefined,
  );

  if (!topicId) return;

  const toastWriteError = (error: unknown) =>
    toast.error(
      isForbiddenError(error)
        ? t('manageOnlyCreator', { ns: 'common' })
        : t('operationFailed', { ns: 'common' }),
    );

  return {
    copyId: topicId,
    copyLink: shareUrl,
    delete: canEdit
      ? () =>
          void confirmRemoveTopic({
            onConfirm: async (removeFiles) => {
              try {
                await removeTopic(topicId, removeFiles);
                // The store never closes the pane showing the removed topic.
                closeTopicPortal();
              } catch (error) {
                toastWriteError(error);
              }
            },
            topicIds: [topicId],
          })
      : undefined,
    refresh: () => refreshMessages({ agentId, topicId }),
    rename: canEdit
      ? () =>
          openRenameModal({
            defaultValue: title ?? '',
            description: t('renameModal.description'),
            onSave: async (next) => {
              try {
                await updateTopicTitle(topicId, next);
              } catch (error) {
                toastWriteError(error);
              }
            },
            title: t('renameModal.title'),
          })
      : undefined,
  };
};