fix: Add null checks to prevent 'Cannot read properties of null' errors
This commit is contained in:
@@ -50,12 +50,12 @@ export default function SiteDashboardPage() {
|
|||||||
getCountries(siteId, dateRange.start, dateRange.end, 10),
|
getCountries(siteId, dateRange.start, dateRange.end, 10),
|
||||||
])
|
])
|
||||||
setSite(siteData)
|
setSite(siteData)
|
||||||
setStats(statsData)
|
setStats(statsData || { pageviews: 0, visitors: 0 })
|
||||||
setRealtime(realtimeData.visitors)
|
setRealtime(realtimeData?.visitors || 0)
|
||||||
setDailyStats(dailyData)
|
setDailyStats(Array.isArray(dailyData) ? dailyData : [])
|
||||||
setTopPages(pagesData)
|
setTopPages(Array.isArray(pagesData) ? pagesData : [])
|
||||||
setTopReferrers(referrersData)
|
setTopReferrers(Array.isArray(referrersData) ? referrersData : [])
|
||||||
setCountries(countriesData)
|
setCountries(Array.isArray(countriesData) ? countriesData : [])
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
toast.error('Failed to load data: ' + (error.message || 'Unknown error'))
|
toast.error('Failed to load data: ' + (error.message || 'Unknown error'))
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ interface CountriesProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function Countries({ countries }: CountriesProps) {
|
export default function Countries({ countries }: CountriesProps) {
|
||||||
if (countries.length === 0) {
|
if (!countries || countries.length === 0) {
|
||||||
return (
|
return (
|
||||||
<div className="bg-white dark:bg-neutral-900 border border-neutral-200 dark:border-neutral-800 rounded-xl p-6">
|
<div className="bg-white dark:bg-neutral-900 border border-neutral-200 dark:border-neutral-800 rounded-xl p-6">
|
||||||
<h3 className="text-lg font-semibold mb-4 text-neutral-900 dark:text-white">
|
<h3 className="text-lg font-semibold mb-4 text-neutral-900 dark:text-white">
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ interface TopPagesProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function TopPages({ pages }: TopPagesProps) {
|
export default function TopPages({ pages }: TopPagesProps) {
|
||||||
if (pages.length === 0) {
|
if (!pages || pages.length === 0) {
|
||||||
return (
|
return (
|
||||||
<div className="bg-white dark:bg-neutral-900 border border-neutral-200 dark:border-neutral-800 rounded-xl p-6">
|
<div className="bg-white dark:bg-neutral-900 border border-neutral-200 dark:border-neutral-800 rounded-xl p-6">
|
||||||
<h3 className="text-lg font-semibold mb-4 text-neutral-900 dark:text-white">
|
<h3 className="text-lg font-semibold mb-4 text-neutral-900 dark:text-white">
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ interface TopReferrersProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function TopReferrers({ referrers }: TopReferrersProps) {
|
export default function TopReferrers({ referrers }: TopReferrersProps) {
|
||||||
if (referrers.length === 0) {
|
if (!referrers || referrers.length === 0) {
|
||||||
return (
|
return (
|
||||||
<div className="bg-white dark:bg-neutral-900 border border-neutral-200 dark:border-neutral-800 rounded-xl p-6">
|
<div className="bg-white dark:bg-neutral-900 border border-neutral-200 dark:border-neutral-800 rounded-xl p-6">
|
||||||
<h3 className="text-lg font-semibold mb-4 text-neutral-900 dark:text-white">
|
<h3 className="text-lg font-semibold mb-4 text-neutral-900 dark:text-white">
|
||||||
|
|||||||
@@ -19,9 +19,10 @@ export default function SiteList() {
|
|||||||
try {
|
try {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
const data = await listSites()
|
const data = await listSites()
|
||||||
setSites(data)
|
setSites(Array.isArray(data) ? data : [])
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
toast.error('Failed to load sites: ' + (error.message || 'Unknown error'))
|
toast.error('Failed to load sites: ' + (error.message || 'Unknown error'))
|
||||||
|
setSites([]) // Ensure sites is always an array
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export interface UpdateSiteRequest {
|
|||||||
|
|
||||||
export async function listSites(): Promise<Site[]> {
|
export async function listSites(): Promise<Site[]> {
|
||||||
const response = await apiRequest<{ sites: Site[] }>('/sites')
|
const response = await apiRequest<{ sites: Site[] }>('/sites')
|
||||||
return response.sites
|
return response?.sites || []
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getSite(id: string): Promise<Site> {
|
export async function getSite(id: string): Promise<Site> {
|
||||||
|
|||||||
Reference in New Issue
Block a user