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

Make /browse work with a thousand products

Youssef3cf1fb4683d6
diff --git a/src/app/browse/page.tsx b/src/app/browse/page.tsx
index 9c9e1e2..fa9accf 100644
--- a/src/app/browse/page.tsx
+++ b/src/app/browse/page.tsx
@@ -3,6 +3,7 @@ import SiteHeader from "@/components/SiteHeader";
import SiteFooter from "@/components/SiteFooter";
import { Catalog } from "@/components/home/Sections";
import { PRODUCT_COUNT } from "@/lib/search";
+import { PRODUCTS } from "@/lib/catalog";
export const metadata: Metadata = { title: "All guides | Bygga" };
@@ -13,7 +14,7 @@ export default function Page() {
<main className="browse">
<header className="page-head">
<h1>All guides</h1>
- <p className="lead">{PRODUCT_COUNT} IKEA products, drawn and ready to build. Not listed? Search for it by name on the home page.</p>
+ <p className="lead">{PRODUCT_COUNT} IKEA products in {PRODUCTS.length} sizes, drawn and ready to build. Filter by name or by the article number on the box. Not listed? Search for it by name on the home page.</p>
</header>
<Catalog search />
</main>
diff --git a/src/app/home.css b/src/app/home.css
index d5296d7..937605c 100644
--- a/src/app/home.css
+++ b/src/app/home.css
@@ -249,6 +249,8 @@
.search-box input {
flex: 1;
min-width: 0;
+ /* a text box is as wide as its size attribute unless told otherwise, which pushed phones sideways */
+ width: 0;
border: 0;
outline: none;
background: transparent;
@@ -683,6 +685,13 @@
.cat-tab.on {
color: var(--ink);
}
+.cat-n {
+ margin-left: 6px;
+ font-size: 12px;
+ font-weight: 500;
+ color: var(--ink-3);
+ font-variant-numeric: tabular-nums;
+}
.cat-bg {
position: absolute;
inset: 0;
@@ -790,6 +799,10 @@
.pcard-variant {
color: var(--ink-2);
font-size: 14.5px;
+ display: -webkit-box;
+ -webkit-line-clamp: 2;
+ -webkit-box-orient: vertical;
+ overflow: hidden;
}
.pcard-meta {
display: flex;
@@ -803,12 +816,50 @@
display: inline-flex;
align-items: center;
gap: 5px;
+ white-space: nowrap;
+}
+.pcard-meta .pcard-cat {
+ min-width: 0;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ display: block;
}
.empty {
color: var(--ink-2);
text-align: center;
padding: 40px 0;
}
+.cat-more {
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ gap: 10px;
+ padding: 18px 0 8px;
+ font-size: 13px;
+ color: var(--ink-3);
+ font-variant-numeric: tabular-nums;
+}
+.cat-more-btn {
+ height: 46px;
+ padding: 0 26px;
+ border: 0;
+ border-radius: 999px;
+ background: #fff;
+ box-shadow: 0 0 0 1px var(--line-2);
+ font-weight: 600;
+ font-size: 15px;
+ color: var(--ink);
+ cursor: pointer;
+ transition:
+ box-shadow 0.2s,
+ transform 0.16s var(--ease);
+}
+.cat-more-btn:hover {
+ box-shadow: 0 0 0 1.5px var(--ink);
+}
+.cat-more-btn:active {
+ transform: scale(0.97);
+}
/* ================= floor ================= */
@@ -1509,6 +1560,21 @@
.pgrid {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
+ /* the tabs scroll in their own row instead of wrapping or widening the page */
+ .cat-tabs {
+ flex: 1 1 100%;
+ min-width: 0;
+ flex-wrap: nowrap;
+ overflow-x: auto;
+ max-width: 100%;
+ scrollbar-width: none;
+ }
+ .cat-tabs::-webkit-scrollbar {
+ display: none;
+ }
+ .cat-tab {
+ flex: none;
+ }
.floor-key {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
@@ -1606,16 +1672,40 @@
align-items: flex-start;
gap: 6px;
}
+ /* two cards a row on phones, with smaller type */
.pgrid {
- grid-template-columns: 1fr;
+ gap: 10px;
}
- .cat-tabs {
- flex: 1 1 100%;
- min-width: 0;
- flex-wrap: nowrap;
- overflow-x: auto;
- max-width: 100%;
- scrollbar-width: none;
+ .pcard {
+ border-radius: 18px;
+ }
+ .pcard-art {
+ padding: 7px 7px 0;
+ }
+ .pcard-thumb {
+ border-radius: 12px;
+ }
+ .pcard-text {
+ padding: 2px 11px 13px;
+ }
+ .pcard-top b {
+ font-size: 16px;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ }
+ .pcard-go,
+ .pcard-meta .pcard-cat {
+ display: none;
+ }
+ .pcard-variant {
+ font-size: 12.5px;
+ line-height: 1.35;
+ }
+ .pcard-meta {
+ gap: 10px;
+ margin-top: 8px;
+ font-size: 12px;
}
.cat-search {
width: 100%;
diff --git a/src/components/home/Search.tsx b/src/components/home/Search.tsx
index 57e4b24..41f23fb 100644
--- a/src/components/home/Search.tsx
+++ b/src/components/home/Search.tsx
@@ -3,8 +3,8 @@
import { useEffect, useMemo, useRef, useState } from "react";
import { useRouter } from "next/navigation";
import { AnimatePresence, motion } from "motion/react";
-import { searchCatalog, slugify, matchCatalog } from "@/lib/search";
-import { CATALOG, toSpec } from "@/lib/catalog";
+import { articleNo, articlesReady, loadArticles, searchCatalog, slugify, matchCatalog } from "@/lib/search";
+import { catalogSpec } from "@/lib/catalog";
import { buildPlan } from "@/lib/plan";
import Thumb from "../Thumb";
import { keyHeaders } from "@/lib/userKey";
@@ -55,17 +55,24 @@ export default function Search({ ai, autoFocus }: { ai: boolean; autoFocus?: boo
return () => clearTimeout(t);
}, [q]);
- const hits = useMemo(() => (q.trim() ? searchCatalog(q, 6) : []), [q]);
- const exact = useMemo(() => (q.trim() ? matchCatalog(q) : null), [q]);
+ // article numbers live in their own chunk: fetch it once someone types a few digits
+ const [articles, setArticles] = useState(articlesReady);
+ const digits = /\d{3}/.test(q);
+ useEffect(() => {
+ if (digits && !articles) loadArticles().then(() => setArticles(true));
+ }, [digits, articles]);
+ const hits = useMemo(() => (q.trim() ? searchCatalog(q, 6) : []), [q, articles]); // eslint-disable-line react-hooks/exhaustive-deps
+ const exact = useMemo(() => (q.trim() ? matchCatalog(q) : null), [q, articles]); // eslint-disable-line react-hooks/exhaustive-deps
const showAi = q.trim().length >= 2 && !exact;
const rows = hits.length + (showAi ? 1 : 0);
- function go(target?: string) {
+ async function go(target?: string) {
const text = (target ?? q).trim();
if (!text) {
input.current?.focus();
return;
}
+ if (articleNo(text)) await loadArticles().catch(() => {});
const m = matchCatalog(text);
if (m?.slug) router.push(`/build/${m.slug}`);
else router.push(`/build/${slugify(text)}?q=${encodeURIComponent(text)}`);
@@ -198,8 +205,7 @@ function SuggestRow({ slug, name, variant, minutes, active, onPick, onHover }: {
}
function matchSlug(slug: string) {
- const e = CATALOG.find((c) => c.slug === slug);
- return e ? toSpec(e) : null;
+ return catalogSpec(slug);
}
async function shrink(file: File): Promise<string> {
diff --git a/src/components/home/Sections.tsx b/src/components/home/Sections.tsx
index 42b0c4a..28f30d2 100644
--- a/src/components/home/Sections.tsx
+++ b/src/components/home/Sections.tsx
@@ -3,25 +3,37 @@
import { useEffect, useMemo, useState } from "react";
import Link from "next/link";
import { AnimatePresence, motion } from "motion/react";
-import { POPULAR, fold } from "@/lib/search";
+import { POPULAR, articleNo, fold, loadArticles, searchText } from "@/lib/search";
+import { articleSlug } from "@/lib/ikea";
import { PRODUCTS, toSpec, type CatalogEntry } from "@/lib/catalog";
import { buildPlan } from "@/lib/plan";
-import type { Archetype } from "@/lib/types";
+import type { Kind } from "@/lib/products";
import Thumb from "../Thumb";
import { getUserKey } from "@/lib/userKey";
import { Dots } from "../guide/ui";
import { IArrowRight, IClock, ISearch } from "../art/Icons";
-export const CATS: { id: string; label: string; arch: Archetype[] }[] = [
- { id: "all", label: "All", arch: [] },
- { id: "shelves", label: "Shelves", arch: ["shelf"] },
- { id: "drawers", label: "Drawers", arch: ["drawers"] },
- { id: "storage", label: "Storage", arch: ["cabinet"] },
- { id: "tables", label: "Tables", arch: ["table"] },
- { id: "beds", label: "Beds", arch: ["bed"] },
- { id: "seating", label: "Seating", arch: ["seat", "sofa"] },
+/** Tabs over the kinds of furniture. Kids and Outdoor cut across them, by keyword. */
+export const CATS: { id: string; label: string; kinds?: Kind[]; tag?: string }[] = [
+ { id: "all", label: "All" },
+ { id: "shelves", label: "Shelves", kinds: ["cube", "bookcase", "open", "wallshelf"] },
+ { id: "drawers", label: "Drawers", kinds: ["chest", "nightstand"] },
+ { id: "wardrobes", label: "Wardrobes", kinds: ["wardrobe"] },
+ { id: "storage", label: "Cabinets", kinds: ["cabinet", "sideboard", "tv", "shoe", "wallcab", "kitchen"] },
+ { id: "tables", label: "Tables", kinds: ["dining", "coffee", "side", "bar", "console", "trolley"] },
+ { id: "desks", label: "Desks", kinds: ["desk"] },
+ { id: "beds", label: "Beds", kinds: ["bed"] },
+ { id: "seating", label: "Chairs", kinds: ["chair", "stool", "bench"] },
+ { id: "sofas", label: "Sofas", kinds: ["sofa", "armchair"] },
+ { id: "kids", label: "Kids", kinds: ["kidstable", "kidschair"], tag: "kids" },
+ { id: "outdoor", label: "Outdoor", tag: "outdoor" },
];
+const inCat = (e: CatalogEntry, c: (typeof CATS)[number]) => (!c.kinds && !c.tag) || (!!e.kind && !!c.kinds?.includes(e.kind)) || (!!c.tag && !!e.keywords?.includes(c.tag));
+
+/** Cards added per "Show more": a multiple of the 2 and 3 column grids. */
+const PAGE = 24;
+
export function ProductCard({ e, i = 0 }: { e: CatalogEntry; i?: number }) {
const lazyPlan = useMemo(() => () => buildPlan(toSpec(e)), [e]);
return (
@@ -43,7 +55,7 @@ export function ProductCard({ e, i = 0 }: { e: CatalogEntry; i?: number }) {
<IClock size={15} /> {e.minutes < 60 ? `${e.minutes} min` : `${Math.round(e.minutes / 6) / 10} h`}
</span>
<Dots value={e.difficulty} />
- <span>{e.category}</span>
+ <span className="pcard-cat">{e.category}</span>
</span>
</span>
</Link>
@@ -51,18 +63,45 @@ export function ProductCard({ e, i = 0 }: { e: CatalogEntry; i?: number }) {
);
}
-/** Category tabs plus a grid. On the home page it shows a short pick; /browse shows everything with a filter. */
+/**
+ * Category tabs plus a grid. On the home page it shows a short pick; /browse shows everything with a filter,
+ * a page of cards at a time (each card's drawing renders only while it is on screen).
+ */
export function Catalog({ limit, search }: { limit?: number; search?: boolean }) {
const [cat, setCat] = useState("all");
const [q, setQ] = useState("");
+ const [more, setMore] = useState({ view: "", n: PAGE });
+ const [articles, setArticles] = useState(false);
+ const no = search ? articleNo(q) : null;
+
+ useEffect(() => {
+ if (no && !articles) loadArticles().then(() => setArticles(true));
+ }, [no, articles]);
+
+ const counts = useMemo(() => (search ? new Map(CATS.map((c) => [c.id, PRODUCTS.filter((e) => inCat(e, c)).length])) : null), [search]);
const list = useMemo(() => {
const c = CATS.find((x) => x.id === cat)!;
- let l = c.arch.length ? PRODUCTS.filter((p) => c.arch.includes(p.archetype)) : limit ? POPULAR : PRODUCTS;
+ let l = c.kinds || c.tag ? PRODUCTS.filter((e) => inCat(e, c)) : limit ? POPULAR : PRODUCTS;
const f = fold(q);
- if (f) l = l.filter((p) => fold([p.name, p.variant, p.category, ...(p.keywords ?? [])].join(" ")).includes(f));
- if (!limit) l = [...l].sort((a, b) => a.name.localeCompare(b.name, "sv"));
+ if (no) {
+ const slug = articles ? articleSlug(no) : null;
+ l = l.filter((e) => e.slug === slug);
+ } else if (f) {
+ // every word starts a word of the product's name, type, category or keywords
+ const words = f.split(" ");
+ l = l.filter((e) => {
+ const text = searchText(e);
+ return words.every((w) => text.includes(" " + w));
+ });
+ }
+ if (!limit) l = [...l].sort((a, b) => a.name.localeCompare(b.name, "sv") || a.variant.localeCompare(b.variant, "sv"));
return limit ? l.slice(0, limit) : l;
- }, [cat, q, limit]);
+ }, [cat, q, limit, no, articles]);
+
+ const view = cat + "|" + q;
+ const n = limit ? list.length : more.view === view ? more.n : PAGE;
+ const shown = list.slice(0, n);
+
return (
<div className="catalog">
<div className="cat-bar">
@@ -71,24 +110,35 @@ export function Catalog({ limit, search }: { limit?: number; search?: boolean })
<button key={c.id} role="tab" aria-selected={cat === c.id} className={"cat-tab" + (cat === c.id ? " on" : "")} onClick={() => setCat(c.id)}>
{cat === c.id && <motion.i layoutId={"cat-bg" + (search ? "b" : "h")} className="cat-bg" transition={{ type: "spring", stiffness: 500, damping: 40 }} />}
<span>{c.label}</span>
+ {counts && <small className="cat-n">{counts.get(c.id)}</small>}
</button>
))}
</div>
{search && (
<label className="cat-search">
<ISearch size={18} />
- <input value={q} onChange={(e) => setQ(e.target.value)} placeholder="Filter by name" aria-label="Filter products" />
+ <input value={q} onChange={(e) => setQ(e.target.value)} placeholder="Filter by name or article number" aria-label="Filter products" enterKeyHint="search" />
</label>
)}
</div>
<motion.div layout className="pgrid">
<AnimatePresence mode="popLayout">
- {list.map((e, i) => (
+ {shown.map((e, i) => (
<ProductCard key={e.slug} e={e} i={i} />
))}
</AnimatePresence>
</motion.div>
- {list.length === 0 && <p className="empty">Nothing matches. Try another name, or search for it on the home page.</p>}
+ {list.length === 0 && <p className="empty">{no && !articles ? "Looking up that article number" : no ? "We don't have a guide for that article number yet. Try the name on the box." : "Nothing matches. Try another name, or search for it on the home page."}</p>}
+ {!limit && list.length > shown.length && (
+ <div className="cat-more">
+ <button type="button" className="cat-more-btn" onClick={() => setMore({ view, n: n + PAGE })}>
+ Show {Math.min(PAGE, list.length - shown.length)} more
+ </button>
+ <span>
+ {shown.length} of {list.length}
+ </span>
+ </div>
+ )}
</div>
);
}