Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Refactoring

Blue Mouse80370f2a1817
diff --git a/web/app/lib/models/Entry.ts b/web/app/lib/models/Entry.ts
index c8c6250ac..85faea4b6 100644
--- a/web/app/lib/models/Entry.ts
+++ b/web/app/lib/models/Entry.ts
@@ -1,5 +1,5 @@
export interface Entry {
- name: string | null
+ name: string
count: number
cc?: string
rgc?: string
diff --git a/web/app/pages/Project/View/Panels.tsx b/web/app/pages/Project/View/Panels.tsx
index 769c0e0b9..a1c2662b1 100644
--- a/web/app/pages/Project/View/Panels.tsx
+++ b/web/app/pages/Project/View/Panels.tsx
@@ -1129,7 +1129,7 @@ const DetailsTable = ({
},
)}
onClick={() => {
- if (disableRowClick) return
+ if (disableRowClick || entryName === null) return
const link = getFilterLink(id, entryName)
if (link) {
navigate(link)
@@ -1257,7 +1257,8 @@ const Panel = ({
})
}
- const hasVersions = (itemName: string) => {
+ const hasVersions = (itemName: string | null) => {
+ if (itemName === null) return false
return versionData && versionData[itemName] && versionData[itemName].length > 0
}
@@ -1296,7 +1297,7 @@ const Panel = ({
const isExpanded = expandedItems.has(entryName)
const versions = versionData?.[entryName] || []
- const link = getFilterLink(id, entryName)
+ const link = entryName === null ? undefined : getFilterLink(id, entryName)
return (
<div key={`${id}-${entryName}-${Object.values(rest).join('-')}`} className='space-y-0.5'>
diff --git a/web/app/pages/Project/View/ViewProject.helpers.tsx b/web/app/pages/Project/View/ViewProject.helpers.tsx
index 154be90bd..934c3efd6 100644
--- a/web/app/pages/Project/View/ViewProject.helpers.tsx
+++ b/web/app/pages/Project/View/ViewProject.helpers.tsx
@@ -203,7 +203,7 @@ const onCSVExportClick = (
const perc = _round((entry.count / total) * 100 || 0, 2)
if (item === 'cc') {
- const name = countries.getName(entry.name, language)
+ const name = countries.getName(entry.name || '', language)
return [`"${name}"`, entry.count, `${perc}%`]
}
diff --git a/web/app/pages/Project/View/components/RefRow.tsx b/web/app/pages/Project/View/components/RefRow.tsx
index b8ab90087..d81b6627a 100644
--- a/web/app/pages/Project/View/components/RefRow.tsx
+++ b/web/app/pages/Project/View/components/RefRow.tsx
@@ -4,31 +4,28 @@ import { useTranslation } from 'react-i18next'
import { REFERRER_MAP, extractHostname } from '~/utils/referrers'
-const RefRow = ({ rowName }: { rowName: string }) => {
+const RefRow = ({ rowName }: { rowName: string | null }) => {
const { t } = useTranslation('common')
const { isUrl, faviconHost } = useMemo(() => {
- // Try parse as URL
+ if (!rowName) return { isUrl: false, faviconHost: null as string | null }
+
try {
- const urlObj = new URL(rowName as string)
+ const urlObj = new URL(rowName)
return { isUrl: true, faviconHost: urlObj.hostname }
} catch {
- // Not a full URL. Try to resolve a hostname for grouped names/domains
- const hostFromValue = extractHostname(rowName as string)
+ const hostFromValue = extractHostname(rowName)
if (hostFromValue) {
return { isUrl: false, faviconHost: hostFromValue }
}
- // Try map canonical name → representative domain (first literal domain pattern)
+ // Find first literal domain pattern
const mapping = REFERRER_MAP.find((m) => (rowName || '').toLowerCase() === m.name.toLowerCase())
if (mapping) {
- const literal =
- mapping.patterns.find((p) => !['[', ']', '{', '}', '*'].some((c) => p.includes(c))) || mapping.patterns[0]
- const safe = literal.replace(/\[[^\]]+\]/g, 'com') // e.g., google.[a-z]{2,3} -> google.com
- return { isUrl: false, faviconHost: safe }
+ return { isUrl: false, faviconHost: mapping.patterns[0] }
}
- return { isUrl: false, faviconHost: null as any }
+ return { isUrl: false, faviconHost: null as string | null }
}
}, [rowName])
diff --git a/web/app/utils/referrers.map.json b/web/app/referrers.map.json
similarity index 85%
rename from web/app/utils/referrers.map.json
rename to web/app/referrers.map.json
index d5a057806..d2f749b98 100644
--- a/web/app/utils/referrers.map.json
+++ b/web/app/referrers.map.json
@@ -1,7 +1,11 @@
[
{ "name": "Linktree", "patterns": ["linktr.ee"] },
{ "name": "Perplexity", "patterns": ["perplexity.ai"] },
- { "name": "Yandex", "patterns": ["yandex.ru", "yandex.com", "yandex.kz", "yandex.by", "ya.ru", "yandex.eu", "yandex.fr"] },
+ { "name": "Wikipedia", "patterns": ["wikipedia.org"] },
+ {
+ "name": "Yandex",
+ "patterns": ["yandex.ru", "yandex.com", "yandex.kz", "yandex.by", "ya.ru", "yandex.eu", "yandex.fr"]
+ },
{ "name": "GitHub", "patterns": ["github.com"] },
{ "name": "Google", "patterns": ["google.com", "doubleclick.net"] },
{ "name": "Bing", "patterns": ["bing.com", "ntp.msn.com"] },
diff --git a/web/app/utils/referrers.ts b/web/app/utils/referrers.ts
index f5a82c2c0..657b2e95f 100644
--- a/web/app/utils/referrers.ts
+++ b/web/app/utils/referrers.ts
@@ -5,9 +5,9 @@
Otherwise, group by hostname (strips leading www.).
*/
-import MAP from './referrers.map.json'
+import MAP from '../referrers.map.json'
-export type ReferrerMapping = {
+type ReferrerMapping = {
name: string
patterns: string[]
}
@@ -21,10 +21,21 @@ export const extractHostname = (value: string | null | undefined): string | null
const url = new URL(value)
return url.hostname.toLowerCase()
} catch {
- // If it's already a hostname (no protocol), return as lowercased
const trimmed = value.trim().toLowerCase()
if (trimmed.includes(' ')) return null
- if (trimmed.includes('.') || /^[a-z0-9-]+\.[a-z]{2,}$/.test(trimmed)) return trimmed
+ // Try to parse assuming http scheme to reliably extract host from strings like "example.com/path"
+ try {
+ const url = new URL(`http://${trimmed}`)
+ const host = url.hostname.toLowerCase()
+ // Accept only real domains (has a dot) or IPv4; reject bare words like "reddit"
+ if (host.includes('.') || /^\d{1,3}(\.\d{1,3}){3}$/.test(host)) {
+ return host
+ }
+ } catch {
+ // Fallback: accept bare hostnames only
+ const bare = trimmed.split('/')[0].split('?')[0].split('#')[0]
+ if (/^[a-z0-9.-]+\.[a-z]{2,}$/i.test(bare)) return bare
+ }
return null
}
}
@@ -43,7 +54,7 @@ const matchByMap = (host: string): string | null => {
return null
}
-export const getCanonicalRefGroup = (refValue: string | null | undefined): { key: string; name: string } | null => {
+const getCanonicalRefGroup = (refValue: string | null | undefined): { key: string; name: string } | null => {
const value = (refValue || '').trim()
// If a non-http(s) scheme is present, keep as-is (do not group)
const scheme = value.match(/^([a-z][a-z0-9+.-]*):/i)?.[1]
@@ -59,7 +70,7 @@ export const getCanonicalRefGroup = (refValue: string | null | undefined): { key
return { key: plainHost, name: plainHost }
}
-export type Entry = { name: string | null; count: number; [k: string]: any }
+type Entry = { name: string | null; count: number; [k: string]: any }
// Group a list of ref entries by canonical name or hostname.
export const groupRefEntries = (entries: Entry[]): Entry[] => {