2 import { X, Music, Video } from '@lucide/svelte';
12 import { ActionIcon } from '$lib/components/app';
13 import { AttachmentType } from '$lib/enums';
16 attachment?: DatabaseMessageExtra;
19 onclick?: (event: MouseEvent) => void;
20 onRemove?: (id: string) => void;
25 // Either uploaded file or stored attachment
26 uploadedFile?: ChatUploadedFile;
31 class: className = '',
42 let isPdf = $derived(isPdfFile(attachment, uploadedFile));
43 let isAudio = $derived(isAudioFile(attachment, uploadedFile));
44 let isVideo = $derived(isVideoFile(attachment, uploadedFile));
45 let isPdfWithContent = $derived(isPdf && !!textContent);
47 let isText = $derived(isTextFile(attachment, uploadedFile));
48 let isTextWithContent = $derived(isText && !!textContent);
50 let fileTypeLabel = $derived.by(() => {
51 if (uploadedFile?.type) {
52 return getFileTypeLabel(uploadedFile.type);
56 if ('mimeType' in attachment && attachment.mimeType) {
57 return getFileTypeLabel(attachment.mimeType);
60 if (attachment.type) {
61 return getFileTypeLabel(attachment.type);
65 return getFileTypeLabel(name);
68 let pdfProcessingMode = $derived.by(() => {
69 if (attachment?.type === AttachmentType.PDF) {
70 const pdfAttachment = attachment as DatabaseMessageExtraPdfFile;
72 return pdfAttachment.processedAsImages ? 'Sent as Image' : 'Sent as Text';
79 {#snippet textPreview(content: string)}
80 <div class="relative">
82 class="font-mono text-xs leading-relaxed break-words whitespace-pre-wrap text-muted-foreground {!readonly
83 ? 'max-h-3rem line-height-1.2'
86 {getPreviewText(content)}
89 {#if content.length > 150}
91 class="pointer-events-none absolute right-0 bottom-0 left-0 h-4 bg-gradient-to-t from-muted to-transparent {readonly
99 {#snippet removeButton()}
101 class="absolute top-2 right-2 opacity-0 transition-opacity group-focus-within:opacity-100 group-hover:opacity-100"
103 <ActionIcon icon={X} tooltip="Remove" stopPropagationOnClick onclick={() => onRemove?.(id)} />
107 {#snippet fileIcon()}
109 class="flex h-8 w-8 items-center justify-center rounded bg-primary/10 text-xs font-medium text-primary"
112 <Music class="h-4 w-4 text-white/70" />
114 <Video class="h-4 w-4 text-white/70" />
121 {#snippet info(text: string | undefined)}
123 <span class="text-xs text-muted-foreground">{text}</span>
127 {#if isTextWithContent || isPdfWithContent}
129 aria-label={readonly ? `Preview ${name}` : undefined}
130 class="rounded-lg border border-border bg-muted p-3 {className} cursor-pointer {readonly
131 ? 'w-full max-w-2xl transition-shadow hover:shadow-md'
132 : `group relative text-left ${textContent ? 'max-h-24 max-w-72' : 'max-w-36'}`} overflow-hidden"
137 {@render removeButton()}
140 <div class={[!readonly && 'pr-8', 'overflow-hidden']}>
142 <div class="flex items-start gap-3">
143 <div class="flex min-w-0 flex-1 flex-col items-start text-left">
144 <span class="w-full truncate text-sm font-medium text-foreground">{name}</span>
146 {@render info(pdfProcessingMode || (size ? formatFileSize(size) : undefined))}
149 {@render textPreview(textContent)}
154 <span class="mb-3 block truncate text-sm font-medium text-foreground">{name}</span>
157 {@render textPreview(textContent)}
164 class="group flex items-center gap-3 rounded-lg border border-border bg-muted p-3 {className} relative"
170 <div class="flex flex-col items-start gap-0.5">
172 class="max-w-24 truncate text-sm font-medium text-foreground {readonly
174 : 'group-hover:pr-6'} md:max-w-32"
179 {@render info(pdfProcessingMode || (size ? formatFileSize(size) : undefined))}
183 {@render removeButton()}