|
| 1 | +import { FileDiff } from "@pierre/diffs/react"; |
| 2 | +import { TurnId } from "@okcode/contracts"; |
| 3 | +import { ExternalLinkIcon, LoaderCircleIcon } from "lucide-react"; |
| 4 | +import { useEffect, useMemo, useState } from "react"; |
| 5 | + |
| 6 | +import type { ChatFileAttachment } from "../../types"; |
| 7 | +import { |
| 8 | + buildAttachmentPreviewModel, |
| 9 | + buildAttachmentPreviewTreeFiles, |
| 10 | + type AttachmentPreviewModel, |
| 11 | +} from "../../lib/attachmentPreview"; |
| 12 | +import { resolveDiffThemeName } from "../../lib/diffRendering"; |
| 13 | +import { ChangedFilesTree } from "./ChangedFilesTree"; |
| 14 | +import { PR_REVIEW_DIFF_UNSAFE_CSS, resolveFileDiffPath } from "../pr-review/pr-review-utils"; |
| 15 | +import { |
| 16 | + Dialog, |
| 17 | + DialogDescription, |
| 18 | + DialogFooter, |
| 19 | + DialogHeader, |
| 20 | + DialogPanel, |
| 21 | + DialogPopup, |
| 22 | + DialogTitle, |
| 23 | +} from "../ui/dialog"; |
| 24 | + |
| 25 | +interface AttachmentPreviewDialogProps { |
| 26 | + attachment: ChatFileAttachment; |
| 27 | + open: boolean; |
| 28 | + onOpenChange: (open: boolean) => void; |
| 29 | + resolvedTheme: "light" | "dark"; |
| 30 | +} |
| 31 | + |
| 32 | +interface AttachmentPreviewState { |
| 33 | + status: "idle" | "loading" | "ready" | "error"; |
| 34 | + preview: AttachmentPreviewModel | null; |
| 35 | + error: string | null; |
| 36 | +} |
| 37 | + |
| 38 | +function emptyPreviewState(): AttachmentPreviewState { |
| 39 | + return { |
| 40 | + status: "idle", |
| 41 | + preview: null, |
| 42 | + error: null, |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +export function AttachmentPreviewDialog({ |
| 47 | + attachment, |
| 48 | + open, |
| 49 | + onOpenChange, |
| 50 | + resolvedTheme, |
| 51 | +}: AttachmentPreviewDialogProps) { |
| 52 | + const [state, setState] = useState<AttachmentPreviewState>(() => emptyPreviewState()); |
| 53 | + const [selectedFilePath, setSelectedFilePath] = useState<string | null>(null); |
| 54 | + |
| 55 | + useEffect(() => { |
| 56 | + if (!open) { |
| 57 | + setState(emptyPreviewState()); |
| 58 | + setSelectedFilePath(null); |
| 59 | + return; |
| 60 | + } |
| 61 | + if (!attachment.url) { |
| 62 | + setState({ |
| 63 | + status: "error", |
| 64 | + preview: null, |
| 65 | + error: "This attachment does not have a preview URL.", |
| 66 | + }); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + const controller = new AbortController(); |
| 71 | + setState({ |
| 72 | + status: "loading", |
| 73 | + preview: null, |
| 74 | + error: null, |
| 75 | + }); |
| 76 | + |
| 77 | + void fetch(attachment.url, { signal: controller.signal }) |
| 78 | + .then(async (response) => { |
| 79 | + if (!response.ok) { |
| 80 | + throw new Error(`Failed to load attachment (${response.status}).`); |
| 81 | + } |
| 82 | + const text = await response.text(); |
| 83 | + if (controller.signal.aborted) return; |
| 84 | + const preview = buildAttachmentPreviewModel({ |
| 85 | + name: attachment.name, |
| 86 | + mimeType: attachment.mimeType, |
| 87 | + text, |
| 88 | + }); |
| 89 | + const firstDiffFilePath = |
| 90 | + preview.kind === "diff" && preview.files[0] |
| 91 | + ? resolveFileDiffPath(preview.files[0]) |
| 92 | + : null; |
| 93 | + setState({ |
| 94 | + status: "ready", |
| 95 | + preview, |
| 96 | + error: null, |
| 97 | + }); |
| 98 | + setSelectedFilePath(firstDiffFilePath); |
| 99 | + }) |
| 100 | + .catch((error) => { |
| 101 | + if (controller.signal.aborted) return; |
| 102 | + setState({ |
| 103 | + status: "error", |
| 104 | + preview: null, |
| 105 | + error: error instanceof Error ? error.message : "Failed to load attachment preview.", |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + return () => { |
| 110 | + controller.abort(); |
| 111 | + }; |
| 112 | + }, [attachment.mimeType, attachment.name, attachment.url, open]); |
| 113 | + |
| 114 | + const diffFiles = useMemo( |
| 115 | + () => |
| 116 | + state.preview?.kind === "diff" ? buildAttachmentPreviewTreeFiles(state.preview.files) : [], |
| 117 | + [state.preview], |
| 118 | + ); |
| 119 | + const selectedDiffFile = useMemo(() => { |
| 120 | + if (state.preview?.kind !== "diff") { |
| 121 | + return null; |
| 122 | + } |
| 123 | + if (state.preview.files.length === 0) { |
| 124 | + return null; |
| 125 | + } |
| 126 | + return ( |
| 127 | + state.preview.files.find((fileDiff) => resolveFileDiffPath(fileDiff) === selectedFilePath) ?? |
| 128 | + state.preview.files[0] ?? |
| 129 | + null |
| 130 | + ); |
| 131 | + }, [selectedFilePath, state.preview]); |
| 132 | + |
| 133 | + return ( |
| 134 | + <Dialog open={open} onOpenChange={onOpenChange}> |
| 135 | + <DialogPopup className="max-w-6xl"> |
| 136 | + <DialogHeader> |
| 137 | + <DialogTitle>{attachment.name}</DialogTitle> |
| 138 | + <DialogDescription> |
| 139 | + {attachment.mimeType} · {new Intl.NumberFormat().format(attachment.sizeBytes)} bytes |
| 140 | + </DialogDescription> |
| 141 | + </DialogHeader> |
| 142 | + <DialogPanel className="min-h-[28rem]"> |
| 143 | + {state.status === "loading" && ( |
| 144 | + <div className="flex h-full min-h-[22rem] items-center justify-center gap-2 text-sm text-muted-foreground"> |
| 145 | + <LoaderCircleIcon className="size-4 animate-spin" /> |
| 146 | + Loading attachment preview… |
| 147 | + </div> |
| 148 | + )} |
| 149 | + |
| 150 | + {state.status === "error" && ( |
| 151 | + <div className="flex h-full min-h-[22rem] flex-col items-center justify-center gap-3 rounded-xl border border-dashed border-border/70 bg-muted/20 px-6 text-center"> |
| 152 | + <p className="max-w-md text-sm text-muted-foreground">{state.error}</p> |
| 153 | + {attachment.url && ( |
| 154 | + <a |
| 155 | + href={attachment.url} |
| 156 | + target="_blank" |
| 157 | + rel="noreferrer" |
| 158 | + className="inline-flex h-8 items-center justify-center gap-2 rounded-lg border border-foreground/12 bg-card px-3 text-xs font-medium text-foreground shadow-[inset_0_1px_0_hsl(0_0%_100%/0.06)] transition-all duration-200 hover:border-primary/35 hover:bg-accent/80" |
| 159 | + > |
| 160 | + <ExternalLinkIcon className="size-3.5" /> |
| 161 | + Open raw attachment |
| 162 | + </a> |
| 163 | + )} |
| 164 | + </div> |
| 165 | + )} |
| 166 | + |
| 167 | + {state.status === "ready" && state.preview?.kind === "text" && ( |
| 168 | + <div className="rounded-xl border border-border/70 bg-background/70"> |
| 169 | + <pre className="max-h-[70vh] overflow-auto whitespace-pre-wrap break-all p-4 font-mono text-[12px] leading-5 text-foreground/85"> |
| 170 | + {state.preview.text} |
| 171 | + </pre> |
| 172 | + </div> |
| 173 | + )} |
| 174 | + |
| 175 | + {state.status === "ready" && state.preview?.kind === "diff" && selectedDiffFile && ( |
| 176 | + <div className="grid min-h-[28rem] gap-4 lg:grid-cols-[18rem_minmax(0,1fr)]"> |
| 177 | + <div className="rounded-xl border border-border/70 bg-background/70 p-2"> |
| 178 | + <ChangedFilesTree |
| 179 | + turnId={TurnId.makeUnsafe(`attachment-${attachment.id}`)} |
| 180 | + files={diffFiles} |
| 181 | + allDirectoriesExpanded |
| 182 | + resolvedTheme={resolvedTheme} |
| 183 | + cwd={undefined} |
| 184 | + onSelectFile={setSelectedFilePath} |
| 185 | + selectedFilePath={resolveFileDiffPath(selectedDiffFile)} |
| 186 | + /> |
| 187 | + </div> |
| 188 | + <div className="overflow-hidden rounded-xl border border-border/70 bg-background/70"> |
| 189 | + <div className="border-b border-border/70 px-4 py-3"> |
| 190 | + <p className="truncate font-mono text-xs text-muted-foreground"> |
| 191 | + {resolveFileDiffPath(selectedDiffFile)} |
| 192 | + </p> |
| 193 | + </div> |
| 194 | + <div className="max-h-[70vh] overflow-auto p-2"> |
| 195 | + <FileDiff |
| 196 | + fileDiff={selectedDiffFile} |
| 197 | + options={{ |
| 198 | + diffStyle: "unified", |
| 199 | + lineDiffType: "none", |
| 200 | + overflow: "wrap", |
| 201 | + theme: resolveDiffThemeName(resolvedTheme), |
| 202 | + themeType: resolvedTheme, |
| 203 | + unsafeCSS: PR_REVIEW_DIFF_UNSAFE_CSS, |
| 204 | + }} |
| 205 | + /> |
| 206 | + </div> |
| 207 | + </div> |
| 208 | + </div> |
| 209 | + )} |
| 210 | + </DialogPanel> |
| 211 | + <DialogFooter variant="bare" className="justify-end gap-2"> |
| 212 | + {attachment.url && ( |
| 213 | + <a |
| 214 | + href={attachment.url} |
| 215 | + target="_blank" |
| 216 | + rel="noreferrer" |
| 217 | + className="inline-flex h-8 items-center justify-center gap-2 rounded-lg border border-foreground/12 bg-card px-3 text-xs font-medium text-foreground shadow-[inset_0_1px_0_hsl(0_0%_100%/0.06)] transition-all duration-200 hover:border-primary/35 hover:bg-accent/80" |
| 218 | + > |
| 219 | + <ExternalLinkIcon className="size-3.5" /> |
| 220 | + Open raw attachment |
| 221 | + </a> |
| 222 | + )} |
| 223 | + </DialogFooter> |
| 224 | + </DialogPopup> |
| 225 | + </Dialog> |
| 226 | + ); |
| 227 | +} |
0 commit comments