2 import { ICON_CLASS_DEFAULT } from '$lib/constants/css-classes';
3 import { X, Music, Video } from '@lucide/svelte';
13 import { ActionIcon } from '$lib/components/app';
14 import { AttachmentType } from '$lib/enums';
17 attachment?: DatabaseMessageExtra;
20 onclick?: (event: MouseEvent) => void;
21 onRemove?: (id: string) => void;
26 // Either uploaded file or stored attachment
27 uploadedFile?: ChatUploadedFile;
32 class: className = '',
43 let isPdf = $derived(isPdfFile(attachment, uploadedFile));
44 let isAudio = $derived(isAudioFile(attachment, uploadedFile));
45 let isVideo = $derived(isVideoFile(attachment, uploadedFile));
46 let isPdfWithContent = $derived(isPdf && !!textContent);
48 let isText = $derived(isTextFile(attachment, uploadedFile));
49 let isTextWithContent = $derived(isText && !!textContent);
51 let fileTypeLabel = $derived.by(() => {
52 if (uploadedFile?.type) {
53 return getFileTypeLabel(uploadedFile.type);
57 if ('mimeType' in attachment && attachment.mimeType) {
58 return getFileTypeLabel(attachment.mimeType);
61 if (attachment.type) {
62 return getFileTypeLabel(attachment.type);
66 return getFileTypeLabel(name);
69 let pdfProcessingMode = $derived.by(() => {
70 if (attachment?.type === AttachmentType.PDF) {
71 const pdfAttachment = attachment as DatabaseMessageExtraPdfFile;
73 return pdfAttachment.processedAsImages ? 'Sent as Image' : 'Sent as Text';
80 {#snippet textPreview(content: string)}
81 <div class="relative">
83 class="font-mono text-xs leading-relaxed break-words whitespace-pre-wrap text-muted-foreground {!readonly
84 ? 'max-h-3rem line-height-1.2'
87 {getPreviewText(content)}
90 {#if content.length > 150}
92 class="pointer-events-none absolute right-0 bottom-0 left-0 h-4 bg-gradient-to-t from-muted to-transparent {readonly
100 {#snippet removeButton()}
102 class="absolute top-2 right-2 opacity-0 transition-opacity group-focus-within:opacity-100 group-hover:opacity-100"
104 <ActionIcon icon={X} tooltip="Remove" stopPropagationOnClick onclick={() => onRemove?.(id)} />
108 {#snippet fileIcon()}
110 class="flex h-8 w-8 items-center justify-center rounded bg-primary/10 text-xs font-medium text-primary"
113 <Music class="{ICON_CLASS_DEFAULT} text-white/70" />
115 <Video class="{ICON_CLASS_DEFAULT} text-white/70" />
122 {#snippet info(text: string | undefined)}
124 <span class="text-xs text-muted-foreground">{text}</span>
128 {#if isTextWithContent || isPdfWithContent}
130 aria-label={readonly ? `Preview ${name}` : undefined}
131 class="rounded-lg border border-border bg-muted p-3 {className} cursor-pointer {readonly
132 ? 'w-full max-w-2xl transition-shadow hover:shadow-md'
133 : `group relative text-left ${textContent ? 'max-h-24 max-w-72' : 'max-w-36'}`} overflow-hidden"
138 {@render removeButton()}
141 <div class={[!readonly && 'pr-8', 'overflow-hidden']}>
143 <div class="flex items-start gap-3">
144 <div class="flex min-w-0 flex-1 flex-col items-start text-left">
145 <span class="w-full truncate text-sm font-medium text-foreground">{name}</span>
147 {@render info(pdfProcessingMode || (size ? formatFileSize(size) : undefined))}
150 {@render textPreview(textContent)}
155 <span class="mb-3 block truncate text-sm font-medium text-foreground">{name}</span>
158 {@render textPreview(textContent)}
165 class="group flex items-center gap-3 rounded-lg border border-border bg-muted p-3 {className} relative"
171 <div class="flex flex-col items-start gap-0.5">
173 class="max-w-24 truncate text-sm font-medium text-foreground {readonly
175 : 'group-hover:pr-6'} md:max-w-32"
180 {@render info(pdfProcessingMode || (size ? formatFileSize(size) : undefined))}
184 {@render removeButton()}