Skip to content

A modern and elegant dashboard for network traffic visualization and analysis.

feat(web): export associated domains as a text file (#54)

luolei728c3e19a426
diff --git a/apps/web/components/features/stats/table/expanded-details.tsx b/apps/web/components/features/stats/table/expanded-details.tsx
index c843566..88b5ce8 100644
--- a/apps/web/components/features/stats/table/expanded-details.tsx
+++ b/apps/web/components/features/stats/table/expanded-details.tsx
@@ -1,10 +1,12 @@
"use client";
-import { Building2, Globe, Link2, Loader2, MapPin, Network, Server, Waypoints } from "lucide-react";
+import { Building2, Download, Globe, Link2, Loader2, MapPin, Network, Server, Waypoints } from "lucide-react";
import { useTranslations } from "next-intl";
import { CountryFlag } from "@/components/features/countries";
import { Favicon } from "@/components/common";
import { DomainPreview } from "@/components/features/domains";
+import { Button } from "@/components/ui/button";
+import { downloadTextFile, getDomainsExportFilename } from "@/lib/download-text-file";
import { getDomainColor, getIPGradient } from "@/lib/stats-utils";
import { formatBytes, formatNumber } from "@/lib/utils";
import { useCountryName } from "@/lib/i18n-country";
@@ -319,6 +321,28 @@ function DomainFallbackChips({ domains }: { domains: string[] }) {
);
}
+function ExportDomainsButton({ entityName, domains }: { entityName: string; domains: string[] }) {
+ const t = useTranslations("domains");
+
+ return (
+ <Button
+ type="button"
+ variant="ghost"
+ size="icon-sm"
+ className="ml-auto h-7 w-7 text-muted-foreground hover:text-foreground hover:bg-muted/60 dark:text-muted-foreground dark:hover:text-foreground dark:hover:bg-muted/60"
+ aria-label={t("exportDomains")}
+ title={t("exportDomains")}
+ disabled={domains.length === 0}
+ onClick={(event) => {
+ event.stopPropagation();
+ downloadTextFile(getDomainsExportFilename(entityName), domains);
+ }}
+ >
+ <Download className="size-3" />
+ </Button>
+ );
+}
+
export function DomainExpandedDetails({
domain,
richExpand = true,
@@ -462,6 +486,13 @@ export function IPExpandedDetails({
const domainsT = useTranslations("domains");
const ipsT = useTranslations("ips");
const localizedName = useCountryName();
+ const associatedDomains = !richExpand
+ ? ip.domains || []
+ : domainDetailsLoading
+ ? []
+ : domainDetails.length > 0
+ ? domainDetails.map((domain) => domain.domain)
+ : ip.domains || [];
const geo = normalizeGeoIP(ip.geoIP);
const countryCode = geo?.countryCode;
@@ -536,6 +567,7 @@ export function IPExpandedDetails({
<p className="text-xs font-medium text-muted-foreground mb-2.5 flex items-center gap-1.5">
<AssociatedDomainsTitleIcon className="h-3 w-3" />
{labels.associatedDomains}
+ <ExportDomainsButton entityName={ip.ip} domains={associatedDomains} />
</p>
<DomainFallbackChips domains={ip.domains || []} />
</div>
@@ -584,6 +616,7 @@ export function IPExpandedDetails({
<p className="text-xs font-medium text-muted-foreground mb-2.5 flex items-center gap-1.5">
<AssociatedDomainsTitleIcon className="h-3 w-3" />
{labels.associatedDomains}
+ <ExportDomainsButton entityName={ip.ip} domains={associatedDomains} />
</p>
{domainDetailsLoading ? (
<AssociatedLoadingList count={ip.domains?.length ?? 0} />
diff --git a/apps/web/lib/download-text-file.ts b/apps/web/lib/download-text-file.ts
new file mode 100644
index 0000000..6b85b7d
--- /dev/null
+++ b/apps/web/lib/download-text-file.ts
@@ -0,0 +1,26 @@
+export function getDomainsExportFilename(entityName: string, date = new Date()): string {
+ const safeName = Array.from(entityName.replace(/[<>:"/\\|?*]|\p{Cc}/gu, "_").trim())
+ .slice(0, 50)
+ .join("")
+ .replace(/^\.+|[. ]+$/g, "") || "entity";
+ const day = `${date.getFullYear()}${String(date.getMonth() + 1).padStart(2, "0")}${String(date.getDate()).padStart(2, "0")}`;
+ return `${safeName}-domains-${day}.txt`;
+}
+
+export function downloadTextFile(filename: string, lines: readonly string[]): void {
+ if (lines.length === 0) return;
+
+ const blob = new Blob([`${lines.join("\n")}\n`], { type: "text/plain;charset=utf-8" });
+ const url = URL.createObjectURL(blob);
+ const link = document.createElement("a");
+ link.href = url;
+ link.download = filename;
+ document.body.appendChild(link);
+ try {
+ link.click();
+ } finally {
+ link.remove();
+ // Let the browser start the download before releasing the object URL.
+ setTimeout(() => URL.revokeObjectURL(url), 1000);
+ }
+}
diff --git a/apps/web/messages/en.json b/apps/web/messages/en.json
index c80ef9f..a8f7dc8 100644
--- a/apps/web/messages/en.json
+++ b/apps/web/messages/en.json
@@ -156,6 +156,7 @@
"domainList": "Domains",
"ipList": "IP Addresses",
"associatedDomains": "Associated Domains",
+ "exportDomains": "Export domains",
"associatedIPs": "Associated IP Addresses",
"search": "Search domains...",
"noResults": "No domains found",
diff --git a/apps/web/messages/zh.json b/apps/web/messages/zh.json
index 4a63225..8da0ffd 100644
--- a/apps/web/messages/zh.json
+++ b/apps/web/messages/zh.json
@@ -127,6 +127,7 @@
},
"topDomains": {
"title": "热门域名",
+ "topDomains": "热门域名",
"sortByTraffic": "按流量排序",
"sortByConnections": "按连接数排序",
"viewAll": "查看全部",
@@ -155,6 +156,7 @@
"domainList": "域名",
"ipList": "IP 地址",
"associatedDomains": "关联域名",
+ "exportDomains": "导出域名",
"associatedIPs": "关联 IP 地址",
"search": "搜索域名...",
"noResults": "未找到域名",