This commit is contained in:
@@ -222,13 +222,13 @@ export const CounterDetail: React.FC = () => {
|
||||
Recent Entries
|
||||
</h2>
|
||||
|
||||
{entries.length === 0 ? (
|
||||
{(entries?.length || 0) === 0 ? (
|
||||
<div className="text-center py-8 text-gray-500">
|
||||
No entries yet. Start by incrementing your counter!
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{entries.slice(0, 10).map((entry) => (
|
||||
{entries?.slice(0, 10).map((entry) => (
|
||||
<div
|
||||
key={entry.id}
|
||||
className="flex justify-between items-center py-2 px-4 bg-gray-50 rounded-lg"
|
||||
|
||||
@@ -17,9 +17,9 @@ export const Dashboard: React.FC = () => {
|
||||
searchCounters(query);
|
||||
};
|
||||
|
||||
const totalCounters = counters.length;
|
||||
const totalValue = counters.reduce((sum, counter) => sum + counter.total_value, 0);
|
||||
const todayValue = counters.reduce((sum, counter) => sum + counter.today_value, 0);
|
||||
const totalCounters = counters?.length || 0;
|
||||
const totalValue = counters?.reduce((sum, counter) => sum + counter.total_value, 0) || 0;
|
||||
const todayValue = counters?.reduce((sum, counter) => sum + counter.today_value, 0) || 0;
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
@@ -109,7 +109,7 @@ export const Dashboard: React.FC = () => {
|
||||
<div className="flex justify-center items-center py-12">
|
||||
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-primary-600"></div>
|
||||
</div>
|
||||
) : counters.length === 0 ? (
|
||||
) : (counters?.length || 0) === 0 ? (
|
||||
<div className="text-center py-12">
|
||||
<TrendingUp className="mx-auto h-12 w-12 text-gray-400" />
|
||||
<h3 className="mt-2 text-sm font-medium text-gray-900">No counters</h3>
|
||||
@@ -128,7 +128,7 @@ export const Dashboard: React.FC = () => {
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
{counters.map((counter) => (
|
||||
{counters?.map((counter) => (
|
||||
<CounterCard key={counter.id} counter={counter} />
|
||||
))}
|
||||
</div>
|
||||
|
||||
@@ -110,7 +110,7 @@ export const useCounters = () => {
|
||||
} else {
|
||||
// Increment anonymous counter
|
||||
const today = localStorageService.getTodayString();
|
||||
const counter = anonymousCounters.find(c => c.id === id);
|
||||
const counter = anonymousCounters.find((c: AnonymousCounter) => c.id === id);
|
||||
if (counter) {
|
||||
const newTotal = counter.total_value + value;
|
||||
const newEntries = { ...counter.entries };
|
||||
@@ -146,9 +146,9 @@ export const useCounters = () => {
|
||||
// Convert anonymous counters to CounterWithStats format for consistent UI
|
||||
const getDisplayCounters = (): CounterWithStats[] => {
|
||||
if (isAuthenticated) {
|
||||
return counters;
|
||||
return counters || [];
|
||||
} else {
|
||||
return anonymousCounters.map(counter => ({
|
||||
return (anonymousCounters || []).map((counter: AnonymousCounter) => ({
|
||||
id: parseInt(counter.id), // Convert string ID to number for display
|
||||
name: counter.name,
|
||||
description: counter.description,
|
||||
@@ -156,23 +156,23 @@ export const useCounters = () => {
|
||||
updated_at: counter.updated_at,
|
||||
total_value: counter.total_value,
|
||||
today_value: counter.entries[localStorageService.getTodayString()] || 0,
|
||||
week_value: Object.entries(counter.entries)
|
||||
.filter(([date]) => {
|
||||
week_value: Object.keys(counter.entries)
|
||||
.filter((date) => {
|
||||
const entryDate = new Date(date);
|
||||
const weekAgo = new Date();
|
||||
weekAgo.setDate(weekAgo.getDate() - 7);
|
||||
return entryDate >= weekAgo;
|
||||
})
|
||||
.reduce((sum, [, value]) => sum + (value as number), 0),
|
||||
month_value: Object.entries(counter.entries)
|
||||
.filter(([date]) => {
|
||||
.reduce((sum, date) => sum + (counter.entries[date] as number), 0),
|
||||
month_value: Object.keys(counter.entries)
|
||||
.filter((date) => {
|
||||
const entryDate = new Date(date);
|
||||
const monthAgo = new Date();
|
||||
monthAgo.setMonth(monthAgo.getMonth() - 1);
|
||||
return entryDate >= monthAgo;
|
||||
})
|
||||
.reduce((sum, [, value]) => sum + (value as number), 0),
|
||||
entry_count: Object.values(counter.entries).reduce((sum: number, value: unknown) => sum + Math.abs(value as number), 0),
|
||||
.reduce((sum, date) => sum + (counter.entries[date] as number), 0),
|
||||
entry_count: Object.keys(counter.entries).reduce((sum: number, date: string) => sum + Math.abs(counter.entries[date] as number), 0),
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user