Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

improve(web): redesign analytics breakdown cards

Richie McIlroyb099c00a430c
diff --git a/apps/web/app/(org)/dashboard/analytics/components/OtherStatBox.tsx b/apps/web/app/(org)/dashboard/analytics/components/OtherStatBox.tsx
deleted file mode 100644
index 8a5f4f37f..000000000
--- a/apps/web/app/(org)/dashboard/analytics/components/OtherStatBox.tsx
+++ /dev/null
@@ -1,32 +0,0 @@
-import type { FontAwesomeIconProps } from "@fortawesome/react-fontawesome";
-import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
-import { clsx } from "clsx";
-
-interface OtherStatBoxProps {
- title: string;
- icon: FontAwesomeIconProps["icon"];
- children: React.ReactNode;
- className?: string;
-}
-
-export default function OtherStatBox({
- title,
- icon,
- children,
- className,
-}: OtherStatBoxProps) {
- return (
- <div
- className={clsx(
- className,
- "p-6 space-y-6 w-full rounded-xl border bg-gray-1 border-gray-3 h-fit",
- )}
- >
- <div className="flex gap-2 items-center">
- <FontAwesomeIcon icon={icon} className="size-4 text-gray-10" />
- <p className="text-xl font-medium text-gray-12">{title}</p>
- </div>
- {children}
- </div>
- );
-}
diff --git a/apps/web/app/(org)/dashboard/analytics/components/OtherStats.tsx b/apps/web/app/(org)/dashboard/analytics/components/OtherStats.tsx
index d2e630b12..15ebd7584 100644
--- a/apps/web/app/(org)/dashboard/analytics/components/OtherStats.tsx
+++ b/apps/web/app/(org)/dashboard/analytics/components/OtherStats.tsx
@@ -1,21 +1,31 @@
"use client";
import {
- faDesktop,
- faGlobe,
- faMobileScreen,
- faRecordVinyl,
-} from "@fortawesome/free-solid-svg-icons";
+ faAndroid,
+ faApple,
+ faEdge,
+ faLinux,
+ faUbuntu,
+ faWindows,
+} from "@fortawesome/free-brands-svg-icons";
+import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
+import clsx from "clsx";
+import getUnicodeFlagIcon from "country-flag-icons/unicode";
+import { motion } from "framer-motion";
+import {
+ ChevronRight,
+ Globe2,
+ Monitor,
+ Play,
+ Smartphone,
+ Tablet,
+} from "lucide-react";
+import Image from "next/image";
+import { useRouter } from "next/navigation";
+import { type ReactNode, useId, useState } from "react";
import type { BreakdownRow } from "../types";
-import OtherStatBox from "./OtherStatBox";
-import TableCard, {
- type BrowserRowData,
- type CapRowData,
- type CityRowData,
- type CountryRowData,
- type DeviceRowData,
- type OSRowData,
-} from "./TableCard";
+import { GlobeDoodle, LaptopDoodle, StackDoodle } from "./Doodles";
+import { countryName, formatCompact, formatPercent } from "./format";
export interface OtherStatsData {
countries: BreakdownRow[];
@@ -31,172 +41,378 @@ interface OtherStatsProps {
isLoading?: boolean;
}
-const deviceMap: Record<string, DeviceRowData["device"]> = {
- desktop: "desktop",
- mobile: "mobile",
- tablet: "tablet",
+type Row = {
+ key: string;
+ label: string;
+ icon: ReactNode;
+ views: number;
+ percentage: number;
+ onSelect?: () => void;
};
-const toCountryRow = (row: BreakdownRow): CountryRowData => ({
- countryCode: row.name,
- name: row.name,
- views: row.views,
- comments: null,
- reactions: null,
- percentage: row.percentage,
-});
-
-const toCityRow = (row: BreakdownRow): CityRowData => ({
- countryCode: row.subtitle || "",
- name: row.subtitle ? `${row.name}, ${row.subtitle}` : row.name,
- views: row.views,
- comments: null,
- reactions: null,
- percentage: row.percentage,
-});
-
-const toBrowserRow = (row: BreakdownRow): BrowserRowData => ({
- browser: browserNameToSlug(row.name),
- name: row.name,
- views: row.views,
- comments: null,
- reactions: null,
- percentage: row.percentage,
-});
-
-const toOSRow = (row: BreakdownRow): OSRowData => ({
- os: osNameToKey(row.name),
- name: row.name,
- views: row.views,
- comments: null,
- reactions: null,
- percentage: row.percentage,
-});
-
-const toCapRow = (row: BreakdownRow & { id?: string }): CapRowData => ({
- name: row.name,
- views: row.views,
- comments: null,
- reactions: null,
- percentage: row.percentage,
- id: row.id,
-});
-
-const browserNameToSlug = (name: string): BrowserRowData["browser"] => {
- switch (name.toLowerCase()) {
- case "chrome":
- return "google-chrome";
- case "firefox":
- return "firefox";
- case "safari":
- return "safari";
- case "edge":
- case "internet explorer":
- return "explorer";
- case "opera":
- return "opera";
- case "brave":
- return "brave";
- default:
- return "google-chrome";
- }
+type Tab = {
+ id: string;
+ label: string;
+ rows: Row[];
};
-const osNameToKey = (name: string): OSRowData["os"] => {
- const normalized = name.toLowerCase().trim();
- if (normalized.includes("mac") || normalized === "ios") {
- return "ios";
+const flag = (code: string | null | undefined) => {
+ if (!code || !/^[A-Za-z]{2}$/.test(code.trim())) {
+ return <Globe2 className="size-4 text-gray-10" />;
}
- switch (normalized) {
- case "linux":
- return "linux";
- case "ubuntu":
- return "ubuntu";
- case "fedora":
- return "fedora";
- default:
- return "windows";
+ return (
+ <span className="text-[15px] leading-none">
+ {getUnicodeFlagIcon(code.trim().toUpperCase())}
+ </span>
+ );
+};
+
+const BROWSER_LOGOS: Record<string, string> = {
+ chrome: "google-chrome",
+ firefox: "firefox",
+ safari: "safari",
+ opera: "opera",
+ brave: "brave",
+ vivaldi: "vivaldi",
+ yandex: "yandex",
+ duckduckgo: "duckduckgo",
+ "internet explorer": "explorer",
+};
+
+const browserIcon = (name: string) => {
+ const lower = name.toLowerCase();
+ if (lower.includes("edge")) {
+ return <FontAwesomeIcon icon={faEdge} className="size-4 text-[#0f7cd8]" />;
}
+ const logo = BROWSER_LOGOS[lower];
+ if (!logo) return <Globe2 className="size-4 text-gray-10" />;
+ return (
+ <Image
+ src={`/logos/browsers/${logo}.svg`}
+ alt=""
+ width={16}
+ height={16}
+ className="size-4"
+ />
+ );
+};
+
+const osIcon = (name: string) => {
+ const lower = name.toLowerCase();
+ if (lower.includes("mac") || lower === "ios")
+ return <FontAwesomeIcon icon={faApple} className="size-4 text-gray-12" />;
+ if (lower.includes("windows"))
+ return (
+ <FontAwesomeIcon icon={faWindows} className="size-4 text-[#0078d4]" />
+ );
+ if (lower.includes("android"))
+ return (
+ <FontAwesomeIcon icon={faAndroid} className="size-4 text-[#3ddc84]" />
+ );
+ if (lower.includes("ubuntu"))
+ return (
+ <FontAwesomeIcon icon={faUbuntu} className="size-4 text-[#e95420]" />
+ );
+ if (lower.includes("linux") || lower.includes("fedora"))
+ return <FontAwesomeIcon icon={faLinux} className="size-4 text-gray-12" />;
+ return <Monitor className="size-4 text-gray-10" />;
};
+const deviceIcon = (name: string) => {
+ const lower = name.toLowerCase();
+ if (lower === "mobile") return <Smartphone className="size-4 text-gray-11" />;
+ if (lower === "tablet") return <Tablet className="size-4 text-gray-11" />;
+ return <Monitor className="size-4 text-gray-11" />;
+};
+
+const toRows = (
+ rows: BreakdownRow[],
+ map: (row: BreakdownRow) => Pick<Row, "label" | "icon">,
+): Row[] =>
+ rows.map((row, i) => ({
+ key: `${row.name}-${row.subtitle ?? ""}-${i}`,
+ views: row.views,
+ percentage: row.percentage,
+ ...map(row),
+ }));
+
export default function OtherStats({ data, isLoading }: OtherStatsProps) {
+ const router = useRouter();
+
+ const locationTabs: Tab[] = [
+ {
+ id: "country",
+ label: "Country",
+ rows: toRows(data.countries, (row) => ({
+ label: countryName(row.name),
+ icon: flag(row.name),
+ })),
+ },
+ {
+ id: "city",
+ label: "City",
+ rows: toRows(data.cities, (row) => ({
+ label: row.subtitle
+ ? `${row.name}, ${countryName(row.subtitle)}`
+ : row.name,
+ icon: flag(row.subtitle),
+ })),
+ },
+ ];
+
+ const techTabs: Tab[] = [
+ {
+ id: "browser",
+ label: "Browser",
+ rows: toRows(data.browsers, (row) => ({
+ label: row.name,
+ icon: browserIcon(row.name),
+ })),
+ },
+ {
+ id: "os",
+ label: "OS",
+ rows: toRows(data.operatingSystems, (row) => ({
+ label: row.name,
+ icon: osIcon(row.name),
+ })),
+ },
+ {
+ id: "device",
+ label: "Device",
+ rows: toRows(data.deviceTypes, (row) => ({
+ label: row.name,
+ icon: deviceIcon(row.name),
+ })),
+ },
+ ];
+
+ const topCaps = data.topCaps;
+
return (
- <div className="grid grid-cols-1 gap-8 w-full xl:grid-cols-4">
- <OtherStatBox className="col-span-2" title="Geography" icon={faGlobe}>
- <div className="flex flex-col flex-1 gap-5 justify-center w-full">
- <TableCard
- title="Countries"
- columns={["Country", "Views", "Percentage"]}
- rows={data.countries.map(toCountryRow)}
- type="country"
- isLoading={isLoading}
- />
- <TableCard
- title="Cities"
- columns={["City", "Views", "Percentage"]}
- rows={data.cities.map(toCityRow)}
- type="city"
- isLoading={isLoading}
- />
- </div>
- </OtherStatBox>
- <OtherStatBox className="col-span-2" title="Software" icon={faDesktop}>
- <div className="flex flex-col flex-1 gap-5 justify-center w-full">
- <TableCard
- title="Browsers"
- columns={["Browser", "Views", "Percentage"]}
- rows={data.browsers.map(toBrowserRow)}
- type="browser"
- isLoading={isLoading}
- />
- <TableCard
- title="Operating Systems"
- columns={["Operating System", "Views", "Percentage"]}
- rows={data.operatingSystems.map(toOSRow)}
- type="os"
- isLoading={isLoading}
- />
- </div>
- </OtherStatBox>
- <OtherStatBox
- className="col-span-2"
- title="Devices"
- icon={faMobileScreen}
- >
- <div className="flex flex-col flex-1 gap-5 justify-center w-full">
- <TableCard
- title="Device Type"
- columns={["Device", "Views", "Percentage"]}
- rows={data.deviceTypes.map((device) => ({
- device: deviceMap[device.name.toLowerCase()] ?? "desktop",
- name: device.name,
- views: device.views,
- comments: null,
- reactions: null,
- percentage: device.percentage,
- }))}
- type="device"
- isLoading={isLoading}
- />
- </div>
- </OtherStatBox>
- {data.topCaps && data.topCaps.length > 0 && (
- <OtherStatBox
- className="col-span-2"
- title="Top Caps"
- icon={faRecordVinyl}
- >
- <div className="flex flex-col flex-1 gap-5 justify-center w-full">
- <TableCard
- title="Caps"
- columns={["Name", "Views", "Percentage"]}
- rows={data.topCaps.map(toCapRow)}
- type="cap"
- isLoading={isLoading}
- />
- </div>
- </OtherStatBox>
+ <div className="grid grid-cols-1 gap-5 w-full lg:grid-cols-2">
+ <BreakdownCard
+ tabs={locationTabs}
+ isLoading={isLoading}
+ empty={{
+ doodle: <GlobeDoodle />,
+ title: "Nowhere yet",
+ body: "Countries and cities show up as people watch.",
+ }}
+ />
+ <BreakdownCard
+ tabs={techTabs}
+ isLoading={isLoading}
+ empty={{
+ doodle: <LaptopDoodle />,
+ title: "No devices yet",
+ body: "Browsers, systems and devices show up as people watch.",
+ }}
+ />
+ {topCaps && (
+ <BreakdownCard
+ className="lg:col-span-2"
+ tabs={[
+ {
+ id: "caps",
+ label: "Top Caps",
+ rows: topCaps.map((row, i) => ({
+ key: row.id ?? `${row.name}-${i}`,
+ label: row.name,
+ icon: (
+ <span className="flex justify-center items-center rounded-md size-5 bg-gray-3">
+ <Play className="size-2.5 fill-gray-11 text-gray-11" />
+ </span>
+ ),
+ views: row.views,
+ percentage: row.percentage,
+ onSelect: row.id
+ ? () => router.push(`/dashboard/analytics?capId=${row.id}`)
+ : undefined,
+ })),
+ },
+ ]}
+ isLoading={isLoading}
+ empty={{
+ doodle: <StackDoodle />,
+ title: "No Caps watched yet",
+ body: "Your most watched Caps will line up here.",
+ }}
+ />
)}
</div>
);
}
+
+function BreakdownCard({
+ tabs,
+ isLoading,
+ empty,
+ className,
+}: {
+ tabs: Tab[];
+ isLoading?: boolean;
+ empty: { doodle: ReactNode; title: string; body: string };
+ className?: string;
+}) {
+ const [activeId, setActiveId] = useState(tabs[0]?.id ?? "");
+ const layoutId = useId();
+ const active = tabs.find((t) => t.id === activeId) ?? tabs[0];
+ const rows = active?.rows ?? [];
+ const max = rows.reduce((m, r) => Math.max(m, r.views), 0);
+
+ return (
+ <section
+ className={clsx(
+ "flex flex-col w-full rounded-2xl border shadow-[0_1px_2px_rgba(0,0,0,0.04)] bg-gray-1 border-gray-4",
+ tabs.length > 1 && "min-h-[460px]",
+ className,
+ )}
+ >
+ <div className="flex justify-between items-center px-3 pt-3 pb-2">
+ {tabs.length === 1 ? (
+ <h3 className="px-3 py-1.5 text-[15px] font-medium text-gray-12">
+ {tabs[0]?.label}
+ </h3>
+ ) : (
+ <div
+ className="flex gap-0.5 items-center p-1 rounded-xl bg-gray-3"
+ role="tablist"
+ >
+ {tabs.map((tab) => {
+ const selected = tab.id === active?.id;
+ return (
+ <button
+ key={tab.id}
+ type="button"
+ role="tab"
+ aria-selected={selected}
+ onClick={() => setActiveId(tab.id)}
+ className={clsx(
+ "relative px-3 py-1 text-[13px] font-medium rounded-lg transition-colors outline-none focus-visible:ring-2 focus-visible:ring-blue-9/40",
+ selected
+ ? "text-gray-12"
+ : "text-gray-10 hover:text-gray-12",
+ )}
+ >
+ {selected && (
+ <motion.span
+ layoutId={layoutId}
+ className="absolute inset-0 rounded-lg border shadow-sm bg-gray-1 border-gray-4"
+ transition={{
+ type: "spring",
+ stiffness: 500,
+ damping: 38,
+ }}
+ />
+ )}
+ <span className="relative">{tab.label}</span>
+ </button>
+ );
+ })}
+ </div>
+ )}
+ <span className="pr-3 text-[13px] font-medium text-gray-10">Views</span>
+ </div>
+
+ <div className="flex flex-col flex-1 px-2 pb-3">
+ {isLoading ? (
+ <div className="flex flex-col gap-1 pt-1">
+ {[86, 64, 48, 36, 28, 20].map((w) => (
+ <div key={w} className="relative h-10 rounded-lg">
+ <div
+ className="absolute inset-y-0 left-0 rounded-lg animate-pulse bg-gray-3"
+ style={{ width: `${w}%` }}
+ />
+ </div>
+ ))}
+ </div>
+ ) : rows.length === 0 ? (
+ <div className="flex flex-col flex-1 justify-center items-center px-6 pb-6 text-center">
+ {empty.doodle}
+ <p className="mt-3 text-[15px] font-medium text-gray-12">
+ {empty.title}
+ </p>
+ <p className="mt-1 max-w-[260px] text-[13px] text-gray-10 text-pretty">
+ {empty.body}
+ </p>
+ </div>
+ ) : (
+ <ul key={active?.id} className="flex flex-col gap-1 pt-1">
+ {rows.map((row, i) => (
+ <BreakdownRowItem key={row.key} row={row} max={max} index={i} />
+ ))}
+ </ul>
+ )}
+ </div>
+ </section>
+ );
+}
+
+function BreakdownRowItem({
+ row,
+ max,
+ index,
+}: {
+ row: Row;
+ max: number;
+ index: number;
+}) {
+ const width = max > 0 ? Math.max(2, (row.views / max) * 100) : 0;
+ const content = (
+ <>
+ <motion.span
+ className="absolute inset-y-0 left-0 rounded-lg bg-[#4785ff]/[0.09] transition-colors group-hover:bg-[#4785ff]/[0.16] dark:bg-[#4785ff]/[0.16] dark:group-hover:bg-[#4785ff]/[0.24]"
+ initial={{ width: 0 }}
+ animate={{ width: `${width}%` }}
+ transition={{
+ duration: 0.6,
+ delay: index * 0.035,
+ ease: [0.3, 0.9, 0.4, 1],
+ }}
+ />
+ <span className="flex relative z-[1] flex-1 gap-2.5 items-center min-w-0">
+ <span className="flex flex-shrink-0 justify-center items-center size-5">
+ {row.icon}
+ </span>
+ <span className="text-sm truncate text-gray-12">{row.label}</span>
+ {row.onSelect && (
+ <ChevronRight className="flex-shrink-0 opacity-0 transition-opacity size-3.5 text-gray-10 group-hover:opacity-100" />
+ )}
+ </span>
+ <span className="flex relative z-[1] flex-shrink-0 gap-2 items-baseline pl-3 tabular-nums">
+ <span className="text-xs opacity-0 transition-opacity text-gray-10 group-hover:opacity-100">
+ {formatPercent(row.percentage)}
+ </span>
+ <span className="text-sm font-medium text-gray-12">
+ {formatCompact(row.views)}
+ </span>
+ </span>
+ </>
+ );
+
+ const rowClass =
+ "group relative flex items-center w-full h-10 px-3 rounded-lg text-left overflow-hidden";
+
+ return (
+ <li>
+ {row.onSelect ? (
+ <button
+ type="button"
+ onClick={row.onSelect}
+ className={clsx(
+ rowClass,
+ "cursor-pointer outline-none focus-visible:ring-2 focus-visible:ring-blue-9/40",
+ )}
+ title={row.label}
+ >
+ {content}
+ </button>
+ ) : (
+ <div className={rowClass} title={row.label}>
+ {content}
+ </div>
+ )}
+ </li>
+ );
+}
diff --git a/apps/web/app/(org)/dashboard/analytics/components/TableCard.tsx b/apps/web/app/(org)/dashboard/analytics/components/TableCard.tsx
deleted file mode 100644
index 8a3034ec8..000000000
--- a/apps/web/app/(org)/dashboard/analytics/components/TableCard.tsx
+++ /dev/null
@@ -1,331 +0,0 @@
-"use client";
-
-import {
- LogoBadge,
- Table,
- TableBody,
- TableCell,
- TableHead,
- TableHeader,
- TableRow,
-} from "@cap/ui";
-import {
- faAppleWhole,
- faDesktop,
- faMobileScreen,
- faTablet,
-} from "@fortawesome/free-solid-svg-icons";
-import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
-import clsx from "clsx";
-import getUnicodeFlagIcon from "country-flag-icons/unicode";
-import Image from "next/image";
-import { useRouter } from "next/navigation";
-
-const countryCodeToIcon = (countryCode: string | undefined | null) => {
- if (!countryCode || countryCode.trim() === "") {
- return null;
- }
- return getUnicodeFlagIcon(countryCode.toUpperCase());
-};
-
-const formatNumber = (value?: number | null) =>
- value == null ? "—" : value.toLocaleString();
-const formatPercentage = (value?: number | null) =>
- value == null ? "—" : `${Math.round(value * 100)}%`;
-const skeletonBar = (width = 48) => (
- <div
- className="h-4 rounded bg-gray-4 animate-pulse"
- style={{ width: `${width}px` }}
- />
-);
-
-type BrowserType =
- | "google-chrome"
- | "firefox"
- | "safari"
- | "explorer"
- | "opera"
- | "brave"
- | "vivaldi"
- | "yandex"
- | "duckduckgo"
- | "internet-explorer"
- | "samsung-internet"
- | "uc-browser"
- | "qq-browser"
- | "maxthon"
- | "arora"
- | "lunascape";
-
-type OperatingSystemType = "windows" | "ios" | "linux" | "fedora" | "ubuntu";
-
-type DeviceType = "desktop" | "tablet" | "mobile";
-
-export interface CountryRowData {
- countryCode: string;
- name: string;
- views: number;
- comments?: number | null;
- reactions?: number | null;
- percentage: number;
-}
-
-export interface CityRowData {
- countryCode: string;
- name: string;
- views: number;
- comments?: number | null;
- reactions?: number | null;
- percentage: number;
-}
-
-export interface BrowserRowData {
- browser: BrowserType;
- name: string;
- views: number;
- comments?: number | null;
- reactions?: number | null;
- percentage: number;
-}
-
-export interface OSRowData {
- os: OperatingSystemType;
- name: string;
- views: number;
- comments?: number | null;
- reactions?: number | null;
- percentage: number;
-}
-
-export interface DeviceRowData {
- device: DeviceType;
- name: string;
- views: number;
- comments?: number | null;
- reactions?: number | null;
- percentage: number;
-}
-
-export interface CapRowData {
- name: string;
- views: number;
- comments?: number | null;
- reactions?: number | null;
- percentage: number;
- id?: string;
-}
-
-export interface TableCardProps {
- title: string;
- columns: string[];
- tableClassname?: string;
- type: "country" | "city" | "browser" | "os" | "device" | "cap";
- rows:
- | CountryRowData[]
- | CityRowData[]
- | BrowserRowData[]
- | OSRowData[]
- | DeviceRowData[]
- | CapRowData[];
- isLoading?: boolean;
-}
-
-const TableCard = ({
- title: _title,
- columns,
- rows,
- type,
- tableClassname,
- isLoading,
-}: TableCardProps) => {
- const router = useRouter();
- const hasRows = rows.length > 0;
- const placeholders = Array.from({ length: 4 }, (_, index) => ({
- name: `placeholder-${index}`,
- views: 0,
- comments: null,
- reactions: null,
- percentage: 0,
- })) as TableCardProps["rows"];
-
- const displayRows = isLoading || !hasRows ? placeholders : rows;
- const showSkeletons = isLoading || !hasRows;
-
- const handleCapNameClick = (capId: string | undefined) => {
- if (capId && type === "cap") {
- router.push(`/dashboard/analytics?capId=${capId}`);
- }
- };
-
- return (
- <div className="relative p-5 w-full rounded-xl border bg-gray-2 border-gray-4 min-h-[400px] max-h-[400px] overflow-y-auto">
- {/* <div className="flex flex-1 gap-2 justify-between items-center h-[48px]">
- <p className="text-lg font-medium text-gray-12">{title}</p>
- <Select
- variant="light"
- placeholder="Views"
- icon={<FontAwesomeIcon icon={faFilter} className="text-gray-11" />}
- options={[
- { value: "views", label: "Views" },
- { value: "comments", label: "Comments" },
- { value: "reactions", label: "Reactions" },
- ]}
- value="views"
- onValueChange={() => {}}
- size="sm"
- />
- </div> */}
- <div className="flex flex-1 flex-col gap-4 justify-center">
- <div className="relative w-full">
- <Table className="w-full border-separate table-fixed border-spacing-y-2">
- <TableHeader className="sticky top-0 z-10 bg-gray-2 text-xs uppercase text-gray-11 opacity-70">
- <TableRow>
- {columns.map((column) => (
- <TableHead className="px-3 text-left" key={column}>
- {column}
- </TableHead>
- ))}
- </TableRow>
- </TableHeader>
- <TableBody
- className={clsx(
- "before:content-[''] before:block w-full",
- tableClassname,
- )}
- >
- {displayRows.map((row, index) => {
- const capRow = type === "cap" ? (row as CapRowData) : null;
- const uniqueKey = `${row.name ?? `row-${index}`}-${index}`;
- const isCap = type === "cap";
- const isClickable = isCap && capRow?.id && !showSkeletons;
- return (
- <TableRow className="w-full" key={uniqueKey}>
- <TableCell className="p-2.5 text-sm rounded-l-lg border-l border-y text-gray-11 bg-gray-3 border-gray-5">
- <div className="flex gap-2 items-center min-w-0">
- <span className="flex-shrink-0 fill-[var(--gray-12)]">
- {showSkeletons ? (
- <div className="size-4 rounded bg-gray-4 animate-pulse" />
- ) : (
- getIconForRow(row, type)
- )}
- </span>
- {isClickable ? (
- <button
- type="button"
- onClick={() => handleCapNameClick(capRow?.id)}
- className="truncate text-left hover:text-gray-12 transition-colors cursor-pointer"
- title={row.name}
- >
- {row.name}
- </button>
- ) : (
- <span
- className={clsx(
- "truncate",
- isCap && !showSkeletons && "cursor-default",
- )}
- title={
- isCap && !showSkeletons ? row.name : undefined
- }
- >
- {showSkeletons ? (
- <div className="h-4 w-24 rounded bg-gray-4 animate-pulse" />
- ) : (
- row.name
- )}
- </span>
- )}
- </div>
- </TableCell>
- <TableCell className="py-2.5 px-3 text-sm text-nowrap text-gray-11 bg-gray-3 border-y border-gray-5 whitespace-nowrap">
- {showSkeletons
- ? skeletonBar(48)
- : formatNumber(row.views)}
- </TableCell>
- <TableCell className="p-2.5 px-3 text-sm text-nowrap rounded-r-lg border-r text-gray-11 bg-gray-3 border-y border-gray-5 whitespace-nowrap">
- {showSkeletons
- ? skeletonBar(40)
- : formatPercentage(row.percentage)}
- </TableCell>
- </TableRow>
- );
- })}
- </TableBody>
- </Table>
- </div>
- </div>
- </div>
- );
-};
-
-const getIconForRow = (
- row:
- | CountryRowData
- | CityRowData
- | BrowserRowData
- | OSRowData
- | DeviceRowData
- | CapRowData,
- type: TableCardProps["type"],
-) => {
- switch (type) {
- case "country":
- return countryCodeToIcon((row as CountryRowData).countryCode);
- case "city":
- return countryCodeToIcon((row as CityRowData).countryCode);
- case "browser":
- return <BrowserIcon browser={(row as BrowserRowData).browser} />;
- case "os":
- return <OperatingSystemIcon operatingSystem={(row as OSRowData).os} />;
- case "device": {
- const device = (row as DeviceRowData).device;
- const iconMap = {
- desktop: faDesktop,
- tablet: faTablet,
- mobile: faMobileScreen,
- };
- return (
- <FontAwesomeIcon icon={iconMap[device]} className="text-gray-12" />
- );
- }
- case "cap":
- return <LogoBadge className="size-4" />;
- default:
- return null;
- }
-};
-
-type OperatingSystemIconProps = {
- operatingSystem: OperatingSystemType;
-};
-
-const OperatingSystemIcon = ({ operatingSystem }: OperatingSystemIconProps) => {
- if (operatingSystem === "ios") {
- return <FontAwesomeIcon className="text-gray-12" icon={faAppleWhole} />;
- }
- return (
- <Image
- src={`/logos/os/${operatingSystem}.svg`}
- alt={operatingSystem}
- width={16}
- height={16}
- className="size-4"
- />
- );
-};
-
-type BrowserIconProps = {
- browser: BrowserType;
-};
-
-const BrowserIcon = ({ browser }: BrowserIconProps) => (
- <Image
- src={`/logos/browsers/${browser}.svg`}
- alt={browser}
- width={16}
- height={16}
- className="size-4"
- />
-);
-
-export default TableCard;