Skip to content

TagSpaces is an offline, open source, document manager with tagging support

Adding files to AI chat broken (#2262)

Stanimir7a8f0f9b23d3
diff --git a/src/renderer/components/CustomDragLayer.tsx b/src/renderer/components/CustomDragLayer.tsx
index 77ac96452..eaaaa97aa 100644
--- a/src/renderer/components/CustomDragLayer.tsx
+++ b/src/renderer/components/CustomDragLayer.tsx
@@ -57,7 +57,8 @@ const CustomDragLayer: React.FC<CustomDragLayerProps> = (props) => {
switch (itemType) {
case DragItemTypes.FILE:
return (
- <FilesDragPreview entries={item.selectedEntries} path={item.path} />
+ <FilesDragPreview /*entries={item.selectedEntries} path={item.path}*/
+ />
);
case DragItemTypes.TAG:
return <TagDragPreview tag={item.tag} />;
diff --git a/src/renderer/components/FileSourceDnd.tsx b/src/renderer/components/FileSourceDnd.tsx
index 9767d5297..9d9fdc0a0 100644
--- a/src/renderer/components/FileSourceDnd.tsx
+++ b/src/renderer/components/FileSourceDnd.tsx
@@ -26,21 +26,21 @@ interface Props {
children: React.ReactNode;
}
interface ChildProps {
- entryPath: string;
- selectedEntries: TS.FileSystemEntry[];
+ entry: TS.FileSystemEntry;
+ //selectedEntries: TS.FileSystemEntry[];
}
const FileSourceDnd: React.FC<Props> = ({ children }) => {
const childProps = children as React.ReactElement<ChildProps>;
- const entryPath = childProps.props.entryPath;
- const selectedEntries = childProps.props.selectedEntries || [];
- let entries = !selectedEntries.some((entry) => entry.path === entryPath)
+ const entry = childProps.props.entry;
+ //const selectedEntries = childProps.props.selectedEntries || [];
+ /*let entries = !selectedEntries.some((entry) => entry.path === entryPath)
? [{ path: entryPath }]
- : selectedEntries;
+ : selectedEntries;*/
const [collected, drag, preview] = useDrag({
type: DragItemTypes.FILE,
- item: { path: entryPath, selectedEntries: entries },
+ item: { entry: entry },
collect: (monitor) => ({
isDragging: monitor.isDragging(),
}),
diff --git a/src/renderer/components/FilesDragPreview.tsx b/src/renderer/components/FilesDragPreview.tsx
index 72048002c..09e457c84 100644
--- a/src/renderer/components/FilesDragPreview.tsx
+++ b/src/renderer/components/FilesDragPreview.tsx
@@ -2,24 +2,29 @@ import React from 'react';
import Avatar from '@mui/material/Avatar';
import Chip from '@mui/material/Chip';
import { useTranslation } from 'react-i18next';
+import { useSelectedEntriesContext } from '-/hooks/useSelectedEntriesContext';
export interface Props {
- entries: Array<any>;
- path: string;
+ // entriesLength: number;
+ //entries: Array<any>;
+ //path: string;
}
export function FilesDragPreview(props: Props) {
- const { entries, path } = props;
+ //const { entriesLength } = props;
+ const { selectedEntries } = useSelectedEntriesContext();
const { t } = useTranslation();
+ const entriesLength = selectedEntries.length > 0 ? selectedEntries.length : 1;
return (
<Chip
size="small"
- avatar={<Avatar>{entries.length}</Avatar>}
+ avatar={<Avatar>{entriesLength}</Avatar>}
style={{ marginLeft: 15 }}
label={t('core:selectedFiles')}
color="primary"
/>
);
}
-const areEqual = (prevProp, nextProp) => nextProp.path === prevProp.path;
-export default React.memo(FilesDragPreview, areEqual);
+//const areEqual = (prevProp, nextProp) => nextProp.entriesLength === prevProp.entriesLength;
+//export default React.memo(FilesDragPreview, areEqual);
+export default React.memo(FilesDragPreview);
diff --git a/src/renderer/components/LocationView.tsx b/src/renderer/components/LocationView.tsx
index 69ac47db7..85dd1a6fd 100644
--- a/src/renderer/components/LocationView.tsx
+++ b/src/renderer/components/LocationView.tsx
@@ -75,7 +75,7 @@ function LocationView(props: Props) {
const { handleEntryExist, openEntryExistDialog } =
useEntryExistDialogContext();
const { setActions } = usePerspectiveActionsContext();
- const { setSelectedEntries } = useSelectedEntriesContext();
+ const { setSelectedEntries, selectedEntries } = useSelectedEntriesContext();
const { currentLocationPath, openDirectory } = useDirectoryContentContext();
const { showNotification } = useNotificationContext();
const directoryTreeRef = useRef<DirectoryTreeViewRef>(null);
@@ -128,16 +128,18 @@ function LocationView(props: Props) {
*/
const handleFileMoveDrop = (item, monitor) => {
if (item) {
- const { path, selectedEntries } = item; //monitor.getItem();
- const arrPath = [];
- if (selectedEntries && selectedEntries.length > 0) {
- selectedEntries.map((entry) => {
- arrPath.push(entry.path);
- return true;
- });
- } else {
- arrPath.push(path);
+ const { entry } = item;
+ let arrPath = [];
+ if (
+ selectedEntries &&
+ selectedEntries.length > 0 &&
+ selectedEntries.some((e) => e.path === entry.path)
+ ) {
+ arrPath = selectedEntries.map((i) => i.path);
+ } else if (entry) {
+ arrPath.push(entry.path);
}
+
if (readOnlyMode) {
showNotification(t('core:dndDisabledReadOnlyMode'), 'error', true);
return;
@@ -150,7 +152,7 @@ function LocationView(props: Props) {
if (targetPath !== undefined && targetLocation !== undefined) {
// TODO handle monitor -> isOver and change folder icon
- console.log('Dropped files: ' + path);
+ console.log('Dropped files: ' + JSON.stringify(arrPath));
if (targetLocation.type !== currentLocation.type) {
//locationType.TYPE_CLOUD) {
dispatch(AppActions.resetProgress());
diff --git a/src/renderer/components/chat/ChatDndTargetFile.tsx b/src/renderer/components/chat/ChatDndTargetFile.tsx
index 52106fa8c..ff2f00ae5 100644
--- a/src/renderer/components/chat/ChatDndTargetFile.tsx
+++ b/src/renderer/components/chat/ChatDndTargetFile.tsx
@@ -23,11 +23,11 @@ import { alpha, useTheme } from '@mui/material/styles';
import { Identifier } from 'dnd-core';
import React, { ReactNode } from 'react';
import { DropTargetMonitor, useDrop } from 'react-dnd';
+import { useSelectedEntriesContext } from '-/hooks/useSelectedEntriesContext';
type DragItem = {
files?: File[];
- path?: string;
- selectedEntries?: TS.FileSystemEntry[];
+ entry?: TS.FileSystemEntry;
items: DataTransferItemList;
};
type DragProps = {
@@ -44,30 +44,33 @@ interface Props {
function ChatDndTargetFile(props: Props) {
const theme = useTheme();
const { children, accepts, style } = props;
- //const { findLocalLocation } = useCurrentLocationContext();
+ const { selectedEntries } = useSelectedEntriesContext();
const { setImages } = useChatContext();
function isSupportedImageFormat(filePath: string) {
- const aiSupportedImages = [
- 'jpeg',
- 'jpg',
- 'png',
- 'gif',
- 'bmp',
- 'tiff',
- // 'avif', // currently not supported
- // 'webp', // currently not supported
- ];
- return aiSupportedImages.some((ext) =>
- filePath.toLowerCase().endsWith(ext),
- );
+ if (filePath) {
+ const aiSupportedImages = [
+ 'jpeg',
+ 'jpg',
+ 'png',
+ 'gif',
+ 'bmp',
+ 'tiff',
+ // 'avif', // currently not supported
+ // 'webp', // currently not supported
+ ];
+ return aiSupportedImages.some((ext) =>
+ filePath.toLowerCase().endsWith(ext),
+ );
+ }
+ return false;
}
const [collectedProps, drop] = useDrop<DragItem, unknown, DragProps>(
() => ({
accept: accepts,
drop: (item, m) => {
- const { files, path, selectedEntries } = item;
+ const { files, entry } = item;
const didDrop = m.didDrop();
if (didDrop || !AppConfig.isElectron) {
return;
@@ -81,19 +84,21 @@ function ChatDndTargetFile(props: Props) {
return file.path;
});
setImages(filePaths);
- } else {
- const arrPath = [];
- if (selectedEntries && selectedEntries.length > 0) {
- selectedEntries.map((entry) => {
- if (isSupportedImageFormat(entry.path)) {
- arrPath.push(entry.path);
- }
- return true;
- });
- } else if (isSupportedImageFormat(path)) {
- arrPath.push(path);
+ } else if (entry) {
+ if (
+ selectedEntries &&
+ selectedEntries.length > 0 &&
+ selectedEntries.some((e) => e.path === entry.path)
+ ) {
+ const images = selectedEntries.filter((entry) =>
+ isSupportedImageFormat(entry.path),
+ );
+ if (images.length > 0) {
+ setImages(images.map((i) => i.path));
+ }
+ } else if (entry.isFile && isSupportedImageFormat(entry.path)) {
+ setImages([entry.path]);
}
- setImages(arrPath);
}
},
collect: (m: DropTargetMonitor) => ({
@@ -102,7 +107,7 @@ function ChatDndTargetFile(props: Props) {
isActive: m.isOver({ shallow: true }) && m.canDrop(),
}),
}),
- [setImages],
+ [setImages, selectedEntries],
);
const { isActive, handlerId } = collectedProps;
diff --git a/src/renderer/perspectives/common/CellView.tsx b/src/renderer/perspectives/common/CellView.tsx
index 5724f6dc6..4bf397191 100644
--- a/src/renderer/perspectives/common/CellView.tsx
+++ b/src/renderer/perspectives/common/CellView.tsx
@@ -273,23 +273,27 @@ function CellView(props: Props) {
return;
}
if (item) {
- // const mItem = monitor.getItem();
+ const { entry } = item;
let arrPath;
- if (item.selectedEntries && item.selectedEntries.length > 0) {
- const arrSelected = item.selectedEntries
+ if (
+ selectedEntries &&
+ selectedEntries.length > 0 &&
+ selectedEntries.some((e) => e.path === entry.path)
+ ) {
+ const arrSelected = selectedEntries
.map((entry) => entry.path)
// remove target folder selection
.filter((epath) => epath !== item.targetPath);
if (arrSelected.length > 0) {
arrPath = arrSelected;
} else {
- arrPath = [item.path];
+ arrPath = [entry.path];
}
- } else {
- arrPath = [item.path];
+ } else if (entry) {
+ arrPath = [entry.path];
}
- console.log('Dropped files: ' + item.path);
- handleEntryExist(item.selectedEntries, item.targetPath).then((exist) => {
+ console.log('Dropped files: ' + JSON.stringify(arrPath));
+ handleEntryExist(selectedEntries, item.targetPath).then((exist) => {
if (exist) {
openEntryExistDialog(exist, () => {
moveFiles(arrPath, item.targetPath, currentLocationId);
diff --git a/src/renderer/perspectives/grid/components/MainContainer.tsx b/src/renderer/perspectives/grid/components/MainContainer.tsx
index 49ba09222..08e488a46 100644
--- a/src/renderer/perspectives/grid/components/MainContainer.tsx
+++ b/src/renderer/perspectives/grid/components/MainContainer.tsx
@@ -319,7 +319,12 @@ function GridPerspective(props: Props) {
const selectionMode = selectedEntries.length > 1;
return (
- <TagDropContainer entry={fsEntry}>
+ <TagDropContainer
+ entry={fsEntry}
+ /*selectedEntries={
+ selectedEntries.length > 0 ? selectedEntries : [fsEntry]
+ }*/
+ >
<GridCell
selected={selected}
fsEntry={fsEntry}