import { useEffect, useState } from "react"; import { useQuery } from "@tanstack/react-query"; import { Download } from "lucide-react"; import { issuesApi } from "@/api/issues"; import { Button } from "@/components/ui/button"; import { QueryErrorState, useQueryView } from "@/components/QueryView"; import { CsvPreview } from "@/components/CsvPreview"; import { isCsvFile } from "@/lib/csv-preview"; import { PatchLines } from "@/components/PatchLines"; import { isPatchFile } from "@/lib/patch"; import { MarkdownBody } from "@/components/MarkdownBody"; import { HtmlArtifactPreview } from "@/components/HtmlArtifactPreview"; import { FilePreviewModeToggle, type FilePreviewMode } from "@/components/FilePreviewModeToggle"; import { isHtmlPreview } from "@/lib/html-preview"; import { attachmentDownloadPath, isMarkdownAttachment, isTextAttachment } from "@/lib/issue-attachments"; import { queryKeys } from "@/lib/queryKeys"; import { readTextPreview, TEXT_PREVIEW_MAX_BYTES } from "@/lib/text-preview"; export { readTextPreview, TEXT_PREVIEW_MAX_BYTES } from "@/lib/text-preview"; export function TextAttachmentPreview({ title, text, markdown, html = false, csv = false, patch = isPatchFile(title), downloadUrl }: { title: string; text: string; markdown: boolean; html?: boolean; csv?: boolean; patch?: boolean; downloadUrl: string; }) { const [mode, setMode] = useState("rendered"); useEffect(() => setMode("rendered"), [title]); const renderCsv = csv && mode === "rendered" && text.length > 0; const renderHtml = html && mode === "rendered" && text.length > 0; return (

{title}

{markdown || html || csv ? : null}
{text.length === 0 ?

File is empty.

: renderCsv ? : renderHtml ? : markdown && mode === "rendered" ? {text} : patch ?
:
{text}
}
); } export function TaskAttachmentPanel({ issueId, attachmentId }: { issueId: string; attachmentId: string }) { const attachments = useQuery({ queryKey: queryKeys.issues.attachments(issueId), queryFn: () => issuesApi.listAttachments(issueId), }); // Re-resolve against this task's authorized attachment list; never trust persisted URLs. const attachment = attachments.data?.find((item) => item.id === attachmentId); const eligible = attachment && isTextAttachment(attachment) && attachment.byteSize <= TEXT_PREVIEW_MAX_BYTES; const content = useQuery({ queryKey: ["task-text-attachment", issueId, attachmentId], queryFn: async ({ signal }) => readTextPreview(await fetch( `/api/attachments/${encodeURIComponent(attachmentId)}/content`, { signal, credentials: "same-origin" }, )), enabled: Boolean(eligible), }); const attachmentsView = useQueryView(attachments); const contentView = useQueryView(content); if (attachments.isLoading || attachmentsView.kind === "reconnecting") return

Loading file…

; if (attachmentsView.kind === "error") { return ( ); } if (!attachment) return

File no longer available. Close this tab or choose another file.

; const downloadUrl = attachmentDownloadPath(attachment); if (!eligible || contentView.kind === "error") { return (

{contentView.kind === "error" ? "Could not preview this file. Retry or download it." : "This file is too large or is not supported for text preview. Download it instead."}

{eligible ? : null}
); } if (content.data === undefined) return

Loading file…

; return ; }