|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import Script from "next/script"; |
| 4 | +import { Card, CardContent, CardHeader, CardTitle } from "../ui/card"; |
| 5 | +import Link from "next/link"; |
| 6 | +import { cn } from "@/lib/utils"; |
| 7 | +import { FC, useEffect, useState } from "react"; |
| 8 | +import { Skeleton } from "../ui/skeleton"; |
| 9 | + |
| 10 | +export const TwitterEmbed: FC<{ className?: string }> = ({ className }) => { |
| 11 | + const [loaded, setLoaded] = useState(false); |
| 12 | + |
| 13 | + useEffect(() => { |
| 14 | + // This useEffect is basically a hack to allow somekind of UI indication that an element is being loaded. |
| 15 | + // Complete and totally inadequate hack to cope with the fact that twitter is a zombie social media company. |
| 16 | + // The timeline embed is completely broken and does not respect requests to limit the total number of posts loaded. |
| 17 | + // As a result, this embed adds an additional ***~20mb*** to the page load. This is why we delay it. |
| 18 | + function checkIframeLoaded() { |
| 19 | + // Get a handle to the iframe element |
| 20 | + const iframeDiv = document.getElementsByClassName("twitter-timeline-rendered"); |
| 21 | + if (iframeDiv === undefined || iframeDiv.length === 0 ) { |
| 22 | + // Nothing loaded yet, wait. |
| 23 | + window.setTimeout(checkIframeLoaded, 1000); |
| 24 | + } else { |
| 25 | + // console.log(iframeDiv); |
| 26 | + // Check if the iframe element itself is loaded yet |
| 27 | + const iframeDoc = document.getElementById("twitter-widget-0"); |
| 28 | + if (iframeDoc === undefined) { |
| 29 | + window.setTimeout(checkIframeLoaded, 1000); |
| 30 | + } else { |
| 31 | + // Completely arbitrary. Seems to work OK on simulated low band 4g connections. |
| 32 | + window.setTimeout(() => setLoaded(true), 5000); |
| 33 | + return; |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + checkIframeLoaded(); |
| 38 | + }, []); |
| 39 | + |
| 40 | + return ( |
| 41 | + <Card className={cn("bg-card/60", className)}> |
| 42 | + <CardHeader className="flex justify-center"> |
| 43 | + <CardTitle className="text-lg font-medium"> |
| 44 | + <span className="font-mono font-bold">@penumbrazone</span> on Twitter |
| 45 | + </CardTitle> |
| 46 | + </CardHeader> |
| 47 | + <CardContent className="p-2 h-[320px]"> |
| 48 | + <div className="grid grid-cols-1 grid-rows-1"> |
| 49 | + <div className="col-span-1 row-span-1 col-start-1 row-start-1"> |
| 50 | + <Link |
| 51 | + id="twitter-tl" |
| 52 | + className={cn("twitter-timeline")} |
| 53 | + data-tweet-limit={5} |
| 54 | + data-limit="5" |
| 55 | + data-height="300" |
| 56 | + data-chrome="noheader" |
| 57 | + href="https://twitter.com/penumbrazone?ref_src=twsrc%5Etfw" |
| 58 | + /> |
| 59 | + </div> |
| 60 | + { !loaded ? ( |
| 61 | + <div className="col-span-1 row-span-1 col-start-1 row-start-1"> |
| 62 | + <Skeleton className={cn("w-full h-[300px] opacity-85")}/> |
| 63 | + </div> |
| 64 | + ) : null} |
| 65 | + <Script |
| 66 | + async |
| 67 | + src="https://platform.twitter.com/widgets.js" |
| 68 | + strategy="lazyOnload" |
| 69 | + data-tweet-limit="5" |
| 70 | + data-limit="5" |
| 71 | + data-height="300" |
| 72 | + data-chrome="noheader" |
| 73 | + /> |
| 74 | + </div> |
| 75 | + </CardContent> |
| 76 | + </Card> |
| 77 | + ); |
| 78 | +}; |
0 commit comments