11"use client" ;
22
33import dynamic from "next/dynamic" ;
4- import {
5- Drawer ,
6- DrawerClose ,
7- DrawerContent ,
8- DrawerDescription ,
9- DrawerFooter ,
10- DrawerHeader ,
11- DrawerTitle ,
12- DrawerTrigger ,
13- } from "@/components/ui/drawer" ;
14- import { Button , buttonVariants } from "@/components/ui/button" ;
15- import { type SelectBean } from "@/lib/db/beans/get-bean-details" ;
16- import { createUrlFromFormSchema } from "@/lib/beanconqueror/proto/proto-helpers" ;
17- import { type beanInformationFormSchema } from "@/lib/beanconqueror/validations/bean-information-form-schema" ;
4+
5+ import { Button } from "@/components/ui/button" ;
186import { useState } from "react" ;
19- import { getBeankLink } from "@/lib/beanlink" ;
7+ import { getBeanLink } from "@/lib/beanlink" ;
208import CopyContainer from "@/components/copy-container" ;
21- import { useMediaQuery } from "@/lib/hooks" ;
22- import {
23- Dialog ,
24- DialogContent ,
25- DialogDescription ,
26- DialogHeader ,
27- DialogTitle ,
28- DialogTrigger ,
29- } from "@/components/ui/dialog" ;
30- import { getShortShareLink } from "@/lib/share/actions" ;
9+
3110import { useToast } from "@/components/ui/use-toast" ;
3211
3312const QRCode = dynamic (
3413 ( ) => import ( "@/components/qrcode-card" ) . then ( ( module ) => module . QRCode ) ,
3514 { ssr : false } ,
3615) ;
3716
38- type BeanDetails = Awaited < SelectBean > ;
39- type Varieties = BeanDetails [ "varieties" ] ;
40-
4117/**
4218 * Maps the length value to a BeanMix value, where
4319 * 0 is unknown, 1 is single origin and 2 is a blend
@@ -51,55 +27,6 @@ function getBeanMix(
5127 return "SINGLE_ORIGIN" ;
5228}
5329
54- /**
55- * Map varieties to bean information schema
56- * @param varieties
57- */
58- function getVarietyInformation (
59- varieties : Varieties ,
60- ) : beanInformationFormSchema [ "varietyInformation" ] {
61- if ( varieties . length === 0 ) {
62- return undefined ;
63- }
64-
65- return varieties . map ( ( variety ) => ( {
66- variety : variety . name ?? undefined ,
67- country : variety . country ?? undefined ,
68- region : variety . region ?? undefined ,
69- farm : variety . farm ?? undefined ,
70- farmer : variety . farmer ?? undefined ,
71- elevation : variety . elevation ?? undefined ,
72- processing : variety . processing ?? undefined ,
73- } ) ) ;
74- }
75-
76- /**
77- * Create bean information scheme from provided BeanDetails
78- * @param bean
79- */
80- function createBeanInformationSchema (
81- bean : BeanDetails ,
82- ) : beanInformationFormSchema | null {
83- if ( bean === undefined ) return null ;
84-
85- return {
86- coffeeName : bean . name ,
87- roaster : bean . roaster . name ,
88- roastingDate : bean . roastDate ? new Date ( bean . roastDate ) : undefined ,
89- // beanRoastingType: NOT IMPLEMENTED
90- // degreeOfRoast: NOT IMPLEMENTED
91- // roast: NOT IMPLEMENTED
92- beanMix : getBeanMix ( bean . varieties . length ) ,
93- weight : bean . weight ?? undefined ,
94- cost : bean . price ?? undefined ,
95- // flavourProfile: // TODO: add this to the frontend
96- // cuppingPoints: NOT IMPLEMENTED
97- // decaffeinated: bean.isDecaf // TODO: add this too
98- notes : bean . notes ?? undefined ,
99- varietyInformation : getVarietyInformation ( bean . varieties ) ,
100- } ;
101- }
102-
10330/**
10431 * Represents a section where the Beanconqueror QR code can be rendered
10532 * @param url
@@ -111,8 +38,8 @@ function QRCodeSection({ url }: { url: string }) {
11138
11239 const onButtonClick = async ( ) => {
11340 try {
114- const beanlink = await getBeankLink ( url ) ;
115- setShareUrl ( beanlink ) ;
41+ const beanlink = await getBeanLink ( url ) ;
42+ setShareUrl ( beanlink . link ) ;
11643 setShow ( true ) ;
11744 toast ( {
11845 title : "Success" ,
@@ -142,96 +69,3 @@ function QRCodeSection({ url }: { url: string }) {
14269 </ >
14370 ) ;
14471}
145-
146- /**
147- * Container for all the share options
148- * @param bean
149- * @constructor
150- */
151- function ShareContainer ( { bean } : { bean : BeanDetails } ) {
152- const values = createBeanInformationSchema ( bean ) ;
153-
154- if ( ! values ) {
155- return null ;
156- }
157-
158- const shareUrl = createUrlFromFormSchema ( values ) ;
159- const beanstatsText = bean . isPublic
160- ? "This coffee is set to public, so the url can be shared with others."
161- : "To share this coffee via its url, you have to set it to 'public'." ;
162-
163- return (
164- < div className = { "divide-y space-y-4 max-w-xl" } >
165- < section className = { "space-y-2" } >
166- < h3 className = { "font-semibold mb-1" } > Share via Beanstats link</ h3 >
167- < div className = { "text-sm text-muted-foreground" } > { beanstatsText } </ div >
168- { bean . isPublic && (
169- < CopyContainer
170- value = { window . location . toString ( ) }
171- displayValue = { "Copy url" }
172- />
173- ) }
174- </ section >
175- < section className = { "space-y-2" } >
176- < h3 className = { "font-semibold mb-1" } > Share with Beanconqueror</ h3 >
177- < QRCodeSection url = { shareUrl } />
178- </ section >
179- </ div >
180- ) ;
181- }
182-
183- /**
184- * Share component which renders a shareable link (if public) and the beanconqueror import QR Code
185- * @param bean
186- * @constructor
187- */
188- export function ShareComponent ( { bean } : { bean : BeanDetails } ) {
189- const [ open , setOpen ] = useState < boolean > ( false ) ;
190- const isDesktop = useMediaQuery ( "(min-width: 768px)" ) ;
191-
192- if ( bean === null ) {
193- return null ;
194- }
195-
196- if ( isDesktop ) {
197- return (
198- < Dialog open = { open } onOpenChange = { setOpen } >
199- < DialogTrigger asChild >
200- < Button variant = { "outline" } size = { "sm" } >
201- Share
202- </ Button >
203- </ DialogTrigger >
204- < DialogContent className = { "sm:max-w-[425px]" } >
205- < DialogHeader >
206- < DialogTitle > Share</ DialogTitle >
207- < DialogDescription > Share this coffee.</ DialogDescription >
208- </ DialogHeader >
209- < ShareContainer bean = { bean } />
210- </ DialogContent >
211- </ Dialog >
212- ) ;
213- }
214-
215- return (
216- < Drawer open = { open } onOpenChange = { setOpen } >
217- < DrawerTrigger
218- className = { buttonVariants ( { variant : "outline" , size : "sm" } ) }
219- >
220- Share
221- </ DrawerTrigger >
222- < DrawerContent >
223- < DrawerHeader className = { "max-w-xl" } >
224- < DrawerTitle > Share</ DrawerTitle >
225- < DrawerDescription > Share this coffee.</ DrawerDescription >
226- </ DrawerHeader >
227- < DrawerFooter className = { "max-w-xl mx-auto" } >
228- < ShareContainer bean = { bean } />
229- < DrawerClose className = { "self-end" } >
230- < Button variant = "outline" > Cancel</ Button >
231- </ DrawerClose >
232- </ DrawerFooter >
233- </ DrawerContent >
234- </ Drawer >
235- ) ;
236- }
237-
0 commit comments