Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat: sfs image/video support for email message attachment (#599)
Peter Chinmanf135820fd498
diff --git a/js/app/packages/block-channel/component/Attachment.tsx b/js/app/packages/block-channel/component/Attachment.tsxindex 818da86026..54e6e2d725 100644--- a/js/app/packages/block-channel/component/Attachment.tsx+++ b/js/app/packages/block-channel/component/Attachment.tsx@@ -2,11 +2,11 @@ import { useSplitLayout } from '@app/component/split-layout/layout';import { EntityIcon } from '@core/component/EntityIcon';import { ImagePreview } from '@core/component/ImagePreview';import { TextButton } from '@core/component/TextButton';+import { VideoPreview } from '@core/component/VideoPreview';import {blockNameToDefaultFile,fileTypeToBlockName,} from '@core/constant/allBlocks';-import { staticFileIdEndpoint } from '@core/constant/servers';import {type InputAttachment,isStaticAttachmentType,@@ -71,17 +71,10 @@ export function Attachment(props: AttachmentProps) {</div></Match><Match when={props.attachment.blockName === STATIC_IMAGE}>- <ImagePreview- id={props.attachment.id}- variant="small"- isCurrentUser={true}- />+ <ImagePreview id={props.attachment.id} variant="small" /></Match><Match when={props.attachment.blockName === STATIC_VIDEO}>- <video- src={staticFileIdEndpoint(props.attachment.id)}- class="size-15"- />+ <VideoPreview id={props.attachment.id} variant="small" /></Match><Matchwhen={matches(diff --git a/js/app/packages/block-channel/component/Message/DynamicImageList.tsx b/js/app/packages/block-channel/component/Message/DynamicImageList.tsxindex 938f61d12e..26981ef4f5 100644--- a/js/app/packages/block-channel/component/Message/DynamicImageList.tsx+++ b/js/app/packages/block-channel/component/Message/DynamicImageList.tsx@@ -1,5 +1,6 @@import { ImageGalleryPreview } from '@core/component/ImageGalleryPreview';import { ImagePreview } from '@core/component/ImagePreview';+import { commsServiceClient } from '@service-comms/client';import { Match, Switch } from 'solid-js';type DynamicImageListProps = {@@ -14,6 +15,16 @@ type DynamicImageListProps = {// TODO: wipexport function DynamicImageList(props: DynamicImageListProps) {+ const handleDelete = async (attachmentId: string) => {+ if (!props.isCurrentUser || !props.channelId || !props.messageId) return;+ await commsServiceClient.patchMessage({+ channel_id: props.channelId,+ message_id: props.messageId,+ content: props.content,+ attachment_ids_to_delete: [attachmentId],+ });+ };+return (<Switch><Match when={props.ids.length === 1}>@@ -21,13 +32,11 @@ export function DynamicImageList(props: DynamicImageListProps) {<ImagePreviewid={props.ids[0]}variant="dynamic"- isCurrentUser={props.isCurrentUser}- channelId={props.isCurrentUser ? props.channelId : undefined}- messageId={props.isCurrentUser ? props.messageId : undefined}- attachmentId={- props.isCurrentUser ? props.attachmentIds[0] : undefined+ onDelete={+ props.isCurrentUser+ ? () => handleDelete(props.attachmentIds[0])+ : undefined}- content={props.content}isContext={props.isContext}/></div>@@ -38,11 +47,8 @@ export function DynamicImageList(props: DynamicImageListProps) {<ImageGalleryPreviewids={props.ids}variant="dynamic"- isCurrentUser={props.isCurrentUser}- channelId={props.isCurrentUser ? props.channelId : undefined}- messageId={props.isCurrentUser ? props.messageId : undefined}- attachmentIds={props.isCurrentUser ? props.attachmentIds : []}- content={props.content}+ attachmentIds={props.attachmentIds}+ onDelete={props.isCurrentUser ? handleDelete : undefined}isContext={props.isContext}/></div>diff --git a/js/app/packages/block-channel/component/Message/MessageAttachments.tsx b/js/app/packages/block-channel/component/Message/MessageAttachments.tsxindex 22235d878e..2a8a692226 100644--- a/js/app/packages/block-channel/component/Message/MessageAttachments.tsx+++ b/js/app/packages/block-channel/component/Message/MessageAttachments.tsx@@ -1,5 +1,5 @@import { ItemPreview } from '@core/component/ItemPreview';-import { staticFileIdEndpoint } from '@core/constant/servers';+import { VideoPreview } from '@core/component/VideoPreview';import type { Attachment } from '@service-comms/generated/models/attachment';import { stringToItemType } from '@service-storage/client';import { type Accessor, For, Show } from 'solid-js';@@ -22,16 +22,12 @@ export function MessageAttachments(props: MessageAttachmentsProps) {{/* Video attachments */}<Show when={props.videoAttachments()?.length > 0 && !props.isDeleted()}><For each={props.videoAttachments()}>- {(item) => (- <div class="flex size-full max-w-50 max-h-100 object-contain">- <video controls src={staticFileIdEndpoint(item.entity_id)} />- </div>- )}+ {(item) => <VideoPreview id={item.entity_id} variant="dynamic" />}</For></Show>{/* Image attachments */}<Show when={props.imageAttachments()?.length > 0 && !props.isDeleted()}>- <div class="flex">+ <div class="flex not-first:mt-2"><DynamicImageListids={props.imageAttachments()?.map((a) => a.entity_id)}attachmentIds={props.imageAttachments()?.map((a) => a.id)}@@ -46,7 +42,7 @@ export function MessageAttachments(props: MessageAttachmentsProps) {<Showwhen={props.documentAttachments()?.length > 0 && !props.isDeleted()}>- <div class={`flex flex-row mt-0.5 gap-2 flex-wrap max-w-full`}>+ <div class={`flex flex-row mt-2 gap-2 flex-wrap max-w-full`}><For each={props.documentAttachments()}>{(attachment) => (<ItemPreviewdiff --git a/js/app/packages/block-email/component/AttachmentPill.tsx b/js/app/packages/block-email/component/AttachmentPill.tsxindex e9337d7d35..7f650a92fe 100644--- a/js/app/packages/block-email/component/AttachmentPill.tsx+++ b/js/app/packages/block-email/component/AttachmentPill.tsx@@ -24,7 +24,10 @@ export function EmailAttachmentPill(props: {return (<divref={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`}+ class="items-center text-xs flex flex-row p-2 w-36 rounded border border-edge hover:bg-hover hover-transition-bg"+ classList={{+ 'pl-2': props.removable,+ }}onclick={() => props.onClick(props.attachment, fileType)}><Show when={fileType !== undefined || props.attachment.mime_type}>diff --git a/js/app/packages/block-email/component/EmailMessageBody.tsx b/js/app/packages/block-email/component/EmailMessageBody.tsxindex 333b9fad3a..87245843d6 100644--- a/js/app/packages/block-email/component/EmailMessageBody.tsx+++ b/js/app/packages/block-email/component/EmailMessageBody.tsx@@ -2,9 +2,8 @@ import { IconButton } from '@core/component/IconButton';import { StaticMarkdown } from '@core/component/LexicalMarkdown/component/core/StaticMarkdown';import { channelTheme } from '@core/component/LexicalMarkdown/theme';import { DEV_MODE_ENV } from '@core/constant/featureFlags';-import { isErr } from '@core/util/maybeResult';+import { SERVER_HOSTS } from '@core/constant/servers';import DotsThree from '@icon/regular/dots-three.svg';-import { emailClient } from '@service-email/client';import type { MessageWithBodyReplyless } from '@service-email/generated/schemas';import { useEmail } from '@service-gql/client';import {@@ -127,41 +126,34 @@ export function EmailMessageBody(props: EmailMessageBodyProps) {return hostContainer;});- // Get the attachment URLs for inline images that reference attachments via cid: URLs+ // Resolve inline images that reference attachments via cid: URLscreateEffect(() => {const root = host().shadowRoot;if (root) {- // Resolve inline images that reference attachments via cid: URLs- queueMicrotask(async () => {- // Build a map from normalized content-id => attachment db_id- const contentIdToDbId = new Map<string, string>();+ queueMicrotask(() => {+ // Build a map from normalized content-id => sfs_id+ const contentIdToSfsId = new Map<string, string>();for (const att of props.message.attachments ?? []) {const contentId = att.content_id;- const dbId = att.db_id;- if (!contentId || !dbId) continue;+ const sfsId = att.sfs_id;+ if (!contentId || !sfsId) continue;const normalized = contentId.replace(/[<>]/g, '');- contentIdToDbId.set(normalized, dbId);+ contentIdToSfsId.set(normalized, sfsId);}const images = root.querySelectorAll('img[src^="cid:"]');- await Promise.all(- Array.from(images).map(async (img) => {- if (!(img instanceof HTMLImageElement)) return;- if (img.dataset.cidResolved === 'true') return;- const src = img.getAttribute('src');- if (!src?.startsWith('cid:')) return;- const rawCid = src.slice(4);- const normalizedCid = rawCid.replace(/[<>]/g, '');- const dbId = contentIdToDbId.get(normalizedCid);- if (!dbId) return;- const res = await emailClient.getAttachmentUrl({ id: dbId });- if (isErr(res)) return;- const dataUrl = res[1].attachment.data_url;- if (!dataUrl) return;- img.src = dataUrl;- img.dataset.cidResolved = 'true';- })- );+ for (const img of images) {+ if (!(img instanceof HTMLImageElement)) continue;+ if (img.dataset.cidResolved === 'true') continue;+ const src = img.getAttribute('src');+ if (!src?.startsWith('cid:')) continue;+ const rawCid = src.slice(4);+ const normalizedCid = rawCid.replace(/[<>]/g, '');+ const sfsId = contentIdToSfsId.get(normalizedCid);+ if (!sfsId) continue;+ img.src = `${SERVER_HOSTS['static-file']}/file/${sfsId}`;+ img.dataset.cidResolved = 'true';+ }});}});diff --git a/js/app/packages/block-email/component/MessageContainer.tsx b/js/app/packages/block-email/component/MessageContainer.tsxindex 42edc22d10..0646817030 100644--- a/js/app/packages/block-email/component/MessageContainer.tsx+++ b/js/app/packages/block-email/component/MessageContainer.tsx@@ -1,6 +1,9 @@import { useSplitLayout } from '@app/component/split-layout/layout';+import { ImageGalleryPreview } from '@core/component/ImageGalleryPreview';+import { ImagePreview } from '@core/component/ImagePreview';import { Message } from '@core/component/Message';import { toast } from '@core/component/Toast/Toast';+import { VideoPreview } from '@core/component/VideoPreview';import { fileTypeToBlockName } from '@core/constant/allBlocks';import { useDisplayName } from '@core/user';import { isErr } from '@core/util/maybeResult';@@ -14,7 +17,15 @@ import type {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 {+ createEffect,+ createMemo,+ createSignal,+ For,+ Match,+ Show,+ Switch,+} from 'solid-js';import type { SetStoreFunction } from 'solid-js/store';import { Portal } from 'solid-js/web';import { EmailAttachmentPill } from './AttachmentPill';@@ -90,6 +101,27 @@ export function MessageContainer(props: MessageContainerProps) {});});+ const imageAttachmentsWithSfs = createMemo(() => {+ return visibleAttachments().filter(+ (a) => a.mime_type?.startsWith('image/') && a.sfs_id+ );+ });++ const videoAttachmentsWithSfs = createMemo(() => {+ return visibleAttachments().filter(+ (a) => a.mime_type?.startsWith('video/') && a.sfs_id+ );+ });++ const otherAttachments = createMemo(() => {+ return visibleAttachments().filter(+ (a) =>+ !a.sfs_id ||+ (!a.mime_type?.startsWith('image/') &&+ !a.mime_type?.startsWith('video/'))+ );+ });+// expand appropriate messagescreateEffect(() => {const id = props.message.db_id;@@ -187,18 +219,50 @@ export function MessageContainer(props: MessageContainerProps) {threadMessageIndex={props.threadMessageIndex}/></Message.Body>- <Show when={visibleAttachments().length > 0}>- <div class="flex flex-row overflow-x-scroll my-1">- <For each={visibleAttachments()}>- {(attachment) => {- if (attachment.db_id)- return (- <EmailAttachmentPill- attachment={attachment}- onClick={onClickAttachment}- />- );- }}+ {/* Image attachments */}+ <Show when={imageAttachmentsWithSfs().length > 0}>+ <Switch>+ <Match when={imageAttachmentsWithSfs().length === 1}>+ <div class="max-w-[400px] w-fit mt-2">+ <ImagePreview+ id={imageAttachmentsWithSfs()[0].sfs_id!}+ variant="dynamic"+ />+ </div>+ </Match>+ <Match when={imageAttachmentsWithSfs().length > 1}>+ <div class="flex flex-wrap gap-2 mt-2">+ <ImageGalleryPreview+ ids={imageAttachmentsWithSfs().map((a) => a.sfs_id!)}+ variant="dynamic"+ attachmentIds={imageAttachmentsWithSfs().map(+ (a) => a.db_id!+ )}+ />+ </div>+ </Match>+ </Switch>+ </Show>++ {/* Video attachments */}+ <Show when={videoAttachmentsWithSfs().length > 0}>+ <For each={videoAttachmentsWithSfs()}>+ {(attachment) => (+ <VideoPreview id={attachment.sfs_id!} variant="dynamic" />+ )}+ </For>+ </Show>++ {/* Other attachments (non-media or without sfs_id) */}+ <Show when={otherAttachments().length > 0}>+ <div class="flex flex-row overflow-x-scroll mt-2 gap-2">+ <For each={otherAttachments()}>+ {(attachment) => (+ <EmailAttachmentPill+ attachment={attachment}+ onClick={onClickAttachment}+ />+ )}</For></div></Show>diff --git a/js/app/packages/core/component/AI/component/input/Attachment.tsx b/js/app/packages/core/component/AI/component/input/Attachment.tsxindex df1a55050e..1737d4657f 100644--- a/js/app/packages/core/component/AI/component/input/Attachment.tsx+++ b/js/app/packages/core/component/AI/component/input/Attachment.tsx@@ -87,7 +87,6 @@ function ImageAttachment(props: {<ImagePreviewid={props.attachment.attachmentId}variant="small"- isCurrentUser={true}isDss={isDssImage(props.attachment)}onError={(e) => {console.error('Failed to load image', e);diff --git a/js/app/packages/core/component/AI/component/message/UserMessage.tsx b/js/app/packages/core/component/AI/component/message/UserMessage.tsxindex 9c31481d12..c9095324e2 100644--- a/js/app/packages/core/component/AI/component/message/UserMessage.tsx+++ b/js/app/packages/core/component/AI/component/message/UserMessage.tsx@@ -92,7 +92,6 @@ export function UserMessage(props: {<ImagePreviewid={attachment.attachmentId}variant="small"- isCurrentUser={true}isDss={isDssImage(attachment)}/>)}diff --git a/js/app/packages/core/component/ImageGalleryPreview.tsx b/js/app/packages/core/component/ImageGalleryPreview.tsxindex f06f2d2555..954c95757f 100644--- a/js/app/packages/core/component/ImageGalleryPreview.tsx+++ b/js/app/packages/core/component/ImageGalleryPreview.tsx@@ -13,7 +13,6 @@ import XIcon from '@icon/regular/x.svg';import { Dialog } from '@kobalte/core/dialog';import { DropdownMenu } from '@kobalte/core/dropdown-menu';import Panzoom, { type PanzoomObject } from '@panzoom/panzoom';-import { commsServiceClient } from '@service-comms/client';import {type Component,createEffect,@@ -33,11 +32,8 @@ export type ImageGalleryPreviewProps = {variant: 'small' | 'dynamic';square?: boolean;wrapperClass?: string;- channelId?: string;- messageId?: string;attachmentIds: string[];- isCurrentUser: boolean;- content?: string;+ onDelete?: (attachmentId: string) => void;isContext?: boolean;};@@ -419,15 +415,8 @@ export const ImageGalleryPreview: Component<ImageGalleryPreviewProps> = (panzoomInstance()?.reset({ animate: false });});- const handleDeleteMedia = async (attachmentId: string) => {- console.log('deleting media 2', attachmentId);- if (!props.channelId || !props.messageId) return;- await commsServiceClient.patchMessage({- channel_id: props.channelId,- message_id: props.messageId,- attachment_ids_to_delete: [attachmentId],- content: props.content,- });+ const handleDelete = (attachmentId: string) => {+ props.onDelete?.(attachmentId);};return (@@ -474,13 +463,13 @@ export const ImageGalleryPreview: Component<ImageGalleryPreviewProps> = (icon={DownloadIcon}onClick={() => downloadImageById(id)}/>- <Show when={props.isCurrentUser}>+ <Show when={props.onDelete}><MenuSeparator /><MenuItemtext="Delete image"icon={TrashIcon}onClick={() =>- handleDeleteMedia(props.attachmentIds[index()])+ handleDelete(props.attachmentIds[index()])}/></Show>diff --git a/js/app/packages/core/component/ImagePreview.tsx b/js/app/packages/core/component/ImagePreview.tsxindex 88ce61b4e4..872bfedfba 100644--- a/js/app/packages/core/component/ImagePreview.tsx+++ b/js/app/packages/core/component/ImagePreview.tsx@@ -13,7 +13,6 @@ import { Dialog } from '@kobalte/core/dialog';import { DropdownMenu } from '@kobalte/core/dropdown-menu';import Panzoom from '@panzoom/panzoom';import Spinner from '@phosphor-icons/core/bold/spinner-gap-bold.svg?component-solid';-import { commsServiceClient } from '@service-comms/client';import { storageServiceClient } from '@service-storage/client';import { fetchBinary } from '@service-storage/util/fetchBinary';import { createEffect, createSignal, onCleanup, Show } from 'solid-js';@@ -26,11 +25,7 @@ export type ImagePreviewProps = {id: string;variant: 'small' | 'dynamic';square?: boolean;- channelId?: string;- messageId?: string;- attachmentId?: string;- isCurrentUser: boolean;- content?: string;+ onDelete?: () => void;isContext?: boolean;isDss?: boolean;onError?: (err: any) => void;@@ -133,23 +128,8 @@ export function ImagePreview(props: ImagePreviewProps) {}};- const showDeleteMediaButton = () => {- return (- props.channelId &&- props.messageId &&- props.attachmentId &&- props.isCurrentUser- );- };-- const handleDeleteChannelMedia = async () => {- if (!props.channelId || !props.messageId || !props.attachmentId) return;- await commsServiceClient.patchMessage({- channel_id: props.channelId,- message_id: props.messageId,- content: props.content,- attachment_ids_to_delete: [props.attachmentId],- });+ const handleDelete = () => {+ props.onDelete?.();};const handleMouseMove = () => {@@ -296,12 +276,12 @@ export function ImagePreview(props: ImagePreviewProps) {icon={DownloadIcon}onClick={downloadImage}/>- <Show when={showDeleteMediaButton()}>+ <Show when={props.onDelete}><MenuSeparator /><MenuItemtext="Delete image"icon={TrashIcon}- onClick={handleDeleteChannelMedia}+ onClick={handleDelete}/></Show></DropdownMenuContent>diff --git a/js/app/packages/core/component/VideoPreview.tsx b/js/app/packages/core/component/VideoPreview.tsxnew file mode 100644index 0000000000..4e08502e53--- /dev/null+++ b/js/app/packages/core/component/VideoPreview.tsx@@ -0,0 +1,19 @@+import { staticFileIdEndpoint } from '@core/constant/servers';++export type VideoPreviewProps = {+ id: string;+ variant: 'small' | 'dynamic';+};++const THEMES = {+ small: 'min-h-[80px] size-15',+ dynamic: 'flex min-h-20 max-w-[480px] max-h-[480px] not-first:mt-2',+};++export function VideoPreview(props: VideoPreviewProps) {+ return (+ <div class={THEMES[props.variant]}>+ <video controls src={staticFileIdEndpoint(props.id)} />+ </div>+ );+}