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.

feat(new-channels): consistent loading media sizing (#2129)

teo7bbe20adaa23
diff --git a/js/app/packages/channel/Input/message-payload.ts b/js/app/packages/channel/Input/message-payload.ts
index 237e1e0e64..91d151740e 100644
--- a/js/app/packages/channel/Input/message-payload.ts
+++ b/js/app/packages/channel/Input/message-payload.ts
@@ -87,6 +87,8 @@ export function buildPostMessageRequest(
attachments: snapshot.attachments.map((attachment) => ({
entity_id: attachment.id,
entity_type: attachmentEntityType(attachment),
+ width: attachment.width ?? null,
+ height: attachment.height ?? null,
})),
};
}
diff --git a/js/app/packages/channel/Input/types.ts b/js/app/packages/channel/Input/types.ts
index 7fb2b6a3cb..64c4acb279 100644
--- a/js/app/packages/channel/Input/types.ts
+++ b/js/app/packages/channel/Input/types.ts
@@ -11,6 +11,10 @@ export type InputAttachmentData = {
kind: InputAttachmentKind;
iconType?: EntityIconSelector;
pending?: boolean;
+ /** Image/video width in pixels (set after upload). */
+ width?: number;
+ /** Image/video height in pixels (set after upload). */
+ height?: number;
};
export type InputPersistenceKey = PersistenceKey;
diff --git a/js/app/packages/channel/Input/upload-attachments.ts b/js/app/packages/channel/Input/upload-attachments.ts
index e98389fd69..bc6a40d018 100644
--- a/js/app/packages/channel/Input/upload-attachments.ts
+++ b/js/app/packages/channel/Input/upload-attachments.ts
@@ -1,4 +1,5 @@
import { toast } from '@core/component/Toast/Toast';
+import { getImageDimensions, getVideoDimensions } from '@core/util/media';
import {
getAttachmentKindFromFile,
iconTypeFromFilename,
@@ -9,6 +10,27 @@ import type { InputAttachmentTracker } from './types';
export { getAttachmentKindFromFile } from './utils/file-helpers';
+/**
+ * Resolve media dimensions from a File for image/video attachments.
+ * Returns undefined for documents or on failure.
+ */
+async function resolveMediaDimensions(
+ file: File,
+ kind: 'image' | 'video' | 'document'
+): Promise<{ width: number; height: number } | undefined> {
+ if (kind === 'document') return undefined;
+ try {
+ const dims =
+ kind === 'image'
+ ? await getImageDimensions(file)
+ : await getVideoDimensions(file);
+ if (dims.width > 0 && dims.height > 0) return dims;
+ } catch {
+ // Dimension extraction is best-effort
+ }
+ return undefined;
+}
+
export async function uploadInputAttachments(options: {
files: File[];
tracker: InputAttachmentTracker;
@@ -30,7 +52,10 @@ export async function uploadInputAttachments(options: {
});
try {
- const result = await options.uploadFile(file);
+ const [result, dims] = await Promise.all([
+ options.uploadFile(file),
+ resolveMediaDimensions(file, pendingKind),
+ ]);
if (result.failed) {
options.tracker.removeAttachment(pendingId);
@@ -45,6 +70,11 @@ export async function uploadInputAttachments(options: {
continue;
}
+ if (dims) {
+ uploaded.width = dims.width;
+ uploaded.height = dims.height;
+ }
+
options.tracker.removeAttachment(pendingId);
options.tracker.addAttachment(uploaded);
} catch (error) {
diff --git a/js/app/packages/channel/Message/Attachments.tsx b/js/app/packages/channel/Message/Attachments.tsx
index 3484cdda5a..32e730dc1c 100644
--- a/js/app/packages/channel/Message/Attachments.tsx
+++ b/js/app/packages/channel/Message/Attachments.tsx
@@ -59,6 +59,8 @@ export function Attachments(props: AttachmentsProps) {
const imagePreviewData = createMemo(() =>
buckets().imageAttachments.map((attachment) => ({
id: attachment.entity_id,
+ width: attachment.width ?? undefined,
+ height: attachment.height ?? undefined,
}))
);
const imageAttachmentIds = createMemo(() =>
@@ -83,7 +85,12 @@ export function Attachments(props: AttachmentsProps) {
<Show when={buckets().videoAttachments.length > 0}>
<For each={buckets().videoAttachments}>
{(attachment) => (
- <MediaPreview kind="video" id={attachment.entity_id} />
+ <MediaPreview
+ kind="video"
+ id={attachment.entity_id}
+ width={attachment.width ?? undefined}
+ height={attachment.height ?? undefined}
+ />
)}
</For>
</Show>
diff --git a/js/app/packages/channel/Message/MediaPreview.tsx b/js/app/packages/channel/Message/MediaPreview.tsx
index 628e760209..3c700b74e9 100644
--- a/js/app/packages/channel/Message/MediaPreview.tsx
+++ b/js/app/packages/channel/Message/MediaPreview.tsx
@@ -7,6 +7,8 @@ import { Match, Switch } from 'solid-js';
type ImageAttachment = {
id: string;
+ width?: number | undefined;
+ height?: number | undefined;
};
type SingleImageMediaPreviewProps = {
@@ -25,6 +27,8 @@ type ImageGalleryMediaPreviewProps = {
type VideoMediaPreviewProps = {
kind: 'video';
id: string;
+ width?: number | undefined;
+ height?: number | undefined;
class?: string;
};
@@ -87,7 +91,12 @@ function VideoMediaPreview(props: VideoMediaPreviewProps) {
)}
data-message-media-preview="video"
>
- <VideoPreview id={props.id} variant="dynamic" />
+ <VideoPreview
+ id={props.id}
+ variant="dynamic"
+ width={props.width}
+ height={props.height}
+ />
</div>
);
}
diff --git a/js/app/packages/core/component/ImageGalleryPreview.tsx b/js/app/packages/core/component/ImageGalleryPreview.tsx
index dde3555309..0c3ff3e646 100644
--- a/js/app/packages/core/component/ImageGalleryPreview.tsx
+++ b/js/app/packages/core/component/ImageGalleryPreview.tsx
@@ -5,8 +5,10 @@ import ClipboardIcon from '@icon/regular/clipboard.svg';
import ThreeDotsIcon from '@icon/regular/dots-three-vertical.svg';
import DownloadIcon from '@icon/regular/download-simple.svg';
import TrashIcon from '@icon/regular/trash.svg';
+import { constrainImageDimensions } from '@lexical-core/utils/media';
import { Dialog } from '@kobalte/core/dialog';
import { DropdownMenu } from '@kobalte/core/dropdown-menu';
+import Spinner from '@phosphor-icons/core/bold/spinner-gap-bold.svg?component-solid';
import { type Component, createSignal, For, Show } from 'solid-js';
import { copyImageToClipboard, downloadImage } from '../util/imageActions';
import { platformFetch } from '../util/platformFetch';
@@ -38,6 +40,33 @@ const THEMES = {
'min-w-[100px] max-h-[200px] object-contain w-full rounded-2xl select-none border border-edge hover:border-accent hover-transition-border',
};
+/** Max dimensions for gallery image thumbnails */
+const GALLERY_MAX_WIDTH = 200;
+const GALLERY_MAX_HEIGHT = 200;
+
+function GalleryImagePlaceholder(props: {
+ dims: { width: number; height: number } | undefined;
+}) {
+ return (
+ <div
+ class="flex items-center justify-center border border-edge rounded-2xl bg-menu"
+ style={
+ props.dims
+ ? {
+ width: `${props.dims.width}px`,
+ height: `${props.dims.height}px`,
+ }
+ : {
+ width: '60px',
+ height: '60px',
+ }
+ }
+ >
+ <Spinner class="w-4 h-4 animate-spin" />
+ </div>
+ );
+}
+
export const ImageGalleryPreview: Component<ImageGalleryPreviewProps> = (
props
) => {
@@ -140,26 +169,52 @@ export const ImageGalleryPreview: Component<ImageGalleryPreviewProps> = (
onClick={() => setClickedIndex(index())}
disabled={props.isContext}
>
- <img
- class={`${THEMES[props.variant]} select-none`}
- src={getImageUrl(image.id)}
- alt="preview"
- style={{
- '-webkit-touch-callout': 'none',
- '-webkit-user-select': 'none',
- '-khtml-user-select': 'none',
- '-moz-user-select': 'none',
- '-ms-user-select': 'none',
- 'user-select': 'none',
- }}
- draggable={!isTouchDevice()}
- onDragStart={(e) => {
- e.dataTransfer?.setData(
- 'application/x-macro-internal',
- '1'
- );
- }}
- />
+ {(() => {
+ const dims = constrainImageDimensions(
+ image.width,
+ image.height,
+ GALLERY_MAX_WIDTH,
+ GALLERY_MAX_HEIGHT
+ );
+ const [loaded, setLoaded] = createSignal(false);
+ return (
+ <>
+ <Show when={!loaded()}>
+ <GalleryImagePlaceholder dims={dims} />
+ </Show>
+ <img
+ class={`${THEMES[props.variant]} select-none`}
+ classList={{ hidden: !loaded() }}
+ src={getImageUrl(image.id)}
+ alt="preview"
+ width={dims?.width}
+ height={dims?.height}
+ style={{
+ '-webkit-touch-callout': 'none',
+ '-webkit-user-select': 'none',
+ '-khtml-user-select': 'none',
+ '-moz-user-select': 'none',
+ '-ms-user-select': 'none',
+ 'user-select': 'none',
+ ...(dims
+ ? {
+ 'aspect-ratio': `${dims.width} / ${dims.height}`,
+ 'max-width': `${dims.width}px`,
+ }
+ : {}),
+ }}
+ draggable={!isTouchDevice()}
+ onLoad={() => setLoaded(true)}
+ onDragStart={(e) => {
+ e.dataTransfer?.setData(
+ 'application/x-macro-internal',
+ '1'
+ );
+ }}
+ />
+ </>
+ );
+ })()}
</Dialog.Trigger>
</div>
</div>
diff --git a/js/app/packages/core/component/ImagePreview.tsx b/js/app/packages/core/component/ImagePreview.tsx
index d7fe747590..beeb9133e9 100644
--- a/js/app/packages/core/component/ImagePreview.tsx
+++ b/js/app/packages/core/component/ImagePreview.tsx
@@ -6,6 +6,7 @@ import ClipboardIcon from '@icon/regular/clipboard.svg';
import ThreeDotsIcon from '@icon/regular/dots-three-vertical.svg';
import DownloadIcon from '@icon/regular/download-simple.svg';
import TrashIcon from '@icon/regular/trash.svg';
+import { constrainImageDimensions } from '@lexical-core/utils/media';
import { Dialog } from '@kobalte/core/dialog';
import { DropdownMenu } from '@kobalte/core/dropdown-menu';
import Spinner from '@phosphor-icons/core/bold/spinner-gap-bold.svg?component-solid';
@@ -57,9 +58,43 @@ const getDssImageBlob = async (documentId: string) => {
return blob;
};
+/** Max width for single image preview containers (matches MediaPreview max-w-[400px]) */
+const SINGLE_IMAGE_MAX_WIDTH = 400;
+
+function ImagePlaceholder(props: {
+ dims: { width: number; height: number } | undefined;
+}) {
+ return (
+ <div
+ class="flex items-center justify-center border border-edge rounded-2xl bg-menu"
+ style={
+ props.dims
+ ? {
+ width: `${props.dims.width}px`,
+ height: `${props.dims.height}px`,
+ }
+ : {
+ width: '60px',
+ height: '60px',
+ }
+ }
+ >
+ <Spinner class="w-4 h-4 animate-spin" />
+ </div>
+ );
+}
+
export function ImagePreview(props: ImagePreviewProps) {
const [imageBlob, setImageBlob] = createSignal<Blob>();
const [objectUrl, setObjectUrl] = createSignal<string>();
+ const [loaded, setLoaded] = createSignal(false);
+
+ const scaledDimensions = () =>
+ constrainImageDimensions(
+ props.image.width,
+ props.image.height,
+ SINGLE_IMAGE_MAX_WIDTH
+ );
const sfsImageUrl = () => {
if (props.isDss) {
@@ -142,20 +177,17 @@ export function ImagePreview(props: ImagePreviewProps) {
</div>
</Show>
<Dialog.Trigger class="flex" disabled={props.isContext}>
- <Show
- when={imageSrc()}
- fallback={
- <div class="flex flex-col items-center justify-center gap-2 w-[60px] h-[60px] border border-edge rounded-md bg-menu">
- <Spinner class="w-4 h-4 animate-spin" />
- </div>
- }
- >
+ <Show when={!loaded()}>
+ <ImagePlaceholder dims={scaledDimensions()} />
+ </Show>
+ <Show when={imageSrc()}>
<img
class={`${THEMES[props.variant]} select-none`}
+ classList={{ hidden: !loaded() }}
src={imageSrc()}
alt="preview"
- width={props.image.width}
- height={props.image.height}
+ width={scaledDimensions()?.width ?? props.image.width}
+ height={scaledDimensions()?.height ?? props.image.height}
style={{
'-webkit-touch-callout': 'none',
'-webkit-user-select': 'none',
@@ -163,8 +195,15 @@ export function ImagePreview(props: ImagePreviewProps) {
'-moz-user-select': 'none',
'-ms-user-select': 'none',
'user-select': 'none',
+ ...(scaledDimensions()
+ ? {
+ 'aspect-ratio': `${scaledDimensions()!.width} / ${scaledDimensions()!.height}`,
+ 'max-width': `${scaledDimensions()!.width}px`,
+ }
+ : {}),
}}
draggable={!isTouchDevice()}
+ onLoad={() => setLoaded(true)}
onDragStart={(e) => {
e.dataTransfer?.setData('application/x-macro-internal', '1');
}}
diff --git a/js/app/packages/lexical-core/utils/media.ts b/js/app/packages/lexical-core/utils/media.ts
index 297bdfd757..007c54baca 100644
--- a/js/app/packages/lexical-core/utils/media.ts
+++ b/js/app/packages/lexical-core/utils/media.ts
@@ -1,3 +1,19 @@
+/**
+ * Parse a loose width/height value and scale it to fit within constraints.
+ * Returns undefined when dimensions are missing or invalid.
+ */
+export function constrainImageDimensions(
+ width: string | number | undefined,
+ height: string | number | undefined,
+ maxWidth?: number,
+ maxHeight?: number
+): { width: number; height: number } | undefined {
+ const w = typeof width === 'string' ? Number.parseInt(width, 10) : width;
+ const h = typeof height === 'string' ? Number.parseInt(height, 10) : height;
+ if (!w || !h || w <= 0 || h <= 0) return undefined;
+ return calculateEffectiveDimensions(w, h, maxWidth, maxHeight);
+}
+
/**
* Calculates the effective dimensions based on constrained width and height.
* Uses the more constrained dimension to maintain aspect ratio.