forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change also: * Makes the site text size slightly smaller. Previously, it seemed too big. * Adds a `<TabsBar />` component. Closes denoland#370 ![localhost_8000_dashboard_users (2)](https://github.com/denoland/saaskit/assets/29347852/7f26c0bf-bd4c-4749-b5ae-d921497c0a58)
- Loading branch information
Showing
7 changed files
with
270 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
import { LINK_STYLES } from "@/utils/constants.ts"; | ||
import { cx } from "@twind/core"; | ||
|
||
export default function TabsBar( | ||
props: { links: { path: string; innerText: string }[]; currentPath: string }, | ||
) { | ||
return ( | ||
<div class="flex flex-row w-full mb-8"> | ||
{props.links.map((link) => ( | ||
<a | ||
href={link.path} | ||
class={cx( | ||
"px-4 py-2 rounded-lg", | ||
link.path === props.currentPath | ||
? "bg-gray-100 text-black dark:(bg-gray-800 text-white)" | ||
: "", | ||
LINK_STYLES, | ||
)} | ||
> | ||
{link.innerText} | ||
</a> | ||
))} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,10 @@ | ||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
import type { Handlers, PageProps } from "$fresh/server.ts"; | ||
import { DAY } from "std/datetime/constants.ts"; | ||
import Chart from "@/islands/Chart.tsx"; | ||
import { getDatesSince, getManyMetrics } from "@/utils/db.ts"; | ||
import Head from "@/components/Head.tsx"; | ||
import type { SignedInState } from "@/utils/middleware.ts"; | ||
|
||
interface DashboardPageData extends SignedInState { | ||
dates: Date[]; | ||
visitsCounts: number[]; | ||
usersCounts: number[]; | ||
itemsCounts: number[]; | ||
votesCounts: number[]; | ||
} | ||
import type { Handlers } from "$fresh/server.ts"; | ||
import { redirect } from "@/utils/redirect.ts"; | ||
|
||
export const handler: Handlers<DashboardPageData, SignedInState> = { | ||
async GET(_req, ctx) { | ||
const msAgo = 30 * DAY; | ||
const dates = getDatesSince(msAgo).map((date) => new Date(date)); | ||
|
||
const [ | ||
visitsCounts, | ||
usersCounts, | ||
itemsCounts, | ||
votesCounts, | ||
] = await Promise.all([ | ||
getManyMetrics("visits_count", dates), | ||
getManyMetrics("users_count", dates), | ||
getManyMetrics("items_count", dates), | ||
getManyMetrics("votes_count", dates), | ||
]); | ||
|
||
return ctx.render({ | ||
...ctx.state, | ||
dates, | ||
visitsCounts: visitsCounts.map(Number), | ||
usersCounts: usersCounts.map(Number), | ||
itemsCounts: itemsCounts.map(Number), | ||
votesCounts: votesCounts.map(Number), | ||
}); | ||
export const handler: Handlers = { | ||
GET(_req) { | ||
return redirect("/dashboard/stats"); | ||
}, | ||
}; | ||
|
||
export default function DashboardPage(props: PageProps<DashboardPageData>) { | ||
const datasets = [ | ||
{ | ||
label: "Site visits", | ||
data: props.data.visitsCounts, | ||
borderColor: "#be185d", | ||
}, | ||
{ | ||
label: "Users created", | ||
data: props.data.usersCounts, | ||
borderColor: "#e85d04", | ||
}, | ||
{ | ||
label: "Items created", | ||
data: props.data.itemsCounts, | ||
borderColor: "#219ebc", | ||
}, | ||
{ | ||
label: "Votes", | ||
data: props.data.votesCounts, | ||
borderColor: "#4338ca", | ||
}, | ||
]; | ||
|
||
const max = Math.max(...datasets[0].data); | ||
|
||
const labels = props.data.dates.map((date) => | ||
new Date(date).toLocaleDateString("en-us", { | ||
month: "short", | ||
day: "numeric", | ||
}) | ||
); | ||
|
||
return ( | ||
<> | ||
<Head title="Dashboard" href={props.url.href} /> | ||
<main class="flex-1 p-4 flex flex-col"> | ||
<h1 class="text-3xl font-bold">Dashboard</h1> | ||
<div class="flex-1 relative"> | ||
<Chart | ||
type="line" | ||
options={{ | ||
maintainAspectRatio: false, | ||
interaction: { | ||
intersect: false, | ||
mode: "index", | ||
}, | ||
scales: { | ||
x: { | ||
max, | ||
grid: { display: false }, | ||
}, | ||
y: { | ||
beginAtZero: true, | ||
grid: { display: false }, | ||
ticks: { precision: 0 }, | ||
}, | ||
}, | ||
}} | ||
data={{ | ||
labels, | ||
datasets: datasets.map((dataset) => ({ | ||
...dataset, | ||
pointRadius: 0, | ||
cubicInterpolationMode: "monotone", | ||
})), | ||
}} | ||
/> | ||
</div> | ||
</main> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
import type { Handlers, PageProps } from "$fresh/server.ts"; | ||
import { DAY } from "std/datetime/constants.ts"; | ||
import Chart from "@/islands/Chart.tsx"; | ||
import { getDatesSince, getManyMetrics } from "@/utils/db.ts"; | ||
import Head from "@/components/Head.tsx"; | ||
import type { SignedInState } from "@/utils/middleware.ts"; | ||
import TabsBar from "@/components/TabsBar.tsx"; | ||
|
||
interface DashboardPageData extends SignedInState { | ||
dates: Date[]; | ||
visitsCounts: number[]; | ||
usersCounts: number[]; | ||
itemsCounts: number[]; | ||
votesCounts: number[]; | ||
} | ||
|
||
export const handler: Handlers<DashboardPageData, SignedInState> = { | ||
async GET(_req, ctx) { | ||
const msAgo = 30 * DAY; | ||
const dates = getDatesSince(msAgo).map((date) => new Date(date)); | ||
|
||
const [ | ||
visitsCounts, | ||
usersCounts, | ||
itemsCounts, | ||
votesCounts, | ||
] = await Promise.all([ | ||
getManyMetrics("visits_count", dates), | ||
getManyMetrics("users_count", dates), | ||
getManyMetrics("items_count", dates), | ||
getManyMetrics("votes_count", dates), | ||
]); | ||
|
||
return ctx.render({ | ||
...ctx.state, | ||
dates, | ||
visitsCounts: visitsCounts.map(Number), | ||
usersCounts: usersCounts.map(Number), | ||
itemsCounts: itemsCounts.map(Number), | ||
votesCounts: votesCounts.map(Number), | ||
}); | ||
}, | ||
}; | ||
|
||
export default function DashboardPage(props: PageProps<DashboardPageData>) { | ||
const datasets = [ | ||
{ | ||
label: "Site visits", | ||
data: props.data.visitsCounts, | ||
borderColor: "#be185d", | ||
}, | ||
{ | ||
label: "Users created", | ||
data: props.data.usersCounts, | ||
borderColor: "#e85d04", | ||
}, | ||
{ | ||
label: "Items created", | ||
data: props.data.itemsCounts, | ||
borderColor: "#219ebc", | ||
}, | ||
{ | ||
label: "Votes", | ||
data: props.data.votesCounts, | ||
borderColor: "#4338ca", | ||
}, | ||
]; | ||
|
||
const max = Math.max(...datasets[0].data); | ||
|
||
const labels = props.data.dates.map((date) => | ||
new Date(date).toLocaleDateString("en-us", { | ||
month: "short", | ||
day: "numeric", | ||
}) | ||
); | ||
|
||
return ( | ||
<> | ||
<Head title="Dashboard" href={props.url.href} /> | ||
<main class="flex-1 p-4 flex flex-col"> | ||
<h1 class="text-3xl font-bold mb-8">Dashboard</h1> | ||
<TabsBar | ||
links={[{ | ||
path: "/dashboard/stats", | ||
innerText: "Stats", | ||
}, { | ||
path: "/dashboard/users", | ||
innerText: "Users", | ||
}]} | ||
currentPath={props.url.pathname} | ||
/> | ||
<div class="flex-1 relative"> | ||
<Chart | ||
type="line" | ||
options={{ | ||
maintainAspectRatio: false, | ||
interaction: { | ||
intersect: false, | ||
mode: "index", | ||
}, | ||
scales: { | ||
x: { | ||
max, | ||
grid: { display: false }, | ||
}, | ||
y: { | ||
beginAtZero: true, | ||
grid: { display: false }, | ||
ticks: { precision: 0 }, | ||
}, | ||
}, | ||
}} | ||
data={{ | ||
labels, | ||
datasets: datasets.map((dataset) => ({ | ||
...dataset, | ||
pointRadius: 0, | ||
cubicInterpolationMode: "monotone", | ||
})), | ||
}} | ||
/> | ||
</div> | ||
</main> | ||
</> | ||
); | ||
} |
Oops, something went wrong.