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: email attachment pills only create a single macro entity (#499)

Peter Chinman17032767717d
diff --git a/js/app/packages/block-email/component/AttachmentPill.tsx b/js/app/packages/block-email/component/AttachmentPill.tsx
index 0fdb6bf252..e9337d7d35 100644
--- a/js/app/packages/block-email/component/AttachmentPill.tsx
+++ b/js/app/packages/block-email/component/AttachmentPill.tsx
@@ -1,15 +1,9 @@
import { EntityIcon } from '@core/component/EntityIcon';
-import { fileTypeToBlockName } from '@core/constant/allBlocks';
-import { isErr } from '@core/util/maybeResult';
-import { uploadFile } from '@core/util/upload';
import X from '@icon/regular/x.svg';
-import { emailClient } from '@service-email/client';
import type { Attachment } from '@service-email/generated/schemas/attachment';
import { FileTypeMap } from '@service-storage/fileTypeMap';
import type { FileType } from '@service-storage/generated/schemas/fileType';
-import { platformFetch } from 'core/util/platformFetch';
import { Show } from 'solid-js';
-import { useSplitLayout } from '../../app/component/split-layout/layout';
const mimeToFileExtTypeMap = new Map<string, string>(
Object.values(FileTypeMap).map((value) => [value.mime, value.extension])
@@ -19,11 +13,10 @@ export function EmailAttachmentPill(props: {
attachment: Attachment;
removable?: boolean;
onRemove?: () => void;
+ onClick: (attachment: Attachment, fileType?: FileType) => void;
}) {
let parentDiv!: HTMLDivElement;
- const { replaceOrInsertSplit } = useSplitLayout();
-
const fileType = props.attachment.mime_type
? (mimeToFileExtTypeMap.get(props.attachment.mime_type) as FileType)
: undefined;
@@ -32,31 +25,7 @@ export function EmailAttachmentPill(props: {
<div
ref={parentDiv}
class={`items-center text-xs flex flex-row ${props.removable ? 'pl-2' : 'p-2'} m-1 w-36 rounded border border-edge hover:bg-hover hover-transition-bg`}
- onclick={async () => {
- const dbId = props.attachment.db_id;
- if (!dbId) return;
- const response = await emailClient.getAttachmentUrl({ id: dbId });
- if (isErr(response)) return;
-
- const dataUrl = response[1].attachment.data_url;
- if (!dataUrl) return;
- const fileBlob = await platformFetch(dataUrl)
- .then((res) => res.blob())
- .then((blob) => {
- return blob;
- });
- const file = new File(
- [fileBlob],
- props.attachment.filename ?? 'EmailAttachment'
- );
- const uploadedFile = await uploadFile(file, 'dss');
- if (uploadedFile.failed || uploadedFile.type !== 'document') return;
- const blockName = fileTypeToBlockName(uploadedFile.fileType);
- replaceOrInsertSplit({
- type: blockName,
- id: uploadedFile.documentId,
- });
- }}
+ onclick={() => props.onClick(props.attachment, fileType)}
>
<Show when={fileType !== undefined || props.attachment.mime_type}>
<EntityIcon
diff --git a/js/app/packages/block-email/component/MessageContainer.tsx b/js/app/packages/block-email/component/MessageContainer.tsx
index 7723130545..7bb5e94cf2 100644
--- a/js/app/packages/block-email/component/MessageContainer.tsx
+++ b/js/app/packages/block-email/component/MessageContainer.tsx
@@ -1,7 +1,19 @@
+import { useSplitLayout } from '@app/component/split-layout/layout';
import { Message } from '@core/component/Message';
+import { toast } from '@core/component/Toast/Toast';
+import { fileTypeToBlockName } from '@core/constant/allBlocks';
import { useDisplayName } from '@core/user';
-import type { MessageWithBodyReplyless } from '@service-email/generated/schemas';
+import { isErr } from '@core/util/maybeResult';
+import { queryKeys, useQueryClient } from '@macro-entity';
+import { logger } from '@observability';
+import { emailClient } from '@service-email/client';
+import type {
+ Attachment,
+ MessageWithBodyReplyless,
+} from '@service-email/generated/schemas';
import { useUserId } from '@service-gql/client';
+import { storageServiceClient } from '@service-storage/client';
+import type { FileType } from '@service-storage/generated/schemas/fileType';
import { createEffect, createMemo, createSignal, For, Show } from 'solid-js';
import type { SetStoreFunction } from 'solid-js/store';
import { Portal } from 'solid-js/web';
@@ -88,6 +100,56 @@ export function MessageContainer(props: MessageContainerProps) {
}
});
+ const { replaceOrInsertSplit } = useSplitLayout();
+ const entityQueryClient = useQueryClient();
+
+ const onClickAttachment = async (
+ attachment: Attachment,
+ fileType?: FileType
+ ) => {
+ const dbId = attachment.db_id;
+ if (!dbId) return;
+ const response = await emailClient.getOrCreateAttachmentDocumentId({
+ id: dbId,
+ });
+ if (isErr(response)) {
+ toast.failure('Failed to get attachment. Please try again.');
+ return logger.error('Failed to get or create attachment document id', {
+ error: new Error(
+ 'Failed to get or create attachment document id: ' + response[0]
+ ),
+ });
+ }
+ const { document_id } = response[1];
+
+ const maybeDocumentMetadata =
+ await storageServiceClient.getDocumentMetadata({
+ documentId: document_id,
+ });
+ if (isErr(maybeDocumentMetadata)) {
+ toast.failure('Failed to get attachment. Please try again.');
+ return logger.error(
+ 'Failed to get or create attachment document metadata',
+ {
+ error: new Error(
+ 'Failed to get or create attachment document metadata: ' +
+ maybeDocumentMetadata[0]
+ ),
+ }
+ );
+ }
+
+ entityQueryClient.invalidateQueries({
+ queryKey: queryKeys.all.dss,
+ });
+
+ const blockName = fileType ? fileTypeToBlockName(fileType) : 'unknown';
+ replaceOrInsertSplit({
+ type: blockName,
+ id: document_id,
+ });
+ };
+
return (
<div class="shrink-0 flex justify-center w-full">
<div class="macro-message-width w-full">
@@ -128,7 +190,12 @@ export function MessageContainer(props: MessageContainerProps) {
<For each={visibleAttachments()}>
{(attachment) => {
if (attachment.db_id)
- return <EmailAttachmentPill attachment={attachment} />;
+ return (
+ <EmailAttachmentPill
+ attachment={attachment}
+ onClick={onClickAttachment}
+ />
+ );
}}
</For>
</div>
diff --git a/js/app/packages/service-clients/service-email/client.ts b/js/app/packages/service-clients/service-email/client.ts
index 122444fb97..8f526f06d2 100644
--- a/js/app/packages/service-clients/service-email/client.ts
+++ b/js/app/packages/service-clients/service-email/client.ts
@@ -14,6 +14,7 @@ import type {
ApiPaginatedThreadCursor,
CreateDraftRequest,
CreateDraftResponse,
+ GetAttachmentDocumentIDResponse,
GetAttachmentResponse,
GetThreadResponse,
ListContactsResponse,
@@ -173,6 +174,18 @@ export const emailClient = {
(result) => result
);
},
+ async getOrCreateAttachmentDocumentId(args: { id: string }) {
+ const { id } = args;
+ return mapOk(
+ await emailFetch<GetAttachmentDocumentIDResponse>(
+ `/email/attachments/${id}/document_id`,
+ {
+ method: 'GET',
+ }
+ ),
+ (result) => result
+ );
+ },
async createDraft(args: CreateDraftRequest) {
return mapOk(
await emailFetch<CreateDraftResponse>('/email/drafts', {