Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

1.1 KB
Raw Blame
'use server';

import { Image } from '@lobehub/ui/mdx';
import { getPlaiceholder } from 'plaiceholder';
import { type FC } from 'react';

const DEFAULT_WIDTH = 800;

const fetchImage = async (url: string) => {
  const buffer = await fetch(url, { cache: 'force-cache' }).then(async (res) =>
    Buffer.from(await res.arrayBuffer()),
  );
  const {
    base64,
    metadata: { height, width },
  } = await getPlaiceholder(buffer, { format: ['webp'] });
  return {
    base64,
    height: (DEFAULT_WIDTH / width) * height,
  };
};

const ImageWrapper: FC<{ alt: string; src: string }> = async ({ alt, src, ...rest }) => {
  try {
    const { base64, height } = await fetchImage(src);
    return (
      <Image
        alt={alt}
        height={height}
        src={src}
        width={DEFAULT_WIDTH}
        styles={{
          wrapper: {
            backgroundImage: `url(${base64})`,
            backgroundPosition: 'center',
            backgroundSize: 'cover',
          },
        }}
      />
    );
  } catch {
    return <Image alt={alt} src={src} {...rest} />;
  }
};

export default ImageWrapper;