|
| 1 | + |
| 2 | +import { |
| 3 | + Dialog, |
| 4 | + DialogContent, |
| 5 | + DialogDescription, |
| 6 | + DialogFooter, |
| 7 | + DialogHeader, |
| 8 | + DialogTitle, |
| 9 | + DialogTrigger, |
| 10 | +} from "@/components/ui/dialog" |
| 11 | +import { useRef, useState } from "react"; |
| 12 | +import { Button } from "@/components/ui/button"; |
| 13 | +import type { QuoteFormData } from "@/model/quote.model"; |
| 14 | +import { ArrowLeftIcon, CircleArrowDown, LoaderCircle, Save, Share } from "lucide-react"; |
| 15 | +import useBackground, { type Pri_set, type TextureKey } from "@/hook/use-background.hook"; |
| 16 | +import { sanitizeHTML } from "@/helper/sanitize-html"; |
| 17 | +import { cn } from "@/lib/utils"; |
| 18 | +import { ListTags } from "@/feature/quote/list.tags"; |
| 19 | +import { toPng } from "html-to-image" |
| 20 | + |
| 21 | +interface Props { |
| 22 | + quoteFormData: QuoteFormData |
| 23 | +} |
| 24 | + |
| 25 | +export function ShareBackground(props: Props) { |
| 26 | + const { quoteFormData } = props |
| 27 | + const { buildStyle } = useBackground() |
| 28 | + const noteRef = useRef<HTMLDivElement>(null) |
| 29 | + const [downloading, setDownloading] = useState(false) |
| 30 | + const cardStyle = buildStyle(quoteFormData.texture!, quoteFormData.pri_set!) |
| 31 | + |
| 32 | + const exportAsImage = async () => { |
| 33 | + if (!noteRef.current) return |
| 34 | + setDownloading(true) |
| 35 | + const dataUrl = await toPng(noteRef.current, { |
| 36 | + pixelRatio: 2, // crisp image |
| 37 | + //backgroundColor: "#fff" |
| 38 | + cacheBust: true, |
| 39 | + backgroundColor: cardStyle.backgroundColor |
| 40 | + }) |
| 41 | + |
| 42 | + const link = document.createElement("a") |
| 43 | + link.download = "note-by-pri-" + new Date().getTime() + "-.png" |
| 44 | + link.href = dataUrl |
| 45 | + link.click() |
| 46 | + setTimeout(() => { |
| 47 | + setDownloading(false) |
| 48 | + }, 500); |
| 49 | + } |
| 50 | + |
| 51 | + return ( |
| 52 | + <Dialog > |
| 53 | + <DialogTrigger asChild> |
| 54 | + <Button variant="outline" size="icon"> |
| 55 | + <Share /> |
| 56 | + </Button> |
| 57 | + </DialogTrigger> |
| 58 | + |
| 59 | + <DialogContent className="p-4" aria-describedby="Choose Background"> |
| 60 | + <DialogHeader> |
| 61 | + <DialogTitle>Share</DialogTitle> |
| 62 | + <DialogDescription> |
| 63 | + Share it with your close one. |
| 64 | + </DialogDescription> |
| 65 | + </DialogHeader> |
| 66 | + <div className="flex flex-col gap-4"> |
| 67 | + |
| 68 | + {/* Live preview */} |
| 69 | + <div className="w-full rounded-lg bg-transparent overflow-auto max-h-[65dvh]" |
| 70 | + ref={noteRef} |
| 71 | + > |
| 72 | + <div |
| 73 | + style={cardStyle} |
| 74 | + className={ |
| 75 | + cn( |
| 76 | + "border p-2 bg-card rounded-xl ", |
| 77 | + "flex flex-col justify-between items-start gap-2", |
| 78 | + "", |
| 79 | + ) |
| 80 | + |
| 81 | + }> |
| 82 | + <div className=" |
| 83 | + tiptap |
| 84 | + prose-sm |
| 85 | + removed-prose |
| 86 | + removed-sm:prose-base |
| 87 | + removed-lg:prose-lg |
| 88 | + removed-xl:prose-2xl |
| 89 | + removed-prose-foreground |
| 90 | + "> {/* IMPORTANT */} |
| 91 | + <div dangerouslySetInnerHTML={{ __html: sanitizeHTML(quoteFormData.text!) }}></div> |
| 92 | + </div> |
| 93 | + <div className="flex w-full items-end justify-between gap-2"> |
| 94 | + <ListTags tags={quoteFormData?.tags?.map((tag, i) => { return { id: i, name: tag } }) || []} /> |
| 95 | + |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + |
| 100 | + {/* Action */} |
| 101 | + |
| 102 | + <div className="flex items-center justify-between gap-4 "> |
| 103 | + |
| 104 | + <div className="flex flex-col gap-4"> |
| 105 | + <Button |
| 106 | + className="" |
| 107 | + variant={"outline"} |
| 108 | + onClick={(e) => { |
| 109 | + e.preventDefault() |
| 110 | + exportAsImage() |
| 111 | + }} |
| 112 | + aria-label="Download quote" |
| 113 | + size={"lg"} |
| 114 | + > |
| 115 | + {downloading ? <LoaderCircle className="animate-spin" /> : <CircleArrowDown />} |
| 116 | + </Button> |
| 117 | + <p className="text-xs"> |
| 118 | + {downloading ? "Downloading" : "Download"} |
| 119 | + </p> |
| 120 | + </div> |
| 121 | + |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + |
| 125 | + </DialogContent> |
| 126 | + </Dialog> |
| 127 | + ); |
| 128 | +} |
0 commit comments