import { create } from "@bufbuild/protobuf"; import { FileIcon } from "lucide-react"; import { extractMemoIdFromName } from "@/helpers/resource-names"; import { cn } from "@/lib/utils"; import type { Attachment } from "@/types/proto/api/v1/attachment_service_pb"; import { MemoSchema } from "@/types/proto/api/v1/memo_service_pb"; import type { User } from "@/types/proto/api/v1/user_service_pb"; import { getAttachmentType, getAttachmentUrl } from "@/utils/attachment"; import MemoContent from "../MemoContent"; import { MemoViewContext, type MemoViewContextValue } from "../MemoView/MemoViewContext"; interface MemoPreviewProps { content: string; attachments: Attachment[]; name?: string; compact?: boolean; className?: string; creator?: User; showCreator?: boolean; showMemoId?: boolean; truncate?: boolean; } const STUB_CONTEXT: MemoViewContextValue = { memo: create(MemoSchema), creator: undefined, currentUser: undefined, parentPage: "/", isArchived: false, readonly: true, showBlurredContent: false, blurred: false, openEditor: () => {}, toggleBlurVisibility: () => {}, openPreview: () => {}, }; const AttachmentThumbnails = ({ attachments }: { attachments: Attachment[] }) => { const images: Attachment[] = []; const others: Attachment[] = []; for (const a of attachments) { if (getAttachmentType(a) === "image/*") images.push(a); else others.push(a); } return (
{images.map((a) => ( {a.filename} ))} {others.map((a) => (
{a.filename}
))}
); }; const PreviewMeta = ({ creator, showCreator, memoName, showMemoId, }: { creator?: User; showCreator?: boolean; memoName?: string; showMemoId?: boolean; }) => { const creatorName = creator?.displayName || creator?.username; const memoId = showMemoId && memoName ? extractMemoIdFromName(memoName).slice(0, 6) : undefined; if (!creatorName && !memoId) { return null; } return (
{showMemoId && memoId && ( {memoId} )} {showCreator && creatorName && {creatorName}}
); }; const MemoPreview = ({ content, attachments, name, compact = true, className, creator, showCreator = false, showMemoId = false, truncate = false, }: MemoPreviewProps) => { const hasContent = content.trim().length > 0; const hasAttachments = attachments.length > 0; const showMeta = showCreator || showMemoId; if (!hasContent && !hasAttachments) { return null; } const meta = ; const contentNode = truncate ? ( hasContent ? (
{content}
) : hasAttachments ? null : (
No content
) ) : ( hasContent && ); return (
{showMeta && meta} {showMeta && truncate && hasContent &&
ยท
} {contentNode} {hasAttachments && }
); }; export default MemoPreview;