fix: enhance date range calculations and optimize date range change handling

This commit is contained in:
Max Kowalski
2025-06-28 00:39:35 +02:00
parent 5b22c0f1f8
commit c4cfe2f389
3 changed files with 50 additions and 17 deletions

View File

@ -91,13 +91,24 @@ export async function GET(request: NextRequest) {
const metrics = sessionMetrics(chatSessions, companyConfigForMetrics);
// Calculate date range from sessions
// Calculate date range from ALL sessions (not filtered) to get the full available range
let dateRange: { minDate: string; maxDate: string } | null = null;
if (prismaSessions.length > 0) {
const dates = prismaSessions.map(s => new Date(s.startTime)).sort((a, b) => a.getTime() - b.getTime());
const allSessions = await prisma.session.findMany({
where: {
companyId: user.companyId,
},
select: {
startTime: true,
},
orderBy: {
startTime: 'asc',
},
});
if (allSessions.length > 0) {
dateRange = {
minDate: dates[0].toISOString().split('T')[0], // First session date
maxDate: dates[dates.length - 1].toISOString().split('T')[0] // Last session date
minDate: allSessions[0].startTime.toISOString().split('T')[0], // First session date
maxDate: allSessions[allSessions.length - 1].startTime.toISOString().split('T')[0] // Last session date
};
}

View File

@ -84,12 +84,15 @@ function DashboardContent() {
}
};
// Handle date range changes
// Handle date range changes with proper memoization
const handleDateRangeChange = useCallback((startDate: string, endDate: string) => {
setSelectedStartDate(startDate);
setSelectedEndDate(endDate);
fetchMetrics(startDate, endDate);
}, []);
// Only update if dates actually changed to prevent unnecessary API calls
if (startDate !== selectedStartDate || endDate !== selectedEndDate) {
setSelectedStartDate(startDate);
setSelectedEndDate(endDate);
fetchMetrics(startDate, endDate);
}
}, [selectedStartDate, selectedEndDate]);
useEffect(() => {
// Redirect if not authenticated
@ -329,8 +332,8 @@ function DashboardContent() {
</CardHeader>
</Card>
{/* Date Range Picker - Temporarily disabled to debug infinite loop */}
{/* {dateRange && (
{/* Date Range Picker */}
{dateRange && (
<DateRangePicker
minDate={dateRange.minDate}
maxDate={dateRange.maxDate}
@ -338,7 +341,7 @@ function DashboardContent() {
initialStartDate={selectedStartDate}
initialEndDate={selectedEndDate}
/>
)} */}
)}
{/* Modern Metrics Grid */}
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6">