wip: worker & pages

This commit is contained in:
lyc8503 committed 2023-10-14 15:21:52 +08:00
1 parent c67ea1bd65
commit cdd43685f2
18 files changed
+4118 -118

No files matched your search

+59 -97
View File
@@ -1,114 +1,76 @@
import Head from 'next/head'
import Image from 'next/image'
import { Inter } from 'next/font/google'
import styles from '@/styles/Home.module.css'
const inter = Inter({ subsets: ['latin'] })
export default function Home() {
import { MonitorState } from '@/uptime.types'
import { KVNamespace } from '@cloudflare/workers-types'
import config from '@/uptime.config'
export const runtime = 'experimental-edge'
export default function Home({ state }: { state: MonitorState }) {
// Calculate overall status
// TODO: Move this to a component
let downCount = 0
let upCount = 0
for (const monitor of config.monitors) {
if (state.history[monitor.id]?.slice(-1)[0].up) {
upCount += 1
} else {
downCount += 1
}
}
let overallStatus = ""
if (downCount === 0 && upCount === 0) {
overallStatus = "No monitors configured"
}
if (downCount === 0) {
overallStatus = "All systems operational"
}
if (upCount === 0) {
overallStatus = "All systems not operational"
}
if (upCount > 0 && downCount > 0) {
overallStatus = "Some systems not operational"
}
return (
<>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>{config.page.title}</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={`${styles.main} ${inter.className}`}>
<div className={styles.description}>
<p>
Get started by editing&nbsp;
<code className={styles.code}>pages/index.tsx</code>
</p>
<div>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
By{' '}
<Image
src="/vercel.svg"
alt="Vercel Logo"
className={styles.vercelLogo}
width={100}
height={24}
priority
/>
</a>
</div>
</div>
<div className={styles.center}>
<Image
className={styles.logo}
src="/next.svg"
alt="Next.js Logo"
width={180}
height={37}
priority
/>
</div>
<main>
<h1>Uptime Flare</h1>
<div className={styles.grid}>
<a
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className={styles.card}
target="_blank"
rel="noopener noreferrer"
>
<h2>
Docs <span>-&gt;</span>
</h2>
<p>
Find in-depth information about Next.js features and&nbsp;API.
</p>
</a>
<h2>Current Status: {overallStatus}</h2>
<h2>Last updated on: {state.lastUpdate.toString()}</h2>
<a
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className={styles.card}
target="_blank"
rel="noopener noreferrer"
>
<h2>
Learn <span>-&gt;</span>
</h2>
<p>
Learn about Next.js in an interactive course with&nbsp;quizzes!
</p>
</a>
<a
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className={styles.card}
target="_blank"
rel="noopener noreferrer"
>
<h2>
Templates <span>-&gt;</span>
</h2>
<p>
Discover and deploy boilerplate example Next.js&nbsp;projects.
</p>
</a>
<a
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className={styles.card}
target="_blank"
rel="noopener noreferrer"
>
<h2>
Deploy <span>-&gt;</span>
</h2>
<p>
Instantly deploy your Next.js site to a shareable URL
with&nbsp;Vercel.
</p>
</a>
</div>
{
config.monitors.map(monitor => (
<div key={monitor.id}>
<h2>{monitor.name}</h2>
</div>
))
}
<p>{JSON.stringify(state)}</p>
</main>
</>
)
}
export async function getServerSideProps() {
const { UPTIMEFLARE_STATE } = process.env as unknown as { UPTIMEFLARE_STATE: KVNamespace }
const state = await UPTIMEFLARE_STATE.get('state', 'json') as unknown as MonitorState
return { props: { state } }
}