11"use client" ;
22
3+ import { toPng } from "html-to-image" ;
34import { useAtom } from "jotai" ;
45import { useRouter } from "next/navigation" ;
56import * as React from "react" ;
6- import { useMemo } from "react" ;
7- import { LuDownloadCloud } from "react-icons/lu" ;
7+ import { useCallback , useMemo , useRef } from "react" ;
88
99import { planFamily } from "@/atoms/plan-family" ;
1010import { plansIds } from "@/atoms/plans-ids" ;
@@ -20,7 +20,9 @@ export function SharePlanPage({ planId }: { planId: string }) {
2020 const [ planToCopy , setPlanToCopy ] = useAtom ( planFamily ( { id : uuid } ) ) ;
2121
2222 const router = useRouter ( ) ;
23+ const captureRef = useRef < HTMLDivElement > ( null ) ;
2324
25+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
2426 const copyPlan = ( ) => {
2527 const newPlan = {
2628 id : uuid ,
@@ -41,18 +43,38 @@ export function SharePlanPage({ planId }: { planId: string }) {
4143 router . push ( `/plans/edit/${ newPlan . id } ` ) ;
4244 } , 200 ) ;
4345 } ;
46+
47+ const downloadPlan = useCallback ( ( ) => {
48+ if ( captureRef . current === null ) {
49+ return ;
50+ }
51+
52+ toPng ( captureRef . current , { cacheBust : true } )
53+ . then ( ( dataUrl ) => {
54+ const link = document . createElement ( "a" ) ;
55+ link . download = `${ plan . name } .png` ;
56+ link . href = dataUrl ;
57+ link . click ( ) ;
58+ } )
59+ . catch ( ( error : unknown ) => {
60+ console . error ( error ) ;
61+ } ) ;
62+ } , [ captureRef , plan . name ] ) ;
4463 return (
4564 < div className = "flex grow flex-col" >
4665 < div className = "flex items-center justify-center gap-4 p-2" >
4766 < Button
48- onClick = { copyPlan }
67+ onClick = { downloadPlan }
4968 className = "flex items-center justify-center gap-4 text-nowrap rounded-md text-lg"
5069 >
51- Skopiuj
52- < LuDownloadCloud />
70+ Pobierz .jpg
5371 </ Button >
5472 </ div >
55- < div className = "flex flex-col gap-2 overflow-auto p-1 scrollbar-thin scrollbar-track-sky-300 scrollbar-thumb-sky-900" >
73+
74+ < div
75+ ref = { captureRef }
76+ className = "flex flex-col gap-2 overflow-auto bg-white p-1 scrollbar-thin scrollbar-track-sky-300 scrollbar-thumb-sky-900"
77+ >
5678 { [
5779 { day : Day . MONDAY , label : "Poniedziałek" } ,
5880 { day : Day . TUESDAY , label : "Wtorek" } ,
0 commit comments