refactor: use list instead of string parsing

This commit is contained in:
Ankit Matth
2025-08-23 20:25:29 +05:30
parent bd73fa9ae7
commit 07d59b6640
6 changed files with 68 additions and 61 deletions

View File

@@ -17,7 +17,7 @@ type SourcesPopupProps = {
isOpen: boolean;
onClose: () => void;
anchorRef: React.RefObject<HTMLButtonElement | null>;
handlePostDocumentSelect: (doc: Doc | null) => void;
handlePostDocumentSelect: (doc: Doc[] | null) => void;
setUploadModalState: React.Dispatch<React.SetStateAction<ActiveState>>;
};
@@ -149,9 +149,12 @@ export default function SourcesPopup({
if (option.model === embeddingsName) {
const isSelected =
selectedDocs &&
Array.isArray(selectedDocs) && selectedDocs.length > 0 &&
selectedDocs.some(doc =>
option.id ? doc.id === option.id : doc.date === option.date
Array.isArray(selectedDocs) &&
selectedDocs.length > 0 &&
selectedDocs.some((doc) =>
option.id
? doc.id === option.id
: doc.date === option.date,
);
return (
@@ -160,17 +163,27 @@ export default function SourcesPopup({
className="border-opacity-80 dark:border-dim-gray flex cursor-pointer items-center border-b border-[#D9D9D9] p-3 transition-colors hover:bg-gray-100 dark:text-[14px] dark:hover:bg-[#2C2E3C]"
onClick={() => {
if (isSelected) {
const updatedDocs = (selectedDocs && Array.isArray(selectedDocs))
? selectedDocs.filter(doc =>
option.id ? doc.id !== option.id : doc.date !== option.date
)
: [];
dispatch(setSelectedDocs(updatedDocs.length > 0 ? updatedDocs : null));
handlePostDocumentSelect(updatedDocs.length > 0 ? updatedDocs : null);
const updatedDocs =
selectedDocs && Array.isArray(selectedDocs)
? selectedDocs.filter((doc) =>
option.id
? doc.id !== option.id
: doc.date !== option.date,
)
: [];
dispatch(
setSelectedDocs(
updatedDocs.length > 0 ? updatedDocs : null,
),
);
handlePostDocumentSelect(
updatedDocs.length > 0 ? updatedDocs : null,
);
} else {
const updatedDocs = (selectedDocs && Array.isArray(selectedDocs))
? [...selectedDocs, option]
: [option];
const updatedDocs =
selectedDocs && Array.isArray(selectedDocs)
? [...selectedDocs, option]
: [option];
dispatch(setSelectedDocs(updatedDocs));
handlePostDocumentSelect(updatedDocs);
}