Files
memos/web/src/components/AttachmentLibrary/AttachmentFileRows.tsx
T
2026-11-13 09:13:37 +08:00

130 lines
5.7 KiB
TypeScript

import { FileAudioIcon, FileIcon, PlayIcon } from "lucide-react";
import AudioAttachmentItem from "@/components/MemoMetadata/Attachment/AudioAttachmentItem";
import VideoPoster from "@/components/VideoPoster";
import type { AttachmentLibraryListItem } from "@/hooks/useAttachmentLibrary";
import { cn } from "@/lib/utils";
import type { User } from "@/types/proto/api/user_service_pb";
import { getAttachmentThumbnailUrl, getAttachmentType, isMotionAttachment } from "@/utils/attachment";
import { AttachmentCreator, AttachmentMetadataLine, AttachmentOpenButton, AttachmentSourceChip } from "./AttachmentLibraryPrimitives";
type AttachmentCreators = Map<string, User | undefined>;
const AttachmentThumb = ({ item, className }: { item: AttachmentLibraryListItem; className?: string }) => {
const type = getAttachmentType(item.attachment);
const isMotion = isMotionAttachment(item.attachment);
if (type === "image/*" || isMotion) {
return (
<div className={cn("overflow-hidden rounded-mu-xl bg-mu-well/35", className)}>
<img
src={getAttachmentThumbnailUrl(item.attachment)}
alt={item.attachment.filename}
className="h-full w-full object-cover"
loading="lazy"
decoding="async"
/>
</div>
);
}
if (type === "video/*") {
return (
<div className={cn("relative overflow-hidden rounded-mu-xl bg-mu-well/35", className)}>
<VideoPoster sourceUrl={item.sourceUrl} alt={item.attachment.filename} className="h-full w-full object-cover" />
<span className="absolute bottom-2 right-2 inline-flex h-7 w-7 items-center justify-center rounded-full bg-mu-canvas/85 text-mu-text">
<PlayIcon className="mu-icon-sm fill-current" />
</span>
</div>
);
}
return (
<div className={cn("flex items-center justify-center rounded-mu-xl bg-mu-well/45 text-mu-text-secondary", className)}>
{type === "audio/*" ? <FileAudioIcon className="mu-icon h-5 w-5" /> : <FileIcon className="mu-icon h-5 w-5" />}
</div>
);
};
export const AttachmentDocumentRows = ({ items, creators }: { items: AttachmentLibraryListItem[]; creators?: AttachmentCreators }) => {
return (
<div className="space-y-3">
{items.map((item) => (
<article
key={item.attachment.name}
className="flex items-center gap-2.5 rounded-[18px] border border-mu-line/60 bg-mu-canvas/90 p-3"
>
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-mu-lg bg-mu-well/45 text-mu-text-secondary">
<FileIcon className="mu-icon h-4.5 w-4.5" />
</div>
<div className="min-w-0 flex-1">
<div className="truncate mu-label text-mu-text" title={item.attachment.filename}>
{item.attachment.filename}
</div>
<div className="mt-0.5 flex flex-wrap items-center gap-1.5">
<AttachmentMetadataLine className="min-w-0 max-w-full" items={[item.fileTypeLabel, item.fileSizeLabel, item.createdLabel]} />
<AttachmentCreator creatorName={item.attachment.creator} user={creators?.get(item.attachment.creator)} />
<AttachmentSourceChip memoName={item.memoName} />
</div>
</div>
<AttachmentOpenButton href={item.sourceUrl} />
</article>
))}
</div>
);
};
export const AttachmentAudioRows = ({ items, creators }: { items: AttachmentLibraryListItem[]; creators?: AttachmentCreators }) => {
return (
<div className="space-y-2.5">
{items.map((item) => (
<article key={item.attachment.name} className="rounded-[18px] border border-mu-line/60 bg-mu-canvas/90 p-2.5">
<AudioAttachmentItem
filename={item.attachment.filename}
sourceUrl={item.sourceUrl}
mimeType={item.attachment.type}
size={Number(item.attachment.size)}
/>
<div className="mt-2.5 flex items-center justify-between gap-2 border-t border-mu-line/60 px-0.5 pt-2.5">
<div className="min-w-0 flex flex-wrap items-center gap-1.5">
<AttachmentMetadataLine className="min-w-0 max-w-full" items={[item.createdLabel]} />
<AttachmentCreator creatorName={item.attachment.creator} user={creators?.get(item.attachment.creator)} />
<AttachmentSourceChip memoName={item.memoName} />
</div>
<AttachmentOpenButton href={item.sourceUrl} />
</div>
</article>
))}
</div>
);
};
export const AttachmentUnusedRows = ({ items, creators }: { items: AttachmentLibraryListItem[]; creators?: AttachmentCreators }) => {
return (
<div className="space-y-2.5">
{items.map((item) => (
<article
key={item.attachment.name}
className="flex items-center gap-2.5 rounded-[18px] border border-mu-warning/30 bg-mu-warning-soft p-3"
>
<AttachmentThumb item={item} className="h-10 w-10 shrink-0" />
<div className="min-w-0 flex-1">
<div className="truncate mu-label text-mu-text" title={item.attachment.filename}>
{item.attachment.filename}
</div>
<div className="mt-0.5 flex flex-wrap items-center gap-1.5">
<AttachmentMetadataLine className="min-w-0 max-w-full" items={[item.fileTypeLabel, item.fileSizeLabel, item.createdLabel]} />
<AttachmentCreator creatorName={item.attachment.creator} user={creators?.get(item.attachment.creator)} />
<AttachmentSourceChip unlinkedLabelKey="attachment-library.labels.not-linked" />
</div>
</div>
<AttachmentOpenButton className="text-mu-warning/90 hover:text-mu-warning" href={item.sourceUrl} />
</article>
))}
</div>
);
};