From f28d23eae7422e95078f3cf1f40c271674c47dd0 Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 3 Aug 2023 21:56:12 +0800 Subject: [PATCH] feat: add analytic to shortcut detail --- web/src/components/AnalyticsDialog.tsx | 107 +-------------------- web/src/components/AnalyticsView.tsx | 127 +++++++++++++++++++++++++ web/src/pages/ShortcutDetail.tsx | 30 +++--- 3 files changed, 142 insertions(+), 122 deletions(-) create mode 100644 web/src/components/AnalyticsView.tsx diff --git a/web/src/components/AnalyticsDialog.tsx b/web/src/components/AnalyticsDialog.tsx index c0fca2b0..300a9645 100644 --- a/web/src/components/AnalyticsDialog.tsx +++ b/web/src/components/AnalyticsDialog.tsx @@ -1,6 +1,5 @@ import { Button, Modal, ModalDialog } from "@mui/joy"; -import { useEffect, useState } from "react"; -import * as api from "../helpers/api"; +import AnalyticsView from "./AnalyticsView"; import Icon from "./Icon"; interface Props { @@ -10,14 +9,6 @@ interface Props { const AnalyticsDialog: React.FC = (props: Props) => { const { shortcutId, onClose } = props; - const [analytics, setAnalytics] = useState(null); - const [selectedDeviceTab, setSelectedDeviceTab] = useState<"os" | "browser">("browser"); - - useEffect(() => { - api.getShortcutAnalytics(shortcutId).then(({ data }) => { - setAnalytics(data); - }); - }, []); return ( @@ -29,101 +20,7 @@ const AnalyticsDialog: React.FC = (props: Props) => {
- {analytics ? ( - <> -

Top Sources

-
-
-
- Source - Visitors -
-
- {analytics.referenceData.map((reference) => ( -
- - {reference.name ? ( - - {reference.name} - - ) : ( - "Direct" - )} - - {reference.count} -
- ))} -
-
-
- -
- Devices -
- - / - -
-
- -
- {selectedDeviceTab === "browser" ? ( -
-
- Browsers - Visitors -
-
- {analytics.browserData.map((reference) => ( -
- {reference.name || "Unknown"} - {reference.count} -
- ))} -
-
- ) : ( -
-
- Operating system - Visitors -
-
- {analytics.deviceData.map((device) => ( -
- {device.name || "Unknown"} - {device.count} -
- ))} -
-
- )} -
- - ) : ( -
- - loading -
- )} +
diff --git a/web/src/components/AnalyticsView.tsx b/web/src/components/AnalyticsView.tsx new file mode 100644 index 00000000..cf4968d2 --- /dev/null +++ b/web/src/components/AnalyticsView.tsx @@ -0,0 +1,127 @@ +import classNames from "classnames"; +import { useEffect, useState } from "react"; +import * as api from "../helpers/api"; +import Icon from "./Icon"; + +interface Props { + shortcutId: ShortcutId; + className?: string; +} + +const AnalyticsView: React.FC = (props: Props) => { + const { shortcutId, className } = props; + const [analytics, setAnalytics] = useState(null); + const [selectedDeviceTab, setSelectedDeviceTab] = useState<"os" | "browser">("browser"); + + useEffect(() => { + api.getShortcutAnalytics(shortcutId).then(({ data }) => { + setAnalytics(data); + }); + }, []); + + return ( +
+ {analytics ? ( + <> +
+

Top Sources

+
+
+
+ Source + Visitors +
+
+ {analytics.referenceData.map((reference) => ( +
+ + {reference.name ? ( + + {reference.name} + + ) : ( + "Direct" + )} + + {reference.count} +
+ ))} +
+
+
+
+ +
+
+ Devices +
+ + / + +
+
+ +
+ {selectedDeviceTab === "browser" ? ( +
+
+ Browsers + Visitors +
+
+ {analytics.browserData.map((reference) => ( +
+ {reference.name || "Unknown"} + {reference.count} +
+ ))} +
+
+ ) : ( +
+
+ Operating system + Visitors +
+
+ {analytics.deviceData.map((device) => ( +
+ {device.name || "Unknown"} + {device.count} +
+ ))} +
+
+ )} +
+
+ + ) : ( +
+ + loading +
+ )} +
+ ); +}; + +export default AnalyticsView; diff --git a/web/src/pages/ShortcutDetail.tsx b/web/src/pages/ShortcutDetail.tsx index ba1ed300..df0a2e50 100644 --- a/web/src/pages/ShortcutDetail.tsx +++ b/web/src/pages/ShortcutDetail.tsx @@ -5,7 +5,7 @@ import toast from "react-hot-toast"; import { useTranslation } from "react-i18next"; import { useLoaderData, useNavigate } from "react-router-dom"; import { showCommonDialog } from "../components/Alert"; -import AnalyticsDialog from "../components/AnalyticsDialog"; +import AnalyticsView from "../components/AnalyticsView"; import Dropdown from "../components/common/Dropdown"; import CreateShortcutDialog from "../components/CreateShortcutDialog"; import GenerateQRCodeDialog from "../components/GenerateQRCodeDialog"; @@ -32,7 +32,6 @@ const ShortcutDetail = () => { }); const [favicon, setFavicon] = useState(undefined); const [showQRCodeDialog, setShowQRCodeDialog] = useState(false); - const [showAnalyticsDialog, setShowAnalyticsDialog] = useState(false); const havePermission = currentUser.role === "ADMIN" || shortcut.creatorId === currentUser.id; const shortcutLink = absolutifyLink(`/s/${shortcut.name}`); @@ -70,7 +69,7 @@ const ShortcutDetail = () => { {favicon ? ( ) : ( - + )} { > Edit -