Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

2.2 KB
Raw Blame
import { timestampDate } from "@bufbuild/protobuf/wkt";
import { separateAttachments } from "@/components/MemoMetadata/Attachment/attachmentHelpers";
import type { Memo } from "@/types/proto/api/memo_service_pb";
import type { User } from "@/types/proto/api/user_service_pb";
import { filterInlineManagedAttachments } from "@/utils/managed-attachment";
import { type AttachmentVisualItem, buildAttachmentVisualItems, countLogicalAttachmentItems } from "@/utils/media-item";
import { getMemoSharePreviewAvatarUrl } from "./memoShareImage";

interface BuildMemoShareImagePreviewModelOptions {
  memo: Memo;
  creator?: User;
  fallbackDisplayName: string;
  locale: string;
}

export interface MemoShareImageAttachmentSummaryBadge {
  type: "attachment-summary";
  count: number;
}

export type MemoShareImageFooterBadge = MemoShareImageAttachmentSummaryBadge;

export interface MemoShareImagePreviewModel {
  displayName: string;
  avatarUrl?: string;
  formattedDisplayTime?: string;
  visualItems: AttachmentVisualItem[];
  footerBadges: MemoShareImageFooterBadge[];
}

export const buildMemoShareImagePreviewModel = ({
  memo,
  creator,
  fallbackDisplayName,
  locale,
}: BuildMemoShareImagePreviewModelOptions): MemoShareImagePreviewModel => {
  const displayName = creator?.displayName || creator?.username || fallbackDisplayName;
  const avatarUrl = getMemoSharePreviewAvatarUrl(creator?.avatarUrl);
  const displayTime = memo.createTime ? timestampDate(memo.createTime) : undefined;
  const formattedDisplayTime = displayTime?.toLocaleString(locale, {
    dateStyle: "medium",
    timeStyle: "short",
  });

  const attachmentOnlyItems = filterInlineManagedAttachments(memo.content, memo.attachments);
  const attachmentGroups = separateAttachments(attachmentOnlyItems);
  const visualItems = buildAttachmentVisualItems(attachmentGroups.visual);
  const attachmentCount = countLogicalAttachmentItems(attachmentOnlyItems);
  const nonVisualAttachmentCount = Math.max(attachmentCount - visualItems.length, 0);
  const footerBadges: MemoShareImageFooterBadge[] =
    nonVisualAttachmentCount > 0 ? [{ type: "attachment-summary", count: attachmentCount }] : [];

  return {
    displayName,
    avatarUrl,
    formattedDisplayTime,
    visualItems,
    footerBadges,
  };
};