Files
garden-astro/src/layouts/Base.astro
T
wesandClaude Opus 4.8 f9e45f32f8 Move odometer into the shared footer
Widget + script + styles live in Base.astro now, so every page ticks;
index.astro is back to just rendering the vault homepage.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-15 15:21:17 -04:00

341 lines
9.4 KiB
Plaintext

---
import 'katex/dist/katex.min.css';
import { getCollection } from 'astro:content';
import { buildNavTree } from '../lib/nav.mjs';
import NavTree from '../components/NavTree.astro';
interface Props {
title: string;
description?: string;
}
const { title, description } = Astro.props;
const isHome = Astro.url.pathname === '/';
const navTree = buildNavTree(await getCollection('garden'));
---
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>{title}{isHome ? '' : ' // Wesley Ray'}</title>
{description && <meta name="description" content={description} />}
<script
defer
data-domain="garden.c0smere.net"
src="https://plausible.io/js/script.js"></script>
</head>
<body>
<div class="layout">
<div class="content-col">
<header>
{!isHome && <nav><a href="/">← home</a></nav>}
</header>
<main>
<slot />
</main>
<footer>
<p>
Wesley Ray · <a href="https://blog.c0smere.net">blog</a> ·
<a href="https://git.c0smere.net/wes">git</a> ·
<a href="https://resume.c0smere.net">resume</a>
</p>
<div id="odometer" hidden>
<div class="odo-display" id="odo-digits" aria-live="off"></div>
<p class="odo-caption">
bytes in + out of the homelab since <span id="odo-since"></span> ·
ticking at the lifetime average ·
<a href="https://api.c0smere.net/docs">api</a>
</p>
</div>
</footer>
</div>
<aside class="sidebar">
<p class="sidebar-title"><a href="/">the garden</a></p>
<NavTree nodes={navTree} currentPath={Astro.url.pathname} />
</aside>
</div>
<script>
// Odometer: fetch totals once, then animate forward at the lifetime
// average rate. Looks live, but deliberately never exposes current
// throughput. Fail-closed: any error leaves the footer widget hidden.
const API = 'https://api.c0smere.net/bandwidth/odometer';
const section = document.getElementById('odometer')!;
const digitsEl = document.getElementById('odo-digits')!;
const sinceEl = document.getElementById('odo-since')!;
function renderDigits(total: bigint) {
const s = total.toString();
let html = '';
for (let i = 0; i < s.length; i++) {
if (i > 0 && (s.length - i) % 3 === 0)
html += '<span class="odo-sep"></span>';
html += `<span class="odo-digit">${s[i]}</span>`;
}
digitsEl.innerHTML = html;
}
try {
const res = await fetch(API);
if (!res.ok) throw new Error(String(res.status));
const data = await res.json();
const base = BigInt(data.total_bytes);
const rate = Number(data.avg_bytes_per_sec);
const asOf = Date.parse(data.as_of);
if (!Number.isFinite(rate) || !Number.isFinite(asOf))
throw new Error('bad payload');
sinceEl.textContent = new Date(data.since).toLocaleDateString(
undefined,
{ year: 'numeric', month: 'long', day: 'numeric' },
);
const tick = () => {
const elapsed = (Date.now() - asOf) / 1000;
renderDigits(base + BigInt(Math.floor(elapsed * rate)));
};
tick();
section.hidden = false;
setInterval(tick, 100);
} catch {
/* API down or blocked: widget stays hidden */
}
</script>
<script>
// mermaid: only load the (large) module on pages that need it
const blocks = document.querySelectorAll('pre > code.language-mermaid');
if (blocks.length) {
const { default: mermaid } = await import('mermaid');
blocks.forEach((code) => {
const pre = document.createElement('pre');
pre.className = 'mermaid';
pre.textContent = code.textContent ?? '';
code.parentElement?.replaceWith(pre);
});
mermaid.initialize({
startOnLoad: false,
theme: matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'default',
});
await mermaid.run();
}
</script>
<style is:global>
:root {
--bg: #faf8f8;
--fg: #2b2b2b;
--muted: #4e4e4e;
--faint: #e5e5e5;
--accent: #284b63;
--accent2: #84a59d;
color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #161618;
--fg: #ebebec;
--muted: #d4d4d4;
--faint: #393639;
--accent: #7b97aa;
--accent2: #84a59d;
}
}
* {
box-sizing: border-box;
}
body {
margin: 0;
background: var(--bg);
color: var(--fg);
font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
line-height: 1.6;
}
.layout {
margin: 0 auto;
padding: 1.5rem 1rem 3rem;
max-width: 44rem;
}
/* mobile: sidebar flows after the footer as a full site index */
.sidebar {
margin-top: 2.5rem;
padding-top: 1rem;
border-top: 1px solid var(--faint);
}
@media (min-width: 72rem) {
.layout {
display: grid;
grid-template-columns: 17rem minmax(0, 44rem);
gap: 3rem;
max-width: 66rem;
}
.sidebar {
order: -1;
margin-top: 0;
padding-top: 0;
border-top: none;
position: sticky;
top: 0;
align-self: start;
max-height: 100vh;
overflow-y: auto;
padding: 1.5rem 0.5rem 1.5rem 0;
}
}
.sidebar-title {
margin: 0 0 0.5rem;
font-weight: 600;
}
.sidebar-title a {
color: var(--fg);
text-decoration: none;
}
.sidebar ul {
list-style: none;
margin: 0;
padding-left: 0.85rem;
font-size: 0.85rem;
}
.sidebar > ul,
.sidebar-title + ul {
padding-left: 0;
}
.sidebar li {
margin: 0.15rem 0;
}
.sidebar a {
color: var(--muted);
text-decoration: none;
}
.sidebar a:hover {
color: var(--accent);
text-decoration: underline;
}
.sidebar a[aria-current='page'] {
color: var(--accent);
font-weight: 600;
}
.sidebar summary {
cursor: pointer;
list-style: none;
}
.sidebar summary::before {
content: '▸';
display: inline-block;
width: 0.9em;
color: var(--muted);
font-size: 0.8em;
transition: transform 0.15s;
}
.sidebar details[open] > summary::before {
transform: rotate(90deg);
}
.sidebar summary a {
font-weight: 550;
color: var(--fg);
}
a {
color: var(--accent);
}
h1, h2, h3, h4 {
line-height: 1.25;
}
header nav {
margin-bottom: 1rem;
font-size: 0.9rem;
}
footer {
margin-top: 3rem;
padding-top: 1rem;
border-top: 1px solid var(--faint);
font-size: 0.85rem;
color: var(--muted);
}
.odo-display {
display: flex;
flex-wrap: wrap;
gap: 2px;
font-family: ui-monospace, 'Cascadia Mono', 'JetBrains Mono', monospace;
font-size: 0.95rem;
font-variant-numeric: tabular-nums;
}
.odo-display .odo-digit {
display: inline-block;
min-width: 1.3ch;
padding: 0.12em 0.08em;
text-align: center;
background: #1c1c1e;
color: #f2f2f3;
border-radius: 3px;
border-bottom: 2px solid #000;
}
.odo-display .odo-sep {
width: 0.3ch;
}
.odo-caption {
margin-top: 0.35rem;
font-size: 0.75rem;
}
img {
max-width: 100%;
height: auto;
}
pre {
padding: 0.9rem 1rem;
border-radius: 6px;
overflow-x: auto;
font-size: 0.88rem;
}
:not(pre) > code {
background: var(--faint);
padding: 0.1em 0.35em;
border-radius: 4px;
font-size: 0.9em;
}
table {
border-collapse: collapse;
display: block;
overflow-x: auto;
max-width: 100%;
}
th, td {
border: 1px solid var(--faint);
padding: 0.35rem 0.6rem;
}
blockquote {
margin: 1rem 0;
padding: 0.1rem 1rem;
border-left: 3px solid var(--accent2);
color: var(--muted);
}
.callout {
border-left-width: 4px;
border-radius: 4px;
background: color-mix(in srgb, var(--accent2) 8%, transparent);
}
.callout-title {
color: var(--accent);
}
.callout-warning, .callout-attention, .callout-important {
border-left-color: #c9803a;
}
.callout-warning .callout-title,
.callout-attention .callout-title,
.callout-important .callout-title {
color: #c9803a;
}
/* dark-mode-aware shiki dual themes */
@media (prefers-color-scheme: dark) {
.astro-code,
.astro-code span {
color: var(--shiki-dark) !important;
background-color: var(--shiki-dark-bg) !important;
}
}
</style>
</body>
</html>