|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import useWorkspace from "@/lib/swr/use-workspace"; |
| 4 | +import { BlurImage, ExpandingArrow } from "@dub/ui"; |
| 5 | +import { |
| 6 | + cn, |
| 7 | + DICEBEAR_AVATAR_URL, |
| 8 | + smartTruncate, |
| 9 | + STAGGER_CHILD_VARIANTS, |
| 10 | +} from "@dub/utils"; |
| 11 | +import { COUNTRIES } from "@dub/utils/src/constants/countries"; |
| 12 | +import NumberFlow from "@number-flow/react"; |
| 13 | +import { motion } from "framer-motion"; |
| 14 | +import { redirect, useParams } from "next/navigation"; |
| 15 | + |
| 16 | +export default function WrappedPageClient() { |
| 17 | + const { slug, year } = useParams(); |
| 18 | + const { name, logo, yearInReview, loading } = useWorkspace(); |
| 19 | + |
| 20 | + const { totalLinks, totalClicks, topLinks, topCountries } = |
| 21 | + yearInReview || {}; |
| 22 | + |
| 23 | + const stats = { |
| 24 | + "Total Links": totalLinks, |
| 25 | + "Total Clicks": totalClicks, |
| 26 | + }; |
| 27 | + |
| 28 | + const placeholderArray = Array.from({ length: 5 }, (_, index) => ({ |
| 29 | + item: "placeholder", |
| 30 | + count: 0, |
| 31 | + })); |
| 32 | + |
| 33 | + if (!loading && !yearInReview) { |
| 34 | + redirect(`/${slug}`); |
| 35 | + } |
| 36 | + |
| 37 | + return ( |
| 38 | + <div className="relative mx-auto my-10 max-w-lg px-4 sm:px-8"> |
| 39 | + <h1 className="animate-slide-up-fade font-display mx-0 mb-4 mt-8 p-0 text-center text-xl font-semibold text-black [animation-delay:150ms] [animation-duration:1s] [animation-fill-mode:both]"> |
| 40 | + Dub {year} Year in Review 🎊 |
| 41 | + </h1> |
| 42 | + <p className="animate-slide-up-fade text-center text-sm leading-6 text-black [animation-delay:300ms] [animation-duration:1s] [animation-fill-mode:both]"> |
| 43 | + As we put a wrap on {year}, we wanted to say thank you for your support! |
| 44 | + Here's a look back at your activity in {year}: |
| 45 | + </p> |
| 46 | + |
| 47 | + <div className="animate-slide-up-fade my-8 rounded-lg border border-neutral-200 bg-white p-2 shadow-md [animation-delay:450ms] [animation-duration:1s] [animation-fill-mode:both]"> |
| 48 | + <div |
| 49 | + className="flex h-24 flex-col items-center justify-center rounded-lg" |
| 50 | + style={{ |
| 51 | + backgroundImage: `url(https://assets.dub.co/misc/year-in-review-header.jpg)`, |
| 52 | + backgroundSize: "cover", |
| 53 | + backgroundPosition: "center", |
| 54 | + }} |
| 55 | + > |
| 56 | + {name ? ( |
| 57 | + <> |
| 58 | + <BlurImage |
| 59 | + src={logo || `${DICEBEAR_AVATAR_URL}${name}`} |
| 60 | + alt={name || "Workspace Logo"} |
| 61 | + className="h-8 rounded-full" |
| 62 | + width={32} |
| 63 | + height={32} |
| 64 | + /> |
| 65 | + <h2 className="mt-1 text-xl font-semibold">{name}</h2> |
| 66 | + </> |
| 67 | + ) : ( |
| 68 | + <> |
| 69 | + <div className="h-8 animate-pulse rounded-full bg-neutral-200" /> |
| 70 | + <div className="h-5 w-12 animate-pulse rounded-md bg-neutral-200" /> |
| 71 | + </> |
| 72 | + )} |
| 73 | + </div> |
| 74 | + <div className="grid w-full grid-cols-2 gap-2 p-4"> |
| 75 | + {Object.entries(stats).map(([key, value]) => ( |
| 76 | + <StatCard key={key} title={key} value={value} /> |
| 77 | + ))} |
| 78 | + </div> |
| 79 | + <div className="grid gap-2 p-4"> |
| 80 | + <StatTable |
| 81 | + title="Top Links" |
| 82 | + value={ |
| 83 | + topLinks |
| 84 | + ? (topLinks as { item: string; count: number }[]) |
| 85 | + : placeholderArray |
| 86 | + } |
| 87 | + /> |
| 88 | + <StatTable |
| 89 | + title="Top Countries" |
| 90 | + value={ |
| 91 | + topCountries |
| 92 | + ? (topCountries as { item: string; count: number }[]) |
| 93 | + : placeholderArray |
| 94 | + } |
| 95 | + /> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + ); |
| 100 | +} |
| 101 | + |
| 102 | +const StatCard = ({ |
| 103 | + title, |
| 104 | + value, |
| 105 | +}: { |
| 106 | + title: string; |
| 107 | + value: number | undefined; |
| 108 | +}) => { |
| 109 | + return ( |
| 110 | + <div className="text-center"> |
| 111 | + <h3 className="font-medium text-neutral-500">{title}</h3> |
| 112 | + <NumberFlow |
| 113 | + value={value || 0} |
| 114 | + className={cn( |
| 115 | + "text-lg font-medium text-black", |
| 116 | + value === undefined && "text-neutral-300", |
| 117 | + )} |
| 118 | + /> |
| 119 | + </div> |
| 120 | + ); |
| 121 | +}; |
| 122 | + |
| 123 | +const StatTable = ({ |
| 124 | + title, |
| 125 | + value, |
| 126 | +}: { |
| 127 | + title: string; |
| 128 | + value: { item: string; count: number }[]; |
| 129 | +}) => { |
| 130 | + const { slug } = useParams(); |
| 131 | + return ( |
| 132 | + <div className="mb-2"> |
| 133 | + <h3 className="mb-2 font-medium text-neutral-500">{title}</h3> |
| 134 | + <motion.div |
| 135 | + variants={{ |
| 136 | + show: { |
| 137 | + transition: { |
| 138 | + delayChildren: 0.5, |
| 139 | + staggerChildren: 0.08, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }} |
| 143 | + initial="hidden" |
| 144 | + animate="show" |
| 145 | + className="grid divide-y divide-neutral-200 text-sm" |
| 146 | + > |
| 147 | + {value.map(({ item, count }, index) => { |
| 148 | + const [domain, ...pathParts] = item.split("/"); |
| 149 | + const path = pathParts.join("/") || "_root"; |
| 150 | + return ( |
| 151 | + <motion.div |
| 152 | + key={index} |
| 153 | + variants={STAGGER_CHILD_VARIANTS} |
| 154 | + className="text-sm text-gray-500" |
| 155 | + > |
| 156 | + <a |
| 157 | + href={`/${slug}/analytics?${new URLSearchParams({ |
| 158 | + ...(title === "Top Links" |
| 159 | + ? { |
| 160 | + domain, |
| 161 | + key: path, |
| 162 | + } |
| 163 | + : { |
| 164 | + country: item, |
| 165 | + }), |
| 166 | + interval: "1y", |
| 167 | + }).toString()}`} |
| 168 | + key={index} |
| 169 | + className="group flex justify-between py-1.5" |
| 170 | + > |
| 171 | + {item === "placeholder" ? ( |
| 172 | + <div className="h-4 w-12 animate-pulse rounded-md bg-neutral-200" /> |
| 173 | + ) : ( |
| 174 | + <div className="flex items-center gap-2"> |
| 175 | + {title === "Top Countries" && ( |
| 176 | + <img |
| 177 | + src={`https://hatscripts.github.io/circle-flags/flags/${item.toLowerCase()}.svg`} |
| 178 | + alt={COUNTRIES[item]} |
| 179 | + className="size-4" |
| 180 | + /> |
| 181 | + )} |
| 182 | + <div className="flex gap-0.5"> |
| 183 | + <p className="font-medium text-black"> |
| 184 | + {title === "Top Links" |
| 185 | + ? smartTruncate(item, 33) |
| 186 | + : COUNTRIES[item]}{" "} |
| 187 | + </p> |
| 188 | + <ExpandingArrow className="size-3" /> |
| 189 | + </div> |
| 190 | + </div> |
| 191 | + )} |
| 192 | + <NumberFlow |
| 193 | + value={count} |
| 194 | + className={cn( |
| 195 | + "text-neutral-600 group-hover:text-black", |
| 196 | + count === 0 && "text-neutral-300", |
| 197 | + )} |
| 198 | + /> |
| 199 | + </a> |
| 200 | + </motion.div> |
| 201 | + ); |
| 202 | + })} |
| 203 | + </motion.div> |
| 204 | + </div> |
| 205 | + ); |
| 206 | +}; |
0 commit comments