Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(ai): dnd to chat (#1125)

Eric Hayesc61363dda238
diff --git a/js/app/packages/app/component/rightbar/Rightbar.tsx b/js/app/packages/app/component/rightbar/Rightbar.tsx
index 9075e56c70..89133b0e67 100644
--- a/js/app/packages/app/component/rightbar/Rightbar.tsx
+++ b/js/app/packages/app/component/rightbar/Rightbar.tsx
@@ -5,6 +5,7 @@ import { DragDropWrapper } from '@core/component/AI/component/DragDrop';
import { useBuildChatSendRequest } from '@core/component/AI/component/input/buildRequest';
import { useChatInput } from '@core/component/AI/component/input/useChatInput';
import { ChatMessages } from '@core/component/AI/component/message/ChatMessages';
+import { useEntityDropAttachment } from '@core/component/AI/hook/useEntityDropAttachment';
import { getPendingSend } from '@core/component/AI/signal/pendingSend';
import { registerToolHandler } from '@core/component/AI/signal/tool';
import type {
@@ -309,6 +310,13 @@ export function Rightbar(props: {
uploadQueue,
} = useChatInput({ initialValue: props.initialState?.text });
+ // Entity drag-and-drop support
+ const { droppable, isDraggingOver } = useEntityDropAttachment(
+ 'rightbar-chat-input',
+ attachments
+ );
+ false && droppable;
+
createEffect(() => {
setChatId(props.chatId);
if (!props.initialState) return;
@@ -369,8 +377,10 @@ export function Rightbar(props: {
<DragDropWrapper
class="relative flex flex-col size-full select-none"
uploadQueue={uploadQueue}
+ isEntityDraggingOver={isDraggingOver}
>
- <div class="overflow-hidden size-full flex flex-col items-center">
+ <div class="overflow-hidden size-full flex flex-col items-center relative">
+ <div class="absolute inset-0 pointer-events-none" use:droppable />
<TopBar
chatId={props.chatId}
setChatId={props.setState.setChatId}
diff --git a/js/app/packages/block-chat/component/Chat.tsx b/js/app/packages/block-chat/component/Chat.tsx
index fcdfc783bb..fa8aa8d583 100644
--- a/js/app/packages/block-chat/component/Chat.tsx
+++ b/js/app/packages/block-chat/component/Chat.tsx
@@ -6,6 +6,7 @@ import { DragDropWrapper } from '@core/component/AI/component/DragDrop';
import { useBuildChatSendRequest } from '@core/component/AI/component/input/buildRequest';
import { useChatInput } from '@core/component/AI/component/input/useChatInput';
import { useChatMessages } from '@core/component/AI/component/message';
+import { useEntityDropAttachment } from '@core/component/AI/hook/useEntityDropAttachment';
import { getPendingSend } from '@core/component/AI/signal/pendingSend';
import { registerToolHandler } from '@core/component/AI/signal/tool';
import type {
@@ -18,6 +19,7 @@ import {
type StoredStuff,
storeChatState,
} from '@core/component/AI/util/storage';
+import { useBlockId } from '@core/block';
import { CustomScrollbar } from '@core/component/CustomScrollbar';
import { usePaywallState } from '@core/constant/PaywallState';
import { TOKENS } from '@core/hotkey/tokens';
@@ -89,6 +91,14 @@ export function Chat(props: { data: ChatData }) {
setModel(loadedState.model);
}
+ // Entity drag-and-drop support
+ const chatId = useBlockId();
+ const { droppable, isDraggingOver } = useEntityDropAttachment(
+ 'chat-input-' + chatId,
+ attachments
+ );
+ false && droppable;
+
registerToolHandler(stream);
const { showPaywall } = usePaywallState();
@@ -192,9 +202,11 @@ export function Chat(props: { data: ChatData }) {
<DragDropWrapper
class="size-full bg-panel overscroll-none overflow-hidden flex flex-col"
uploadQueue={uploadQueue}
+ isEntityDraggingOver={isDraggingOver}
>
<TopBar />
<div class="size-full flex-1 min-h-0 p-2 relative">
+ <div class="absolute inset-0 pointer-events-none" use:droppable />
<div
data-chat-scroll
class="h-full min-h-0 overflow-auto scrollbar-hidden"
diff --git a/js/app/packages/core/component/AI/component/DragDrop.tsx b/js/app/packages/core/component/AI/component/DragDrop.tsx
index e18799f2e0..b766435c1f 100644
--- a/js/app/packages/core/component/AI/component/DragDrop.tsx
+++ b/js/app/packages/core/component/AI/component/DragDrop.tsx
@@ -6,7 +6,7 @@ const { track, TrackingEvents } = withAnalytics();
import { SUPPORTED_ATTACHMENT_EXTENSIONS } from '@core/component/AI/constant';
import type { UploadQueue } from '@core/component/AI/types';
-import type { Component, ParentProps } from 'solid-js';
+import type { Accessor, Component, ParentProps } from 'solid-js';
import { createSignal, Show } from 'solid-js';
false && fileDrop; // Reference for SolidJS directive
@@ -15,6 +15,8 @@ type DragDropWrapperProps = ParentProps<{
uploadQueue: UploadQueue;
class?: string;
overlayMessage?: string;
+ /** Signal indicating if an entity is being dragged over (from useEntityDropAttachment) */
+ isEntityDraggingOver?: Accessor<boolean>;
}>;
/**
@@ -22,7 +24,9 @@ type DragDropWrapperProps = ParentProps<{
* to its children. Shows a visual overlay when files are dragged over the area.
*/
export const DragDropWrapper: Component<DragDropWrapperProps> = (props) => {
- const [isDragging, setIsDragging] = createSignal(false);
+ const [isFileDragging, setIsFileDragging] = createSignal(false);
+
+ const showOverlay = () => isFileDragging() || props.isEntityDraggingOver?.();
return (
<div
@@ -30,8 +34,8 @@ export const DragDropWrapper: Component<DragDropWrapperProps> = (props) => {
use:fileDrop={{
acceptedFileExtensions: SUPPORTED_ATTACHMENT_EXTENSIONS,
multiple: true,
- onDragStart: () => setIsDragging(true),
- onDragEnd: () => setIsDragging(false),
+ onDragStart: () => setIsFileDragging(true),
+ onDragEnd: () => setIsFileDragging(false),
onDrop: (files) => {
track(TrackingEvents.CHAT.ATTACHMENT.DROP);
props.uploadQueue.upload(files);
@@ -40,7 +44,7 @@ export const DragDropWrapper: Component<DragDropWrapperProps> = (props) => {
>
{props.children}
- <Show when={isDragging()}>
+ <Show when={showOverlay()}>
<FileDropOverlay>
{props.overlayMessage || 'Drop files to attach to your message'}
</FileDropOverlay>
diff --git a/js/app/packages/core/component/AI/hook/useEntityDropAttachment.ts b/js/app/packages/core/component/AI/hook/useEntityDropAttachment.ts
new file mode 100644
index 0000000000..f00458f07f
--- /dev/null
+++ b/js/app/packages/core/component/AI/hook/useEntityDropAttachment.ts
@@ -0,0 +1,136 @@
+import { match } from 'ts-pattern';
+import { SUPPORTED_CHAT_ATTACHMENT_BLOCKS } from '@core/component/AI/constant';
+import type { Attachment, Attachments } from '@core/component/AI/types';
+import { asFileType } from '@core/component/AI/util';
+import { toast } from '@core/component/Toast/Toast';
+import { fileTypeToBlockName } from '@core/constant/allBlocks';
+import type { EntityDragData, EntityDragEvent } from '@macro-entity';
+import { createDroppable, useDragDropContext } from '@thisbeyond/solid-dnd';
+import type { AttachmentType } from '@service-cognition/generated/schemas';
+import { type Accessor, createMemo } from 'solid-js';
+
+/**
+ * Hook to handle entity drag-and-drop for chat attachments.
+ * Creates a droppable zone and handles converting dropped entities to attachments.
+ *
+ * @param droppableId - Unique ID for the droppable zone
+ * @param attachments - The attachments object from useChatInput
+ * @returns Object with droppable directive and isDraggingOver signal
+ */
+export function useEntityDropAttachment(
+ droppableId: string,
+ attachments: Attachments
+): {
+ droppable: ReturnType<typeof createDroppable>;
+ isDraggingOver: Accessor<boolean>;
+} {
+ const droppable = createDroppable(droppableId);
+
+ const [state, { onDragEnd }] = useDragDropContext() ?? [
+ undefined,
+ { onDragEnd: () => {} },
+ ];
+
+ const entityDragData = createMemo(() => {
+ const draggable = state?.active.draggable;
+ if (!draggable) return;
+ const dragData = draggable.data;
+ if (!dragData || dragData.dragType !== 'entity') return;
+ return dragData as EntityDragData;
+ });
+
+ const isDraggingOver = createMemo(() => {
+ const dragData = entityDragData();
+ if (!dragData) return false;
+
+ const activeDroppable = state?.active.droppable;
+ if (!activeDroppable || activeDroppable.id !== droppableId) return false;
+
+ // Check if it's a supported attachment type
+ const fileType = 'fileType' in dragData ? dragData.fileType : undefined;
+ const blockName = fileTypeToBlockName(fileType ?? dragData.type, true);
+ return SUPPORTED_CHAT_ATTACHMENT_BLOCKS.includes(blockName);
+ });
+
+ onDragEnd((event: EntityDragEvent) => {
+ if (!event.droppable) return;
+ if (event.droppable.id !== droppableId) return;
+
+ const data = event.draggable?.data;
+ if (!data || data.dragType !== 'entity') return;
+
+ const entityId = data.id;
+ const entityName = data.name;
+ const entityType = data.type;
+ const fileType = 'fileType' in data ? data.fileType : undefined;
+
+ // Determine block name and check if it's a supported attachment type
+ const blockName = fileTypeToBlockName(fileType ?? entityType, true);
+ if (!SUPPORTED_CHAT_ATTACHMENT_BLOCKS.includes(blockName)) {
+ toast.failure('This file type cannot be attached to chat');
+ return;
+ }
+
+ // Build the attachment based on entity type
+ const attachment = match(entityType)
+ .with('document', () => {
+ const validFileType = asFileType(fileType);
+ if (!validFileType) return;
+ return {
+ id: `${entityId}-document-attachment`,
+ attachmentId: entityId,
+ attachmentType: 'document' satisfies AttachmentType,
+ metadata: {
+ type: 'document',
+ document_type: validFileType,
+ document_name: entityName,
+ },
+ } satisfies Attachment;
+ })
+ .with('project', () => {
+ return {
+ id: `${entityId}-project-attachment`,
+ attachmentId: entityId,
+ attachmentType: 'project' satisfies AttachmentType,
+ metadata: {
+ type: 'project',
+ project_name: entityName,
+ },
+ } satisfies Attachment;
+ })
+ .with('channel', () => {
+ const channelType =
+ 'channelType' in data ? data.channelType : 'organization';
+
+ return {
+ id: `${entityId}-channel-attachment`,
+ attachmentId: entityId,
+ attachmentType: 'channel' satisfies AttachmentType,
+ metadata: {
+ type: 'channel',
+ channel_type: channelType,
+ channel_name: entityName,
+ },
+ } satisfies Attachment;
+ })
+ .with('email', () => {
+ return {
+ id: `${entityId}-email-attachment`,
+ attachmentId: entityId,
+ attachmentType: 'email' satisfies AttachmentType,
+ metadata: {
+ type: 'email',
+ email_subject: entityName,
+ },
+ } satisfies Attachment;
+ })
+ .with('chat', () => undefined)
+ .exhaustive() satisfies Attachment | undefined;
+
+ if (attachment) {
+ attachments.addAttachment(attachment);
+ }
+ });
+
+ return { droppable, isDraggingOver };
+}