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.

1.4 KB
Raw Blame
import { toast } from '@lobehub/ui';
import { t } from 'i18next';
import { useState } from 'react';

import { useChatStore } from '@/store/chat';
import { useProjectDirectoryStore } from '@/store/projectWorkingDirectory';

import type { BindDirectoryOptions } from './BindDirectoryModal';

export function useBindDirectory(options: BindDirectoryOptions, onSaved: () => void) {
  const bind = useProjectDirectoryStore((s) => s.bind);
  const [pending, setPending] = useState(false);
  const [error, setError] = useState<unknown>();
  const save = async (projectId: string, environmentId: string) => {
    setPending(true);
    setError(undefined);
    try {
      const directory = await bind({
        agentId: options.agentId,
        deviceId: options.deviceId,
        environmentId,
        name: options.path.split(/[\\/]/).findLast(Boolean) || options.path,
        path: options.path,
        projectId,
        topicIds: options.topicIds,
      });
      await useChatStore.getState().refreshTopic();
      onSaved();
      // The directory is linked even when some topics stay where they are.
      if (directory.skippedTopics.length)
        toast.warning(
          t('directories.topicsSkipped', { count: directory.skippedTopics.length, ns: 'project' }),
        );
    } catch (error) {
      console.error('Failed to bind project directory', error);
      setError(error);
    } finally {
      setPending(false);
    }
  };
  return { error, pending, save };
}