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.

3.7 KB
Raw Blame
import { internalDrag } from '@core/directive/internalDragState';
import Spinner from '@icon/system/bold/spinner-gap-bold.svg?component-solid';
import { cn } from '@ui';
import {
  createEffect,
  createSignal,
  type JSX,
  on,
  type ParentProps,
  Show,
} from 'solid-js';

false && internalDrag;
const ATTACHMENT_TILE_SIZE = 92;

function ImagePlaceholder(props: {
  dims?: { width: number; height: number };
  square?: boolean;
  /** Fill the parent's fixed box, so the placeholder matches the loaded image. */
  fill?: boolean;
}) {
  return (
    <div
      class={cn(
        'flex items-center justify-center rounded-2xl border border-edge bg-surface',
        props.fill && 'size-full'
      )}
      style={
        props.fill
          ? undefined
          : props.square
            ? {
                width: `${ATTACHMENT_TILE_SIZE}px`,
                height: `${ATTACHMENT_TILE_SIZE}px`,
              }
            : props.dims
              ? {
                  width: `${props.dims.width}px`,
                  height: `${props.dims.height}px`,
                }
              : {
                  width: '60px',
                  height: '60px',
                }
      }
    >
      <Spinner class="size-4 animate-spin" />
    </div>
  );
}

function Root(props: ParentProps<{ class?: string }>) {
  return (
    <div class={cn('relative flex rounded-2xl', props.class)}>
      {props.children}
    </div>
  );
}

function Fallback(props: {
  dims?: { width: number; height: number };
  square?: boolean;
  fill?: boolean;
}) {
  return (
    <ImagePlaceholder
      dims={props.dims}
      square={props.square}
      fill={props.fill}
    />
  );
}

function Image(props: {
  src: string;
  // A source for image preview, e.g. used when we have a local url we can display while the image gets uploaded, as in iOS when sharing images.
  previewSrc?: string;
  onOpen?: () => void;
  class?: string;
  width?: number;
  height?: number;
  style?: JSX.CSSProperties;
  loading?: 'eager' | 'lazy';
  fallback?: JSX.Element;
  /** The file couldn't be loaded; the fallback otherwise stays up. */
  onError?: () => void;
}) {
  const [loaded, setLoaded] = createSignal(false);
  const hasDimensions = () => (props.width ?? 0) > 0 && (props.height ?? 0) > 0;

  createEffect(
    on(
      () => props.src,
      () => {
        setLoaded(false);
      }
    )
  );

  return (
    <>
      <Show when={!loaded()}>
        <div
          classList={{ absolute: hasDimensions(), 'inset-0': hasDimensions() }}
        >
          <Show when={props.previewSrc} fallback={props.fallback}>
            {(previewSrc) => (
              <img
                class={cn(props.class)}
                src={previewSrc()}
                alt="preview"
                width={props.width}
                height={props.height}
                style={props.style}
                loading={props.loading}
                onClick={() => props.onOpen?.()}
              />
            )}
          </Show>
        </div>
      </Show>
      <img
        class={cn(props.class)}
        // Keep the same responsive image box in flow before and after load.
        // Swapping a fixed-size placeholder for it changes virtual row heights.
        classList={{
          invisible: !loaded(),
          absolute: !loaded() && !hasDimensions(),
        }}
        src={props.src}
        alt="preview"
        width={props.width}
        height={props.height}
        style={props.style}
        loading={props.loading}
        onClick={() => props.onOpen?.()}
        onLoad={() => setLoaded(true)}
        onError={() => props.onError?.()}
        use:internalDrag={true}
      />
    </>
  );
}

export const MediaImage = {
  Root,
  Fallback,
  Image,
};