Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

ADd previous time

Bill Yanga4779ca6cc1b
diff --git a/client/src/app/components/MainSection/Chart.tsx b/client/src/app/components/MainSection/Chart.tsx
index 8ce33d4e3..5e6f1bf2c 100644
--- a/client/src/app/components/MainSection/Chart.tsx
+++ b/client/src/app/components/MainSection/Chart.tsx
@@ -4,7 +4,7 @@ import { ResponsiveLine } from "@nivo/line";
import { DateTime } from "luxon";
import { round } from "lodash";
import { Time, useTimeSelection } from "../../../lib/timeSelectionStore";
-import { GetPageViewsResponse } from "../../../hooks/api";
+import { APIResponse, GetPageViewsResponse } from "../../../hooks/api";
export const nivoTheme: Theme = {
axis: {
@@ -77,13 +77,10 @@ const getMax = (time: Time) => {
export function Chart({
data,
+ previousData,
}: {
- data:
- | {
- data?: GetPageViewsResponse;
- error?: string;
- }
- | undefined;
+ data: APIResponse<GetPageViewsResponse> | undefined;
+ previousData: APIResponse<GetPageViewsResponse> | undefined;
}) {
const { time } = useTimeSelection();
@@ -92,9 +89,29 @@ export function Chart({
y: e.pageviews,
}));
+ const timeRange =
+ time.mode === "range"
+ ? DateTime.fromISO(time.startDate).diff(
+ DateTime.fromISO(time.endDate),
+ "days"
+ ).days
+ : 0;
+
+ const formattedPreviousData = previousData?.data?.map((e) => ({
+ x: DateTime.fromSQL(e.time)
+ .plus({ days: timeRange || 1 })
+ .toUTC()
+ .toFormat("yyyy-MM-dd HH:mm:ss"),
+ y: e.pageviews,
+ }));
+
return (
<ResponsiveLine
data={[
+ {
+ id: "2",
+ data: formattedPreviousData ?? [],
+ },
{
id: "1",
data: formattedData ?? [],
@@ -113,7 +130,7 @@ export function Chart({
type: "linear",
min: 0,
max: "auto",
- stacked: true,
+ stacked: false,
reverse: false,
}}
enableGridX={false}
@@ -152,7 +169,7 @@ export function Chart({
useMesh={true}
motionConfig="stiff"
enableSlices={"x"}
- colors={["hsl(var(--fuchsia-400))"]}
+ colors={["hsl(var(--neutral-700))", "hsl(var(--fuchsia-400))"]}
enableArea={true}
areaBaselineValue={0}
areaOpacity={0.3}
@@ -166,7 +183,7 @@ export function Chart({
],
},
]}
- fill={[{ match: "*", id: "gradient" }]}
+ fill={[{ match: (d) => d.id === "1", id: "gradient" }]}
sliceTooltip={({ slice }) => {
return (
<div className="text-sm bg-neutral-900 p-2 rounded-md">
@@ -181,6 +198,7 @@ export function Chart({
</div>
);
}}
+ // layers={["grid", "markers", "areas", "", "lines", "points", "slices", "axes", "legends"]}
/>
);
}
diff --git a/client/src/app/components/MainSection/MainSection.tsx b/client/src/app/components/MainSection/MainSection.tsx
index a9a41c9c0..c5f06f2d7 100644
--- a/client/src/app/components/MainSection/MainSection.tsx
+++ b/client/src/app/components/MainSection/MainSection.tsx
@@ -6,6 +6,11 @@ import { Chart } from "./Chart";
export function MainSection() {
const { data, isFetching, error } = useGetPageviews();
+ const {
+ data: previousData,
+ isFetching: isPreviousFetching,
+ error: previousError,
+ } = useGetPageviews(true);
const {
data: overviewData,
isFetching: isOverviewFetching,
@@ -46,7 +51,7 @@ export function MainSection() {
<BucketSelection />
</div>
<div className="h-[350px]">
- <Chart data={data} />
+ <Chart data={data} previousData={previousData} />
</div>
</CardContent>
</Card>
diff --git a/client/src/components/ui/card.tsx b/client/src/components/ui/card.tsx
index b33d87f00..2f5b6920b 100644
--- a/client/src/components/ui/card.tsx
+++ b/client/src/components/ui/card.tsx
@@ -81,7 +81,7 @@ const CardContent = React.forwardRef<
<div
ref={ref}
className={cn(
- "p-4 pt-0 transition-all duration-300 animate-in fade-in-0 zoom-in-95",
+ "relative p-4 pt-0 transition-all duration-300 animate-in fade-in-0 zoom-in-95",
className
)}
{...props}
diff --git a/client/src/hooks/api.ts b/client/src/hooks/api.ts
index f4eed9b68..a6ba69257 100644
--- a/client/src/hooks/api.ts
+++ b/client/src/hooks/api.ts
@@ -94,15 +94,16 @@ export type GetPageViewsResponse = {
pageviews: number;
}[];
-export function useGetPageviews(): UseQueryResult<
- APIResponse<GetPageViewsResponse>
-> {
- const { time, bucket } = useTimeSelection();
+export function useGetPageviews(
+ usePrevious?: boolean
+): UseQueryResult<APIResponse<GetPageViewsResponse>> {
+ const { time, previousTime, bucket } = useTimeSelection();
+ const timeToUse = usePrevious ? previousTime : time;
- const { startDate, endDate } = getStartAndEndDate(time);
+ const { startDate, endDate } = getStartAndEndDate(timeToUse);
return useQuery({
- queryKey: ["pageviews", time, bucket],
+ queryKey: ["pageviews", timeToUse, bucket],
queryFn: () => {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
return fetch(
diff --git a/client/src/lib/timeSelectionStore.ts b/client/src/lib/timeSelectionStore.ts
index aef3b0099..2ff00344e 100644
--- a/client/src/lib/timeSelectionStore.ts
+++ b/client/src/lib/timeSelectionStore.ts
@@ -33,6 +33,7 @@ export type TimeBucket = "hour" | "day" | "week" | "month";
type Store = {
time: Time;
+ previousTime: Time;
setTime: (time: Time) => void;
bucket: TimeBucket;
setBucket: (bucket: TimeBucket) => void;
@@ -43,10 +44,20 @@ export const useTimeSelection = create<Store>((set) => ({
mode: "day",
day: DateTime.now().toISODate(),
},
+ previousTime: {
+ mode: "day",
+ day: DateTime.now().minus({ days: 1 }).toISODate(),
+ },
setTime: (time) => {
let bucketToUse: TimeBucket = "hour";
+ let previousTime: Time;
+
if (time.mode === "day") {
bucketToUse = "hour";
+ previousTime = {
+ mode: "day",
+ day: DateTime.fromISO(time.day).minus({ days: 1 }).toISODate() ?? "",
+ };
} else if (time.mode === "range") {
const timeRangeLength = DateTime.fromISO(time.endDate).diff(
DateTime.fromISO(time.startDate),
@@ -59,12 +70,34 @@ export const useTimeSelection = create<Store>((set) => ({
bucketToUse = "week";
}
bucketToUse = "day";
+
+ previousTime = {
+ mode: "range",
+ startDate:
+ DateTime.fromISO(time.startDate)
+ .minus({ days: timeRangeLength })
+ .toISODate() ?? "",
+ endDate:
+ DateTime.fromISO(time.startDate).minus({ days: 1 }).toISODate() ?? "",
+ };
} else if (time.mode === "month") {
bucketToUse = "day";
+ previousTime = {
+ mode: "month",
+ month:
+ DateTime.fromISO(time.month).minus({ months: 1 }).toISODate() ?? "",
+ };
} else if (time.mode === "year") {
bucketToUse = "month";
+ previousTime = {
+ mode: "year",
+ year: DateTime.fromISO(time.year).minus({ years: 1 }).toISODate() ?? "",
+ };
+ } else {
+ previousTime = time; // fallback case
}
- set({ time, bucket: bucketToUse });
+
+ set({ time, previousTime, bucket: bucketToUse });
},
bucket: "hour",
setBucket: (bucket) => set({ bucket }),