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
import { MediaImage } from '@channel/Media/MediaImage';
import { MediaVideo } from '@channel/Media/MediaVideo';
import { MediaViewerDialog } from '@channel/Media/MediaViewerDialog';
import type { MediaItem } from '@channel/Media/media-items';
import { EntityIcon } from '@core/component/EntityIcon';
import {
staticFileIdEndpoint,
staticFileSizedEndpoint,
} from '@core/constant/servers';
import SpinnerIcon from '@icon/system/regular/spinner-gap.svg';
import XIcon from '@icon/system/regular/x.svg';
import { cn } from '@ui';
import {
children,
createMemo,
createSignal,
For,
type JSX,
Match,
Show,
Switch,
splitProps,
} from 'solid-js';
import { useInput, useInputCommands } from './context';
import type { InputAttachmentData, InputAttachmentKind } from './types';
type AttachmentsProps = JSX.HTMLAttributes<HTMLDivElement> & {
kind?: InputAttachmentKind | 'media';
};
function truncateFilename(name: string, maxLength = 24): string {
if (name.length <= maxLength) return name;
return `${name.slice(0, maxLength)}…`;
}
function RemoveButton(props: {
attachment: InputAttachmentData;
onRemove: (attachment: InputAttachmentData) => void;
class?: string;
}) {
return (
<button
type="button"
class={cn(
// Hover/press feedback is painted as a scrim over the button's own
// background so the media variant's `bg-surface` chip stays opaque.
'not-touch:hover:overlay-hover not-touch:active:overlay-active rounded-md p-1 [@media(hover:none)]:p-2 items-center flex',
props.class
)}
onClick={(event) => {
event.stopPropagation();
props.onRemove(props.attachment);
}}
aria-label={`Remove ${props.attachment.name}`}
>
<XIcon class="text-ink-muted group-hover:text-failure size-3 [@media(hover:none)]:size-4" />
</button>
);
}
function MediaAttachmentItem(props: {
attachment: InputAttachmentData;
onRemove: (attachment: InputAttachmentData) => void;
onOpen?: () => void;
}) {
const isPending = () => !!props.attachment.pending;
const mediaSrc = () =>
props.attachment.kind === 'image'
? staticFileSizedEndpoint(props.attachment.id, 'medium')
: staticFileIdEndpoint(props.attachment.id);
const removeButton = () => (
<RemoveButton
attachment={props.attachment}
onRemove={props.onRemove}
class="absolute -top-2 -right-2 z-10 rounded-full bg-surface border border-edge-muted opacity-0 group-hover:opacity-100 focus-visible:opacity-100 [@media(hover:none)]:opacity-100"
/>
);
const pendingOverlay = () => (
<Show when={isPending()}>
<div class="absolute inset-0 flex items-center justify-center rounded-2xl">
<SpinnerIcon class="size-4 animate-spin" />
</div>
</Show>
);
return (
<div class="ph-no-capture relative group">
<Switch
fallback={
<div class="size-23 rounded-2xl border border-edge bg-surface" />
}
>
<Match
when={
props.attachment.kind === 'image' &&
(!isPending() || props.attachment.previewSrc)
}
>
<MediaImage.Root>
<button type="button" onClick={() => props.onOpen?.()}>
<MediaImage.Image
src={mediaSrc()}
previewSrc={props.attachment.previewSrc}
class="size-23 select-none rounded-2xl border border-edge object-cover"
width={92}
height={92}
loading="lazy"
fallback={<MediaImage.Fallback square />}
/>
</button>
</MediaImage.Root>
</Match>
<Match when={props.attachment.kind === 'video' && !isPending()}>
<button type="button" onClick={() => props.onOpen?.()}>
<MediaVideo.Root class="size-23 group overflow-hidden border border-edge bg-surface">
<MediaVideo.Preview
src={mediaSrc()}
class="size-full object-cover"
/>
<MediaVideo.PlayOverlay />
</MediaVideo.Root>
</button>
</Match>
</Switch>
{pendingOverlay()}
{removeButton()}
</div>
);
}
function DocumentAttachmentItem(props: {
attachment: InputAttachmentData;
onRemove: (attachment: InputAttachmentData) => void;
}) {
return (
<div class="ph-no-capture group flex items-center px-2 py-1.5 space-x-1.5 hover:bg-hover hover-transition-bg cursor-default text-sm border border-edge-muted rounded-xs">
<Show
when={!props.attachment.pending}
fallback={<SpinnerIcon class="size-4 animate-spin" />}
>
<EntityIcon
targetType={props.attachment.iconType ?? 'unknown'}
size="xs"
/>
</Show>
<span>
{/* Note: using javascript truncation here rather than CSS because `truncate` was causing a complex bug that made it impossible to horizontally scroll documents on mobile. */}
{truncateFilename(props.attachment.name)}
</span>
<RemoveButton attachment={props.attachment} onRemove={props.onRemove} />
</div>
);
}
export function Attachments(props: AttachmentsProps) {
const input = useInput();
const commands = useInputCommands();
const [local, rest] = splitProps(props, ['class', 'children', 'kind']);
const resolved = children(() => local.children);
const [viewerOpen, setViewerOpen] = createSignal(false);
const [selectedIndex, setSelectedIndex] = createSignal(0);
const visibleAttachments = () => {
const items = input().attachments ?? [];
if (!local.kind) return items;
if (local.kind === 'media') {
return items.filter(
(attachment) =>
attachment.kind === 'image' || attachment.kind === 'video'
);
}
return items.filter((attachment) => attachment.kind === local.kind);
};
const mediaAttachments = () =>
visibleAttachments().filter(
(a): a is InputAttachmentData & { kind: 'image' | 'video' } =>
!a.pending && (a.kind === 'image' || a.kind === 'video')
);
const mediaItems = createMemo((): MediaItem[] =>
mediaAttachments().map((a) => {
const fullSrc = staticFileIdEndpoint(a.id);
return {
id: a.id,
src:
a.kind === 'image'
? staticFileSizedEndpoint(a.id, 'medium')
: fullSrc,
fullSrc,
kind: a.kind,
width: a.width,
height: a.height,
};
})
);
const handleRemove = (attachment: InputAttachmentData) => {
commands.removeAttachment(attachment);
};
const handleOpen = (attachment: InputAttachmentData) => {
const index = mediaAttachments().findIndex((a) => a.id === attachment.id);
if (index === -1) return;
setSelectedIndex(index);
setViewerOpen(true);
};
return (
<Show when={visibleAttachments().length > 0}>
<div
class={cn(
'flex flex-row w-full p-2 gap-2 flex-wrap',
// On mobile/tablet, attachments scroll horizontally
'touch:flex-nowrap touch:*:shrink-0 touch:overflow-x-auto touch:scrollbar-hidden',
local.class
)}
data-input-attachments={local.kind ?? 'all'}
{...rest}
>
<Show
when={resolved()}
fallback={
<For each={visibleAttachments()}>
{(attachment) => (
<Switch>
<Match
when={
attachment.kind === 'image' || attachment.kind === 'video'
}
>
<MediaAttachmentItem
attachment={attachment}
onRemove={handleRemove}
onOpen={() => handleOpen(attachment)}
/>
</Match>
<Match when={attachment.kind === 'document'}>
<DocumentAttachmentItem
attachment={attachment}
onRemove={handleRemove}
/>
</Match>
</Switch>
)}
</For>
}
>
{(children) => children()}
</Show>
</div>
<MediaViewerDialog
items={() => mediaItems()}
open={viewerOpen()}
onOpenChange={setViewerOpen}
currentIndex={selectedIndex}
onCurrentIndexChange={setSelectedIndex}
/>
</Show>
);
}