Skip to content

armgit / bygga

Public Imported from GitHub
0

IKEA assembly, step by step: animated, manual-style 3D guides. Scan any paper manual with Together AI.

TypeScript

Continue chip on the home page, guides work offline

Youssefc30a50fc4e93
diff --git a/next.config.ts b/next.config.ts
index ea43aae..3c8be3d 100644
--- a/next.config.ts
+++ b/next.config.ts
@@ -12,6 +12,11 @@ const nextConfig: NextConfig = {
},
async headers() {
return [
+ {
+ // the service worker itself must never be stale
+ source: "/sw.js",
+ headers: [{ key: "Cache-Control", value: "no-cache" }],
+ },
{
// clips are named after their text, so a file only changes when it is re-recorded
source: "/voice/:path*",
diff --git a/public/sw.js b/public/sw.js
new file mode 100644
index 0000000..27936d8
--- /dev/null
+++ b/public/sw.js
@@ -0,0 +1,65 @@
+/*
+ * Keeps guides working with a bad signal (garages, basements, new flats).
+ * Pages: network first, the last copy when offline. Hashed build files and
+ * voice clips: cache first, they never change under the same name.
+ * The API is never cached here.
+ */
+const PAGES = "bygga-pages-v1";
+const FILES = "bygga-files-v1";
+const KEEP = [PAGES, FILES];
+
+self.addEventListener("install", () => self.skipWaiting());
+self.addEventListener("activate", (e) => {
+ e.waitUntil(
+ caches
+ .keys()
+ .then((keys) => Promise.all(keys.filter((k) => !KEEP.includes(k)).map((k) => caches.delete(k))))
+ .then(() => self.clients.claim()),
+ );
+});
+
+async function trim(name, max) {
+ const c = await caches.open(name);
+ const keys = await c.keys();
+ for (const k of keys.slice(0, Math.max(0, keys.length - max))) await c.delete(k);
+}
+
+self.addEventListener("fetch", (e) => {
+ const req = e.request;
+ if (req.method !== "GET") return;
+ const url = new URL(req.url);
+ if (url.origin !== location.origin || url.pathname.startsWith("/api/") || url.pathname.startsWith("/dev/")) return;
+ // media elements ask for byte ranges: let the network answer those
+ if (req.headers.has("range")) return;
+
+ if (req.mode === "navigate") {
+ e.respondWith(
+ fetch(req)
+ .then((res) => {
+ if (res.ok) {
+ const copy = res.clone();
+ caches.open(PAGES).then((c) => c.put(req, copy).then(() => trim(PAGES, 40)));
+ }
+ return res;
+ })
+ .catch(() => caches.match(req).then((hit) => hit || caches.match("/"))),
+ );
+ return;
+ }
+
+ if (url.pathname.startsWith("/_next/static/") || url.pathname.startsWith("/voice/") || /\.(woff2?|png|svg)$/.test(url.pathname)) {
+ e.respondWith(
+ caches.match(req).then(
+ (hit) =>
+ hit ||
+ fetch(req).then((res) => {
+ if (res.ok && res.type === "basic") {
+ const copy = res.clone();
+ caches.open(FILES).then((c) => c.put(req, copy).then(() => trim(FILES, 600)));
+ }
+ return res;
+ }),
+ ),
+ );
+ }
+});
diff --git a/src/app/home.css b/src/app/home.css
index e958760..914d2f9 100644
--- a/src/app/home.css
+++ b/src/app/home.css
@@ -395,6 +395,46 @@
border-radius: 12px;
background: var(--sunk);
}
+.resume {
+ display: inline-flex;
+ align-items: center;
+ gap: 12px;
+ margin-top: 14px;
+ padding: 8px 16px 8px 8px;
+ border-radius: 999px;
+ background: #fff;
+ box-shadow: var(--shadow-1);
+ color: var(--ink);
+ text-decoration: none;
+ transition:
+ box-shadow 0.2s,
+ transform 0.2s var(--ease);
+}
+.resume:hover {
+ box-shadow: inset 0 0 0 1.5px var(--ink);
+}
+.resume:active {
+ transform: scale(0.98);
+}
+.resume > span {
+ display: grid;
+ line-height: 1.25;
+}
+.resume b {
+ font-size: 15px;
+ font-weight: 600;
+}
+.resume span span {
+ font-size: 13.5px;
+ color: var(--ink-2);
+}
+.resume > svg:last-child {
+ margin-left: 6px;
+ transition: transform 0.25s var(--ease);
+}
+.resume:hover > svg:last-child {
+ transform: translateX(2px);
+}
.quick {
display: flex;
flex-wrap: wrap;
diff --git a/src/app/layout.tsx b/src/app/layout.tsx
index 306d80d..c451463 100644
--- a/src/app/layout.tsx
+++ b/src/app/layout.tsx
@@ -5,6 +5,7 @@ import "./guide.css";
import "./home.css";
import "./scan.css";
import { Analytics } from "@vercel/analytics/next";
+import Offline from "@/components/Offline";
import { SITE_URL } from "@/lib/site";
const noto = Noto_Sans({
@@ -46,6 +47,7 @@ export default function RootLayout({ children }: Readonly<{ children: React.Reac
<body>
{children}
<Analytics />
+ <Offline />
</body>
</html>
);
diff --git a/src/app/page.tsx b/src/app/page.tsx
index b774f1b..b92fcf9 100644
--- a/src/app/page.tsx
+++ b/src/app/page.tsx
@@ -3,6 +3,7 @@ import SiteHeader from "@/components/SiteHeader";
import SiteFooter from "@/components/SiteFooter";
import HeroStage from "@/components/home/HeroStage";
import { HeroSearch } from "@/components/home/HomeClient";
+import Resume from "@/components/home/Resume";
import { Catalog } from "@/components/home/Sections";
import FloorFeatures from "@/components/home/FloorFeatures";
import { Figure } from "@/components/art/Figure";
@@ -34,6 +35,7 @@ export default function Home() {
</h1>
<p className="lead">Type the product name from the box. Each step plays in 3D, with the exact parts, screws and tools.</p>
<HeroSearch />
+ <Resume />
<div className="quick">
<span>Try</span>
{QUICK.map((q) => (
diff --git a/src/components/Offline.tsx b/src/components/Offline.tsx
new file mode 100644
index 0000000..a09ed94
--- /dev/null
+++ b/src/components/Offline.tsx
@@ -0,0 +1,14 @@
+"use client";
+
+import { useEffect } from "react";
+
+/** Registers the service worker that keeps opened guides and their voice working offline. */
+export default function Offline() {
+ useEffect(() => {
+ if (process.env.NODE_ENV !== "production" || !("serviceWorker" in navigator)) return;
+ const go = () => navigator.serviceWorker.register("/sw.js").catch(() => {});
+ if (document.readyState === "complete") go();
+ else window.addEventListener("load", go, { once: true });
+ }, []);
+ return null;
+}
diff --git a/src/components/home/Resume.tsx b/src/components/home/Resume.tsx
new file mode 100644
index 0000000..4d87392
--- /dev/null
+++ b/src/components/home/Resume.tsx
@@ -0,0 +1,36 @@
+"use client";
+
+import Link from "next/link";
+import { useMemo } from "react";
+import { motion } from "motion/react";
+import { listProgress } from "@/lib/store";
+import { useMounted } from "@/lib/useMounted";
+import { IArrowRight } from "../art/Icons";
+
+/** For someone coming back mid-build: one tap back to the step they were on. */
+export default function Resume() {
+ const mounted = useMounted();
+ const p = useMemo(() => {
+ if (!mounted) return null;
+ return listProgress().find((x) => x.phase === "steps" || x.phase === "parts" || x.phase === "tools") ?? null;
+ }, [mounted]);
+ if (!p) return null;
+ const pct = p.phase === "steps" ? p.step / Math.max(1, p.steps) : 0.04;
+ const where = p.phase === "steps" ? `step ${p.step + 1} of ${p.steps}` : p.phase === "parts" ? "checking parts" : "checking tools";
+ const href = `/build/${p.slug}${p.spec ? `?q=${encodeURIComponent(p.spec.name)}` : ""}`;
+ return (
+ <motion.div initial={{ opacity: 0, y: 8 }} animate={{ opacity: 1, y: 0 }} transition={{ duration: 0.4, ease: [0.22, 1, 0.36, 1] }}>
+ <Link href={href} className="resume">
+ <svg viewBox="0 0 36 36" width={34} height={34} className="resume-ring" aria-hidden="true">
+ <circle cx="18" cy="18" r="15" fill="none" stroke="var(--line-2)" strokeWidth="3.5" />
+ <circle cx="18" cy="18" r="15" fill="none" stroke="var(--ink)" strokeWidth="3.5" strokeLinecap="round" strokeDasharray={`${Math.max(0.04, pct) * 94.25} 94.25`} transform="rotate(-90 18 18)" />
+ </svg>
+ <span>
+ <b>Continue {p.name}</b>
+ <span>You were on {where}</span>
+ </span>
+ <IArrowRight size={18} />
+ </Link>
+ </motion.div>
+ );
+}