|
| 1 | +import { createQuery } from "@tanstack/solid-query"; |
| 2 | +import { createEffect, createSignal, For, Show } from "solid-js"; |
| 3 | +import { api } from "~/api/client"; |
| 4 | +import { queryKeys } from "~/queries/keys"; |
| 5 | +import type { EventAttachment } from "~/lib/sentry-types"; |
| 6 | +import Button from "~/components/ui/Button"; |
| 7 | +import IconDownload from "~icons/lucide/download"; |
| 8 | +import IconEye from "~icons/lucide/eye"; |
| 9 | +import IconEyeOff from "~icons/lucide/eye-off"; |
| 10 | +import IconPaperclip from "~icons/lucide/paperclip"; |
| 11 | + |
| 12 | +interface AttachmentsPanelProps { |
| 13 | + eventId: number; |
| 14 | +} |
| 15 | + |
| 16 | +function formatBytes(bytes: number): string { |
| 17 | + if (bytes < 1024) return `${bytes} B`; |
| 18 | + const units = ["KB", "MB", "GB"]; |
| 19 | + let value = bytes / 1024; |
| 20 | + let unit = 0; |
| 21 | + while (value >= 1024 && unit < units.length - 1) { |
| 22 | + value /= 1024; |
| 23 | + unit += 1; |
| 24 | + } |
| 25 | + return `${value.toFixed(value >= 10 ? 0 : 1)} ${units[unit]}`; |
| 26 | +} |
| 27 | + |
| 28 | +function downloadFilename(name: string): string { |
| 29 | + const sanitized = name.replace(/[\\/\0-\x1f\x7f]+/g, "_").trim(); |
| 30 | + return sanitized || "attachment"; |
| 31 | +} |
| 32 | + |
| 33 | +export default function AttachmentsPanel(props: AttachmentsPanelProps) { |
| 34 | + const [previewId, setPreviewId] = createSignal<number | null>(null); |
| 35 | + const [downloadingId, setDownloadingId] = createSignal<number | null>(null); |
| 36 | + |
| 37 | + createEffect(() => { |
| 38 | + props.eventId; |
| 39 | + setPreviewId(null); |
| 40 | + }); |
| 41 | + |
| 42 | + const attachmentsQuery = createQuery(() => ({ |
| 43 | + queryKey: queryKeys.events.attachments(props.eventId), |
| 44 | + queryFn: ({ signal }) => |
| 45 | + api.get<EventAttachment[]>( |
| 46 | + `/internal/events/${props.eventId}/attachments`, |
| 47 | + signal, |
| 48 | + ), |
| 49 | + })); |
| 50 | + |
| 51 | + const previewQuery = createQuery(() => ({ |
| 52 | + queryKey: queryKeys.events.attachmentText( |
| 53 | + props.eventId, |
| 54 | + previewId() ?? "none", |
| 55 | + ), |
| 56 | + queryFn: ({ signal }) => |
| 57 | + api.text( |
| 58 | + `/internal/events/${props.eventId}/attachments/${previewId()}/text`, |
| 59 | + signal, |
| 60 | + ), |
| 61 | + enabled: previewId() !== null, |
| 62 | + })); |
| 63 | + |
| 64 | + const attachments = () => attachmentsQuery.data ?? []; |
| 65 | + const selectedAttachment = () => |
| 66 | + attachments().find((attachment) => attachment.id === previewId()) ?? null; |
| 67 | + |
| 68 | + const downloadAttachment = async (attachment: EventAttachment) => { |
| 69 | + setDownloadingId(attachment.id); |
| 70 | + try { |
| 71 | + const blob = await api.blob( |
| 72 | + `/internal/events/${props.eventId}/attachments/${attachment.id}/download`, |
| 73 | + ); |
| 74 | + const url = URL.createObjectURL(blob); |
| 75 | + const link = document.createElement("a"); |
| 76 | + link.href = url; |
| 77 | + link.download = downloadFilename(attachment.name); |
| 78 | + document.body.appendChild(link); |
| 79 | + link.click(); |
| 80 | + link.remove(); |
| 81 | + URL.revokeObjectURL(url); |
| 82 | + } finally { |
| 83 | + setDownloadingId(null); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + return ( |
| 88 | + <Show when={attachments().length > 0}> |
| 89 | + <div class="card attachments"> |
| 90 | + <div class="card__header"> |
| 91 | + <h3 class="inline-gap"> |
| 92 | + <IconPaperclip /> Attachments |
| 93 | + </h3> |
| 94 | + <span class="text-xs text-secondary"> |
| 95 | + {attachments().length} file{attachments().length === 1 ? "" : "s"} |
| 96 | + </span> |
| 97 | + </div> |
| 98 | + <table class="data-table data-table--compact attachments__table"> |
| 99 | + <thead> |
| 100 | + <tr> |
| 101 | + <th>Name</th> |
| 102 | + <th>Type</th> |
| 103 | + <th>Size</th> |
| 104 | + <th data-align="right">Actions</th> |
| 105 | + </tr> |
| 106 | + </thead> |
| 107 | + <tbody> |
| 108 | + <For each={attachments()}> |
| 109 | + {(attachment) => ( |
| 110 | + <tr> |
| 111 | + <td> |
| 112 | + <div class="attachments__name">{attachment.name}</div> |
| 113 | + <Show when={attachment.attachment_type}> |
| 114 | + <div class="text-secondary"> |
| 115 | + {attachment.attachment_type} |
| 116 | + </div> |
| 117 | + </Show> |
| 118 | + </td> |
| 119 | + <td>{attachment.content_type ?? "application/octet-stream"}</td> |
| 120 | + <td>{formatBytes(attachment.size)}</td> |
| 121 | + <td data-align="right"> |
| 122 | + <div class="attachments__actions"> |
| 123 | + <Button |
| 124 | + variant="ghost" |
| 125 | + size="sm" |
| 126 | + onClick={() => |
| 127 | + setPreviewId((current) => |
| 128 | + current === attachment.id ? null : attachment.id, |
| 129 | + ) |
| 130 | + } |
| 131 | + > |
| 132 | + {previewId() === attachment.id ? <IconEyeOff /> : <IconEye />} Plaintext |
| 133 | + </Button> |
| 134 | + <Button |
| 135 | + variant="ghost" |
| 136 | + size="sm" |
| 137 | + disabled={downloadingId() === attachment.id} |
| 138 | + onClick={() => void downloadAttachment(attachment)} |
| 139 | + > |
| 140 | + <IconDownload /> Download |
| 141 | + </Button> |
| 142 | + </div> |
| 143 | + </td> |
| 144 | + </tr> |
| 145 | + )} |
| 146 | + </For> |
| 147 | + </tbody> |
| 148 | + </table> |
| 149 | + <Show when={previewId() !== null}> |
| 150 | + <div class="attachment-preview"> |
| 151 | + <div class="attachment-preview__header"> |
| 152 | + <span>{selectedAttachment()?.name ?? "Attachment"}</span> |
| 153 | + <span class="text-secondary">Plaintext preview</span> |
| 154 | + </div> |
| 155 | + <pre class="attachment-preview__content"> |
| 156 | + <Show |
| 157 | + when={!previewQuery.isError} |
| 158 | + fallback="Unable to load attachment preview." |
| 159 | + > |
| 160 | + <Show when={!previewQuery.isPending} fallback="Loading attachment..."> |
| 161 | + {previewQuery.data ?? ""} |
| 162 | + </Show> |
| 163 | + </Show> |
| 164 | + </pre> |
| 165 | + </div> |
| 166 | + </Show> |
| 167 | + </div> |
| 168 | + </Show> |
| 169 | + ); |
| 170 | +} |
0 commit comments