import { useFocusEffect } from "@react-navigation/native"; import { useCallback, useState } from "react"; import { ActivityIndicator, ScrollView, View } from "react-native"; import { IconButton, Text } from "react-native-paper"; import AppPieChart from "./AppPieChart"; import Chart from "./Chart"; import ConfirmDialog from "./ConfirmDialog"; import DrawerHeader from "./DrawerHeader"; import Select from "./Select"; import { MARGIN, PADDING } from "./constants"; import { AppDataSource } from "./data-source"; import { Periods } from "./periods"; import { DAYS } from "./days"; interface WeekCount { week: string; count: number; } interface HourCount { hour: string; count: number; } export default function InsightsPage() { const [weekCounts, setWeekCounts] = useState(); const [hourCounts, setHourCounts] = useState(); const [period, setPeriod] = useState(Periods.Monthly); const [showWeek, setShowWeek] = useState(false); const [showHour, setShowHour] = useState(false); useFocusEffect( useCallback(() => { let difference = "-1 months"; if (period === Periods.TwoMonths) difference = "-2 months"; if (period === Periods.ThreeMonths) difference = "-3 months"; if (period === Periods.SixMonths) difference = "-6 months"; const selectWeeks = ` SELECT strftime('%w', created) as week, COUNT(*) as count FROM sets WHERE DATE(created) >= DATE('now', 'weekday 0', '${difference}') GROUP BY week HAVING week IS NOT NULL ORDER BY count DESC; `; const selectHours = ` SELECT strftime('%H', created) AS hour, COUNT(*) AS count FROM sets WHERE DATE(created) >= DATE('now', 'weekday 0', '${difference}') GROUP BY hour having hour is not null ORDER BY hour `; setTimeout( () => AppDataSource.manager .query(selectWeeks) .then(setWeekCounts) .then(() => AppDataSource.manager.query(selectHours).then(setHourCounts) ), 400 ); }, [period]) ); const hourLabel = (hour: string) => { let twelveHour = Number(hour); if (twelveHour === 0) return "12AM"; let amPm = "AM"; if (twelveHour >= 12) amPm = "PM"; if (twelveHour > 12) twelveHour -= 12; return `${twelveHour} ${amPm}`; }; return ( <>