🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
improve chart ux
Bill Yang88950f860550
diff --git a/client/src/app/components/MainSection/Chart.tsx b/client/src/app/components/MainSection/Chart.tsxindex 05a95f56f..8ce33d4e3 100644--- a/client/src/app/components/MainSection/Chart.tsx+++ b/client/src/app/components/MainSection/Chart.tsx@@ -1,10 +1,10 @@"use client";import { Theme } from "@nivo/core";import { ResponsiveLine } from "@nivo/line";-import { GetPageViewsResponse } from "../../../actions/getPageviews";import { DateTime } from "luxon";import { round } from "lodash";import { Time, useTimeSelection } from "../../../lib/timeSelectionStore";+import { GetPageViewsResponse } from "../../../hooks/api";export const nivoTheme: Theme = {axis: {@@ -55,17 +55,22 @@ export const nivoTheme: Theme = {export const formatter = Intl.NumberFormat("en", { notation: "compact" });const getMax = (time: Time) => {+ const now = DateTime.now();+if (time.mode === "day") {- return DateTime.fromISO(time.day)+ const dayDate = DateTime.fromISO(time.day).endOf("day")- .minus({ minutes: 59 })- .toJSDate();+ .minus({ minutes: 59 });+ return now < dayDate ? dayDate.toJSDate() : undefined;} else if (time.mode === "week") {- return DateTime.fromISO(time.week).endOf("week").toJSDate();+ const weekDate = DateTime.fromISO(time.week).endOf("week");+ return now < weekDate ? weekDate.toJSDate() : undefined;} else if (time.mode === "month") {- return DateTime.fromISO(time.month).endOf("month").toJSDate();+ const monthDate = DateTime.fromISO(time.month).endOf("month");+ return now < monthDate ? monthDate.toJSDate() : undefined;} else if (time.mode === "year") {- return DateTime.fromISO(time.year).endOf("year").toJSDate();+ const yearDate = DateTime.fromISO(time.year).endOf("year");+ return now < yearDate ? yearDate.toJSDate() : undefined;}return undefined;};@@ -80,7 +85,7 @@ export function Chart({}| undefined;}) {- const { time, bucket } = useTimeSelection();+ const { time } = useTimeSelection();const formattedData = data?.data?.map((e) => ({x: DateTime.fromSQL(e.time).toUTC().toFormat("yyyy-MM-dd HH:mm:ss"),diff --git a/client/src/app/components/MainSection/MainSection.tsx b/client/src/app/components/MainSection/MainSection.tsxindex db1b7aaeb..de3771941 100644--- a/client/src/app/components/MainSection/MainSection.tsx+++ b/client/src/app/components/MainSection/MainSection.tsx@@ -1,17 +1,15 @@"use client";import { Card, CardContent, CardLoader } from "@/components/ui/card";-import { useTimeSelection } from "../../../lib/timeSelectionStore";+import { useGetPageviews } from "../../../hooks/api";import { BucketSelection } from "./BucketSelection";import { Chart } from "./Chart";-import { useGetPageviews } from "../../../hooks/api";export function MainSection() {- const { data, isLoading, error } = useGetPageviews();- console.log(data);+ const { data, isFetching, error } = useGetPageviews();return (<Card>- {isLoading && <CardLoader />}+ {isFetching && <CardLoader />}<CardContent className="pt-4 w-full"><BucketSelection /><div className="h-[350px]">diff --git a/client/src/hooks/api.ts b/client/src/hooks/api.tsindex 6327a17a1..d09be03d2 100644--- a/client/src/hooks/api.ts+++ b/client/src/hooks/api.ts@@ -1,4 +1,8 @@-import { useQuery, UseQueryResult } from "@tanstack/react-query";+import {+ keepPreviousData,+ useQuery,+ UseQueryResult,+} from "@tanstack/react-query";import { useTimeSelection } from "../lib/timeSelectionStore";import { getStartAndEndDate } from "./utils";@@ -34,7 +38,7 @@ export function useGetCountries() {return useGenericQuery<GetCountriesResponse>("countries");}-type GetDevicesResponse = {+export type GetDevicesResponse = {device_type: string;count: number;percentage: number;@@ -44,7 +48,7 @@ export function useGetDevices() {return useGenericQuery<GetDevicesResponse>("devices");}-type GetOperatingSystemsResponse = {+export type GetOperatingSystemsResponse = {operating_system: string;count: number;percentage: number;@@ -54,7 +58,7 @@ export function useGetOperatingSystems() {return useGenericQuery<GetOperatingSystemsResponse>("operating-systems");}-type GetBrowsersResponse = {+export type GetBrowsersResponse = {browser: string;count: number;percentage: number;@@ -64,7 +68,7 @@ export function useGetBrowsers() {return useGenericQuery<GetBrowsersResponse>("browsers");}-type GetPagesResponse = {+export type GetPagesResponse = {pathname: string;count: number;percentage: number;@@ -74,7 +78,7 @@ export function useGetPages() {return useGenericQuery<GetPagesResponse>("pages");}-type GetReferrersResponse = {+export type GetReferrersResponse = {referrer: string;count: number;percentage: number;@@ -84,7 +88,7 @@ export function useGetReferrers() {return useGenericQuery<GetReferrersResponse>("referrers");}-type GetPageViewsResponse = {+export type GetPageViewsResponse = {time: string;pageviews: number;}[];@@ -104,5 +108,6 @@ export function useGetPageviews(): UseQueryResult<`${process.env.BACKEND_URL}/pageviews?startDate=${startDate}&endDate=${endDate}&timezone=${timezone}&bucket=${bucket}`).then((res) => res.json());},+ placeholderData: keepPreviousData,});}