import { useBreakpoint } from 'common' import dayjs from 'dayjs' import { Check } from 'lucide-react' import Link from 'next/link' import { useEffect, useRef, useState } from 'react' import { Button, cn } from 'ui' import useConfData from '~/components/LaunchWeek/hooks/use-conf-data' import { useParams } from '~/hooks/useParams' import { LW_URL, TWEET_TEXT, TWEET_TEXT_PLATINUM, TWEET_TEXT_SECRET } from '~/lib/constants' export default function TicketActions2() { const { userData, supabase } = useConfData() const { platinum, username, metadata, secret: hasSecretTicket } = userData const [_imgReady, setImgReady] = useState(false) const [_loading, setLoading] = useState(false) const isLessThanMd = useBreakpoint() const downloadLink = useRef() const link = `${LW_URL}/tickets/${username}?lw=12${ hasSecretTicket ? '&secret=true' : platinum ? `&platinum=true` : '' }&t=${dayjs(new Date()).format('DHHmmss')}` const permalink = encodeURIComponent(link) const text = hasSecretTicket ? TWEET_TEXT_SECRET : platinum ? TWEET_TEXT_PLATINUM : TWEET_TEXT const encodedText = encodeURIComponent(text) const tweetUrl = `https://twitter.com/intent/tweet?url=${permalink}&text=${encodedText}` const linkedInUrl = `https://www.linkedin.com/sharing/share-offsite/?url=${permalink}&text=${encodedText}` const downloadUrl = `${process.env.NEXT_PUBLIC_SUPABASE_URL}/functions/v1/lw12-og?username=${encodeURIComponent( username ?? '' )}` const params = useParams() const sharePage = !!params.username const TICKETS_TABLE = 'tickets' useEffect(() => { setImgReady(false) const img = new Image() img.src = downloadUrl img.onload = () => { setImgReady(true) setLoading(false) if (downloadLink.current) { downloadLink.current.click() downloadLink.current = undefined } } }, [downloadUrl]) const handleShare = async (social: 'twitter' | 'linkedin') => { if (!supabase) return setTimeout(async () => { if (social === 'twitter') { await supabase .from(TICKETS_TABLE) .update({ shared_on_twitter: 'now', metadata: { ...metadata, hasSharedSecret: hasSecretTicket }, }) .eq('launch_week', 'lw12') .eq('username', username) } else if (social === 'linkedin') { await supabase .from(TICKETS_TABLE) .update({ shared_on_linkedin: 'now', metadata: { ...metadata, hasSharedSecret: hasSecretTicket }, }) .eq('launch_week', 'lw12') .eq('username', username) } if (userData.shared_on_linkedin && userData.shared_on_twitter) { await fetch(`/api-v2/ticket-og?username=${username}&platinum=true`) } }) } return (
{' '}
) }