1
0
mirror of https://github.com/tormachris/cf-workers-status-page.git synced 2024-11-27 16:05:41 +01:00
cf-workers-status-page/pages/index.js

115 lines
3.5 KiB
JavaScript
Raw Normal View History

import { Store } from 'laco'
import { useStore } from 'laco-react'
2020-11-08 13:56:02 +01:00
import Head from 'flareact/head'
import { getKVMonitors, useKeyPress } from '../src/functions/helpers'
2020-11-08 13:56:02 +01:00
import config from '../config.yaml'
import MonitorCard from '../src/components/monitorCard'
2020-11-17 00:50:19 +01:00
import MonitorFilter from '../src/components/monitorFilter'
import MonitorStatusHeader from '../src/components/monitorStatusHeader'
import ThemeSwitcher from '../src/components/themeSwitcher'
2020-11-17 00:50:19 +01:00
const MonitorStore = new Store({
monitors: config.monitors,
visible: config.monitors,
activeFilter: false,
})
2020-11-17 00:50:19 +01:00
const filterByTerm = (term) =>
MonitorStore.set((state) => ({
visible: state.monitors.filter((monitor) =>
monitor.name.toLowerCase().includes(term),
),
}))
2020-11-08 13:56:02 +01:00
export async function getEdgeProps() {
// get KV data
const kvMonitors = await getKVMonitors()
2020-11-08 13:56:02 +01:00
return {
props: {
config,
kvMonitors: kvMonitors ? kvMonitors.monitors : {},
kvMonitorsLastUpdate: kvMonitors ? kvMonitors.lastUpdate : {},
2020-11-08 13:56:02 +01:00
},
// Revalidate these props once every x seconds
revalidate: 5,
}
}
export default function Index({ config, kvMonitors, kvMonitorsLastUpdate }) {
2020-11-17 00:50:19 +01:00
const state = useStore(MonitorStore)
const slash = useKeyPress('/')
2020-11-08 13:56:02 +01:00
return (
<div className="min-h-screen">
2020-11-08 13:56:02 +01:00
<Head>
<title>{config.settings.title}</title>
<link rel="stylesheet" href="./style.css" />
<script>
{`
function setTheme(theme) {
document.documentElement.classList.remove("dark", "light")
document.documentElement.classList.add(theme)
localStorage.theme = theme
}
(() => {
const query = window.matchMedia("(prefers-color-scheme: dark)")
query.addListener(() => {
setTheme(query.matches ? "dark" : "light")
})
if (["dark", "light"].includes(localStorage.theme)) {
setTheme(localStorage.theme)
} else {
setTheme(query.matches ? "dark" : "light")
}
})()
`}
</script>
2020-11-08 13:56:02 +01:00
</Head>
<div className="container mx-auto px-4">
<div className="flex flex-row justify-between items-center p-4">
<div className="flex flex-row items-center">
<img className="h-8 w-auto" src={config.settings.logo} />
<h1 className="ml-4 text-3xl">{config.settings.title}</h1>
</div>
<div className="flex flex-row items-center">
{typeof window !== 'undefined' && <ThemeSwitcher />}
<MonitorFilter active={slash} callback={filterByTerm} />
</div>
2020-11-17 00:50:19 +01:00
</div>
<MonitorStatusHeader kvMonitorsLastUpdate={kvMonitorsLastUpdate} />
2020-11-17 00:50:19 +01:00
{state.visible.map((monitor, key) => {
2020-11-08 13:56:02 +01:00
return (
<MonitorCard
key={key}
monitor={monitor}
data={kvMonitors[monitor.id]}
/>
2020-11-08 13:56:02 +01:00
)
})}
<div className="flex flex-row justify-between mt-4 text-sm">
2020-11-08 13:56:02 +01:00
<div>
Powered by{' '}
<a href="https://workers.cloudflare.com/" target="_blank">
Cloudflare Workers{' '}
</a>
&{' '}
<a href="https://flareact.com/" target="_blank">
Flareact{' '}
</a>
</div>
<div>
<a
2020-11-14 02:57:06 +01:00
href="https://github.com/eidam/cf-workers-status-page"
2020-11-08 13:56:02 +01:00
target="_blank"
>
Get Your Status Page
</a>
</div>
</div>
</div>
</div>
)
}