|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Badge } from "@/components/ui/badge"; |
| 4 | +import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"; |
| 5 | +import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; |
| 6 | +import type { ChangelogEntryDto } from "@/features/changelog/listEntriesApi"; |
| 7 | +import { cn } from "@/lib/utils"; |
| 8 | +import { format } from "date-fns"; |
| 9 | +import { ArrowUpRight } from "lucide-react"; |
| 10 | +import { useEffect, useMemo, useState } from "react"; |
| 11 | +import { createPortal } from "react-dom"; |
| 12 | +import Markdown from "react-markdown"; |
| 13 | +import rehypeRaw from "rehype-raw"; |
| 14 | +import rehypeSanitize, { defaultSchema } from "rehype-sanitize"; |
| 15 | +import remarkGfm from "remark-gfm"; |
| 16 | +import { compareVersions, parseVersion, SOURCEBOT_VERSION } from "@sourcebot/shared/client"; |
| 17 | + |
| 18 | +const VIDEO_EXTENSIONS_RE = /\.(mp4|webm|ogg|mov)$/i; |
| 19 | +const ABSOLUTE_URL_RE = /^(?:[a-z][a-z0-9+\-.]*:|\/\/|#)/i; |
| 20 | + |
| 21 | +// Allow <video> + the attributes we'll set on it. rehypeSanitize otherwise strips them. |
| 22 | +const SANITIZE_SCHEMA = { |
| 23 | + ...defaultSchema, |
| 24 | + tagNames: [...(defaultSchema.tagNames ?? []), "video", "source"], |
| 25 | + attributes: { |
| 26 | + ...defaultSchema.attributes, |
| 27 | + video: ["src", "controls", "poster", "width", "height", "className", "preload", "loop", "muted", "playsInline"], |
| 28 | + source: ["src", "type"], |
| 29 | + }, |
| 30 | +}; |
| 31 | + |
| 32 | +const buildUrlTransform = (entriesBaseUrl: string) => (url: string): string => { |
| 33 | + if (ABSOLUTE_URL_RE.test(url)) { |
| 34 | + return url; |
| 35 | + } |
| 36 | + try { |
| 37 | + return new URL(url, entriesBaseUrl).toString(); |
| 38 | + } catch { |
| 39 | + return url; |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +interface ZoomableImageProps { |
| 44 | + src: string; |
| 45 | + alt?: string | undefined; |
| 46 | +} |
| 47 | + |
| 48 | +const ZoomableImage = ({ src, alt }: ZoomableImageProps) => { |
| 49 | + const [zoomed, setZoomed] = useState(false); |
| 50 | + const [mounted, setMounted] = useState(false); |
| 51 | + |
| 52 | + // Portal target is only available after mount. |
| 53 | + useEffect(() => { |
| 54 | + setMounted(true); |
| 55 | + }, []); |
| 56 | + |
| 57 | + // Intercept Escape during zoom so the changelog dialog doesn't close along with the zoom. |
| 58 | + useEffect(() => { |
| 59 | + if (!zoomed) { |
| 60 | + return; |
| 61 | + } |
| 62 | + const handleKey = (e: KeyboardEvent) => { |
| 63 | + if (e.key === "Escape") { |
| 64 | + e.stopPropagation(); |
| 65 | + setZoomed(false); |
| 66 | + } |
| 67 | + }; |
| 68 | + document.addEventListener("keydown", handleKey, true); |
| 69 | + return () => document.removeEventListener("keydown", handleKey, true); |
| 70 | + }, [zoomed]); |
| 71 | + |
| 72 | + const overlay = ( |
| 73 | + <div |
| 74 | + className={cn( |
| 75 | + "fixed inset-0 z-[100] flex items-center justify-center bg-black/80 transition-opacity duration-200", |
| 76 | + zoomed ? "opacity-100 pointer-events-auto cursor-zoom-out" : "opacity-0 pointer-events-none" |
| 77 | + )} |
| 78 | + onClick={() => setZoomed(false)} |
| 79 | + > |
| 80 | + {/* eslint-disable-next-line @next/next/no-img-element */} |
| 81 | + <img |
| 82 | + src={src} |
| 83 | + alt={alt} |
| 84 | + className={cn( |
| 85 | + "max-w-[90vw] max-h-[90vh] object-contain rounded-lg transition-transform duration-200", |
| 86 | + zoomed ? "scale-100" : "scale-95" |
| 87 | + )} |
| 88 | + /> |
| 89 | + </div> |
| 90 | + ); |
| 91 | + |
| 92 | + return ( |
| 93 | + <> |
| 94 | + {/* eslint-disable-next-line @next/next/no-img-element */} |
| 95 | + <img |
| 96 | + src={src} |
| 97 | + alt={alt} |
| 98 | + className="cursor-zoom-in" |
| 99 | + onClick={() => setZoomed(true)} |
| 100 | + /> |
| 101 | + {mounted && createPortal(overlay, document.body)} |
| 102 | + </> |
| 103 | + ); |
| 104 | +}; |
| 105 | + |
| 106 | +interface ChangelogEntryDialogProps { |
| 107 | + entry: ChangelogEntryDto | null; |
| 108 | + entriesBaseUrl: string; |
| 109 | + open: boolean; |
| 110 | + onOpenChange: (open: boolean) => void; |
| 111 | +} |
| 112 | + |
| 113 | +export function ChangelogEntryDialog({ entry, entriesBaseUrl, open, onOpenChange }: ChangelogEntryDialogProps) { |
| 114 | + const urlTransform = useMemo(() => buildUrlTransform(entriesBaseUrl), [entriesBaseUrl]); |
| 115 | + |
| 116 | + const upgradeAvailable = useMemo(() => { |
| 117 | + if (!entry) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + const entryVersion = parseVersion(entry.version); |
| 121 | + const currentVersion = parseVersion(SOURCEBOT_VERSION); |
| 122 | + if (!entryVersion || !currentVersion) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + return compareVersions(entryVersion, currentVersion) > 0; |
| 126 | + }, [entry]); |
| 127 | + |
| 128 | + return ( |
| 129 | + <Dialog open={open} onOpenChange={onOpenChange}> |
| 130 | + <DialogContent className="max-w-2xl max-h-[90vh] overflow-hidden flex flex-col gap-0 p-0 focus:outline-none"> |
| 131 | + {entry && ( |
| 132 | + <> |
| 133 | + <DialogHeader className="px-6 pt-4 pb-4 border-b space-y-1"> |
| 134 | + <div className="flex items-center gap-2 text-xs text-muted-foreground"> |
| 135 | + <span>{format(new Date(entry.publishedAt), "MMM d")}</span> |
| 136 | + {upgradeAvailable && ( |
| 137 | + <> |
| 138 | + <span>·</span> |
| 139 | + <Tooltip> |
| 140 | + <TooltipTrigger asChild> |
| 141 | + <a |
| 142 | + href={`https://github.com/sourcebot-dev/sourcebot/releases/tag/${entry.version}`} |
| 143 | + target="_blank" |
| 144 | + rel="noopener noreferrer" |
| 145 | + > |
| 146 | + <Badge className="bg-purple-500/20 text-purple-400 border-purple-500/30 hover:bg-purple-500/30 gap-0.5"> |
| 147 | + Upgrade |
| 148 | + <ArrowUpRight className="h-3 w-3" /> |
| 149 | + </Badge> |
| 150 | + </a> |
| 151 | + </TooltipTrigger> |
| 152 | + <TooltipContent side="bottom"> |
| 153 | + <div className="grid grid-cols-[auto_auto] gap-x-3 gap-y-1 text-sm items-center"> |
| 154 | + <span className="text-muted-foreground">Current version</span> |
| 155 | + <span className="font-mono text-[11px] bg-muted rounded px-1.5 py-0.5 justify-self-start">{SOURCEBOT_VERSION}</span> |
| 156 | + <span className="text-muted-foreground">Required version</span> |
| 157 | + <span className="font-mono text-[11px] bg-muted rounded px-1.5 py-0.5 justify-self-start">{entry.version}</span> |
| 158 | + </div> |
| 159 | + </TooltipContent> |
| 160 | + </Tooltip> |
| 161 | + </> |
| 162 | + )} |
| 163 | + </div> |
| 164 | + <DialogTitle className="sr-only">{entry.title}</DialogTitle> |
| 165 | + </DialogHeader> |
| 166 | + <div className="overflow-y-auto px-6 py-5"> |
| 167 | + <div |
| 168 | + className={cn( |
| 169 | + "prose dark:prose-invert max-w-none", |
| 170 | + "prose-p:text-foreground prose-li:text-foreground prose-headings:text-foreground", |
| 171 | + "prose-headings:mt-6 prose-p:my-3 prose-img:rounded-md prose-img:my-4 prose-hr:my-6", |
| 172 | + "prose-h1:text-2xl prose-h2:text-xl prose-h3:text-lg prose-h4:text-base", |
| 173 | + "prose-headings:font-semibold", |
| 174 | + "prose-p:text-sm prose-li:text-sm", |
| 175 | + "prose-p:leading-normal prose-li:leading-normal", |
| 176 | + "prose-li:my-1 prose-ul:my-1 prose-ol:my-1 prose-li:marker:text-foreground", |
| 177 | + "prose-a:text-link prose-a:no-underline hover:prose-a:underline", |
| 178 | + "prose-blockquote:not-italic prose-blockquote:font-normal", |
| 179 | + "prose-code:before:content-none prose-code:after:content-none prose-code:font-normal", |
| 180 | + "prose-code:bg-muted prose-code:rounded prose-code:px-1.5 prose-code:py-0.5 prose-code:text-xs", |
| 181 | + "prose-pre:bg-muted prose-pre:text-foreground prose-pre:leading-snug", |
| 182 | + "[&_video]:rounded-md [&_video]:my-4 [&_video]:w-full", |
| 183 | + "[&>*:first-child]:mt-0" |
| 184 | + )} |
| 185 | + > |
| 186 | + <Markdown |
| 187 | + remarkPlugins={[remarkGfm]} |
| 188 | + rehypePlugins={[rehypeRaw, [rehypeSanitize, SANITIZE_SCHEMA]]} |
| 189 | + urlTransform={urlTransform} |
| 190 | + components={{ |
| 191 | + img: ({ src, alt }) => { |
| 192 | + if (typeof src !== "string") { |
| 193 | + return null; |
| 194 | + } |
| 195 | + if (VIDEO_EXTENSIONS_RE.test(src)) { |
| 196 | + return <video src={src} controls className="aspect-video" />; |
| 197 | + } |
| 198 | + return <ZoomableImage src={src} alt={alt} />; |
| 199 | + }, |
| 200 | + }} |
| 201 | + > |
| 202 | + {entry.bodyMarkdown} |
| 203 | + </Markdown> |
| 204 | + </div> |
| 205 | + </div> |
| 206 | + </> |
| 207 | + )} |
| 208 | + </DialogContent> |
| 209 | + </Dialog> |
| 210 | + ); |
| 211 | +} |
0 commit comments