From 1767cde544c99f67de6d1466e4cf0cddb8f2c520 Mon Sep 17 00:00:00 2001 From: Mark Gibson Date: Thu, 14 Mar 2024 10:59:32 +0000 Subject: [PATCH] Support for _static sub folders Add fade animations for quote/tv --- handler.ts | 1 + lib/handle_route_static.ts | 2 ++ lib/handle_route_static_dir.ts | 13 ++++++++ lib/route_mapper.ts | 10 ++++++ routes.ts | 1 + routes/quote/_cron/generate_quote.ts | 4 +-- routes/quote/_static/tv.css | 50 ++++++++++++++++++++++++++++ routes/quote/_static/tv.js | 14 ++++++++ routes/quote/tv.tsx | 7 ++-- static/quote_tv.css | 28 ---------------- 10 files changed, 98 insertions(+), 32 deletions(-) create mode 100644 lib/handle_route_static_dir.ts create mode 100644 routes/quote/_static/tv.css create mode 100644 routes/quote/_static/tv.js delete mode 100644 static/quote_tv.css diff --git a/handler.ts b/handler.ts index f3b7f43..7c2758c 100644 --- a/handler.ts +++ b/handler.ts @@ -9,5 +9,6 @@ export default handle([ skip(404, 405), ), routes, + // TODO: Migrate /static to a /_static folder instead staticRoute("/", import.meta.resolve("./static")), ]); diff --git a/lib/handle_route_static.ts b/lib/handle_route_static.ts index 3346720..a463e4e 100644 --- a/lib/handle_route_static.ts +++ b/lib/handle_route_static.ts @@ -1,6 +1,8 @@ import { byMethod } from "@http/fns/by_method"; import { fetchContent } from "../lib/content.ts"; +// TODO: Migrate all static content that relies on this to a _static folder instead + export default byMethod({ GET: rawContent, }); diff --git a/lib/handle_route_static_dir.ts b/lib/handle_route_static_dir.ts new file mode 100644 index 0000000..0b1cc6c --- /dev/null +++ b/lib/handle_route_static_dir.ts @@ -0,0 +1,13 @@ +import { byMethod } from "@http/fns/by_method"; +import { fetchContent } from "../lib/content.ts"; + +export default byMethod({ + GET: rawContent, +}); + +function rawContent(_req: Request, match: URLPatternResult) { + const path = match.pathname.groups.path ?? ""; + const prefix = match.pathname.input.slice(0, -path.length); + const route = `../routes${prefix}_static/${path}`; + return fetchContent(import.meta.resolve(route)); +} diff --git a/lib/route_mapper.ts b/lib/route_mapper.ts index e03f8ab..9658bb2 100644 --- a/lib/route_mapper.ts +++ b/lib/route_mapper.ts @@ -3,9 +3,18 @@ import type { DiscoveredRoute, } from "@http/fns/discover_routes"; +// TODO: Fix generateRoutesModule to remove duplicate routes + export default function routeMapper( { parentPath, name, ext, pattern, module }: DiscoveredPath, ): DiscoveredRoute[] { + if (/[/\\]_static/.test(parentPath)) { + return [{ + pattern: pattern.replace(/_static\/.*/, ":path+"), + module: import.meta.resolve("./handle_route_static_dir.ts"), + }]; + } + // Skip any route that has a path segment that starts with an underscore if (name.startsWith("_") || /[/\\]_/.test(parentPath)) { return []; @@ -25,6 +34,7 @@ export default function routeMapper( : `${pattern}{.:ext}?`, module: import.meta.resolve("./handle_route_md.tsx"), }]; + // TODO: Migrate png/svg files into _static folders case ".png": case ".svg": return [{ diff --git a/routes.ts b/routes.ts index 8934053..33edbe3 100644 --- a/routes.ts +++ b/routes.ts @@ -15,6 +15,7 @@ export default cascade( byPattern("/sse", lazy(() => import("./routes/sse/index.tsx"))), byPattern("/sleep", lazy(() => import("./routes/sleep.ts"))), byPattern("/quote/tv", lazy(() => import("./routes/quote/tv.tsx"))), + byPattern(["/quote/:path+","/quote/:path+"], lazy(() => import("./lib/handle_route_static_dir.ts"))), byPattern("/quote", lazy(() => import("./routes/quote/index.tsx"))), byPattern("/quiz/answer/:id/:answer", lazy(() => import("./routes/quiz/answer/:id/:answer.tsx"))), byPattern("/quiz", lazy(() => import("./routes/quiz/index.tsx"))), diff --git a/routes/quote/_cron/generate_quote.ts b/routes/quote/_cron/generate_quote.ts index b95bd51..e6a16cd 100644 --- a/routes/quote/_cron/generate_quote.ts +++ b/routes/quote/_cron/generate_quote.ts @@ -4,7 +4,7 @@ import OpenAI from "npm:openai"; export const name = "Generate a new quote of the moment"; -export const schedule = Deno.env.get("QUOTE_SCHEDULE") ?? "*/15 * * * *"; +export const schedule = Deno.env.get("QUOTE_SCHEDULE") ?? "*/5 * * * *"; export default async function generateQuote() { if (await isQuoteUnseen()) { @@ -17,7 +17,7 @@ export default async function generateQuote() { const openai = new OpenAI(); const completion = await openai.chat.completions.create({ - model: "gpt-4", + model: "gpt-3.5-turbo", messages: [ { role: "user", diff --git a/routes/quote/_static/tv.css b/routes/quote/_static/tv.css new file mode 100644 index 0000000..cb050a3 --- /dev/null +++ b/routes/quote/_static/tv.css @@ -0,0 +1,50 @@ +html { + background: black; + color: white; + height: 100%; +} + +body { + margin: 0; + height: 100%; + opacity: 0; + transition: opacity 3s ease; +} + +body.ready { + opacity: 1; +} + +blockquote { + height: 100%; + display: flex; + flex-direction: column; + justify-content: center; +} + +blockquote p { + font-size: xxx-large; + font-family: Verdana, Geneva, Tahoma, sans-serif; + text-align: center; + padding: 1em; + transform: scale(1); + filter: blur(0.8px); +} + +.out blockquote p { + transform: scale(5); + filter: blur(20px); + transition: all 3s ease-in; +} + +blockquote footer { + text-align: end; + font-family: cursive; + font-style: italic; + text-shadow: 1px 1px 5px whitesmoke; +} + +.out blockquote footer { + filter: blur(20px); + transition: all 3s ease-in; +} \ No newline at end of file diff --git a/routes/quote/_static/tv.js b/routes/quote/_static/tv.js new file mode 100644 index 0000000..d5e81f7 --- /dev/null +++ b/routes/quote/_static/tv.js @@ -0,0 +1,14 @@ +document.body.classList.add("ready"); + +const refresh = + document.head.querySelector('meta[name="refresh"]')?.getAttribute( + "content", + ) ?? 1; + +setTimeout(() => { + document.body.classList.remove("ready"); + document.body.classList.add("out"); + document.body.addEventListener("transitionend", () => { + location.reload(); + }); +}, refresh * 1000); diff --git a/routes/quote/tv.tsx b/routes/quote/tv.tsx index 8e43462..6dd1951 100644 --- a/routes/quote/tv.tsx +++ b/routes/quote/tv.tsx @@ -2,16 +2,19 @@ import { Page } from "../../components/Page.tsx"; import { renderPage } from "../../lib/handle_page.ts"; import { byMethod } from "@http/fns/by_method"; import { Quote } from "./_components/Quote.tsx"; +import { getSearchValues } from "jsr:@http/fns@^0.6.3/request/search_values"; export default byMethod({ GET: (req, match: URLPatternResult) => { + const refresh = getSearchValues(req)("refresh")[0] ?? "420"; return renderPage(() => ( - - + + +