quivr/frontend/lib/components/UploadDocumentModal/hooks/useFeedBrainHandler.ts
Antoine Dewez 18d594493a
fix(frontend): send empty sync is not allowed (#2716)
…d files

The syncFiles function in multiple files has been updated to only sync
connections that have selected files. This change improves the
efficiency of the syncing process and ensures that only relevant
connections are synced.

# Description

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context.

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):
2024-06-24 18:30:42 +02:00

78 lines
2.4 KiB
TypeScript

import { UUID } from "crypto";
import { useFromConnectionsContext } from "@/app/chat/[chatId]/components/ActionsBar/components/KnowledgeToFeed/components/FromConnections/FromConnectionsProvider/hooks/useFromConnectionContext";
import { useSync } from "@/lib/api/sync/useSync";
import { useKnowledgeToFeedInput } from "@/lib/components/KnowledgeToFeedInput/hooks/useKnowledgeToFeedInput.ts";
import { useKnowledgeToFeedFilesAndUrls } from "@/lib/hooks/useKnowledgeToFeed";
import { useOnboarding } from "@/lib/hooks/useOnboarding";
type FeedBrainProps = {
brainId: UUID;
chatId: UUID;
};
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const useFeedBrainHandler = () => {
const { files, urls } = useKnowledgeToFeedFilesAndUrls();
const { crawlWebsiteHandler, uploadFileHandler } = useKnowledgeToFeedInput();
const { updateOnboarding, onboarding } = useOnboarding();
const {
syncFiles,
getActiveSyncsForBrain,
deleteActiveSync,
updateActiveSync,
} = useSync();
const { openedConnections } = useFromConnectionsContext();
const updateOnboardingA = async () => {
if (onboarding.onboarding_a) {
await updateOnboarding({
onboarding_a: false,
});
}
};
const handleFeedBrain = async ({
brainId,
chatId,
}: FeedBrainProps): Promise<void> => {
const uploadPromises = files.map((file) =>
uploadFileHandler(file, brainId, chatId)
);
const crawlPromises = urls.map((url) =>
crawlWebsiteHandler(url, brainId, chatId)
);
const existingConnections = await getActiveSyncsForBrain(brainId);
await Promise.all(
openedConnections
.filter((connection) => connection.selectedFiles.files.length)
.map(async (openedConnection) => {
const existingConnectionIds = existingConnections.map(
(connection) => connection.id
);
if (
!openedConnection.id ||
!existingConnectionIds.includes(openedConnection.id)
) {
await syncFiles(openedConnection, brainId);
} else if (!openedConnection.selectedFiles.files.length) {
await deleteActiveSync(openedConnection.id);
} else {
await updateActiveSync(openedConnection);
}
})
);
await Promise.all([
...uploadPromises,
...crawlPromises,
updateOnboardingA(),
]);
};
return {
handleFeedBrain,
};
};