|
| 1 | +import { FOCUS_VISIBLE_OUTLINE, GRADIENT_LINK } from "@/lib/constants"; |
| 2 | +import { Portal, Transition } from "@headlessui/react"; |
| 3 | +import * as HoverCardPrimitive from "@radix-ui/react-hover-card"; |
| 4 | +import cx from "clsx"; |
| 5 | +import Image from "next/image"; |
| 6 | +import { encode } from "qss"; |
| 7 | +import React from "react"; |
| 8 | + |
| 9 | +export const StaticLinkPreview = ({ children, url }) => { |
| 10 | + const width = 200; |
| 11 | + const height = 125; |
| 12 | + const quality = 50; |
| 13 | + const layout = "fixed"; |
| 14 | + |
| 15 | + // Simplifies things by encoding our microlink params into a query string. |
| 16 | + const params = encode({ |
| 17 | + url, |
| 18 | + screenshot: true, |
| 19 | + meta: false, |
| 20 | + embed: "screenshot.url", |
| 21 | + colorScheme: "dark", |
| 22 | + "viewport.isMobile": true, |
| 23 | + "viewport.deviceScaleFactor": 1, |
| 24 | + |
| 25 | + // To capture useful content, the screenshot viewport needs to be bigger |
| 26 | + // than our images but maintain the same ratio |
| 27 | + "viewport.width": width * 3, |
| 28 | + "viewport.height": height * 3, |
| 29 | + }); |
| 30 | + |
| 31 | + const src = `https://api.microlink.io/?${params}`; |
| 32 | + |
| 33 | + const [isOpen, setOpen] = React.useState(false); |
| 34 | + // const [static, setStatic] = useState(false); |
| 35 | + |
| 36 | + // if (staticImage) setStatic(true); |
| 37 | + |
| 38 | + const [isMounted, setIsMounted] = React.useState(false); |
| 39 | + |
| 40 | + React.useEffect(() => { |
| 41 | + setIsMounted(true); |
| 42 | + }, []); |
| 43 | + |
| 44 | + return ( |
| 45 | + <> |
| 46 | + {/** |
| 47 | + * Microlink.io + next/image can take a few seconds to fetch and generate |
| 48 | + * a screenshot. The delay makes <LinkPreview> pointless. As a hacky |
| 49 | + * solution we create a second <Image> in a Portal after the component has |
| 50 | + * mounted. This <Image> triggers microlink.io + next/image so that the |
| 51 | + * image itself is ready by the time the user hovers on a <LinkPreview>. |
| 52 | + * Not concerned about the performance impact because <Image>'s are cached |
| 53 | + * after they are generated and the images themselves are tiny (< 10kb). |
| 54 | + */} |
| 55 | + {isMounted ? ( |
| 56 | + <Portal> |
| 57 | + <div className="hidden"> |
| 58 | + <Image |
| 59 | + src={src} |
| 60 | + width={width} |
| 61 | + height={height} |
| 62 | + quality={quality} |
| 63 | + layout={layout} |
| 64 | + priority={true} |
| 65 | + /> |
| 66 | + </div> |
| 67 | + </Portal> |
| 68 | + ) : null} |
| 69 | + |
| 70 | + <HoverCardPrimitive.Root |
| 71 | + openDelay={50} |
| 72 | + onOpenChange={(open) => { |
| 73 | + setOpen(open); |
| 74 | + }} |
| 75 | + > |
| 76 | + <HoverCardPrimitive.Trigger |
| 77 | + href={url} |
| 78 | + className={cx(GRADIENT_LINK, FOCUS_VISIBLE_OUTLINE)} |
| 79 | + > |
| 80 | + {children} |
| 81 | + </HoverCardPrimitive.Trigger> |
| 82 | + |
| 83 | + <HoverCardPrimitive.Content side="top" align="center" sideOffset={10}> |
| 84 | + <Transition |
| 85 | + show={isOpen} |
| 86 | + appear={true} |
| 87 | + enter="transform transition duration-300 origin-bottom ease-out" |
| 88 | + enterFrom="opacity-0 translate-y-2 scale-0" |
| 89 | + enterTo="opacity-100 translate-y-0 scale-100" |
| 90 | + className="shadow-xl rounded-xl" |
| 91 | + > |
| 92 | + <span |
| 93 | + className="block p-1 bg-white border border-transparent shadow rounded-xl hover:border-pink-500" |
| 94 | + // Unfortunate hack to remove the weird whitespace left by |
| 95 | + // next/image wrapper div |
| 96 | + // https://github.com/vercel/next.js/issues/18915 |
| 97 | + style={{ fontSize: 0 }} |
| 98 | + > |
| 99 | + <Image |
| 100 | + src={src} |
| 101 | + width={width} |
| 102 | + height={height} |
| 103 | + quality={quality} |
| 104 | + layout={layout} |
| 105 | + priority={true} |
| 106 | + className="rounded-lg" |
| 107 | + /> |
| 108 | + </span> |
| 109 | + </Transition> |
| 110 | + </HoverCardPrimitive.Content> |
| 111 | + </HoverCardPrimitive.Root> |
| 112 | + </> |
| 113 | + ); |
| 114 | +}; |
0 commit comments