fixed issues with app
This commit is contained in:
+21
-8
@@ -1,5 +1,13 @@
|
||||
import { query } from './_generated/server.js';
|
||||
import { requireOwner } from './lib.js';
|
||||
import { dashboardRangeValidator } from './validators.js';
|
||||
|
||||
const rangeDays = {
|
||||
'7d': 7,
|
||||
'30d': 30,
|
||||
'90d': 90,
|
||||
'1y': 365
|
||||
} as const;
|
||||
|
||||
function dayKey(date: Date) {
|
||||
return date.toISOString().slice(0, 10);
|
||||
@@ -13,24 +21,27 @@ function daysAgo(days: number) {
|
||||
}
|
||||
|
||||
export const summary = query({
|
||||
args: {},
|
||||
handler: async (ctx) => {
|
||||
args: { range: dashboardRangeValidator },
|
||||
handler: async (ctx, args) => {
|
||||
const owner = await requireOwner(ctx);
|
||||
const selectedDays = rangeDays[args.range];
|
||||
const entries = await ctx.db
|
||||
.query('entries')
|
||||
.withIndex('by_owner_and_entryDate', (q) => q.eq('owner', owner))
|
||||
.order('desc')
|
||||
.take(250);
|
||||
.take(Math.min(800, selectedDays * 3));
|
||||
const activeEntries = entries.filter((entry) => !entry.archived);
|
||||
const last30Start = daysAgo(29);
|
||||
const rangeStart = daysAgo(selectedDays - 1);
|
||||
const last7Start = daysAgo(6);
|
||||
const last30 = activeEntries.filter((entry) => entry.entryDate >= last30Start);
|
||||
const last30Start = daysAgo(29);
|
||||
const rangedEntries = activeEntries.filter((entry) => entry.entryDate >= rangeStart);
|
||||
const last7 = activeEntries.filter((entry) => entry.entryDate >= last7Start);
|
||||
const last30 = activeEntries.filter((entry) => entry.entryDate >= last30Start);
|
||||
const moodCounts: Record<string, number> = {};
|
||||
const dailyCounts = new Map<string, number>();
|
||||
const tagCounts: Record<string, number> = {};
|
||||
|
||||
for (const entry of last30) {
|
||||
for (const entry of rangedEntries) {
|
||||
moodCounts[entry.mood] = (moodCounts[entry.mood] ?? 0) + 1;
|
||||
dailyCounts.set(entry.entryDate, (dailyCounts.get(entry.entryDate) ?? 0) + 1);
|
||||
|
||||
@@ -46,8 +57,8 @@ export const summary = query({
|
||||
currentStreak += 1;
|
||||
}
|
||||
|
||||
const dailySeries = Array.from({ length: 30 }, (_, index) => {
|
||||
const date = daysAgo(29 - index);
|
||||
const dailySeries = Array.from({ length: selectedDays }, (_, index) => {
|
||||
const date = daysAgo(selectedDays - 1 - index);
|
||||
return { date, count: dailyCounts.get(date) ?? 0 };
|
||||
});
|
||||
|
||||
@@ -55,6 +66,8 @@ export const summary = query({
|
||||
totalEntries: activeEntries.length,
|
||||
entriesThisWeek: last7.length,
|
||||
entriesThisMonth: last30.length,
|
||||
entriesInRange: rangedEntries.length,
|
||||
range: args.range,
|
||||
currentStreak,
|
||||
averageWords:
|
||||
activeEntries.length === 0
|
||||
|
||||
Reference in New Issue
Block a user