Open History to see the last commit
1import * as React from 'react'
2import { flag } from 'country-emoji'
3import useLocalStorage from './useLocalStorage'
4import { AnimatePresence, motion } from 'framer-motion'
5import { IoIosClose } from 'react-icons/io'
6β
7function useClientOnlyRender() {
8 const [rendered, setRendered] = React.useState(false)
9 React.useEffect(() => {
10 setRendered(true)
11 }, [])
12 return rendered
13}
14export function PPPBanner() {
15 const [hidden, setHidden] = useLocalStorage('pppbanner-hidden', false)
16 const [data, setData] = useLocalStorage('pppbanner-data', null)
17β
18 React.useEffect(() => {
19 // This function has CORS configured to allow
20 // react-query.tanstack.com and tanstack.com
21 if (!data) {
22 fetch('https://ui.dev/api/ppp-discount')
23 .then(res => res.json())
24 .then(res => {
25 if (res?.code) {
26 setData(res)
27 }
28 })
29 }
30 }, [data, setData])
31β
32 if (!useClientOnlyRender()) {
33 return null
34 }
35β
36 return (
37 <AnimatePresence initial={false}>
38 {data && !hidden && (
39 <motion.div
40 initial={{ opacity: 0, height: 0 }}
41 animate={{ opacity: 1, height: 'auto' }}
42 exit={{ opacity: 0, height: 0 }}
43 className="w-full bg-coral text-white text-center py-2 relative flex items-center justify-center"
44 >
45 <p>
46 {flag(data.code)} We noticed you're in{' '}
47 <strong>{data.country}</strong>. Get{' '}
48 <strong>{data.discount * 100}% off</strong> the Official React Query
49 Course with code{' '}
50 <a
51 className="underline cursor-pointer"
52 href={`/checkout/react-query?from=tanstack&coupon_code=${data.coupon}`}
53 >
54 <strong>{data.coupon}</strong>
55 </a>
56 .
57 </p>
58 <button
59 onClick={() => setHidden(true)}
60 className="absolute right-0"
61 aria-label="Hide Banner"
62 >
63 <IoIosClose size={30} className="text-white" />
64 </button>
65 </motion.div>
66 )}
67 </AnimatePresence>
68 )
69}