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.

Started working on devices tab, now I just need to add some SVG icons

Steven Teyc17af1b396c9
diff --git a/components/shared/icons/devices.tsx b/components/shared/icons/devices.tsx
new file mode 100644
index 0000000000..7ef32a6de3
--- /dev/null
+++ b/components/shared/icons/devices.tsx
@@ -0,0 +1,84 @@
+export function Chrome({ className }: { className: string }) {
+ return (
+ <svg viewBox="0 0 100 100" className={className}>
+ <linearGradient
+ id="b"
+ x1="55.41"
+ x2="12.11"
+ y1="96.87"
+ y2="21.87"
+ gradientUnits="userSpaceOnUse"
+ >
+ <stop offset="0" stopColor="#1e8e3e" />
+ <stop offset="1" stopColor="#34a853" />
+ </linearGradient>
+ <linearGradient
+ id="c"
+ x1="42.7"
+ x2="86"
+ y1="100"
+ y2="25.13"
+ gradientUnits="userSpaceOnUse"
+ >
+ <stop offset="0" stopColor="#fcc934" />
+ <stop offset="1" stopColor="#fbbc04" />
+ </linearGradient>
+ <linearGradient
+ id="a"
+ x1="6.7"
+ x2="93.29"
+ y1="31.25"
+ y2="31.25"
+ gradientUnits="userSpaceOnUse"
+ >
+ <stop offset="0" stopColor="#d93025" />
+ <stop offset="1" stopColor="#ea4335" />
+ </linearGradient>
+ <path fill="url(#a)" d="M93.29 25a50 50 90 0 0-86.6 0l3 54z" />
+ <path fill="url(#b)" d="M28.35 62.5 6.7 25A50 50 90 0 0 50 100l49-50z" />
+ <path fill="url(#c)" d="M71.65 62.5 50 100a50 50 90 0 0 43.29-75H50z" />
+ <path fill="#fff" d="M50 75a25 25 90 1 0 0-50 25 25 90 0 0 0 50z" />
+ <path
+ fill="#1a73e8"
+ d="M50 69.8a19.8 19.8 90 1 0 0-39.6 19.8 19.8 90 0 0 0 39.6z"
+ />{" "}
+ </svg>
+ );
+}
+
+export function Safari({ className }: { className: string }) {
+ return (
+ <svg className={className} width="66" height="66" viewBox="0 0 66 66">
+ <path
+ fill="#C6C6C6"
+ stroke="#C6C6C6"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="0.5"
+ d="M383.29373 211.97671a31.325188 31.325188 0 0 1-31.32519 31.32519 31.325188 31.325188 0 0 1-31.32518-31.32519 31.325188 31.325188 0 0 1 31.32518-31.32519 31.325188 31.325188 0 0 1 31.32519 31.32519z"
+ paintOrder="markers stroke fill"
+ transform="translate(-318.88562 -180.59501)"
+ />
+ <path
+ fill="#4A9DED"
+ d="M380.83911 211.97671a28.870571 28.870571 0 0 1-28.87057 28.87057 28.870571 28.870571 0 0 1-28.87057-28.87057 28.870571 28.870571 0 0 1 28.87057-28.87057 28.870571 28.870571 0 0 1 28.87057 28.87057z"
+ paintOrder="markers stroke fill"
+ transform="translate(-318.88562 -180.59501)"
+ />
+ <path
+ fill="#ff5150"
+ d="m36.3834003 34.83806178-6.60095092-6.91272438 23.41607429-15.75199774z"
+ paintOrder="markers stroke fill"
+ />
+ <path
+ fill="#f1f1f1"
+ d="m36.38339038 34.83805895-6.60095092-6.91272438-16.81512624 22.66471911z"
+ paintOrder="markers stroke fill"
+ />
+ <path
+ d="m12.96732 50.59006 23.41607-15.75201 16.81513-22.66472z"
+ opacity=".243"
+ />
+ </svg>
+ );
+}
diff --git a/components/stats/charts/pie.tsx b/components/stats/charts/pie.tsx
deleted file mode 100644
index 86c33d0383..0000000000
--- a/components/stats/charts/pie.tsx
+++ /dev/null
@@ -1,40 +0,0 @@
-import { useState } from "react";
-import Pie, { ProvidedProps, PieArcDatum } from "@visx/shape/lib/shapes/Pie";
-
-export default function PieChart() {
- const [width, height] = [350, 350];
-
- const [selectedBrowser, setSelectedBrowser] = useState<string | null>(null);
-
- const donutThickness = 50;
-
- return (
- <figure>
- <svg width={width} height={height}>
- {/* <Pie
- data={
- selectedBrowser ? browsers.filter(({ label }) => label === selectedBrowser) : browsers
- }
- pieValue={usage}
- outerRadius={radius}
- innerRadius={radius - donutThickness}
- cornerRadius={3}
- padAngle={0.005}
- >
- {(pie) => (
- <AnimatedPie<BrowserUsage>
- {...pie}
- animate={animate}
- getKey={(arc) => arc.data.label}
- onClickDatum={({ data: { label } }) =>
- animate &&
- setSelectedBrowser(selectedBrowser && selectedBrowser === label ? null : label)
- }
- getColor={(arc) => getBrowserColor(arc.data.label)}
- />
- )}
- </Pie> */}
- </svg>
- </figure>
- );
-}
diff --git a/components/stats/device-svg.tsx b/components/stats/device-svg.tsx
new file mode 100644
index 0000000000..878f622362
--- /dev/null
+++ b/components/stats/device-svg.tsx
@@ -0,0 +1,31 @@
+import { Chrome, Safari } from "components/shared/icons/devices";
+
+export default function DeviceSVG({
+ display,
+ className,
+}: {
+ display: string;
+ className: string;
+}) {
+ switch (display) {
+ case "Chrome":
+ return <Chrome className={className} />;
+ case "Safari":
+ return <Safari className={className} />;
+ case "Firefox":
+ return <Chrome className={className} />;
+ case "Microsoft Edge":
+ return <Chrome className={className} />;
+ case "Opera":
+ return <Chrome className={className} />;
+ case "Bot":
+ return (
+ <img
+ src={`https://avatars.dicebear.com/api/bottts/dub.svg`}
+ className={className}
+ />
+ );
+ default:
+ return <Chrome className={className} />;
+ }
+}
diff --git a/components/stats/devices.tsx b/components/stats/devices.tsx
index 5e08ef249a..cf6ee8f0db 100644
--- a/components/stats/devices.tsx
+++ b/components/stats/devices.tsx
@@ -1,12 +1,107 @@
-import PieChart from "@/components/stats/charts/pie";
+import { StatsProps, processDeviceData, DeviceType } from "@/lib/stats";
+import BadgeSelect from "@/components/shared/badge-select";
+import { useMemo } from "react";
+import { nFormatter } from "@/lib/utils";
+import { useState, UIEvent } from "react";
+import { AnimatePresence, motion } from "framer-motion";
+import { LoadingDots } from "@/components/shared/icons";
+import DeviceSVG from "./device-svg";
+
+export default function Devices({ data: rawData }: { data: StatsProps }) {
+ const [type, setType] = useState<DeviceType>("browser");
+ const data = {
+ ...rawData,
+ locationData: useMemo(() => {
+ if (rawData?.locationData) {
+ return processDeviceData(rawData.deviceData, 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);
+ }
+ };
-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 h-[420px] overflow-scroll">
- <div className="mb-5 text-left">
- <h1 className="text-2xl dark:text-white font-semibold">Devices</h1>
- <PieChart />
+ <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">Devices</h1>
+ <BadgeSelect
+ options={["browser", "os", "bot"]}
+ selected={type}
+ // @ts-ignore
+ selectAction={setType}
+ />
+ </div>
+ <div
+ className={
+ data.locationData && data.locationData.length > 0
+ ? "grid gap-4"
+ : "h-[300px] flex justify-center items-center"
+ }
+ >
+ {data.locationData ? (
+ data.locationData.length > 0 ? (
+ data.locationData.map(({ display, 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">
+ <DeviceSVG display={display} className="w-4 h-4" />
+ <p className="text-gray-800 dark:text-gray-200 text-sm">
+ {display}
+ </p>
+ </span>
+ <motion.div
+ style={{
+ width: `${(count / data.totalClicks) * 100}%`,
+ }}
+ className="bg-green-100 dark:bg-green-900 absolute h-8 origin-left"
+ transition={{ ease: "easeOut", duration: 0.3 }}
+ initial={{ transform: "scaleX(0)" }}
+ animate={{ transform: "scaleX(1)" }}
+ />
+ </div>
+ <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>
+ )
+ ) : (
+ <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/components/stats/index.tsx b/components/stats/index.tsx
index 58d86eb526..02bb4827c7 100644
--- a/components/stats/index.tsx
+++ b/components/stats/index.tsx
@@ -29,7 +29,7 @@ export default function Stats() {
<Clicks data={data!} isValidating={isValidating} />
<div className="grid grid-cols-1 sm:grid-cols-2 gap-5">
<Locations data={data!} />
- <Devices />
+ <Devices data={data!} />
</div>
</div>
</div>
diff --git a/lib/constants.ts b/lib/constants.ts
index b2899cb15b..a891382ddb 100644
--- a/lib/constants.ts
+++ b/lib/constants.ts
@@ -13,6 +13,38 @@ export const FRAMER_MOTION_LIST_ITEM_VARIANTS = {
show: { scale: 1, opacity: 1, transition: { type: "spring" } },
};
+export const handleDeviceEdgeCases = (ua: string): string => {
+ if (ua.includes("curl")) {
+ return "Curl Request";
+ } else if (ua.includes("Slackbot")) {
+ return "Slack Bot";
+ } else if (ua.includes("Twitterbot")) {
+ return "Twitter Bot";
+ } else if (ua.includes("facebookexternalhit")) {
+ return "Facebook Bot";
+ } else if (ua.includes("LinkedInBot")) {
+ return "LinkedIn Bot";
+ } else if (ua.includes("WhatsApp")) {
+ return "WhatsApp Bot";
+ } else if (ua.includes("TelegramBot")) {
+ return "Telegram Bot";
+ } else if (ua.includes("Discordbot")) {
+ return "Discord Bot";
+ } else if (ua.includes("Googlebot")) {
+ return "Google Bot";
+ } else if (ua.includes("Baiduspider")) {
+ return "Baidu Bot";
+ } else if (ua.includes("bingbot")) {
+ return "Bing Bot";
+ } else if (ua.includes("YandexBot")) {
+ return "Yandex Bot";
+ } else if (ua.includes("DuckDuckBot")) {
+ return "DuckDuckGo Bot";
+ } else {
+ return "Unknown";
+ }
+};
+
export const COUNTRIES: { [key: string]: string } = {
AF: "Afghanistan",
AL: "Albania",
diff --git a/lib/stats.ts b/lib/stats.ts
index 3fa1697582..421de97c4e 100644
--- a/lib/stats.ts
+++ b/lib/stats.ts
@@ -1,5 +1,5 @@
import { NextRequest } from "next/server";
-import { COUNTRIES } from "@/lib/constants";
+import { COUNTRIES, handleDeviceEdgeCases } from "@/lib/constants";
export interface RawStatsProps {
geo: NextRequest["geo"];
@@ -18,7 +18,7 @@ export interface StatsProps {
city: string;
region: string;
}[];
- deviceData: { device: string; browser: string; os: string }[];
+ deviceData: { device: string; browser: string; os: string; bot: string }[];
}
export type IntervalProps = "1h" | "24h" | "7d" | "30d";
@@ -118,11 +118,24 @@ export function processData(
});
const deviceData = data.map(({ ua }) => {
- const { device, browser, os } = ua || {};
+ const { ua: uaString, device, browser, os } = ua || {};
return {
- device: device?.type || "Unknown",
- browser: browser?.name || "Unknown",
- os: os?.name || "Unknown",
+ device: device?.type
+ ? device?.type
+ : handleDeviceEdgeCases(uaString) !== "Unknown"
+ ? "Bot"
+ : "Unknown",
+ browser: browser?.name
+ ? browser?.name
+ : handleDeviceEdgeCases(uaString) !== "Unknown"
+ ? "Bot"
+ : "Unknown",
+ os: os?.name
+ ? os?.name
+ : handleDeviceEdgeCases(uaString) !== "Unknown"
+ ? "Bot"
+ : "Unknown",
+ bot: handleDeviceEdgeCases(uaString),
};
});
@@ -169,32 +182,35 @@ export const processLocationData = (
.sort((a, b) => b.count - a.count);
};
-export type DeviceType = "size" | "browser" | "os";
-
-// export const processDeviceData = (
-// data: StatsProps["deviceData"],
-// type: LocationType
-// ): LocationStatsProps[] => {
-// const countryCodeMap: { [key: string]: string } = {};
-
-// const results =
-// data && data.length > 0
-// ? data.reduce<Record<string, number>>((acc, d) => {
-// const count = acc[d[type]] || 0;
-// acc[d[type]] = count + 1;
-// countryCodeMap[d[type]] = d.countryCode;
-// return acc;
-// }, {})
-// : {};
-
-// return Object.entries(results)
-// .map(([item, count]) => ({
-// display: item,
-// code: countryCodeMap[item],
-// count,
-// }))
-// .sort((a, b) => b.count - a.count);
-// };
+export type DeviceType = "browser" | "os" | "bot";
+
+export interface DeviceStatsProps {
+ display: string;
+ count: number;
+}
+
+export const processDeviceData = (
+ data: StatsProps["deviceData"],
+ type: DeviceType
+): DeviceStatsProps[] => {
+ const results =
+ data && data.length > 0
+ ? data.reduce<Record<string, number>>((acc, d) => {
+ const count = acc[d[type]] || 0;
+ if (!(type === "bot" && d[type] === "Unknown")) {
+ acc[d[type]] = count + 1;
+ }
+ return acc;
+ }, {})
+ : {};
+
+ return Object.entries(results)
+ .map(([display, count]) => ({
+ display,
+ count,
+ }))
+ .sort((a, b) => b.count - a.count);
+};
export const dummyData: StatsProps = {
key: "test",
@@ -205,11 +221,6 @@ export const dummyData: StatsProps = {
})),
// @ts-ignore
locationData: null,
- browserData: [
- { browser: "Chrome", count: 100 },
- { browser: "Firefox", count: 50 },
- { browser: "Safari", count: 25 },
- { browser: "Edge", count: 10 },
- { browser: "Opera", count: 5 },
- ],
+ // @ts-ignore
+ deviceData: null,
};
diff --git a/package-lock.json b/package-lock.json
index 390a022a91..67a0cc1f04 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -18,7 +18,7 @@
"i": "^0.3.7",
"next": "^12.2.6-canary.8",
"npm": "^8.19.0",
- "react": "18.1.0",
+ "react": "^18.2.0",
"react-dom": "18.1.0",
"swr": "^2.0.0-beta.1",
"tailwind-scrollbar-hide": "^1.1.7",
@@ -4099,10 +4099,9 @@
}
},
"node_modules/react": {
- "version": "18.1.0",
- "resolved": "https://registry.npmjs.org/react/-/react-18.1.0.tgz",
- "integrity": "sha512-4oL8ivCz5ZEPyclFQXaNksK3adutVS8l2xzZU0cqEFrE9Sb7fC0EFK5uEk74wIreL1DERyjvsU915j1pcT2uEQ==",
- "license": "MIT",
+ "version": "18.2.0",
+ "resolved": "https://registry.npmjs.org/react/-/react-18.2.0.tgz",
+ "integrity": "sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==",
"dependencies": {
"loose-envify": "^1.1.0"
},
@@ -7258,9 +7257,9 @@
"dev": true
},
"react": {
- "version": "18.1.0",
- "resolved": "https://registry.npmjs.org/react/-/react-18.1.0.tgz",
- "integrity": "sha512-4oL8ivCz5ZEPyclFQXaNksK3adutVS8l2xzZU0cqEFrE9Sb7fC0EFK5uEk74wIreL1DERyjvsU915j1pcT2uEQ==",
+ "version": "18.2.0",
+ "resolved": "https://registry.npmjs.org/react/-/react-18.2.0.tgz",
+ "integrity": "sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==",
"requires": {
"loose-envify": "^1.1.0"
}
diff --git a/package.json b/package.json
index 146064b0f4..3bdc4af92a 100644
--- a/package.json
+++ b/package.json
@@ -19,7 +19,7 @@
"i": "^0.3.7",
"next": "^12.2.6-canary.8",
"npm": "^8.19.0",
- "react": "18.1.0",
+ "react": "^18.2.0",
"react-dom": "18.1.0",
"swr": "^2.0.0-beta.1",
"tailwind-scrollbar-hide": "^1.1.7",
diff --git a/yarn.lock b/yarn.lock
index 6e11530cfe..ec3237e7b0 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1960,10 +1960,10 @@
dependencies:
"debounce" "^1.2.1"
-"react@^16 || ^17 || ^18", "react@^16.0.0-0 || ^17.0.0-0 || ^18.0.0-0", "react@^16.11.0 || ^17.0.0 || ^18.0.0", "react@^16.3.0-0 || ^17.0.0-0 || ^18.0.0-0", "react@^16.8.0 || ^17.0.0 || ^18.0.0", "react@^16.8.0-0 || ^17.0.0-0 || ^18.0.0-0", "react@^17.0.2 || ^18.0.0-0", "react@^18.0.0", "react@^18.1.0", "react@>= 16.8.0 || 17.x.x || ^18.0.0-0", "react@>=16.13", "[email protected]":
- "integrity" "sha512-4oL8ivCz5ZEPyclFQXaNksK3adutVS8l2xzZU0cqEFrE9Sb7fC0EFK5uEk74wIreL1DERyjvsU915j1pcT2uEQ=="
- "resolved" "https://registry.npmjs.org/react/-/react-18.1.0.tgz"
- "version" "18.1.0"
+"react@^16 || ^17 || ^18", "react@^16.0.0-0 || ^17.0.0-0 || ^18.0.0-0", "react@^16.11.0 || ^17.0.0 || ^18.0.0", "react@^16.3.0-0 || ^17.0.0-0 || ^18.0.0-0", "react@^16.8.0 || ^17.0.0 || ^18.0.0", "react@^16.8.0-0 || ^17.0.0-0 || ^18.0.0-0", "react@^17.0.2 || ^18.0.0-0", "react@^18.0.0", "react@^18.1.0", "react@^18.2.0", "react@>= 16.8.0 || 17.x.x || ^18.0.0-0", "react@>=16.13":
+ "integrity" "sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ=="
+ "resolved" "https://registry.npmjs.org/react/-/react-18.2.0.tgz"
+ "version" "18.2.0"
dependencies:
"loose-envify" "^1.1.0"