Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

added some nice animations :)

Steven Tey82dd11986ce0
diff --git a/components/home/link-card.tsx b/components/home/link-card.tsx
index e434cc6a51..22e7cade56 100644
--- a/components/home/link-card.tsx
+++ b/components/home/link-card.tsx
@@ -7,6 +7,7 @@ import useSWR from "swr";
import { fetcher, nFormatter, linkConstructor } from "@/lib/utils";
import Link from "next/link";
import { useStatsContext } from "@/components/stats/context";
+import { motion } from "framer-motion";
export default function LinkCard({
_key: key,
@@ -46,8 +47,11 @@ export default function LinkCard({
}, [onKeyDown]);
return (
- <div
- key={key}
+ <motion.li
+ variants={{
+ hidden: { scale: 0.8, opacity: 0 },
+ show: { scale: 1, opacity: 1, transition: { type: "spring" } },
+ }}
className="flex items-center border border-gray-200 dark:border-gray-600 hover:border-black dark:hover:border-white p-3 rounded-md transition-all"
>
<BlurImage
@@ -90,6 +94,6 @@ export default function LinkCard({
{url}
</p>
</div>
- </div>
+ </motion.li>
);
}
diff --git a/components/home/placeholder-card.tsx b/components/home/placeholder-card.tsx
index 25376d1a07..54f8f30dd1 100644
--- a/components/home/placeholder-card.tsx
+++ b/components/home/placeholder-card.tsx
@@ -1,6 +1,14 @@
+import { motion } from "framer-motion";
+
export default function PlaceholderCard() {
return (
- <div className="flex items-center border border-gray-200 dark:border-gray-600 hover:border-black dark:hover:border-white p-3 rounded-md transition-all">
+ <motion.li
+ variants={{
+ hidden: { scale: 0.8, opacity: 0 },
+ show: { scale: 1, opacity: 1, transition: { type: "spring" } },
+ }}
+ className="flex items-center border border-gray-200 dark:border-gray-600 hover:border-black dark:hover:border-white p-3 rounded-md transition-all"
+ >
<div className="w-10 h-10 rounded-full mr-2 bg-gray-200 dark:bg-gray-600 animate-pulse" />
<div>
<div className="flex items-center space-x-2 mb-2.5">
@@ -10,6 +18,6 @@ export default function PlaceholderCard() {
</div>
<div className="w-72 h-4 rounded-md bg-gray-200 dark:bg-gray-600 animate-pulse" />
</div>
- </div>
+ </motion.li>
);
}
diff --git a/components/stats/charts/bar.tsx b/components/stats/charts/bar.tsx
index 77a6d316ca..670c167d29 100644
--- a/components/stats/charts/bar.tsx
+++ b/components/stats/charts/bar.tsx
@@ -34,22 +34,20 @@ const BarChart = ({
data,
isValidating,
screenWidth,
- screenHeight,
}: {
data: StatsProps["clicksData"];
isValidating: boolean;
screenWidth?: number;
- screenHeight?: number;
}) => {
const [CHART_WIDTH, CHART_HEIGHT] = useMemo(() => {
const width = screenWidth
? Math.min(screenWidth * 0.7, CHART_MAX_WIDTH)
: CHART_MAX_WIDTH;
- const height = screenHeight
- ? Math.min(screenHeight * 0.4, CHART_MAX_HEIGHT)
+ const height = screenWidth
+ ? Math.min(screenWidth * 0.4, CHART_MAX_HEIGHT)
: CHART_MAX_HEIGHT;
return [width, height];
- }, [screenWidth, screenHeight]);
+ }, [screenWidth]);
const router = useRouter();
const interval = (router.query.interval as IntervalProps) || "7d";
diff --git a/components/stats/devices.tsx b/components/stats/devices.tsx
index a36e397b63..5e08ef249a 100644
--- a/components/stats/devices.tsx
+++ b/components/stats/devices.tsx
@@ -2,7 +2,7 @@ import PieChart from "@/components/stats/charts/pie";
export default function Devices() {
return (
- <div className="bg-white dark:bg-black px-7 py-5 shadow-lg dark:shadow-none rounded-lg border border-gray-100 dark:border-gray-600">
+ <div className="bg-white dark:bg-black px-7 py-5 shadow-lg dark:shadow-none rounded-lg border border-gray-100 dark:border-gray-600 h-[420px] overflow-scroll">
<div className="mb-5 text-left">
<h1 className="text-2xl dark:text-white font-semibold">Devices</h1>
<PieChart />
diff --git a/components/stats/index.tsx b/components/stats/index.tsx
index c4b6dad0af..58d86eb526 100644
--- a/components/stats/index.tsx
+++ b/components/stats/index.tsx
@@ -23,7 +23,7 @@ export default function Stats() {
);
return (
- <div className="relative bg-gray-50 dark:bg-black py-20">
+ <div className="relative bg-gray-50 dark:bg-black py-20 mx-auto lg:px-0 px-2.5">
<Toggle data={data!} />
<div className="max-w-4xl mx-auto grid gap-5">
<Clicks data={data!} isValidating={isValidating} />
diff --git a/components/stats/locations.tsx b/components/stats/locations.tsx
index 83690b61f9..824f9fa44a 100644
--- a/components/stats/locations.tsx
+++ b/components/stats/locations.tsx
@@ -1,15 +1,39 @@
import { StatsProps, processLocationData, LocationType } from "@/lib/stats";
import BadgeSelect from "@/components/shared/badge-select";
+import { useMemo } from "react";
import { nFormatter } from "@/lib/utils";
-import { useState } from "react";
-import { motion } from "framer-motion";
+import { useState, UIEvent } from "react";
+import { AnimatePresence, motion } from "framer-motion";
import { LoadingDots } from "../shared/icons";
-export default function Locations({ data }: { data: StatsProps }) {
+export default function Locations({ data: rawData }: { data: StatsProps }) {
const [type, setType] = useState<LocationType>("country");
+ const data = {
+ ...rawData,
+ locationData: useMemo(() => {
+ if (rawData?.locationData) {
+ return processLocationData(rawData.locationData, type);
+ } else {
+ return null;
+ }
+ }, [rawData, type]),
+ };
+
+ const [scrolled, setScrolled] = useState(false);
+
+ const handleScroll = (event: UIEvent<HTMLElement>) => {
+ if (event.currentTarget.scrollTop > 0) {
+ setScrolled(true);
+ } else {
+ setScrolled(false);
+ }
+ };
return (
- <div className="bg-white dark:bg-black px-7 py-5 shadow-lg dark:shadow-none rounded-lg border border-gray-100 dark:border-gray-600">
+ <div
+ className="relative bg-white dark:bg-black px-7 py-5 shadow-lg dark:shadow-none rounded-lg border border-gray-100 dark:border-gray-600 h-[420px] overflow-scroll scrollbar-hide"
+ onScroll={handleScroll}
+ >
<div className="mb-5 flex justify-between">
<h1 className="text-xl dark:text-white font-semibold">Locations</h1>
<BadgeSelect
@@ -19,10 +43,16 @@ export default function Locations({ data }: { data: StatsProps }) {
selectAction={setType}
/>
</div>
- <div className="grid gap-4">
+ <div
+ className={
+ data.locationData && data.locationData.length > 0
+ ? "grid gap-4"
+ : "h-[300px] flex justify-center items-center"
+ }
+ >
{data.locationData ? (
- processLocationData(data.locationData, type).map(
- ({ display, code, count }) => (
+ data.locationData.length > 0 ? (
+ data.locationData.map(({ display, code, count }) => (
<div className="flex justify-between items-center">
<div className="relative flex items-center z-10 w-full max-w-[calc(100%-3rem)]">
<span className="flex space-x-2 px-2 items-center z-10">
@@ -44,18 +74,36 @@ export default function Locations({ data }: { data: StatsProps }) {
animate={{ transform: "scaleX(1)" }}
/>
</div>
- <p className="text-gray-600 dark:text-gray-400 text-sm">
+ <p className="text-gray-600 dark:text-gray-400 text-sm z-10">
{nFormatter(count)}
</p>
</div>
- )
+ ))
+ ) : (
+ <p className="text-gray-600 dark:text-gray-400 text-sm">
+ No data available
+ </p>
)
) : (
- <div className="h-96 w-full flex justify-center items-center">
- <LoadingDots color="#71717A" />
- </div>
+ <LoadingDots color="#71717A" />
)}
</div>
+ <AnimatePresence>
+ {data.locationData && data.locationData.length > 9 && !scrolled && (
+ <motion.div
+ initial={{ opacity: 0, y: 50 }}
+ animate={{
+ opacity: 1,
+ y: 0,
+ transition: { type: "linear", duration: 0.2 },
+ }}
+ exit={{ opacity: 0, y: 50, transition: { duration: 0 } }}
+ className="absolute w-full h-8 flex justify-center items-center bottom-0 left-0 right-0 bg-gradient-to-b from-white to-gray-100 dark:from-transparent dark:to-[#060606] text-sm text-gray-500 dark:text-gray-400"
+ >
+ Show more
+ </motion.div>
+ )}
+ </AnimatePresence>
</div>
);
}
diff --git a/lib/stats.ts b/lib/stats.ts
index 2338f442b4..70b84ac84e 100644
--- a/lib/stats.ts
+++ b/lib/stats.ts
@@ -11,14 +11,12 @@ export interface StatsProps {
key: string;
totalClicks: number;
clicksData: { start: number; end: number; count: number }[];
- locationData:
- | {
- country: string;
- countryCode: string;
- city: string;
- region: string;
- }[]
- | null;
+ locationData: {
+ country: string;
+ countryCode: string;
+ city: string;
+ region: string;
+ }[];
browserData: { browser: string; count: number }[];
}
@@ -104,16 +102,17 @@ export function processData(
}));
const locationData = data.map(({ geo }) => {
- const { country, city, region } = geo || {};
+ const { country: countryCode, city, region } = geo || {};
+ const country = countryCode
+ ? COUNTRIES[countryCode]
+ ? COUNTRIES[countryCode]
+ : countryCode
+ : "Unknown";
return {
- country: country
- ? COUNTRIES[country]
- ? COUNTRIES[country]
- : country
- : "Unknown",
- countryCode: country || "Unknown",
- city: city || "Unknown",
- region: region || "Unknown",
+ country,
+ countryCode: countryCode || "Unknown",
+ city: city || country,
+ region: region || country,
};
});
@@ -170,11 +169,13 @@ export const processLocationData = (
}, {})
: {};
- return Object.entries(results).map(([item, count]) => ({
- display: item,
- code: countryCodeMap[item],
- count,
- }));
+ return Object.entries(results)
+ .map(([item, count]) => ({
+ display: item,
+ code: countryCodeMap[item],
+ count,
+ }))
+ .sort((a, b) => b.count - a.count);
};
export const dummyData: StatsProps = {
@@ -184,6 +185,7 @@ export const dummyData: StatsProps = {
...interval,
count: 0,
})),
+ // @ts-ignore
locationData: null,
browserData: [
{ browser: "Chrome", count: 100 },
diff --git a/pages/index.tsx b/pages/index.tsx
index 8efb6b87a5..5b3d0d334c 100644
--- a/pages/index.tsx
+++ b/pages/index.tsx
@@ -5,6 +5,7 @@ import LinkCard from "@/components/home/link-card";
import PlaceholderCard from "@/components/home/placeholder-card";
import { LoadingDots } from "@/components/shared/icons";
import { StatsProvider } from "@/components/stats/context";
+import { motion } from "framer-motion";
export default function Home() {
const [saving, setSaving] = useState(false);
@@ -75,9 +76,26 @@ export default function Home() {
</div>
</form>
- <div className="grid gap-2 mt-3">
- <StatsProvider>
- <LinkCard _key={"github"} url={"https://github.com/steven-tey/dub"} />
+ <StatsProvider>
+ <motion.ul
+ key={hashes.length + 1} // workaround for https://github.com/framer/motion/issues/776, add 1 to account for the demo GH link
+ initial="hidden"
+ animate="show"
+ variants={{
+ hidden: {},
+ show: {
+ transition: {
+ staggerChildren: 0.1,
+ },
+ },
+ }}
+ className="grid gap-2 mt-3"
+ >
+ <LinkCard
+ key="github"
+ _key="github"
+ url={"https://github.com/steven-tey/dub"}
+ />
{hashes.length > 0
? hashes.map(({ key, url }) => (
<LinkCard key={key} _key={key} url={url} />
@@ -85,8 +103,8 @@ export default function Home() {
: Array.from({ length: 3 }).map((_, i) => (
<PlaceholderCard key={i} />
))}
- </StatsProvider>
- </div>
+ </motion.ul>
+ </StatsProvider>
</main>
);
}