LibreChat/client/src/hooks/SSE/useAttachmentHandler.ts
José Pedro Silva 18d5a75cdc
🌐 feat: Add support to SubDirectory hosting (#9155)
* feat: Add support to SubDirectory hosting

* fix: address linting and failing test

* fix: browser context validation

---------

Co-authored-by: Danny Avila <danny@librechat.ai>
2025-08-27 02:00:18 -04:00

41 lines
1.5 KiB
TypeScript

import { useSetRecoilState } from 'recoil';
import type { QueryClient } from '@tanstack/react-query';
import { QueryKeys, Tools } from 'librechat-data-provider';
import type { TAttachment, EventSubmission, MemoriesResponse } from 'librechat-data-provider';
import { handleMemoryArtifact } from '~/utils/memory';
import store from '~/store';
export default function useAttachmentHandler(queryClient?: QueryClient) {
const setAttachmentsMap = useSetRecoilState(store.messageAttachmentsMap);
return ({ data }: { data: TAttachment; submission: EventSubmission }) => {
const { messageId } = data;
if (queryClient && data?.filepath && !data.filepath.includes('/api/files')) {
queryClient.setQueryData([QueryKeys.files], (oldData: TAttachment[] | undefined) => {
return [data, ...(oldData || [])];
});
}
if (queryClient && data.type === Tools.memory && data[Tools.memory]) {
const memoryArtifact = data[Tools.memory];
queryClient.setQueryData([QueryKeys.memories], (oldData: MemoriesResponse | undefined) => {
if (!oldData) {
return oldData;
}
return handleMemoryArtifact({ memoryArtifact, currentData: oldData }) || oldData;
});
}
setAttachmentsMap((prevMap) => {
const messageAttachments =
(prevMap as Record<string, TAttachment[] | undefined>)[messageId] || [];
return {
...prevMap,
[messageId]: [...messageAttachments, data],
};
});
};
}