Move all code to old/ to prepare for Nuxt rewrite

This commit is contained in:
Hornwitser 2025-03-01 16:32:51 +01:00
parent 6007f4caeb
commit 51ff27c569
33 changed files with 0 additions and 1 deletions

View file

@ -0,0 +1,129 @@
"use server";
import webPush from "web-push";
import { Schedule } from "@/app/schedule/types";
import { readFile, writeFile } from "fs/promises";
import { broadcastUpdate } from "./streams";
webPush.setVapidDetails(
"mailto:webmaster@hornwitser.no",
process.env.VAPID_PUBLIC_KEY!,
process.env.VAPID_PRIVATE_KEY!,
)
async function sendPush(title: string, body: string) {
const payload = JSON.stringify({ title, body });
let subscriptions: PushSubscriptionJSON[];
try {
subscriptions = JSON.parse(await readFile("push-subscriptions.json", "utf-8"));
} catch (err: any) {
if (err.code !== "ENOENT") {
console.log(`Dropping "${payload}", no push subscribers`);
return;
}
subscriptions = [];
}
console.log(`Sending "${payload}" to ${subscriptions.length} subscribers`);
const removeIndexes = [];
for (let index = 0; index < subscriptions.length; index += 1) {
const subscription = subscriptions[index];
try {
await webPush.sendNotification(
subscription as webPush.PushSubscription,
payload,
{
TTL: 3600,
urgency: "high",
}
)
} catch (err: any) {
console.error("Received error sending push notice:", err.message, err?.statusCode)
console.error(err);
if (err?.statusCode >= 400 && err?.statusCode < 500) {
removeIndexes.push(index)
}
}
}
if (removeIndexes.length) {
console.log(`Removing indexes ${removeIndexes} from subscriptions`)
removeIndexes.reverse();
for (const index of removeIndexes) {
subscriptions.splice(index, 1);
}
await writeFile(
"push-subscriptions.json",
JSON.stringify(subscriptions, undefined, "\t"),
"utf-8"
);
}
console.log("Push notices sent");
}
export async function createEvent(formData: FormData) {
const schedule: Schedule = JSON.parse(await readFile("schedule.json", "utf-8"));
const id = formData.get("id") as string;
const name = formData.get("name") as string;
const description = formData.get("description") as string;
const start = formData.get("start") as string;
const end = formData.get("end") as string;
const location = formData.get("location") as string;
schedule.events.push({
name,
id,
description,
slots: [
{
id: `${id}-1`,
start: start + "Z",
end: end + "Z",
locations: [location],
}
]
});
broadcastUpdate(schedule);
await writeFile("schedule.json", JSON.stringify(schedule, null, "\t"), "utf-8");
await sendPush("New event", `${name} will start at ${start}`);
}
export async function modifyEvent(formData: FormData) {
const schedule: Schedule = JSON.parse(await readFile("schedule.json", "utf-8"));
const id = formData.get("id") as string;
const name = formData.get("name") as string;
const description = formData.get("description") as string;
const start = formData.get("start") as string;
const end = formData.get("end") as string;
const location = formData.get("location") as string;
const index = schedule.events.findIndex(event => event.id === id);
if (index === -1) {
throw Error("No such event");
}
const timeChanged = schedule.events[index].slots[0].start !== start + "Z";
schedule.events[index] = {
name,
id,
description,
slots: [
{
id: `${id}-1`,
start: start + "Z",
end: end + "Z",
locations: [location],
}
]
};
broadcastUpdate(schedule);
await writeFile("schedule.json", JSON.stringify(schedule, null, "\t"), "utf-8");
if (timeChanged)
await sendPush(`New time for ${name}`, `${name} will now start at ${start}`);
}
export async function deleteEvent(formData: FormData) {
const schedule: Schedule = JSON.parse(await readFile("schedule.json", "utf-8"));
const id = formData.get("id") as string;
const index = schedule.events.findIndex(event => event.id === id);
if (index === -1) {
throw Error("No such event");
}
schedule.events.splice(index, 1);
broadcastUpdate(schedule);
await writeFile("schedule.json", JSON.stringify(schedule, null, "\t"), "utf-8");
}

View file

@ -0,0 +1,31 @@
import { addStream, deleteStream } from "./streams";
export async function GET(request: Request) {
const encoder = new TextEncoder();
const source = request.headers.get("x-forwarded-for");
console.log(`starting event stream for ${source}`)
const stream = new TransformStream<string, Uint8Array>({
transform(chunk, controller) {
controller.enqueue(encoder.encode(chunk));
},
flush(controller) {
console.log(`finished event stream for ${source}`);
deleteStream(stream.writable);
},
// @ts-expect-error experimental API
cancel(reason) {
console.log(`cancelled event stream for ${source}`);
deleteStream(stream.writable);
}
})
addStream(stream.writable);
return new Response(
stream.readable,
{
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "text/event-stream",
}
}
);
}

View file

@ -0,0 +1,50 @@
import { Schedule } from "@/app/schedule/types";
function sendMessage(
stream: WritableStream<string>,
message: string,
) {
const writer = stream.getWriter();
writer.ready
.then(() => writer.write(message))
.catch(console.error)
.finally(() => writer.releaseLock())
;
}
declare global {
var streams: Set<WritableStream<string>>;
}
global.streams = global.streams ?? new Set<WritableStream<string>>();
let keepaliveInterval: ReturnType<typeof setInterval> | null = null
export function addStream(stream: WritableStream<string>) {
if (streams.size === 0) {
console.log("Starting keepalive")
keepaliveInterval = setInterval(sendKeepalive, 4000)
}
streams.add(stream);
}
export function deleteStream(stream: WritableStream<string>) {
streams.delete(stream);
if (streams.size === 0) {
console.log("Ending keepalive")
clearInterval(keepaliveInterval!);
}
}
export function broadcastUpdate(schedule: Schedule) {
const id = Date.now();
const data = JSON.stringify(schedule);
const message = `id: ${id}\nevent: update\ndata: ${data}\n\n`
console.log(`broadcasting update from ${process.pid} to ${streams.size} clients`);
for (const stream of streams) {
sendMessage(stream, message);
}
}
function sendKeepalive() {
for (const stream of streams) {
sendMessage(stream, "data: keepalive\n\n");
}
}

View file

@ -0,0 +1,29 @@
import { readFile, writeFile } from "fs/promises";
export async function POST(request: Request) {
const body: { subscription: PushSubscriptionJSON } = await request.json();
let subscriptions: PushSubscriptionJSON[];
try {
subscriptions = JSON.parse(await readFile("push-subscriptions.json", "utf-8"));
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
subscriptions = [];
}
const existingIndex = subscriptions.findIndex(sub => sub.endpoint === body.subscription.endpoint);
if (existingIndex !== -1) {
subscriptions[existingIndex] = body.subscription;
} else {
subscriptions.push(body.subscription);
}
await writeFile(
"push-subscriptions.json",
JSON.stringify(subscriptions, undefined, "\t"),
"utf-8"
);
if (existingIndex !== -1) {
return new Response(JSON.stringify({ message: "Existing subscription refreshed."}));
}
return new Response(JSON.stringify({ message: "New subscription registered."}));
}

View file

@ -0,0 +1,26 @@
import { readFile, writeFile } from "fs/promises";
export async function POST(request: Request) {
const body: { subscription: PushSubscriptionJSON } = await request.json();
let subscriptions: PushSubscriptionJSON[];
try {
subscriptions = JSON.parse(await readFile("push-subscriptions.json", "utf-8"));
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
subscriptions = [];
}
const existingIndex = subscriptions.findIndex(sub => sub.endpoint === body.subscription.endpoint);
if (existingIndex !== -1) {
subscriptions.splice(existingIndex, 1);
} else {
return new Response(JSON.stringify({ message: "No subscription registered."}));
}
await writeFile(
"push-subscriptions.json",
JSON.stringify(subscriptions, undefined, "\t"),
"utf-8"
);
return new Response(JSON.stringify({ message: "Existing subscription removed."}));
}

BIN
old/app/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

70
old/app/globals.css Normal file
View file

@ -0,0 +1,70 @@
:root {
--background: #ffffff;
--foreground: #171717;
}
@media (prefers-color-scheme: dark) {
:root {
--background: #0a0a0a;
--foreground: #ededed;
}
}
body {
padding-inline: 1rem;
color: var(--foreground);
background: var(--background);
font-family: Arial, Helvetica, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
ul, ol {
padding-inline-start: 1.5rem;
}
h1, h2, h3, h4 {
margin-block: 0.75em 0.25em;
}
@media (prefers-color-scheme: dark) {
html {
color-scheme: dark;
}
}
a {
text-decoration: none;
}
/* Enable hover only on non-touch devices */
@media (hover: hover) and (pointer: fine) {
a:hover {
color: color-mix(in oklab, var(--foreground), blue 50%);
text-decoration: underline;
text-underline-offset: 2px;
border-color: transparent;
}
}
label {
display: block;
}
label>* {
margin-inline-start: 0.5rem;
}
p + p {
margin-block-start: 0.5rem;
}
label + label {
margin-block-start: 0.5rem;
}

21
old/app/layout.tsx Normal file
View file

@ -0,0 +1,21 @@
import type { Metadata } from "next";
import "./globals.css";
export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body>
{children}
</body>
</html>
);
}

12
old/app/page.tsx Normal file
View file

@ -0,0 +1,12 @@
import Link from "next/link";
export default function Home() {
return <main>
<h1>Schedule demo</h1>
<ul>
<li>
<Link href="/schedule">Schedule demo</Link>
</li>
</ul>
</main>;
}

View file

@ -0,0 +1,39 @@
"use client";
import { createContext, useContext, useEffect, useState } from "react";
import { Schedule } from "./types";
const ScheduleContext = createContext<Schedule | null>(null);
interface ScheduleProviderProps {
children: React.ReactElement;
schedule: Schedule;
}
export function ScheduleProvider(props: ScheduleProviderProps) {
const [schedule, setSchedule] = useState(props.schedule);
useEffect(() => {
console.log("Opening event source")
const source = new EventSource("/api/events");
source.addEventListener("message", (message) => {
console.log("Message", message.data);
});
source.addEventListener("update", (message) => {
const updatedSchedule: Schedule = JSON.parse(message.data);
console.log("Update", updatedSchedule);
setSchedule(updatedSchedule);
});
return () => {
console.log("Closing event source")
source.close();
}
}, [])
return (
<ScheduleContext.Provider value={schedule}>
{props.children}
</ScheduleContext.Provider>
);
}
export function useSchedule() {
return useContext(ScheduleContext);
}

35
old/app/schedule/page.tsx Normal file
View file

@ -0,0 +1,35 @@
import Timetable from "@/ui/timetable"
import { Schedule } from "./types"
import { readFile } from "fs/promises"
import { ScheduleProvider } from "./context"
import { Events } from "@/ui/events";
import { Locations } from "@/ui/locations";
import { EventsEdit } from "@/ui/events-edit";
import { PushNotification } from "@/ui/push-notification";
export const dynamic = "force-dynamic";
export default async function page() {
const schedule: Schedule = JSON.parse(await readFile("schedule.json", "utf-8"));
return (
<ScheduleProvider schedule={schedule}>
<main>
<h1>Schedule & Events</h1>
<p>
Study carefully, we only hold these events once a year.
</p>
<p>
Get notified about updates
</p>
<PushNotification vapidPublicKey={process.env.VAPID_PUBLIC_KEY!} />
<h2>Schedule</h2>
<Timetable />
<EventsEdit />
<h2>Events</h2>
<Events />
<h2>Locations</h2>
<Locations />
</main>
</ScheduleProvider>
);
}

26
old/app/schedule/types.ts Normal file
View file

@ -0,0 +1,26 @@
export interface ScheduleEvent {
name: string,
id: string,
host?: string,
cancelled?: boolean,
description?: string,
slots: TimeSlot[],
}
export interface ScheduleLocation {
name: string,
id: string,
description?: string,
}
export interface TimeSlot {
id: string,
start: string,
end: string,
locations: string[],
}
export interface Schedule {
locations: ScheduleLocation[],
events: ScheduleEvent[],
}