155 Commits

Author SHA1 Message Date
hsiegeln
2f0a45f487 chore: bump package.json + package-lock auf 1.4.1
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 3m59s
Patch-Release: fix fuer $effect-Loop auf der Startseite (sort=viewed)
und Wunschliste-Card-Layout auf Mobile.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 16:24:58 +02:00
hsiegeln
a68b99c807 fix(wishlist): 2-Spalten-Grid auf Mobile statt stacked Footer
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m17s
Der stacked Card-Footer liess unter dem Bild (96x96) eine tote
Weissflaeche entstehen — Buttons rechts unten, links leer, unaufgeraeumt.

Neues Layout auf <=600px: Card ist 2-Spalten-Grid (96px | 1fr), Bild
spannt vertikal ueber alle Rows, rechts stapeln sich Titel -> Meta ->
Actions direkt untereinander. `display: contents` auf .body/.text zieht
die DOM-Kinder ohne Markup-Umbau in die Grid-Cells.

Ergebnis: Card-Hoehe orientiert sich am Content, keine toten Zonen,
Bild fuellt seinen Streifen vertikal, Buttons sitzen eng unter der Meta
(0.5rem padding) — tap-friendly ohne Kleben.

Getestet: svelte-check 0 errors.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 16:05:28 +02:00
hsiegeln
2573f80940 style(wishlist): Actions als Card-Footer ohne Trenner
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m15s
Auf Mobile wirkten die Buttons in der abgesetzten grauen Row mit
border-top etwas verloren. Nun sitzen sie im gleichen weissen
Card-Background ohne Border/Background-Wechsel — das Footer-Padding
reicht als visuelle Trennung, die Buttons gehoeren erkennbar zur Card.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 15:50:12 +02:00
hsiegeln
0a97ea2fea fix(wishlist): Card stacked auf Mobile, Titel-Overflow behoben
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m15s
Auf schmalen Viewports (~390px) ueberlagerten die drei Action-Buttons
den Titel: .text reservierte 170px padding-right, aber nach 96px Bild
+ Gaps blieb kaum Platz fuer den Titel — lange Woerter wie
"Spaetzle-Pfanne" liefen hinter die Buttons.

Fix: @media (max-width: 600px) — Card wird flex-direction:column,
Actions-Row rutscht aus position:absolute in eine statische Reihe mit
border-top unter dem Body, full-width. Zusaetzlich overflow-wrap +
word-break als Safety-Net gegen bindestrich-gefuellte Monstertitel.

Desktop-Layout unveraendert.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 15:39:12 +02:00
hsiegeln
12f499cb98 fix(home): $effect-Loop bei sort=viewed via untrack
Der Profile-Switch-Refetch-Effect las allLoading in der sync tracking-
Phase. Der await fetch beendete die Sync-Phase, das finale
allLoading = false im finally lief ausserhalb → wurde als externer
Write interpretiert → Effect rerun → naechster Fetch → Endlosschleife.

2136 GETs auf /api/recipes/all?sort=viewed in 8s beobachtet.

Fix: nur profileStore.active bleibt tracked (der tatsaechliche
Trigger). allSort/allLoading werden in untrack() gelesen — die Writes
auf allLoading im finally triggern damit keinen Effect-Rerun mehr.

Verifiziert lokal: 1 Request statt 2000+ bei mount mit allSort=viewed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 15:39:02 +02:00
hsiegeln
829850aa88 chore: bump package.json + package-lock auf 1.4.0
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 3m21s
Tag v1.4.0 ist schon gesetzt (auf 2b0bd4d), der synchrone Version-Bump
in package.json und package-lock.json wurde dabei vergessen. Mit dem
Pattern aus vorigen Releases (v1.2.0/v1.3.0) wieder konsistent.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:58:45 +02:00
hsiegeln
2b0bd4dc44 fix(recipe): View-Beacon ueber \$effect statt onMount
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 33s
Live-Test auf kochwas-dev: bei Hard-Reload/Cold-Start (nicht SPA-Click)
landete kein view-Eintrag in der DB. Ursache: Recipe-Page-onMount
feuert vor Layout-onMount, profileStore.load() laeuft aber im Layout-
onMount und macht erst danach einen async fetch — zum Zeitpunkt des
Beacons war profileStore.active noch null.

Loesung: Beacon im \$effect, das auf profileStore.active reagiert.
viewBeaconSent-Flag verhindert duplicate POSTs wenn der User waehrend
der Page-Lifetime das Profil wechselt — der ursprueglich getrackte
Profil-View bleibt der "richtige" fuer dieses Page-Open.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:50:54 +02:00
hsiegeln
e7318164cb fix(home): focus-visible auf section-head + scoped chev-CSS
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m14s
Code-Review zu commit 2216c89: button hatte keinen :focus-visible
Outline (Safari zeigt sonst gar nichts an) — Pattern aus dem Rest
der Page uebernommen (#2b6a3d Outline). Globale .chev-Selektoren
unter .section-head gescoped, damit andere Komponenten den Klassen-
Namen kuenftig wiederverwenden koennen ohne Konflikte.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:42:44 +02:00
hsiegeln
2216c89a04 feat(home): Collapsible Sections fuer Favoriten + Zuletzt hinzugefuegt
Header als <button> mit Chevron + Count-Pill, slide-Transition (180ms).
State in localStorage unter kochwas.collapsed.sections — JSON-Map
{favorites, recent}, default beide offen, corrupt-JSON faellt auf
Default zurueck.

Alle Rezepte bleibt absichtlich nicht-collapsibel — Hauptliste, immer
sichtbar.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:38:43 +02:00
hsiegeln
01d29bff0e feat(home): Sort-Chip 'Zuletzt angesehen' + Profile-Switch-Refetch
Neuer Wert 'viewed' im AllSort-Enum + ALL_SORTS-Array. localStorage-
Whitelist ergaenzt. Reactive $effect lauscht auf profileStore.active
und refetcht offset=0 nur wenn aktueller Sort 'viewed' ist — andere
Sortierungen sind profilunabhaengig.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:35:28 +02:00
hsiegeln
a5321d620a feat(home): profile_id in alle /api/recipes/all-Fetches
buildAllUrl-Helper haengt profile_id an wenn ein Profil aktiv ist;
nutzt es loadAllMore, setAllSort und rehydrateAll. Voraussetzung fuer
sort=viewed (Server braucht profile_id fuer den View-Join).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:33:42 +02:00
hsiegeln
b31223add5 feat(api): /api/recipes/all akzeptiert sort=viewed + profile_id
VALID_SORTS um 'viewed' erweitert. parseProfileId-Helper analog zu
/api/wishlist. Wert wird an listAllRecipesPaginated als 5. Param
durchgereicht.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:31:17 +02:00
hsiegeln
f495c024c6 feat(recipe): View-Beacon beim oeffnen der Detailseite
Fire-and-forget POST /api/recipes/[id]/view in onMount, nur wenn
profileStore.active gesetzt. Schreibt last_viewed_at fuers Profil —
Voraussetzung fuer den Sort 'Zuletzt angesehen' auf der Hauptseite.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:28:32 +02:00
hsiegeln
1214b9e01d refactor(api): parsePositiveIntParam fuer view-endpoint
Code-Review-Finding zu commit 82d4348: das Sibling-Endpoint
recipes/[id]/+server.ts nutzt schon parsePositiveIntParam aus
api-helpers.ts. Der neue View-Endpoint hatte die Logik inline
nachgebaut — jetzt aufgeraeumt fuer Konsistenz.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:27:10 +02:00
hsiegeln
82d4348873 feat(api): POST /api/recipes/[id]/view fuer View-Beacon
Body { profile_id } via zod validiert. FK-Violation (unbekanntes
Profil oder Rezept) wird zu 404 normalisiert. Erfolg liefert 204
ohne Body — fire-and-forget vom Client.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:23:36 +02:00
hsiegeln
6f54b004ca test(views): NULL-Tiebreaker explizit verifizieren
Code-Review-Finding zu commit 226ca5e: vorheriges Test seedete nur ein
NULL-Recipe, der alphabetische Tiebreaker fuer ungesehene Eintraege
wurde nicht exerciert. Zweites ungesehenes Rezept mit anderer
Einsatzreihenfolge ergaenzt — beweist dass Donauwelle vor
Zwiebelkuchen kommt.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:20:51 +02:00
hsiegeln
226ca5e5ed feat(search): sort=viewed in listAllRecipesPaginated
Neuer Sort 'viewed' macht LEFT JOIN gegen recipe_view, ordert nach
last_viewed_at DESC mit alphabetischem Tiebreaker. NULL-Recipes (nie
angesehen) landen alphabetisch sortiert hinter den angesehenen
(CASE-NULL-last statt SQLite 3.30+ NULLS LAST).

Ohne profileId faellt der Sort auf alphabetisch zurueck — Sort-Chip
bleibt klickbar, ergibt aber sinnvolles Default-Verhalten ohne
aktiviertes Profil.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:17:17 +02:00
hsiegeln
5357c9787b refactor(views): ON CONFLICT DO UPDATE statt INSERT OR REPLACE
Code-Review-Finding zu commit 6c8de6f: INSERT OR REPLACE ist intern
DELETE+INSERT, das wuerde eventuelle FK-Children kuenftig stillschweigend
mitloeschen. ON CONFLICT DO UPDATE bumpt nur das Timestamp-Feld und
matcht den Stil der anderen Repos (shopping/repository.ts:43).

Migration-Dateiname zu recipe_view (singular) angeglichen, matcht
jetzt den Tabellennamen aus 543008b.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:14:44 +02:00
hsiegeln
6c8de6fa3a feat(db): recordView/listViews fuer recipe_view
INSERT OR REPLACE fuer idempotenten Bump des last_viewed_at Timestamps.
listViews-Helper nur fuer Tests; Sort-Query laeuft direkt in
listAllRecipesPaginated via LEFT JOIN.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:10:52 +02:00
hsiegeln
866a222265 docs: plan/spec auf recipe_view (singular) angeglichen
Tabellen-Konvention im Repo ist singular — siehe Code-Review-Findings
zu Task 1 (commit 543008b). Plan und Spec angeglichen damit weitere
Tasks nicht mit dem alten Plural arbeiten.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:08:54 +02:00
hsiegeln
543008b0f2 refactor(db): recipe_views -> recipe_view, TIMESTAMP-Konsistenz
Code-Review-Findings nachgezogen: Tabellen-Konvention im Repo ist
singular (profile, recipe, favorite, cooking_log, thumbnail_cache),
deshalb recipe_view statt recipe_views; Index analog umbenannt.
last_viewed_at auf TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
gewechselt — matcht den Rest des Schemas. Header-Kommentar +
notnull-Assertion fuer recipe_id ergaenzt.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:08:17 +02:00
hsiegeln
2cd9b47450 feat(db): recipe_views table mit Profil-FK und Recent-Index
Tracking-Tabelle fuer Sort-Option Zuletzt angesehen. Composite-PK
(profile_id, recipe_id) erlaubt INSERT OR REPLACE per Default-Timestamp.
Index nach profile_id + last_viewed_at DESC fuer Sort-Query.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:04:27 +02:00
hsiegeln
98894bb895 docs(plan): Implementation-Plan fuer Views-Sort + Collapsibles
10 Tasks (Migration -> Repo -> Sort-Branch -> API -> Beacon -> URL-
Helper -> Sort-Chip + Reactive Refetch -> Collapsibles -> Push&Verify)
mit TDD-Schritten, exakten Filepfaden und vollstaendigem Code in
jedem Step.

Spec-Migrationsnummer auf 014 korrigiert (war 010 — letzte aktuelle
Migration ist 013_shopping_list).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 14:00:22 +02:00
hsiegeln
363ea6fbe7 docs(spec): Hauptseite Zuletzt-angesehen-Sort + Collapsibles
Spec fuer zwei Hauptseite-Features aus Brainstorming am 2026-04-22:

1) Neue Sort-Option "Zuletzt angesehen" fuer "Alle Rezepte". Tracking
   per Profil in neuer SQLite-Tabelle recipe_views, beim Laden der
   Detail-Seite per Beacon (POST /api/recipes/[id]/view) gesetzt.
   Server-Sort macht LEFT JOIN mit ORDER BY last_viewed_at DESC NULLS
   LAST, alphabetischer Tiebreaker.

2) "Deine Favoriten" und "Zuletzt hinzugefuegt" auf-/zuklappbar.
   Default offen, User-Wahl persistiert in localStorage pro Device.
   Header als button mit Chevron + Count-Pill, slide-Transition.
   "Alle Rezepte" bleibt nicht-collapsibel (Hauptliste).

Spec deckt Schema, API-Endpoint, DB-Layer, Markup-Pattern,
Reactive-Refetch bei Profile-Switch, Snapshot-Kompatibilitaet (rehydrate
muss profile_id mitbekommen), Test-Strategie und Reihenfolge.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 13:54:52 +02:00
hsiegeln
005c3ea7b5 fix(home): rehydrate-Trigger aus snapshot.restore, nicht ueber onMount
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m16s
Live-Test mit Fetch-Hook auf kochwas-dev hat bewiesen: bei tiefem
Endless-Scroll (60 Items) und Back-Nav fired rehydrateAll nie. Der
Logger zeigte nur limit=10&offset=0 plus IO-Trigger (10&offset=10).

Root Cause: SvelteKit ruft snapshot.restore *nach* onMount (post-mount
tick). Der vorherige Code parkte die Tiefe in pendingPagination und
liess onMount entscheiden — onMount lief aber zuerst, sah null, fiel
auf loadAllMore() zurueck. Restore setzte danach pendingPagination,
aber niemand las es mehr.

Fix: rehydrateAll direkt aus restore aufrufen (fire-and-forget).
onMount macht unkonditional loadAllMore() fuer den Fresh-Mount-Fall;
beide Pfade greifen ueber das allLoading-Flag bzw. ueber den
allRecipes-Overwrite (rehydrateAll's groesseres Result gewinnt
spaeter). Wasted-Fetch im Worst-Case: 10 Items (~2 KB) — vertretbar.

pendingPagination raus, onMount-Conditional vereinfacht.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 09:59:11 +02:00
hsiegeln
1d7731edbb chore: ci-log.txt und .claude/ aus dem Repo werfen
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m39s
In 0bfeba2 sind via git add -A versehentlich zwei lokale Artefakte
mitgewandert: ci-log.txt (lokaler CI-Log-Dump) und
.claude/scheduled_tasks.lock (Claude-Code Session-Lock). Beide gehören
nicht ins Repo. Per git rm --cached entfernt und in .gitignore
geblacklistet.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 09:42:53 +02:00
hsiegeln
0bfeba2c0a feat(home): Pagination-Tiefe per Snapshot, scroll-restore deep-scroll-fest
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
Bei tiefer Endless-Scroll (z. B. 60 nachgeladene Items) versagte der
generische scroll-restore: nach dem Back-Mount lud onMount() nur die
initialen 10 Items via loadAllMore(), das Dokument blieb ~1000px hoch
und der rAF-Poll konnte die gespeicherte scrollY (z. B. 4000) nie
erreichen — Best-Effort scrollTo clampte auf die erreichbare Hoehe.

Fix per SvelteKit-Snapshot, derselbe Mechanismus den die Page bereits
fuer SearchStore nutzt: Capture nimmt zusaetzlich allRecipes.length,
allSort und allExhausted mit. Restore setzt sort sofort und parkt die
Tiefe in pendingPagination. onMount sieht das Pending und ruft statt
loadAllMore() ein einmaliges rehydrateAll(sort, count, exhausted) —
ein Fetch mit limit=count rehydriert die ganze Liste atomar. Danach
hat das Dokument die Originalhoehe und der Layout-Restore-Poll laesst
die scrollY genau dort landen, wo der User vorher war.

API-Cap (src/routes/api/recipes/all/+server.ts) von 50 auf 200
angehoben — Recipe-Metadaten sind klein (~200 B/Stueck), 200er-
Response ~40 KB. Cap deckt realistische Scroll-Tiefen.

Reload (Cmd-R) behaelt das alte Verhalten: ohne Snapshot greift der
sort-aus-localStorage-Pfad, lade-Sequenz startet wieder bei 10.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 09:42:16 +02:00
hsiegeln
f3e2cebfb4 fix(nav): scroll-restore key auf nav.from.url, nicht location
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m51s
Live-Test auf kochwas-dev offenbarte: bei Browser-Back hat der Browser
die History bereits gepoppt, bevor SvelteKit beforeNavigate feuert —
location.pathname war damit schon die Ziel-URL. recordScroll() schrieb
also den 0-Wert der Recipe-Page in den Slot der Home-Page und wischte
den eigentlich gemerkten Wert (z. B. 500) raus. Restore las dann 0,
fiel unter MIN_RESTORE_Y und tat nichts.

Fix: recordScroll(nav.from?.url) und restoreScroll(type, nav.to?.url).
Helper bekommen die URL explizit reingereicht — keine Abhängigkeit
mehr von location und damit kein Race mit der Browser-History.

Tests: zusätzliche Regression "does not overwrite a stored URL when
called with a different from-url" plus Skip-Pfade fuer null URLs.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 09:28:20 +02:00
hsiegeln
442076a278 fix(nav): Scroll-Position bei Browser-Back robust wiederherstellen
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m44s
Pages, die ihre Daten in onMount per fetch laden (Home, Wunschliste,
Einkaufsliste), waren bei popstate-Navigation kaputt: SvelteKit ruft
scrollTo() synchron nach Mount, aber die Listen sind dann noch leer
und das Dokument zu kurz — der Browser clamped auf 0.

Neuer Helper src/lib/client/scroll-restore.ts merkt scrollY pro URL in
sessionStorage (beforeNavigate) und stellt sie bei popstate per rAF-
Polling wieder her, sobald document.scrollHeight gross genug ist
(Hard-Budget 1.5s, danach best-effort scrollTo).

Layout ruft die zwei Helper im beforeNavigate / afterNavigate. Pages
mit SSR-Daten (z. B. /recipes) bleiben unbeeinflusst — dort matcht
unser Wert SvelteKits eigenen scrollTo bereits beim ersten Frame.

Tests: 7 neue Unit-Tests in tests/unit/scroll-restore.test.ts decken
Recording, Pro-URL-Trennung, Skip fuer Forward-Nav, sofortiges und
verzoegertes Restore ab.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 09:18:00 +02:00
hsiegeln
4afc597689 fix(nav): Header-Back-Pfeil als echtes history.back()
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 3m19s
Der Zurück-Pfeil im Header war fest auf "/" verdrahtet und navigierte
forward, nicht backward. Damit ging die Scroll-Position der Origin-Seite
verloren und z. B. Wunschliste -> Rezept -> Zurück landete auf der
Startseite statt zurück auf der Wunschliste.

Jetzt: history.back() (mit goto('/') als Fallback bei leerer History).
SvelteKits eingebaute Scroll-Restoration greift dadurch wieder, und der
Pfeil tut was er optisch verspricht.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 09:09:45 +02:00
hsiegeln
42b1aed023 fix(shopping-e2e): beforeEach-Cleanup + checked-Count statt first-Row
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 32s
2026-04-22 08:57:17 +02:00
hsiegeln
a15390f4b8 fix(shopping): requireOnline-Guards + 2-space indent
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 3m14s
2026-04-21 23:59:14 +02:00
hsiegeln
52bb83cbd5 fix(shopping-e2e): exact match fuer Leeren-Confirm-Button
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 36s
2026-04-21 23:52:49 +02:00
hsiegeln
4e902b1d98 test(shopping): E2E-Spec + clearShoppingCart-Fixture
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 33s
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 23:50:05 +02:00
hsiegeln
0346a699b9 feat(shopping): Footer-Actions (Erledigte entfernen, Liste leeren)
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
2026-04-21 23:47:30 +02:00
hsiegeln
f4eac4d9c3 feat(shopping): Rezept-Chips mit Portions-Stepper
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
2026-04-21 23:45:32 +02:00
hsiegeln
3c30d1f35a feat(shopping): Zutaten-Rows mit Abhaken
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m17s
2026-04-21 23:43:00 +02:00
hsiegeln
943a645095 feat(shopping): Einkaufslisten-Seite mit Empty-State
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
2026-04-21 23:40:52 +02:00
hsiegeln
7fa1079125 refactor(wishlist): horizontale Actions + Einkaufswagen-Button
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m15s
2026-04-21 23:38:26 +02:00
hsiegeln
0e6d2c93a6 feat(shopping): Header-Badge mit Einkaufswagen-Icon
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m14s
2026-04-21 23:34:57 +02:00
hsiegeln
1bd5dd106f feat(shopping): ShoppingCartStore (Client)
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m16s
Svelte-5-Runes-Store mit uncheckedCount, recipeIds und loaded.
refresh() holt Snapshot via GET /api/shopping-list, addRecipe/
removeRecipe posten bzw. loeschen und refreshen anschliessend.
Bei Netzwerkfehler bleibt der letzte bekannte State erhalten.
2026-04-21 23:31:29 +02:00
hsiegeln
dc15cf04a9 feat(shopping): Service-Worker network-only fuer /api/shopping-list/*
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m25s
Einkaufsliste-Endpunkte duerfen vom SW nie gecached werden — Liste
ist zustaendig fuer Check-States und muss immer live vom Server
gelesen werden. Test + resolveStrategy-Erweiterung analog zu den
anderen online-only-Endpunkten.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 23:28:17 +02:00
hsiegeln
e53cdc96fe feat(shopping): DELETE /api/shopping-list/checked (Erledigte entfernen)
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
2026-04-21 23:26:32 +02:00
hsiegeln
a500a5623e feat(shopping): POST/DELETE /api/shopping-list/check
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
2026-04-21 23:25:05 +02:00
hsiegeln
2750c298e9 feat(shopping): PATCH/DELETE /api/shopping-list/recipe/[id]
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
2026-04-21 23:23:21 +02:00
hsiegeln
7baf60f422 feat(shopping): POST /api/shopping-list/recipe
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
2026-04-21 23:21:35 +02:00
hsiegeln
e176b8c3f2 style(shopping): GET/DELETE endpoint 2-space indent
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
2026-04-21 23:20:03 +02:00
hsiegeln
8570d41f53 feat(shopping): GET /api/shopping-list + DELETE (Liste leeren)
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
2026-04-21 23:18:24 +02:00
hsiegeln
76864a6034 feat(shopping): formatQuantity-Utility
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
2026-04-21 23:16:23 +02:00
hsiegeln
2c61d82935 feat(shopping): clearCart
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m14s
2026-04-21 23:13:58 +02:00
hsiegeln
974227590f feat(shopping): clearCheckedItems + Orphan-Cleanup
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m13s
2026-04-21 23:11:25 +02:00
hsiegeln
1889b0dea0 feat(shopping): toggleCheck (idempotent)
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m20s
Idempotentes Setzen/Loeschen von shopping_cart_check-Eintraegen
ueber (name_key, unit_key). Check ueberlebt Recipe-Removals,
solange ein anderes Rezept weiterhin zur Zeile beitraegt —
verifiziert durch 3 neue Integrationstests (17 total).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 23:08:20 +02:00
hsiegeln
494b672e8d fix(shopping): NULLIF-Guard gegen servings_default=0 in Aggregation
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
2026-04-21 23:06:19 +02:00
hsiegeln
c31a9c6110 feat(shopping): listShoppingList mit Aggregation + Skalierung
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m14s
2026-04-21 23:02:05 +02:00
hsiegeln
85bf197084 feat(shopping): setCartServings mit Positiv-Validation
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m15s
2026-04-21 22:59:12 +02:00
hsiegeln
83fe95ac76 feat(shopping): removeRecipeFromCart
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m27s
2026-04-21 22:56:26 +02:00
hsiegeln
95ba14ad6f refactor(shopping): DEFAULT_SERVINGS-Konstante + Kommentare
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
2026-04-21 22:54:54 +02:00
hsiegeln
8ceb5e95d7 feat(shopping): addRecipeToCart (idempotent via ON CONFLICT)
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m20s
2026-04-21 22:50:58 +02:00
hsiegeln
7dab267033 feat(shopping): Repository-Skeleton mit Types
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m16s
2026-04-21 22:47:21 +02:00
hsiegeln
45223df86d chore(db): Migration 013 fuer Einkaufsliste-Tabellen
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m20s
2026-04-21 22:43:50 +02:00
hsiegeln
fd5d759336 docs(plan): Implementierungs-Plan fuer Einkaufsliste
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 31s
24 Tasks, TDD, bite-sized. Reihenfolge: Migration -> Repository -> Quantity-Formatter
-> API -> Service-Worker -> Client-Store -> Header-Badge -> Wunschlisten-Relayout
-> Shopping-List-Seite -> E2E.

E2E-Tests laufen erst nach Deploy gegen kochwas-dev.siegeln.net.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 22:37:04 +02:00
hsiegeln
956357d5ca docs(spec): Einkaufsliste-Design
Neue Spec fuer das Einkaufslisten-Feature:
- Globale (haushaltsweite) Einkaufsliste, aus Rezepten der Wunschliste gefuellt
- Portionen zentral auf der Listen-Seite skalierbar
- Flache Aggregation via (LOWER(TRIM(name)), LOWER(TRIM(unit)))
- Abhaken persistiert, Cleanup manuell
- Header-Badge zaehlt nicht-abgehakte Zeilen
- Relayout der Wunschlisten-Karte: Action-Icons horizontal oben, Quell-Domain raus
- Kein Fuzzy-Matching, keine manuellen Eintraege (YAGNI fuer v1)

E2E-Tests erst nach Deploy.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 22:25:46 +02:00
hsiegeln
d9490c8073 refactor(search): local search ignores domain filter
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 3m11s
Der Domain-Filter im Header-Dropdown wirkt ab jetzt ausschliesslich auf
die Web-Suche (SearXNG). Die Suche in gespeicherten Rezepten liefert
immer alle Treffer, unabhaengig von der Quelldomain -- wer ein Rezept
gespeichert hat, will es finden, selbst wenn er die Domain aus dem
Filter ausgeschlossen hat.

- SearchStore: filterParam -> webFilterParam, nur noch an Web-Calls
- /api/recipes/search: domains-Query-Param wird nicht mehr gelesen
- searchLocal(): domains-Parameter + SQL-Branch entfernt
- Tests entsprechend angepasst

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 21:59:48 +02:00
hsiegeln
0373dc32da feat(ai): Deutsch als starker Prior im OCR-Prompt
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 30s
Neue SPRACHE-Sektion weist Gemini explizit darauf hin, dass die
Texte ausschliesslich deutsch sind -- Umlaute, deutsche Zutaten,
deutsche Masseinheiten als Prior fuer die Zeichen-Rekonstruktion.
Soll die "Kontext-Detektiv"-Logik bei handgeschriebenen oder
verblassten Rezepten verbessern.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 14:28:38 +02:00
hsiegeln
272a07777e feat(ai): OCR-Experten-Framing + expliziter User-Prompt
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m18s
Auf Gemini-Empfehlung: System-Instruction als OCR-Experte fuer
kulinarische Dokumente, mit "Kontext-Detektiv"-Regel fuer schwer
lesbare Zeichen, "[?]" fuer Unleserliches und strikter "keine
Halluzination"-Regel.

User-Prompt wird jetzt als eigene text-part bei jedem Call
mitgeschickt (Bild + User-Prompt + bei Retry die Korrektur-Note).

Inline-Schema aus dem Prompt entfernt, da es mit unserem
responseSchema konfligierte (servings vs servings_default+unit,
times-nested vs flat, instructions vs steps, kein note-Feld) --
das kann die beobachteten AI_FAILED-Schema-Validation-Fehler
beguenstigt haben. Struktur wird jetzt ausschliesslich ueber
responseSchema enforced.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 14:26:18 +02:00
hsiegeln
efdcace892 feat(ai): reichhaltigeres Logging fuer AI_FAILED-Diagnose
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m15s
Der bisherige Log "[extract-from-photo] AI_FAILED after 43165ms,
385807 bytes" verriet nicht, ob es JSON-Parse, Schema-Validierung
oder ein SDK-Fehler war. Endpoint haengt jetzt e.message an;
gemini-client loggt den First-Attempt-Fehler vor dem Retry und
packt bei AI_FAILED beide Messages in den finalen Error.

Keine Prompt-/Response-Inhalte werden geloggt -- nur unsere eigenen
GeminiError-Messages (Zod-Pfade, "non-JSON output", SDK-toString).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 14:08:10 +02:00
hsiegeln
fb7c2f0e9b feat(photo-upload): zwei Buttons fuer Kamera vs. Datei-Picker
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 30s
Android-Chrome auf Tablet verhaelt sich zickig: mit capture="environment"
nur Kamera, ohne capture nur Datei-Picker -- nie beide. Zwei separate
Buttons (mit jeweils eigenem Input-Element) machen die Wahl explizit
und funktionieren ueberall eindeutig.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 13:45:37 +02:00
hsiegeln
33ee6fbf2e feat(photo-upload): Picker ohne capture -> auch gespeicherte Fotos
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m26s
capture="environment" zwang Mobile-Browser in den Kamera-Modus. Ohne
das Attribut zeigt der Browser auf Mobile die volle Auswahl
(Kamera / Fotomediathek / Datei) -- besser fuer Tablets und User,
die ein schon existierendes Kochbuch-Foto verwenden wollen.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 13:39:07 +02:00
hsiegeln
e2713913e7 feat(photo-upload): Logging fuer Upload-Parse-Fehler
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
Der bisherige Endpoint verschluckte den formData()-Fehler mit einem
generischen "Multipart erwartet" — wir wissen nicht, warum Chrome auf
dem Tablet scheitert. Jetzt wird beim Fehler Content-Type, -Length und
User-Agent geloggt, plus die konkrete Error-Message in der Response.
Kein Foto-Inhalt im Log.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 13:37:42 +02:00
hsiegeln
3bc7fa16e2 feat(photo-upload): Limits hochschrauben fuer Tablet-Fotos
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m16s
Tablet- und iPad-Pro-Kameras liefern JPEGs/HEICs bis 15 MB. Mit den
alten 8-/10-MB-Limits scheiterte das Upload beim SvelteKit-Body-Parser
mit "Multipart erwartet" (undurchsichtiger Fehler, weil SvelteKit den
Body frueher abweist als unser Endpoint-Check).

- Endpoint MAX_BYTES: 8 -> 20 MB
- BODY_SIZE_LIMIT: 10 -> 25 MB (mit Multipart-Overhead)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 13:31:34 +02:00
hsiegeln
173d9d138d fix(ai): sharp via createRequire, nicht ES-Import
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 28s
ES-Dynamic-Import mit @vite-ignore reichte nicht -- adapter-node's
Rollup-Step extrahiert sharp trotzdem in einen shared chunk und
bundelt sharp's interne dynamic-requires kaputt.

createRequire(import.meta.url) plus require('sharp') ist pure Node-
Runtime-Logik, die Rollup komplett ignoriert. sharp wird regulaer aus
node_modules geladen -- inkl. seiner Plattform-.node-Binary aus
@img/sharp-linuxmusl-arm64.

Verifikation: Build-Output enthaelt 0 Vorkommen von "dynamicRequireTargets"
und "sharp.node" (waren vorher in einem 319KB shared chunk).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 13:08:53 +02:00
hsiegeln
5492d4dc24 fix(deploy): BODY_SIZE_LIMIT=10MB fuer Foto-Upload
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m17s
adapter-node limitiert Request-Bodies per Default auf 512 KB.
Unsere Rezept-Fotos sind bis 8 MB gross -- der Upload scheitert
sonst vor dem Endpoint-Check mit "Multipart body erwartet", weil
SvelteKit den Body frueher abweist.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 13:05:09 +02:00
hsiegeln
39de08abf9 fix(ai): sharp via dynamic import, nicht top-level
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m28s
Der vorige Versuch mit ssr.external in vite.config.ts war ein No-op:
adapter-node macht einen eigenen Rollup-Bundle-Schritt nach Vite und
ignoriert ssr.external komplett. Ergebnis: sharp's dynamic-require
fuer die native .node-Binary landet kaputt im Server-Bundle (332KB
Bundle-Chunk, 297 sharp-Referenzen).

Dynamic import mit /* @vite-ignore */ verhindert, dass Rollup sharp
aufloest — die Require geht stattdessen zur Laufzeit regulaer an
Node und findet @img/sharp-linuxmusl-arm64 in node_modules.

Ergebnis lokal: Server-Chunk von 332KB auf 14KB geschrumpft, nur noch
2 Referenzen auf den Paketnamen (der Import-String selbst).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 12:33:59 +02:00
hsiegeln
fd7884e1b2 fix(vite): sharp als ssr.external markieren
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m17s
Der Server-Bundle-Schritt (Rollup via adapter-node) kann sharp's
dynamic-require fuer die native Plattform-.node-Binary nicht aufloesen
und bundelt kaputten Code ins Image. ssr.external sorgt dafuer, dass
sharp zur Laufzeit regulaer aus node_modules geladen wird, wo der
Docker-Build die @img/sharp-linuxmusl-arm64-Binary korrekt abgelegt hat.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 12:27:28 +02:00
hsiegeln
13728f9252 fix(docker): expliziter Plattform-Install fuer sharp-Prebuilts
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 3m5s
Finaler Anlauf gegen den arm64-Build-Fehler. Bisher scheiterte
npm daran, @img/sharp-linuxmusl-arm64 unter Docker-Buildx-QEMU zu
installieren, trotz --include=optional. Mehrschichtiger Fix:

1. --cpu=arm64 --os=linux --libc=musl auf npm install: umgeht QEMU-
   bezogene Detection-Bugs (sharp's offiziell empfohlener Fix).
2. Expliziter Zusatz-Install von @img/sharp-linuxmusl-arm64 und
   @img/sharp-libvips-linuxmusl-arm64: zwingt die Prebuilts auf Disk,
   unabhaengig von der Lockfile-Resolution.
3. --ignore-scripts beim Install + npm rebuild danach: loest den Race,
   wo sharp's postinstall laeuft bevor der Prebuilt-Tarball fertig ist.
4. node-addon-api + node-gyp als Runtime-Deps: from-source-Build-
   Fallback falls alle Prebuilt-Pfade scheitern.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 11:52:55 +02:00
hsiegeln
83f5b88d94 fix(docker): node-addon-api + ignore-scripts/rebuild fuer sharp
Some checks failed
Build & Publish Docker Image / build-and-push (push) Failing after 51s
Drei Schichten Absicherung gegen den arm64-Build-Fehler:

- --ignore-scripts beim npm install verhindert, dass sharp's postinstall
  check.js laeuft, bevor das @img/sharp-linuxmusl-arm64-Paket entpackt
  ist (Race in parallelem Install).
- npm rebuild danach: alle Deps sind jetzt auf Disk, Postinstalls laufen
  sauber in Dependency-Reihenfolge.
- node-addon-api als Runtime-Dep: falls die Prebuilt-Binary im npm-Tree
  nicht landet, kann sharp from-source bauen (vips-dev + python3 + make
  + g++ sind im Dockerfile bereits installiert).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 11:49:41 +02:00
hsiegeln
cb93725139 fix(docker): npm install statt npm ci fuer sharp-Prebuilts
Some checks failed
Build & Publish Docker Image / build-and-push (push) Failing after 48s
Der vorige Fix (ignore-scripts + rebuild, plus Fresh-ci im Builder) hat
den sharp-Prebuilt trotzdem nicht installiert. Ursache: der Windows-
generierte Lockfile markiert @img/sharp-linuxmusl-arm64 als "dev": true,
sodass npm ci die Prebuilt-Binary konsistent auslaesst — egal ob mit
--include=optional. npm install dagegen resolvt Optional-Deps frisch fuer
die Build-Plattform (linux-arm64-musl im Docker) und findet die Prebuilts.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 11:46:55 +02:00
hsiegeln
80c72b6e5b fix(docker): sharp-Prebuilts beim CI-Build korrekt installieren
Some checks failed
Build & Publish Docker Image / build-and-push (push) Failing after 51s
Zwei Fixes gegen den arm64-Build-Fehler:

1. npm ci mit --ignore-scripts + npm rebuild danach — vermeidet
   eine Race, bei der sharp's postinstall check.js laeuft bevor die
   Plattform-Prebuilt-Binary vollstaendig entpackt ist.

2. Statt npm prune --omit=dev ein Fresh-Install via npm ci
   --omit=dev. Grund: Der Lockfile wird auf Windows generiert und
   markiert die linux-musl-arm64-Prebuilts als "dev": true, obwohl
   sie fuer's Runtime gebraucht werden. Prune wuerde sie kappen.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 11:44:05 +02:00
hsiegeln
b88f1fbfa4 chore(release): v1.3.0 — Foto-Rezept-Magie
Some checks failed
Build & Publish Docker Image / build-and-push (push) Failing after 1m31s
- Kamera-Icon im Header (bei gesetztem GEMINI_API_KEY, disabled offline)
- /new/from-photo: File-Picker oder Kamera -> Gemini-Extraktion -> vorbefuellter Editor
- POST /api/recipes/extract-from-photo (sharp preprocess + Gemini 2.5 Flash, keine Persistenz)
- POST /api/recipes fuer Scratch-Insert nach Editor-Save
- 50er Pool deutscher Magie-Phrasen fuer description
- docs, CLAUDE.md, OPERATIONS, ARCHITECTURE aktualisiert

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 10:51:52 +02:00
hsiegeln
f4aefb8e99 docs: Foto-Rezept-Magie in OPERATIONS/ARCHITECTURE/CLAUDE 2026-04-21 10:51:23 +02:00
hsiegeln
6dab36339a test(e2e): Foto-Import Happy-Path und Offline-Icon 2026-04-21 10:50:01 +02:00
hsiegeln
eea5fb7560 feat(ui): Camera-Icon im Header mit Gemini-Config- und Offline-Gate 2026-04-21 10:48:38 +02:00
hsiegeln
47e91de0a1 feat(ui): /new/from-photo Page mit File-Picker, Lade- und Fehler-States 2026-04-21 10:47:33 +02:00
hsiegeln
bc42f35f8c feat(client): PhotoUploadStore mit idle/loading/success/error 2026-04-21 10:45:36 +02:00
hsiegeln
8c23875ba2 feat(editor): Bild-Block skip wenn recipe.id === null 2026-04-21 10:44:48 +02:00
hsiegeln
06e60afc88 feat(api): POST /api/recipes fuer Scratch-Insert aus Foto-Import 2026-04-21 10:43:30 +02:00
hsiegeln
e01f15a2a6 feat(api): POST /api/recipes/extract-from-photo 2026-04-21 10:42:46 +02:00
hsiegeln
3f259a7870 feat(ai): simpler In-Memory-Ratelimiter pro IP 2026-04-21 10:41:16 +02:00
hsiegeln
904edcb3ff feat(ai): Gemini-Client mit Timeout, 1x-Retry und Fehler-Codes 2026-04-21 10:40:58 +02:00
hsiegeln
d479fd61d8 feat(ai): Extraction-Prompt + Gemini-Schema + Zod-Validator 2026-04-21 10:40:03 +02:00
hsiegeln
0cca9a699c feat(ai): image-preprocess mit sharp (Resize + JPEG + EXIF-Strip) 2026-04-21 10:39:22 +02:00
hsiegeln
c284f4b85b feat(ai): 50er-Pool Magie-Phrasen fuer Foto-description 2026-04-21 10:38:32 +02:00
hsiegeln
9e3d6e8d01 chore(deps): @google/generative-ai + vips-dev fuer Foto-Rezept-Magie 2026-04-21 10:37:12 +02:00
hsiegeln
783b782608 docs: implementation plan fuer Foto-Rezept-Magie
15 bite-sized tasks mit TDD-Struktur, von deps+env ueber
Gemini-Client, API-Endpoints bis UI und Release.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 10:35:36 +02:00
hsiegeln
1532880cd5 docs: 50er-Phrasenpool fuer Foto-Rezept-description
Random-Auswahl server-seitig nach AI-Call; description steht
nicht im Gemini-Schema, keine Halluzinationsflaeche.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 10:22:52 +02:00
hsiegeln
aa7f0eff11 docs: spec fuer Foto-Rezept-Magie (v1.3)
Design-Spec fuer Gemini-basierten Foto->Rezept-Import:
Kamera-Icon im Header, Extraktion auf Server, Editor-Prefill
ohne DB-Record, Foto wird nicht persistiert.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 10:16:35 +02:00
hsiegeln
26018eee7f chore: .prettierignore fuer Fixtures, Docs und Templates
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 31s
npm run format hat zuletzt 18k Zeilen HTML-Fixture und alle
Markdown-Plaene angefasst. Ignore-Liste deckt jetzt ab:

- tests/fixtures (byte-exakte HTML-Captures fuer Parser-Tests)
- *.md (hand-aligned Tabellen, historische Plan-Artefakte)
- searxng/settings.yml (Template mit VAR-Platzhaltern)
- data/, build/, .svelte-kit, node_modules, Lockfile

Damit bleibt npm run format auf Code beschraenkt.
2026-04-20 08:45:41 +02:00
hsiegeln
24bd9c1d1b feat(header): Versionsnummer unter dem Logo
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
Liest KOCHWAS_TAG via +layout.server.ts aus $env/dynamic/private
und zeigt den Tag als kleine graue Zeile unter dem Brand-Text auf
der Startseite. Fallback "dev" wenn nicht gesetzt. Auf engen
Screens mit ausgeblendetem Brand verschwindet auch die Version.

docker-compose.prod.yml reicht die Host-Env-Variable jetzt in den
Container durch (vorher nur fuers Image-Tag-Binding interpoliert).
2026-04-20 08:41:18 +02:00
hsiegeln
633e497bdc fix(sw): network-first + 3s timeout statt SWR fuer Daten
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 30s
SWR lieferte bei jedem Cache-Hit sofort die alte Antwort und
aktualisierte das Cache nur fuer den naechsten Request. Folge:
UI zeigte stale Daten, frische Daten erst nach Refresh.

Neu: network-first mit 3 s Timeout-Fallback. Netz gewinnt bei
frischer Antwort; Timeout oder Netzwerk-Fehler fallen auf Cache
zurueck. Pre-Cache-Logik (runSync) bleibt unveraendert, Shell
und Bilder bleiben cache-first.
2026-04-20 08:29:00 +02:00
hsiegeln
b5c01b950e chore(release): v1.2.0 + Doku-Aktualisierung
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m14s
Release-Bundle fuer v1.2.0. Inhaltliche Highlights seit v1.1.0:
- Post-Review-Roadmap: API-Helper, Trash-Kommentar-Delete, Preview-
  Guard, untrack()-Snapshots, CSS-Var --pill-radius, asyncFetch-
  Wrapper, requireProfile(message), Code-Cleanup
- Remote-E2E-Suite (tests/e2e/remote/) gegen kochwas-dev.siegeln.net
  inkl. CRUD, Profile-Fixtures, API-Cleanup-Helpers, serviceWorkers-
  block fuer Chromium-Stabilitaet
- SearchStore (src/lib/client/search.svelte.ts) — gemeinsamer
  Live-Search-Store fuer Header-Dropdown und Startseite mit Debounce,
  Race-Guard, Pagination, Web-Fallback, Snapshot/Restore
- Editor-Split: RecipeEditor in IngredientRow, StepList,
  ImageUploadBox, TimeDisplay + recipe-editor-types zerlegt
- Zutaten-Sektionen: Migration 012 + section_heading-Feld,
  Inline-Insert-Button im Editor, Heading-Rendering in RecipeView,
  4 neue Remote-E2E-Tests mit CRUD-Coverage

Doku-Updates:
- ARCHITECTURE.md: Component-Liste, SearchStore-Erwaehnung,
  section_heading-Semantik, Test-Strategie um E2E local+remote
- OPERATIONS.md: Dev-System kochwas-dev.siegeln.net dokumentiert
- CLAUDE.md: Datei-Map auf Sub-Components ausgeweitet, Stand-
  Abschnitt auf aktuelle Roadmap-Stufen aktualisiert
- package.json / package-lock.json: 0.1.0 -> 1.2.0

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 15:34:01 +02:00
hsiegeln
6bde3909d8 polish(sections): Muelltonne statt X + Ueberschrift groesser/fetter
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m22s
- IngredientRow: Sektion-entfernen-Button nutzt Trash2 (konsistent
  mit dem Zutat-Entfernen-Button daneben)
- RecipeView: section-heading von 1rem/600 auf 1.2rem/700, mehr
  vertikaler Abstand fuer deutlichere optische Trennung
- E2E-Spec: type-inference-Trick durch APIRequestContext-Import
  ersetzt (svelte-check stolperte bei typeof test mit TestDetails-
  Overload)
- Plan-Datei der Feature-Session mitcommitet

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 15:26:39 +02:00
hsiegeln
78c4f56992 Merge ingredient-sections — Zutaten-Gruppierung via section_heading
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 39s
- Migration 012: ingredient.section_heading TEXT NULL
- Editor: Inline-Abschnitt-hinzufuegen-Button (fade-in on hover) vor
  jeder Zeile; Heading-Input + X-Entfernen-Button wenn gesetzt
- View: <li class="section-heading"> vor erster Zutat jeder Sektion
- Scaler preserviert section_heading via Spread
- E2E-Suite: 4 neue Tests mit CRUD gegen kochwas-dev (46/46 gruen)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 15:19:39 +02:00
hsiegeln
c07d2f99ad test(e2e): Zutaten-Sektionen CRUD + UI-Flow auf kochwas-dev
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 40s
4 new remote specs: API roundtrip, editor add-section + view render,
section remove, empty heading -> null on save. All 46 pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 15:19:13 +02:00
hsiegeln
8069c5c246 feat(view): Zutaten-Sektionen als Ueberschriften rendern
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m20s
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 15:08:43 +02:00
hsiegeln
7d6ee04fec feat(editor): Sektionen-Handler + save-Patch mit section_heading
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 15:06:12 +02:00
hsiegeln
b646720a6e fix(editor): :global(.ing-list):hover damit Fade-in wirklich greift 2026-04-19 15:04:26 +02:00
hsiegeln
526c7433f4 feat(editor): Sektionsueberschriften in IngredientRow + Insert-Button
DraftIng bekommt section_heading: string | null. IngredientRow
rendert davor einen Fade-in-Insert-Button (null) oder ein Heading-
Input mit Entfernen-Button (string). Props onaddSection/onremoveSection
ergaenzt; Styles an bestehendem Block angehaengt.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 15:03:29 +02:00
hsiegeln
96cb55495e test(scaler): section_heading ueberlebt Skalierung
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 15:00:21 +02:00
hsiegeln
a1baf7f30a feat(db): section_heading roundtrip in recipe-repository
INSERT/SELECT in insertRecipe, replaceIngredients und getRecipeById
um section_heading ergänzt. IngredientSchema im PATCH-Endpoint sowie
Ingredient-Fixtures in search-local-, scaler- und repository-Tests
auf das neue Pflichtfeld aktualisiert.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 14:55:46 +02:00
hsiegeln
b0d5f921e2 docs(migration): 012 Kommentar an 010/011-Stil angleichen (DE, Begruendung) 2026-04-19 14:52:13 +02:00
hsiegeln
72816d6b35 feat(schema): ingredient.section_heading (Migration 012 + Type)
Fuegt das nullable Feld section_heading zur ingredient-Tabelle hinzu
(Migration 012), erweitert den Ingredient-Typ und aktualisiert alle drei
Return-Stellen in parseIngredient. Downstream-Sites (repository, Editor,
Tests) bleiben rot – werden in Task 2+ behoben.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 14:49:42 +02:00
hsiegeln
ad5a6afcd9 Merge editor-split — Tier 4 Item B + E2E-Stabilitaet
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 29s
4 Sub-Components extrahiert: ImageUploadBox (190 L), IngredientRow
(129 L), StepList (101 L), TimeDisplay (30 L) plus recipe-editor-
types.ts (8 L). RecipeEditor.svelte 628→312 L, RecipeView.svelte
398→387 L. 196/196 Unit-Tests, svelte-check 0 Errors.

Bonus: Playwright-Remote-Suite jetzt stabil 42/42 — Chromium-Crash-
Cascade durch serviceWorkers:block behoben.
2026-04-19 14:15:19 +02:00
hsiegeln
30a409fd16 fix(e2e): serviceWorkers=block behebt Chromium-Crash-Cascade
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m20s
Die Remote-Suite hatte `serviceWorkers: allow` gesetzt, jeder Test
registriert einen frischen SW im neuen Context. Nach 20-30 Specs
akkumuliert das im Single-Worker-Run genug Browser-State, dass
Chromium mitten in der Suite crasht — alle folgenden Tests fallen
dann mit "browser.newContext closed" als Cascade.

'block' entfernt den SW komplett. Diese Suite testet nur Live-API-
Verhalten gegen den Server, keine PWA-Features (dafuer ist
offline.spec.ts lokal zustaendig). Full-Run jetzt stabil 42/42,
Laufzeit zusaetzlich ~3s schneller.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 14:09:32 +02:00
hsiegeln
504fbb6cc6 refactor(view): TimeDisplay als eigenstaendige Component
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m21s
timeSummary-Formatierung in eine wiederverwendbare Component
gezogen. RecipeView liefert nur noch die drei Werte — zukuenftige
Call-Sites (Preview, Hover-Cards) koennen dieselbe Logik reusen.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 13:50:46 +02:00
hsiegeln
d50841c5a6 refactor(editor): StepList als eigenstaendige Component
Zubereitungs-Liste mit Add + Remove als Sub-Component. Parent steuert
nur noch den Wrapper und reicht steps + die zwei Callbacks rein.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 13:45:56 +02:00
hsiegeln
defbb5e24d refactor(editor): IngredientRow + shared types
IngredientRow rendert eine einzelne editierbare Zutat-Zeile. DraftIng
und DraftStep sind jetzt in recipe-editor-types.ts, damit Parent und
Sub-Components auf dieselbe Form referenzieren.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 13:40:10 +02:00
hsiegeln
c43b1dca87 refactor(editor): ImageUploadBox als eigenstaendige Component
Isoliert den Bild-Upload-Flow (File-Input, Preview, Entfernen-Dialog)
aus dem RecipeEditor. Parent haelt nur noch den <section>-Wrapper und
reicht recipe.id + image_path rein, kriegt Aenderungen per onchange
callback zurueck.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 13:33:26 +02:00
hsiegeln
015cb432fb docs(plans): Editor-Split Implementierungsplan (Tier 4 Item B)
5-Task-Plan fuer 4 Sub-Components: ImageUploadBox, IngredientRow,
StepList, TimeDisplay. Parent-owned state bleibt im Parent, Sub-
Components rendern bare Content damit Parent-Scoped-CSS greift.
Keine Component-Unit-Tests (etablierter Codebase-Stil), Manual-
Smoke + existierende e2e-Specs decken Regression.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 13:28:30 +02:00
hsiegeln
f273942286 Merge search-state-store — Tier 2 Post-Review-Roadmap
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 30s
SearchStore extrahiert aus +page.svelte (808→645) und +layout.svelte
(681→569). 12 neue Unit-Tests (196 total), 40/42 E2E grün (1 Flake,
1 Skip). Keine Regression in UAT auf kochwas-dev.
2026-04-19 13:18:04 +02:00
hsiegeln
c45ef2a613 fix(search): runSearch bricht pending Debounce ab
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m20s
Enter waehrend Debounce-Fenster feuerte bislang eine zweite Fetch
fuer dieselbe Query. Race-Guard greift nicht, weil q identisch ist.
runSearch clearTimeout am Anfang behebt's, neuer Unit-Test sichert es.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 13:03:42 +02:00
hsiegeln
e7067971a5 refactor(home): Live-Search auf SearchStore migriert
Entfernt die duplizierten $state-Felder, runSearch, loadMore und
beide Debounce-Effekte. URL-Sync, Snapshot und Filter-Re-Search
bleiben hier — delegieren aber an den Store. All-Recipes-Infinite-
Scroll unberuehrt (separate UI-Concern).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 12:57:58 +02:00
hsiegeln
0ca42f3329 refactor(layout): Header-Dropdown nutzt SearchStore
Ersetzt die 10 lokalen $state-Felder, den Debounce-$effect und die
lokalen Search-Funktionen durch eine SearchStore-Instanz. Nav-Open-
Toggle, Click-outside und Menu-State bleiben lokal — UI-Concerns.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 12:51:11 +02:00
hsiegeln
4b17f19038 docs(plans): Plan-Doc auf runDebounced() ohne Parameter angleichen
Consumer-Patterns (Task 3/4) aktualisiert: $effect liest store.query
explizit und ruft runDebounced() parameterlos — matcht die live Impl
nach Commit 4edddc3.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 12:48:50 +02:00
hsiegeln
4edddc38e3 refactor(search): runDebounced ohne missweisenden Parameter
Der _q-Parameter wurde nie benutzt — Consumer sollen stattdessen
store.query im \$effect lesen, dann runDebounced() callen. Weniger
Footgun, explizitere Call-Site.

Tests-Rename: "mid-flight" → "cleared/changed", beschreibt was der
Test tatsaechlich absichert.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 12:47:40 +02:00
hsiegeln
fc47c78397 fix(search): Race-Guard-Test korrekt auf in-flight abzielen
Der vorherige Test setzte query NACH dem Fetch-Abschluss und erzwang
dafuer einen setter-Side-Effect, der bei normalem Tippen die Treffer
waehrend des Debounce-Fensters fuer 300ms leer geblitzt haette.

Jetzt: echter Race-Test mit manuell aufloesbarem fetch. Setter-Nebenwirkung
entfernt, query ist wieder plain \$state.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 12:41:43 +02:00
hsiegeln
58ce19c160 feat(search): SearchStore fuer Live-Search mit Web-Fallback
Extrahiert die duplizierte Such-Logik aus +page.svelte und
+layout.svelte in eine gemeinsame Klasse. Pure Datenschicht
mit injizierbarem fetch — UI-Concerns (URL-Sync, Dropdown,
Snapshot) bleiben in den Komponenten.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 12:38:33 +02:00
hsiegeln
7fd90643c5 docs(plans): Search-State-Store Implementierungsplan
6-Task-Plan fuer Tier 2 der Post-Review-Roadmap. Extrahiert die
duplizierte Such-Logik aus +page.svelte und +layout.svelte in eine
gemeinsame SearchStore-Klasse mit TDD (12 Unit-Tests), Header-
Dropdown-Migration vor Home-Migration, und UAT-Smoke.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 12:32:50 +02:00
hsiegeln
3021ccb6a9 fix(e2e): 3 Specs robuster gegen reale Runtime
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 31s
- comments: Loeschen-Button im ConfirmDialog war ambig (3 Matches —
  Rezept-Delete, Kommentar-Trash, Dialog-Bestaetigung). Locator auf
  getByRole('dialog', { name: /Kommentar löschen/i }) eingeschraenkt.
- recipe-detail Portionen: getByText(/\b750 g/) trifft nicht wegen
  Whitespace-Layout im <span class="qty">. Auf
  locator('.ing-list li', { hasText: 'Hähnchenbrustfilet' })
  .toContainText('750 g') umgestellt — robust gegenueber Svelte-
  Whitespace-Quirks.
- search empty-state: SearXNG matcht loose, "truly empty" ist nicht
  zuverlaessig reproduzierbar. Test akzeptiert jetzt "Empty-State ODER
  Web-Fallback" und prueft zusaetzlich, dass kein JS-Error fliegt.

admin/backup war eine transiente Flake — 15 Repeat-Runs alle gruen,
kein Code-Fix noetig.

Gate: 12/12 der geaenderten Specs passed local.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 12:21:36 +02:00
hsiegeln
a7ad159c69 test(e2e): Playwright Smoketests gegen kochwas-dev (remote)
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m42s
Automatisierte End-to-End-Tests gegen ein deployed Environment. Loest
die manuellen MCP-Playwright-Runs ab. 42 Tests in 9 Files:

- homepage: H1, Sektionen, Sort-Tabs, Console-Errors
- search: lokaler Treffer, Web-Fallback, Empty-State, Deep-Link
- profile: Switcher, Auswahl-Persistenz, Favoriten-Section, Guard-Dialog
- recipe-detail: Header, Portionen-Scaling (4->6), Favorit-Toggle,
  Rating-Persistenz ueber Reload, Gekocht-Counter, Wunschliste-Toggle
- comments: eigenen erstellen+loeschen via UI, fremder hat kein Delete
- wishlist: Seite, Sort-Tabs, Badge-Sync, requireProfile-Custom-Message
- preview: Guard ohne ?url=, echte URL parst, unparsbare zeigt error-box
- admin: alle 4 Subrouten + /admin redirect
- api-errors: parsePositiveIntParam (4x Invalid id), validateBody (4x
  Invalid body + issues), 404, Sanity /health /profiles /domains

Architektur:
- Separate playwright.remote.config.ts (getrennt von local preview)
- workers: 1 + afterEach API-Cleanup (rating, favorite, wishlist, comments)
- Hardcoded Recipe-ID 66 + Profile 1/2/3 — stabile Dev-DB-Seeds
- E2E_REMOTE_URL ueberschreibt die Ziel-URL

Ausfuehrung: npm run test:e2e:remote

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 12:14:04 +02:00
hsiegeln
7da37d0a3d Merge cleanup-batch-post-review — Tier 1 + 2 UAT-Fixes
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 29s
Sechs atomare Commits aus der Post-Review-Roadmap:
- I: RecipeEditor form-lokale Snapshots via untrack() (10 svelte-check
  WARNINGs weg)
- H: Bild-Upload/Delete auf asyncFetch Wrapper
- F: --pill-radius CSS-Variable (15 Sites dedupliziert)
- G: requireProfile(message?) mit optionalem Parameter
- Preview-Guard wenn ?url= fehlt (UAT-Finding)
- Kommentar-Delete-Button fuer eigene Kommentare (UAT-Finding)

Alles 184/184 Tests gruen, svelte-check 0 Warnings, UAT auf
kochwas-dev durchgeklickt.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 12:05:11 +02:00
hsiegeln
e953ca7870 feat(comments): Trash-Button zum Loeschen eigener Kommentare
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m19s
Der DELETE-Endpunkt fuer Kommentare existierte schon, hatte aber
keine UI-Exposition — Nutzer konnten ihre eigenen Kommentare nur
via API-Call loeschen. Das war beim UAT 2026-04-19 aufgefallen.

Jetzt: pro Kommentar wird nur fuer den Autor (comment.profile_id
=== profileStore.active.id) ein kleiner Trash2-Button in der
Ecke angezeigt. Mit confirmAction-Dialog, weil das Loeschen
nicht undo-bar ist.

Nutzt asyncFetch fuer den DELETE-Call — konsistent mit dem
Rest des Page-Scripts.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 11:56:41 +02:00
hsiegeln
c1789f902e fix(preview): Guard wenn ?url=-Parameter fehlt
/preview ohne Query zeigte endlos "Vorschau wird geladen…", weil
loading initial true war und der $effect bei leerem u nichts tat.

Jetzt: beim leeren u wird errored gesetzt (mit Hinweis, dass das
der falsche Einstieg in die Route ist), so zeigt die bestehende
error-box den passenden Text an.

Im UAT 2026-04-19 aufgefallen, dort als MINOR eingeordnet.
Hier direkt mitgenommen weil 6-Zeilen-Fix.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 11:55:18 +02:00
hsiegeln
02b9cdbc68 refactor(client): requireProfile(message?) + Wunschliste migriert (Item G)
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m21s
Option B aus dem Roadmap-Plan. requireProfile bekommt einen optionalen
message-Parameter mit dem bisherigen Text als Default — die 5 Bestands-
Aufrufe aendern sich nicht, die Wunschliste nutzt die Custom-Message
„um mitzuwünschen" sauber ueber den Helper statt mit dupliziertem
alertAction-Block.

Netto: -3 Zeilen in wishlist/+page.svelte, eine Duplikation weniger,
Helper dokumentiert jetzt explizit den Message-Override-Use-Case.

Gate: svelte-check 0 Warnings, 184/184 Tests, Wunschliste zeigt
korrekte Message beim Klick ohne Profil.

Refs docs/superpowers/plans/2026-04-19-post-review-roadmap.md Item G.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 11:45:00 +02:00
hsiegeln
5a291a53dd refactor(ui): --pill-radius CSS-Variable (Item F)
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m22s
border-radius: 999px war 15x im CSS dupliziert. Ausgelagert als
:root --pill-radius Variable im globalen :root-Block in +layout.svelte,
Call-Sites auf var(--pill-radius) umgestellt.

Bewusst NICHT angefasst (plan war "nur Werte die mehrfach vorkommen"):
- z-index: 10 Distinct Values in 14 Sites, bilden ein implizites
  Layer-System. Konsolidieren = behavior-change-Risiko ohne konkreten
  Nutzen. Wenn kuenftig einheitliche Modal-/Popover-Layer noetig,
  separate Phase.
- setTimeout(): 3 Sites, jeder mit eigener Semantik (Debounce/Print/
  Spinner). Kein DRY-Nutzen durch Extraktion.

Gate: svelte-check 0 Warnings, 184/184 Tests, Build clean, kein
sichtbarer Unterschied (einzige Aenderung: selber Wert ueber Variable).

Refs docs/superpowers/plans/2026-04-19-post-review-roadmap.md Item F.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 11:43:19 +02:00
hsiegeln
98a8022ddf refactor(editor): Bild-Upload/Delete auf asyncFetch (Item H)
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m29s
RecipeEditor war noch die letzte Stelle im UI, die das
handgeschriebene "if (!res.ok) { alertAction(...) }"-Pattern
benutzte, welches wir in review-fixes-2026-04-18 ueberall sonst
durch asyncFetch() ersetzt hatten.

Netto: -14 Zeilen, konsistenter Fehlermessage-Fallback (body.message
> res.status), eine Import-Zeile weniger (alertAction raus, asyncFetch
rein).

Gate: svelte-check clean, 184/184 Tests, Upload/Delete-Flow per
Hand zu testen beim naechsten Editor-Touch.

Refs docs/superpowers/plans/2026-04-19-post-review-roadmap.md Item H.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 11:39:42 +02:00
hsiegeln
5a1ffee3bb refactor(editor): untrack() fuer form-lokale Snapshots (Item I)
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
Alle 10 pre-existing svelte-check WARNINGs ("state_referenced_locally")
in RecipeEditor.svelte und recipes/[id]/+page.svelte addressiert.

Die betroffenen `let foo = $state(recipe.bar)`-Pattern sind
intentional Snapshots: der Editor soll User-Edits behalten und nicht
von prop-Updates ueberschrieben werden. untrack() macht die Intent
explizit und silenced die Warnung sauber statt sie unter den Teppich
zu kehren.

Scope: imagePath, title, description, servings, prepMin, cookMin,
totalMin, ingredients, steps (RecipeEditor) + recipeState
(recipes/[id]/+page).

Gate: svelte-check 0 Warnings (war 10), Tests 184/184.

Refs docs/superpowers/plans/2026-04-19-post-review-roadmap.md Item I.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 11:38:35 +02:00
hsiegeln
9ee8efa479 Merge review-fixes-2026-04-18 — API-Helper + Cleanup + Roadmap
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 33s
Bundelt 10 atomare Refactor/Feature-Commits aus dem Review-Branch:
api-helpers (parsePositiveIntParam, validateBody), alle 13 Handler
migriert, requireProfile()+asyncFetch Wrapper, Unicode-Brueche im
Ingredient-Parser, IMAGE_DIR/DATABASE_PATH zentralisiert, Doku-
Drift behoben, SW-Timing-Konstanten. Plus CI-Trigger fuer alle
Branches und Post-Review-Roadmap fuer die verschobenen Items A-I.

184/184 Tests gruen, svelte-check 0 Errors, UAT auf kochwas-dev
clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 11:34:33 +02:00
hsiegeln
2c1fd29003 docs(plan): Post-Review-Roadmap fuer Items A-I
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 30s
Sequenziert die nach review-fixes-2026-04-18 offenen Punkte aus
OPEN-ISSUES-NEXT.md in 5 Tiers: Cleanup-Batch (I+H+F+G) direkt
nach Merge, Search-State-Store als eigene Phase, SearXNG-Recovery
reaktiv, Rest trigger-basiert.

Jedes Item hat Scope, Files, Gate und Aufwand — tief genug fuer
/gsd-plan-phase als naechsten Schritt.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 11:34:19 +02:00
hsiegeln
cda6e77a9e ci(docker): alle Branches bauen, Branchname als Tag
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m22s
CI triggert jetzt auf 'branches: **' statt nur main. metadata-action
vergibt 'type=ref,event=branch' weiterhin automatisch, damit bekommen
Feature-Branches ihren Namen als Tag (z. B. review-fixes-2026-04-18)
und lassen sich im Registry auseinanderhalten. 'latest' bleibt
weiterhin an main gebunden.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 11:18:29 +02:00
hsiegeln
85fe1312ca docs(review): OPEN-ISSUES-NEXT.md — Stand nach Refactor-Nacht
Zusammenfassung der 8 Commits + Beweise (Tests/Check/Build/Smoke),
bewusst verschobene Items mit Begruendung pro Item, neu entdeckte
und gleich behobene Items, sowie empfohlene Reihenfolge fuer den
naechsten Wurf.

Adressiert REVIEW-2026-04-18.md, dead-code.md, redundancy.md,
structure.md, docs-vs-code.md.
2026-04-18 22:42:29 +02:00
hsiegeln
31c6e5cd1f refactor(server): IMAGE_DIR/DATABASE_PATH zentralisieren + Doku-Drift fixen
src/lib/server/paths.ts: zentrale Auflösung der env-vars; vorher 6×
IMAGE_DIR und 2× DATABASE_PATH dupliziert mit identischen Defaults.

Migrierte Sites:
- src/lib/server/db/index.ts (DATABASE_PATH + IMAGE_DIR)
- src/routes/api/admin/backup/+server.ts
- src/routes/api/domains/+server.ts
- src/routes/api/domains/[id]/+server.ts
- src/routes/api/recipes/import/+server.ts
- src/routes/api/recipes/[id]/image/+server.ts
- src/routes/images/[filename]/+server.ts

ARCHITECTURE.md:
- 49 Flachwitze -> 150 (waren tatsaechlich 150)
- 'search/' Route entfernt — wurde nie als eigene Route gebaut, Suche
  laeuft direkt auf der Homepage via API-Calls

Findings aus zweiter Review-Runde (siehe OPEN-ISSUES-NEXT.md)
2026-04-18 22:41:02 +02:00
hsiegeln
6d9e79d4f0 feat(parser): Unicode-Brueche + Mengen-Plausibilitaet
ingredient.ts:
- UNICODE_FRACTION_MAP fuer ½ ¼ ¾ ⅓ ⅔ ⅕ ⅖ ⅗ ⅘ ⅙ ⅚ ⅛ ⅜ ⅝ ⅞
- clampQuantity() weist 0, negative, > 10000 als null ab
- splitUnitAndName() helper, vorher 2x dupliziert (Unicode + ASCII Pfad)

Tests:
- 13 neue Tests fuer Unicode-Brueche (mit/ohne Unit) und Bounds
- bestaetigt dass deutsches Kommadezimal (0,25 l) bereits funktioniert

Hintergrund: Apple Food App liefert haeufig ½ und ⅓ in JSON-LD
Quantity-Feldern. Vor diesem Fix wurden die Felder als unparsable
behandelt (quantity null, name = '½ TL Salz'), was den Portionen-Slider
fuer importierte Rezepte unbrauchbar machte.

Findings aus REVIEW-2026-04-18.md (Refactor D) und structure.md
2026-04-18 22:25:35 +02:00
hsiegeln
60c8352c96 docs(searxng): Intent-Kommentar fuer Prod-Diagnose-Logs
Die drei [searxng]-Logs sind absichtlich produktiv (Hilfe beim
Debuggen 'warum wurde Domain X gefiltert?'). Kommentar dokumentiert
das, damit kein zukuenftiger Cleanup sie pauschal entfernt.

baseRecipe-Fixture bleibt in tests/integration/recipe-repository.test.ts —
nur dort verwendet, nicht dupliziert (Review-Annahme war falsch).

yauzl/@types/yauzl bleiben als Dependency — bereits in
session-handoff-2026-04-17.md (Phase 5b) und ARCHITECTURE.md
verankert.

Findings aus REVIEW-2026-04-18.md (Wave 5 Cleanup) und structure.md
2026-04-18 22:23:17 +02:00
hsiegeln
30a447a3ea refactor(client): requireProfile() + asyncFetch wrapper
requireProfile():
- src/lib/client/profile.svelte.ts: neuer Helper, returnt das aktive
  Profile oder null nach standardisiertem alertAction
- 5x in recipes/[id]/+page.svelte: setRating, toggleFavorite, logCooked,
  addComment, toggleWishlist verlieren je 7 Zeilen Guard-Klausel
- profile-Variable im Closure macht den ! am profileStore.active obsolet

asyncFetch():
- src/lib/client/api-fetch-wrapper.ts: returnt Response auf 2xx, null
  nach alertAction auf Fehler
- 4 Call-Sites umgestellt: saveRecipe + saveTitle (recipes/[id]),
  saveEdit (admin/domains), rename (admin/profiles)
- admin/domains add() bewusst nicht migriert — inline-Error-UX statt Modal

Findings aus REVIEW-2026-04-18.md (Quick-Win 5) und redundancy.md
2026-04-18 22:22:19 +02:00
hsiegeln
ff293e9db8 refactor(api): alle handler auf api-helpers umstellen
13 +server.ts handler nutzen jetzt parsePositiveIntParam und
validateBody statt jeweils lokaler parseId/safeParse-Bloecke.

Konsequenzen:
- 9 lokale parseId/parsePositiveInt Definitionen geloescht
- 11 safeParse + manueller error()-Throws ersetzt
- domains/[id], domains, profiles: catch-Block reicht jetzt HttpError
  durch (isHttpError) — vorher wurde ein 404 vom updateDomain als 409
  re-emittiert
- recipes/[id]/image: kein function-clutter mehr neben den FormData-Pfaden
- Konsistente Error-Bodies: validateBody schickt {message, issues},
  parsePositiveIntParam {message: 'Missing X' / 'Invalid X'}

Findings aus REVIEW-2026-04-18.md (Refactor A) und redundancy.md
2026-04-18 22:19:12 +02:00
hsiegeln
739cc2d058 feat(server): api-helpers fuer parsePositiveIntParam + validateBody
- src/lib/server/api-helpers.ts mit parsePositiveIntParam(),
  validateBody<T>() und ErrorResponse type
- 13 unit tests fuer die beiden helper (HttpError-Shape verifiziert)
- Konsolidiert spaeter 9x parseId und 11x safeParse-Bloecke aus den
  +server.ts handlern

Findings aus REVIEW-2026-04-18.md (Refactor A) und redundancy.md
2026-04-18 22:16:00 +02:00
hsiegeln
830c740747 refactor(constants): zentrale SW-Timing-Konstanten + minor cleanups
- src/lib/constants.ts: SW_VERSION_QUERY_TIMEOUT_MS, SW_UPDATE_POLL_INTERVAL_MS
- pwa.svelte.ts: nutzt die Konstanten statt 1500/30*60_000
- cache-strategy.ts / diff-manifest.ts: RequestShape/ManifestDiff entkapselt
  (intern statt export, da nirgends extern importiert)
- recipes/[id]/image: deutsche Fehlermeldungen auf Englisch (Konsistenz
  mit allen anderen Endpoints)

Findings aus REVIEW-2026-04-18.md (Quick-Wins 6+7) und dead-code.md
2026-04-18 22:14:38 +02:00
hsiegeln
2289547503 docs(review): fix table names, IMAGE_DIR, image endpoints
- ARCHITECTURE.md: ingredient/step (waren faelschlich recipe_*)
- OPERATIONS.md: IMAGE_DIR (statt IMAGES_PATH)
- session-handoff: /api/recipes/[id]/image POST/DELETE ergaenzt

Findings aus REVIEW-2026-04-18.md / docs-vs-code.md
2026-04-18 22:13:15 +02:00
hsiegeln
10c43c4d4a docs(review): Deep-Code-Review 2026-04-18
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 31s
Vier parallele Review-Passes (Dead-Code, Redundanzen, Struktur,
Docs-vs-Code) plus konsolidierter Hauptreport. Nur Dokumentation —
keine Code-Änderungen. Tests 158/158 grün beim Review-Start.

Haupt-Findings:
- ARCHITECTURE.md:55 nennt falsche Tabellennamen
  (recipe_ingredient/recipe_step statt ingredient/step)
- parseId in 9 API-Handlern dupliziert
- Page-Komponenten teils >750 Zeilen
- yauzl installiert aber ungenutzt (für Phase 5b reserviert)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 21:55:41 +02:00
hsiegeln
5283ab9b51 feat(recipe): Bild manuell hochladen / ersetzen / entfernen
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m22s
- Neuer Endpoint POST/DELETE /api/recipes/:id/image.
  * Multipart-Upload mit Feld "file".
  * Whitelist: JPG, PNG, WebP, GIF, AVIF. Max 10 MB.
  * Dedupe per SHA-256-Filename analog zu downloadImage().
- updateImagePath()-Repo-Funktion ergänzt.
- RecipeEditor: neuer Block "Bild" ganz oben. Preview + Buttons
  "Hochladen"/"Ersetzen"/"Entfernen". Upload passiert direkt beim
  Auswählen, nicht erst bei "Speichern" — das Bild ist eigene
  Ressource, Abbrechen rollt es nicht zurück (okay, da dedupliziert).
- onimagechange-Callback informiert die Detail-Ansicht, damit die
  Preview im RecipeView auch nach Abbrechen aktuell bleibt.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 21:39:54 +02:00
hsiegeln
aaaf762564 feat(editor): Zutaten umsortierbar + Zutat/Notiz gleich breit
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m24s
- Dekorativer GripVertical raus, stattdessen zwei Pfeil-Buttons (↑/↓)
  pro Zeile. An erster/letzter Stelle sind die Buttons disabled.
- moveIngredient() vertauscht Zeile mit Nachbarn; simpel und
  tastatur-/touch-freundlich ohne Drag-and-Drop-Abhängigkeit.
- Grid-Spalten von 1fr 90px (Zutat/Notiz) auf 1fr 1fr — beide Felder
  sind jetzt gleich breit, wie im Family-Feedback gewünscht.
- Mobile-Layout behält gestaffelte Note-Zeile, Move-Spalte rutscht
  als eigene Spalte links daneben.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 21:32:35 +02:00
hsiegeln
dc04f5b032 feat(recipe): Schrift im Tablet/Desktop-Layout vergrößert
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m21s
Auf dem 10"-Tablet war die Schrift in Zutaten und Zubereitung zu klein.
Im 2-Spalten-Layout (>=820px) bumpen wir jetzt:
- Zutaten-Zeilen und Step-Text auf 1.2rem (vorher 1rem)
- qty-Spalte breiter (6rem statt 5rem)
- Portionen-Zahl größer
- Step-Badge auf 2.4rem + 1.1rem Font

Mobile bleibt unverändert — Lesedistanz ist dort anders.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 21:25:17 +02:00
hsiegeln
2f2f7dc7e7 fix(searxng): Mojeek entfernt — blockt die Pi-IP mit 403
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m20s
Nach dem DDG-Rauswurf war Mojeek die verbleibende Lärm-Quelle im Log:
HTTP 403 pro Suche, suspended_time=180. Mojeek hat nach eigenem Muster
Pi-IPs als automatisierten Traffic klassifiziert. Brave (API) deckt die
Websuche zuverlässig ab — Mojeek ist draußen, sowohl im searxng.ts-
Query (engines=brave) als auch in der SearXNG-keep_only-Liste.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 21:15:54 +02:00
hsiegeln
76ea5bed8d fix(searxng): nur Brave+Mojeek abfragen, DDG-Captcha-Noise beseitigen
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m21s
Zwei Fixes gegen die hartnäckigen DDG-CAPTCHA-Fehler im SearXNG-Log:

1. searxng.ts fragt jetzt explizit `engines=brave,mojeek` an.
   Vorher wurde nur `categories=general` gesetzt — dadurch wurden
   alle in dieser Kategorie aktivierten Engines abgefragt, inkl. DDG
   (das trotz `disabled: true` weiter antwortete).

2. settings.yml nutzt `use_default_settings.engines.keep_only` statt
   einzelner `disabled: true`-Overrides. SearXNGs Merge-Semantik für
   partielle Engine-Overrides (nur name + disabled ohne engine:)
   greift in der aktuellen Version nicht zuverlässig, deshalb kam
   DDG durch. keep_only wirft alles außer brave+mojeek vor dem Laden
   raus — kein Captcha-/403-Log-Lärm mehr.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 21:10:55 +02:00
hsiegeln
f89f363183 fix(searxng): auf engine: braveapi wechseln (API-Key wird nun genutzt)
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m21s
Die SearXNG-Engine "brave" ist ein HTML-Scraper von search.brave.com
und ignoriert den api_key-Parameter. Dadurch liefen alle Anfragen
gegen den gescrapten Web-Endpoint, der aus dem Pi-Netz regelmäßig
rate-limited wurde (SearxEngineTooManyRequestsException, 60%).

Fix: engine: braveapi nutzen. Das ist die offizielle Brave-Search-API-
Engine, die den api_key als X-Subscription-Token-Header sendet.
Der Key steht unverändert in .env auf dem Pi und wird vom
searxng-init-Container ins gerenderte settings.yml expandiert.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 21:03:48 +02:00
149 changed files with 17663 additions and 1424 deletions

View File

@@ -15,3 +15,9 @@ BRAVE_API_KEY=
# SearXNG-Secret: beliebig lange Zufallskette. Für Prod mit # SearXNG-Secret: beliebig lange Zufallskette. Für Prod mit
# `openssl rand -hex 32` generieren und in der Pi-.env ablegen. # `openssl rand -hex 32` generieren und in der Pi-.env ablegen.
SEARXNG_SECRET=dev-secret-change-me SEARXNG_SECRET=dev-secret-change-me
# Gemini Vision (Foto-Rezept-Magie). Ohne Key ist die Funktion graceful
# deaktiviert — der Kamera-Button erscheint dann gar nicht erst.
GEMINI_API_KEY=
GEMINI_MODEL=gemini-2.5-flash
GEMINI_TIMEOUT_MS=20000

View File

@@ -2,7 +2,7 @@ name: Build & Publish Docker Image
on: on:
push: push:
branches: [main] branches: ['**']
tags: ['v*'] tags: ['v*']
workflow_dispatch: workflow_dispatch:

3
.gitignore vendored
View File

@@ -7,4 +7,7 @@ data/
*.log *.log
test-results/ test-results/
playwright-report/ playwright-report/
playwright-report-remote/
.playwright-mcp/ .playwright-mcp/
.claude/
ci-log.txt

24
.prettierignore Normal file
View File

@@ -0,0 +1,24 @@
# Generierte / Build-Artefakte
node_modules
.svelte-kit
build
coverage
.vite
# Lockfiles
package-lock.json
# Lokale Laufzeit-Daten
data
# Test-Fixtures: rohe HTML-Captures muessen byte-exakt bleiben,
# sonst schlaegt die JSON-LD-Extraktion im Parser-Test anders an.
tests/fixtures
# Markdown: Tabellen sind hand-aligned, Code-Bloecke in historischen
# Plaenen sollen nicht nachtraeglich umgebrochen werden.
*.md
# SearXNG-Config ist ein Template mit ${VAR}-Platzhaltern, die der
# Init-Container expandiert.
searxng/settings.yml

View File

@@ -19,6 +19,8 @@ Selbstgehostete Rezept-PWA für die Familie Siegeln. Erreichbar unter `https://k
| **Preview-Bilder** | `recipe.image_path` kann **absolute URL** (Preview-Modus) oder **lokaler Filename** sein. `RecipeView.svelte` prüft mit `/^https?:\/\//i`. | | **Preview-Bilder** | `recipe.image_path` kann **absolute URL** (Preview-Modus) oder **lokaler Filename** sein. `RecipeView.svelte` prüft mit `/^https?:\/\//i`. |
| **Service Worker nur ab HTTPS** | `npm run dev` liefert HTTP → SW registriert nicht. Für PWA-Tests `npm run build && npm run preview` (localhost) oder Prod-Docker. | | **Service Worker nur ab HTTPS** | `npm run dev` liefert HTTP → SW registriert nicht. Für PWA-Tests `npm run build && npm run preview` (localhost) oder Prod-Docker. |
| **Icon-Rendering** | `npm run render:icons` rendert `icon-192.png` + `icon-512.png` aus `static/icon.svg`. Nur nach SVG-Änderung erneut ausführen + committen. | | **Icon-Rendering** | `npm run render:icons` rendert `icon-192.png` + `icon-512.png` aus `static/icon.svg`. Nur nach SVG-Änderung erneut ausführen + committen. |
| **Gemini-Key fehlt** | Wenn `GEMINI_API_KEY` leer ist, rendert das Layout das Camera-Icon nicht, und `/new/from-photo` antwortet mit 503 (`+page.server.ts`-Gate). Graceful Degradation — kein Zombie-Button. |
| **sharp + libheif** | Im `Dockerfile`-Builder-Stage ist `vips-dev` nötig, damit `sharp` HEIC-Input (iOS) lesen kann. Runtime-Stage braucht nix zusätzlich (sharp bringt libvips prebuilt mit). |
## Dateien, die man typischerweise anfasst ## Dateien, die man typischerweise anfasst
@@ -26,12 +28,16 @@ Selbstgehostete Rezept-PWA für die Familie Siegeln. Erreichbar unter `https://k
- `src/routes/+layout.svelte` — Header, mobile expand, Dropdown-Search auf Rezeptseiten - `src/routes/+layout.svelte` — Header, mobile expand, Dropdown-Search auf Rezeptseiten
- `src/routes/recipes/[id]/+page.svelte` — Rezept-Detail mit allen Actions (Rating, Favorit, Cooked, Wunschliste, Kommentar, Umbenennen, Löschen) - `src/routes/recipes/[id]/+page.svelte` — Rezept-Detail mit allen Actions (Rating, Favorit, Cooked, Wunschliste, Kommentar, Umbenennen, Löschen)
- `src/routes/preview/+page.svelte` — importierte Vorschau vor dem Speichern - `src/routes/preview/+page.svelte` — importierte Vorschau vor dem Speichern
- `src/routes/new/from-photo/+page.svelte` — Foto-Rezept-Magie (Picker → Spinner → vorbefüllter Editor)
- `src/lib/server/ai/` — Gemini-Client, Prompt-Schema, image-preprocess, rate-limit, description-phrases
- `src/lib/components/RecipeView.svelte` / `RecipeEditor.svelte` — Lesen/Edit-Mode des Rezepts. Editor ist in Sub-Components aufgeteilt: `IngredientRow`, `StepList`, `ImageUploadBox`, `TimeDisplay` (+ shared types `recipe-editor-types.ts`)
- `src/lib/server/search/searxng.ts` — Web-Suche + Thumbnail-Enrichment + SQLite-Cache - `src/lib/server/search/searxng.ts` — Web-Suche + Thumbnail-Enrichment + SQLite-Cache
- `src/lib/server/recipes/importer.ts` — JSON-LD → Recipe, orchestriert Bild-Download - `src/lib/server/recipes/importer.ts` — JSON-LD → Recipe, orchestriert Bild-Download
- `src/lib/server/db/migrations/*.sql` — Schema; bei Änderung immer **neue** Migration statt bestehende bearbeiten - `src/lib/server/db/migrations/*.sql` — Schema; bei Änderung immer **neue** Migration statt bestehende bearbeiten
- `src/service-worker.ts` — Service-Worker-Orchestrator (Shell-Cache + Pre-Cache + SWR) - `src/service-worker.ts` — Service-Worker-Orchestrator (Shell-Cache + Pre-Cache + SWR)
- `src/lib/sw/` — reine Logik (Cache-Strategy-Entscheider, Diff-Manifest) für Unit-Tests - `src/lib/sw/` — reine Logik (Cache-Strategy-Entscheider, Diff-Manifest) für Unit-Tests
- `src/lib/client/*.svelte.ts` — Frontend-Stores (Network, Sync-Status, Toast, Install-Prompt) - `src/lib/client/*.svelte.ts` — Frontend-Stores (Search, Network, Sync-Status, Toast, Install-Prompt, Wishlist, PWA, Profile, Confirm, Search-Filter)
- `tests/e2e/remote/` — Playwright gegen `kochwas-dev.siegeln.net` (CRUD erlaubt; workers:1, serviceWorkers:block)
## Arbeitsweise (wie wir es machen) ## Arbeitsweise (wie wir es machen)
@@ -67,7 +73,7 @@ docker compose -f docker-compose.prod.yml up --build
## Offene Themen / Stand ## Offene Themen / Stand
Siehe Session-Handoff-Dokumente unter `docs/superpowers/` und dort besonders `session-handoff-2026-04-17.md`. Die Roadmap-Phasen liegen als `docs/superpowers/plans/*.md`. Was als „Later" markiert ist, ist nicht beauftragt. Siehe die Plan-Dateien unter `docs/superpowers/plans/*.md` für abgeschlossene Implementierungs-Phasen (v1.0 Foundations → v1.1 Offline-PWA → Post-Review-Roadmap → Search-State-Store → Editor-Split → Ingredient-Sections = v1.2). Was als „Later" markiert ist, ist nicht beauftragt.
## Auto-Memory (lokal, nicht im Repo) ## Auto-Memory (lokal, nicht im Repo)

View File

@@ -3,17 +3,44 @@
FROM node:22-alpine AS builder FROM node:22-alpine AS builder
WORKDIR /app WORKDIR /app
# Alpine needs build tools for better-sqlite3 native module # Alpine needs build tools for better-sqlite3 native module.
RUN apk add --no-cache python3 make g++ libc6-compat # vips-dev provides libvips + libheif for sharp (incl. HEIC input from iOS).
RUN apk add --no-cache python3 make g++ libc6-compat vips-dev
COPY package*.json ./ COPY package*.json ./
RUN npm ci # Sharp-Prebuilt-Install unter Docker-Buildx-QEMU war trotz aller Flag-
# Varianten unzuverlaessig. Finale Strategie:
# - --cpu/--os/--libc explizit setzen: sharp's offizielle Doc-Empfehlung
# fuer Cross-Platform-Docker-Builds (siehe sharp-Install-Doku),
# umgeht QEMU-Detection-Bugs.
# - --ignore-scripts + npm rebuild: loest das Parallel-Install-Race,
# bei dem sharp's install-Skript vor dem Entpacken der Prebuilt-Binary
# laeuft.
# - Explizites Nachinstallieren der Prebuilts als Sicherheit: falls (A)
# noch nicht reicht, zwingt (B) die Plattform-Pakete auf Disk.
# - node-addon-api + node-gyp als Runtime-Deps: falls am Ende doch alles
# nicht klappt und sharp from-source baut (mit dem oben installierten
# python3 + make + g++ + vips-dev).
RUN npm install --cpu=arm64 --os=linux --libc=musl \
--ignore-scripts --include=optional --no-audit --no-fund
RUN npm install --cpu=arm64 --os=linux --libc=musl \
--ignore-scripts --no-save --no-audit --no-fund \
@img/sharp-linuxmusl-arm64@0.34.5 \
@img/sharp-libvips-linuxmusl-arm64@1.2.4
RUN npm rebuild
COPY . . COPY . .
RUN npm run build RUN npm run build
# Remove dev dependencies for the runtime image # Fresh-Install fuer den Runtime-Stage: nur Produktions-Deps, gleiche Strategie.
RUN npm prune --omit=dev RUN rm -rf node_modules \
&& npm install --cpu=arm64 --os=linux --libc=musl \
--ignore-scripts --omit=dev --include=optional --no-audit --no-fund \
&& npm install --cpu=arm64 --os=linux --libc=musl \
--ignore-scripts --no-save --no-audit --no-fund \
@img/sharp-linuxmusl-arm64@0.34.5 \
@img/sharp-libvips-linuxmusl-arm64@1.2.4 \
&& npm rebuild
FROM node:22-alpine AS runner FROM node:22-alpine AS runner
WORKDIR /app WORKDIR /app

View File

@@ -11,6 +11,16 @@ services:
- IMAGE_DIR=/data/images - IMAGE_DIR=/data/images
- SEARXNG_URL=http://searxng:8080 - SEARXNG_URL=http://searxng:8080
- NODE_ENV=production - NODE_ENV=production
# Im Header als kleine Versionsnummer unter dem Logo angezeigt.
- KOCHWAS_TAG=${KOCHWAS_TAG:-dev}
# Gemini (Foto-Rezept-Magie). Leer = Feature deaktiviert.
- GEMINI_API_KEY=${GEMINI_API_KEY:-}
- GEMINI_MODEL=${GEMINI_MODEL:-gemini-2.5-flash}
- GEMINI_TIMEOUT_MS=${GEMINI_TIMEOUT_MS:-20000}
# adapter-node-Default ist 512 KB. Tablet- und iPad-Pro-Kameras liefern
# JPEGs/HEICs bis 15 MB. Endpoint-Limit ist 20 MB; hier 25 MB fuer den
# Multipart-Overhead.
- BODY_SIZE_LIMIT=25000000
depends_on: depends_on:
- searxng - searxng
restart: unless-stopped restart: unless-stopped

View File

@@ -17,8 +17,12 @@ src/
├── app.html, app.d.ts # Shell + Env-Types ├── app.html, app.d.ts # Shell + Env-Types
├── service-worker.ts # PWA-Shell ├── service-worker.ts # PWA-Shell
├── lib/ ├── lib/
│ ├── client/ # clientseitig: Profil-Store, Confirm-Dialog │ ├── client/ # reaktive Stores (Profile, Search, Wishlist, PWA, Network, Sync, Toast, Install, Confirm, API-Fetch-Wrapper)
│ ├── components/ # Svelte-Komponenten (RecipeView, StarRating, ConfirmDialog, ProfileSwitcher) │ ├── components/ # Svelte-Komponenten:
│ │ # - Recipe: RecipeView, RecipeEditor + Editor-Sub-Components
│ │ # (IngredientRow, StepList, ImageUploadBox, TimeDisplay, recipe-editor-types)
│ │ # - UI-Shell: ConfirmDialog, ProfileSwitcher, SyncIndicator, Toast, UpdateToast
│ │ # - Search: SearchFilter, SearchLoader, StarRating
│ ├── recipes/ # shared: Portionen-Scaler (Client UND Server) │ ├── recipes/ # shared: Portionen-Scaler (Client UND Server)
│ ├── server/ # nur Server-Code (nie in Client-Bundle!) │ ├── server/ # nur Server-Code (nie in Client-Bundle!)
│ │ ├── db/ # openDb, Migrations, DB-Singleton │ │ ├── db/ # openDb, Migrations, DB-Singleton
@@ -31,14 +35,13 @@ src/
│ │ ├── search/ # searxng.ts (Web-Suche + Thumbnail-Cache) │ │ ├── search/ # searxng.ts (Web-Suche + Thumbnail-Cache)
│ │ ├── wishlist/ # Repo │ │ ├── wishlist/ # Repo
│ │ └── backup/ # ZIP-Export via archiver, Import via yauzl │ │ └── backup/ # ZIP-Export via archiver, Import via yauzl
│ ├── quotes.ts # 49 Flachwitze für die Homepage │ ├── quotes.ts # 150 Flachwitze für die Homepage
│ └── types.ts # shared types │ └── types.ts # shared types
└── routes/ └── routes/
├── +layout.svelte # Header, Confirm-Dialog-Mount, Header-Search-Dropdown ├── +layout.svelte # Header, Confirm-Dialog-Mount, Header-Search-Dropdown
├── +page.svelte # Home: Hero + Live-Search + Zuletzt-hinzugefügt ├── +page.svelte # Home: Hero + Live-Search + Zuletzt-hinzugefügt
├── recipes/[id]/ # Rezept-Detail ├── recipes/[id]/ # Rezept-Detail
├── preview/ # Vorschau vor dem Speichern ├── preview/ # Vorschau vor dem Speichern
├── search/ # /search (lokal), /search/web (Internet)
├── wishlist/ ├── wishlist/
├── admin/ # Whitelist, Profile, Backup/Restore ├── admin/ # Whitelist, Profile, Backup/Restore
├── images/[filename] # Statische Auslieferung lokaler Bilder ├── images/[filename] # Statische Auslieferung lokaler Bilder
@@ -52,11 +55,23 @@ src/
1. User klickt auf Web-Hit → `/preview?url=...` 1. User klickt auf Web-Hit → `/preview?url=...`
2. `/api/recipes/preview``importer.ts` lädt HTML, `parseHTML` von linkedom, `json-ld-recipe.ts` extrahiert `Recipe`-Objekt mit **externer** Bild-URL 2. `/api/recipes/preview``importer.ts` lädt HTML, `parseHTML` von linkedom, `json-ld-recipe.ts` extrahiert `Recipe`-Objekt mit **externer** Bild-URL
3. Preview-Seite rendert das `Recipe` via `RecipeView.svelte` (erkennt externe URL und lädt direkt vom Original-CDN) 3. Preview-Seite rendert das `Recipe` via `RecipeView.svelte` (erkennt externe URL und lädt direkt vom Original-CDN)
4. User klickt „Speichern" → `/api/recipes/import` → Importer lädt Bild (`images/downloader.ts`), SHA256-Hash-Dedup, speichert lokal, INSERT in `recipe` + `recipe_ingredient` + `recipe_step` + `recipe_tag` 4. User klickt „Speichern" → `/api/recipes/import` → Importer lädt Bild (`images/downloader.ts`), SHA256-Hash-Dedup, speichert lokal, INSERT in `recipe` + `ingredient` + `step` + `recipe_tag`
5. Redirect zu `/recipes/[id]` 5. Redirect zu `/recipes/[id]`
### Foto-Rezept-Magie (AI-Extraktion)
1. User klickt Camera-Icon im Header → `/new/from-photo` (nur gerendert wenn `GEMINI_API_KEY` gesetzt; disabled wenn offline)
2. File-Picker mit `capture="environment"` öffnet direkt die Rückkamera auf Mobile
3. Upload als `multipart/form-data``POST /api/recipes/extract-from-photo`
4. Server: MIME-Whitelist + 8 MB-Gate + Rate-Limit (10/min/IP) → `preprocessImage` (sharp, ≤1600px lange Kante, JPEG-Re-encode, Metadata-Strip) → `extractRecipeFromImage` (Gemini 2.5 Flash mit structured `responseSchema`, `temperature: 0.1`, Zod-validiert, 1× Retry bei Schema-Fehler oder 5xx) → zufällige Description aus `description-phrases.ts` (50er-Pool) → Response mit `Partial<Recipe>`
5. **Das Original-Foto wird nicht persistiert.** Der Server loggt keine Prompt/Response-Inhalte — nur Code, Dauer, Byte-Größe.
6. Client hält das Ergebnis im `PhotoUploadStore` und rendert `<RecipeEditor recipe={extracted}>`. Weil `recipe.id === null` ist, blendet der Editor den `ImageUploadBox`-Block aus und zeigt nur den Hinweis „Bild kannst du nach dem Speichern hinzufügen."
7. User editiert und klickt „Speichern" → `POST /api/recipes` (neuer Scratch-Insert-Endpoint, wrappt `insertRecipe`) → Redirect auf `/recipes/:id`
### Web-Suche ### Web-Suche
Die gesamte Live-Search-Logik ist im `SearchStore` (`src/lib/client/search.svelte.ts`) gekapselt: Debounce, Race-Guard, Pagination, Web-Fallback, Snapshot/Restore für Back-Nav. Sowohl Header-Dropdown (`+layout.svelte`) als auch Startseite (`+page.svelte`) teilen sich die Klasse mit unterschiedlicher `filterParam`-Quelle.
1. User tippt → 300 ms Debounce → `/api/recipes/search?q=...` (lokal FTS5) 1. User tippt → 300 ms Debounce → `/api/recipes/search?q=...` (lokal FTS5)
2. Wenn 0 Treffer: automatisch `/api/recipes/search/web?q=...` 2. Wenn 0 Treffer: automatisch `/api/recipes/search/web?q=...`
3. `searxng.ts` → SearXNG-API mit `site:domain OR site:domain2 ...`-Filter aus Whitelist 3. `searxng.ts` → SearXNG-API mit `site:domain OR site:domain2 ...`-Filter aus Whitelist
@@ -87,7 +102,8 @@ Gemeinsame Komponente `ConfirmDialog.svelte` wird im Root-Layout einmal gemounte
- **JSON-LD first**: Alle drei Ziel-Domains (Chefkoch, Emmi, Experimente) liefern `schema.org/Recipe` im JSON-LD. LLM-Fallback war geplant, aktuell nicht nötig. - **JSON-LD first**: Alle drei Ziel-Domains (Chefkoch, Emmi, Experimente) liefern `schema.org/Recipe` im JSON-LD. LLM-Fallback war geplant, aktuell nicht nötig.
- **SearXNG als Such-Engine**: Self-hosted, daher keine API-Keys. Das Bot-Detection-Theater wird mit gesetzten `X-Forwarded-For`-Headern aus Docker-IPs umgangen. - **SearXNG als Such-Engine**: Self-hosted, daher keine API-Keys. Das Bot-Detection-Theater wird mit gesetzten `X-Forwarded-For`-Headern aus Docker-IPs umgangen.
- **Thumbnail-Cache in SQLite**: 30 Tage TTL (per `KOCHWAS_THUMB_TTL_DAYS`). Negative Einträge (Seite ohne Bild) werden auch gecacht. - **Thumbnail-Cache in SQLite**: 30 Tage TTL (per `KOCHWAS_THUMB_TTL_DAYS`). Negative Einträge (Seite ohne Bild) werden auch gecacht.
- **Svelte 5 Runes** — kein `$:` mehr, keine alten Stores außer `$app/stores`. Neue Stores via Klasse mit `$state`-Feldern. - **Svelte 5 Runes** — kein `$:` mehr, keine alten Stores außer `$app/stores`. Neue Stores via Klasse mit `$state`-Feldern. Form-lokale Snapshots in Edit-Komponenten mit `untrack()` aus `svelte`, damit Prop-Updates nicht laufende Edits überschreiben.
- **Zutaten-Sektionen** (ab Migration 012, v1.2): `ingredient.section_heading TEXT NULL`. Ist das Feld gesetzt, startet an dieser Zeile eine neue Sektion — folgende Zutaten gehören dazu, bis die nächste Zeile wieder ein Heading hat. Kein zweites Tabellen-Modell, Ordnung bleibt `position`. Importer setzt immer `null` (schema.org/Recipe hat das Konzept nicht). Editor erlaubt Inline-Insert via `Abschnitt hinzufügen`-Button vor jeder Zeile; leeres Heading wird beim Save zu `null` normalisiert.
- **Service Worker** rein zum Shell-Cachen für Offline-First-PWA, kein intelligentes Cache-Matching (keine externe Rezept-Seiten). - **Service Worker** rein zum Shell-Cachen für Offline-First-PWA, kein intelligentes Cache-Matching (keine externe Rezept-Seiten).
## Migrations-Workflow ## Migrations-Workflow
@@ -101,10 +117,12 @@ Bei Schema-Änderung:
## Test-Strategie ## Test-Strategie
- **Unit**: `tests/unit/` — pure Funktionen (json-ld-recipe, iso8601-duration, quotes-random, smoke) - **Unit**: `tests/unit/` — pure Funktionen + Client-Stores via jsdom (json-ld-recipe, iso8601-duration, quotes-random, scaler, ingredient-parser, SearchStore, PWA/Toast/Sync-Stores, SW-Logik).
- **Integration**: `tests/integration/` — mit `openInMemoryForTest()` fresh SQLite pro Test. Externe HTTP via `node:http`-TestServer auf Port 0 gemockt. - **Integration**: `tests/integration/` — mit `openInMemoryForTest()` fresh SQLite pro Test. Externe HTTP via `node:http`-TestServer auf Port 0 gemockt.
- **Keine Svelte-Component-Tests** (bewusst, Aufwand/Nutzen stimmt nicht; UI wird manuell getestet) - **E2E local**: `tests/e2e/` — Playwright gegen `npm run preview`, deckt PWA-Offline-Lifecycle ab (`offline.spec.ts`).
- **Vor Commit**: `npm test && npm run check` muss grün sein. - **E2E remote**: `tests/e2e/remote/` — Playwright gegen `kochwas-dev.siegeln.net` via `playwright.remote.config.ts` (`workers:1`, `serviceWorkers:block`). Testet Live-API-Verhalten, inkl. destruktiver CRUD-Flows (Recipes, Kommentare, Favoriten). Run: `npm run test:e2e:remote`. Siehe `tests/e2e/remote/fixtures/` für Profile-Setup + idempotente API-Cleanup-Helper.
- **Keine Svelte-Component-Unit-Tests** (bewusst, Aufwand/Nutzen stimmt nicht; UI wird per E2E und manuell getestet).
- **Vor Commit**: `npm test && npm run check` muss grün sein. Vor Merge zu main: zusätzlich `npm run test:e2e:remote`.
### Service Worker (PWA) ### Service Worker (PWA)
@@ -112,11 +130,11 @@ Bei Schema-Änderung:
- **Pre-Cache** (alle Rezepte + Bilder beim Initial-Sync), über paginierten Fetch von `/api/recipes/all`. - **Pre-Cache** (alle Rezepte + Bilder beim Initial-Sync), über paginierten Fetch von `/api/recipes/all`.
- **Delta-Sync** beim App-Start (diff vs. Cache-Manifest, nur Delta laden). - **Delta-Sync** beim App-Start (diff vs. Cache-Manifest, nur Delta laden).
- **Drei Cache-Strategien** (dispatcht per `resolveStrategy`): Shell = cache-first, Daten = SWR, Bilder = cache-first. - **Drei Cache-Strategien** (dispatcht per `resolveStrategy`): Shell = cache-first, Daten = network-first mit 3 s-Timeout-Fallback auf Cache, Bilder = cache-first.
- **Message-Protokoll** (`sync-start`, `sync-progress`, `sync-done`, `sync-error`) zwischen SW und Client. - **Message-Protokoll** (`sync-start`, `sync-progress`, `sync-done`, `sync-error`) zwischen SW und Client.
Reine Logik-Einheiten (testbar, Unit-Tests in `tests/unit/`): Reine Logik-Einheiten (testbar, Unit-Tests in `tests/unit/`):
- `src/lib/sw/cache-strategy.ts``resolveStrategy({url, method})``'shell' | 'swr' | 'images' | 'network-only'` - `src/lib/sw/cache-strategy.ts``resolveStrategy({url, method})``'shell' | 'network-first' | 'images' | 'network-only'`
- `src/lib/sw/diff-manifest.ts``diffManifest(current, cached)``{toAdd, toRemove}` - `src/lib/sw/diff-manifest.ts``diffManifest(current, cached)``{toAdd, toRemove}`
Client-Stores (SSR-safe via typeof-Guards): Client-Stores (SSR-safe via typeof-Guards):

View File

@@ -133,7 +133,7 @@ Die App hat ein eingebautes Backup unter `/admin` (ZIP-Export mit DB + Bildern).
| `SEARXNG_URL` | `http://localhost:8888` | SearXNG-Endpoint, im Compose auf `http://searxng:8080` | | `SEARXNG_URL` | `http://localhost:8888` | SearXNG-Endpoint, im Compose auf `http://searxng:8080` |
| `KOCHWAS_THUMB_TTL_DAYS` | `30` | TTL für Thumbnail-Cache in der SQLite | | `KOCHWAS_THUMB_TTL_DAYS` | `30` | TTL für Thumbnail-Cache in der SQLite |
| `DATABASE_PATH` | `data/kochwas.db` | Pfad zur SQLite, relativ oder absolut | | `DATABASE_PATH` | `data/kochwas.db` | Pfad zur SQLite, relativ oder absolut |
| `IMAGES_PATH` | `data/images` | Pfad für lokale Bild-Dateien | | `IMAGE_DIR` | `data/images` | Pfad für lokale Bild-Dateien |
| `PORT` | `3000` | Node-HTTP-Port (adapter-node) | | `PORT` | `3000` | Node-HTTP-Port (adapter-node) |
Siehe `.env.example` im Repo. Siehe `.env.example` im Repo.
@@ -155,7 +155,7 @@ Kochwas ist eine installierbare PWA. Erkennbar an:
Caches im Browser (siehe DevTools → Application → Cache Storage): Caches im Browser (siehe DevTools → Application → Cache Storage):
- `kochwas-shell-<version>` — App-Shell (JS/CSS/Static-Icons), cache-first - `kochwas-shell-<version>` — App-Shell (JS/CSS/Static-Icons), cache-first
- `kochwas-data-v1` — Rezept-HTMLs + API-JSON (SWR) - `kochwas-data-v1` — Rezept-HTMLs + API-JSON (network-first, 3 s Timeout → Cache-Fallback)
- `kochwas-images-v1` — Bilder (cache-first) - `kochwas-images-v1` — Bilder (cache-first)
- `kochwas-meta` — Cache-Manifest (Liste der gecachten Rezept-IDs unter `/__cache-manifest__`) - `kochwas-meta` — Cache-Manifest (Liste der gecachten Rezept-IDs unter `/__cache-manifest__`)
@@ -171,3 +171,42 @@ Bei SW-Problemen Debug-Pfad:
E2E-Tests (Playwright): `npm run test:e2e`. Setzt `npm run build` voraus (Playwright startet automatisch `npm run preview`). E2E-Tests (Playwright): `npm run test:e2e`. Setzt `npm run build` voraus (Playwright startet automatisch `npm run preview`).
Icons einmalig rendern: `npm run render:icons` (schreibt nach `static/icon-*.png`, committen). Icons einmalig rendern: `npm run render:icons` (schreibt nach `static/icon-*.png`, committen).
## Dev-System / Remote-E2E
`https://kochwas-dev.siegeln.net/` ist ein separates Deployment (eigener Container, eigene DB unter `/opt/docker/kochwas-dev/data/`). Zweck: E2E-Tests gegen eine prod-nahe Umgebung ohne Angst vor DB-Schäden. Die Remote-Suite (`tests/e2e/remote/`, Config `playwright.remote.config.ts`) darf dort frei CRUDen — User stellt die DB bei Bedarf per Backup wieder her.
```bash
npm run test:e2e:remote # gegen kochwas-dev
E2E_REMOTE_URL=https://... npm run test:e2e:remote # andere URL
```
Wichtige Config-Eigenschaften:
- `workers: 1` — DB-Race-Sicherheit bei CRUD-Tests.
- `serviceWorkers: 'block'` — verhindert Chromium-Crashes durch akkumulierten SW-State über 40+ Contexts.
- Fixtures unter `tests/e2e/remote/fixtures/`: `profile.ts` (Profile-Auswahl via localStorage vor Seitenladen), `api-cleanup.ts` (idempotente DELETE-Helfer für afterEach).
**Niemals gegen `kochwas.siegeln.net` (ohne `-dev`)** die destruktiven Tests laufen lassen — das ist Prod.
## Gemini / Foto-Rezept-Magie
Die Funktion „Foto → Rezept" ruft Google Gemini 2.5 Flash mit Vision auf. Im Header erscheint dann ein Kamera-Icon, das auf `/new/from-photo` führt.
**Env-Vars** (in `docker-compose.prod.yml`):
| Variable | Default | Zweck |
|---|---|---|
| `GEMINI_API_KEY` | _(leer)_ | Ohne Key ist das Feature graceful deaktiviert — Camera-Icon erscheint nicht. |
| `GEMINI_MODEL` | `gemini-2.5-flash` | Modell-Wechsel ohne Rebuild, z. B. auf `gemini-2.5-pro` bei harter Handschrift. |
| `GEMINI_TIMEOUT_MS` | `20000` | Timeout für den Vision-Call. |
**Wichtig:** Env-Änderungen greifen erst nach `docker compose up -d --force-recreate`, nicht nach `restart`.
**Privacy:** Das hochgeladene Foto geht einmal an Google Gemini und wird serverseitig nicht gespeichert. Google trainiert im Paid-Tier nicht auf API-Daten. Der Server loggt nur Status-Code, Dauer und Bildgröße — nie Prompt oder Response-Inhalt.
**Rate-Limit:** 10 Requests/Minute pro IP (in-memory, resettet beim Prozess-Restart).
**Key aus Gitea Secrets:** `GEMINI_API_KEY` als Secret in der CI-Umgebung hinterlegen; der Deploy-Schritt injiziert ihn in die `.env` des Pi-Stacks. Ablauf-Monitoring über die Google-Cloud-Konsole (≥1× pro Quartal checken).
**Build-Dep `sharp`:** Der Foto-Preprocess nutzt `sharp` (libvips). Im `Dockerfile`-Builder-Stage ist `vips-dev` enthalten, damit der npm-install auf arm64 sauber durchläuft — insbesondere für HEIC-Input von iOS-Geräten (libheif kommt via vips-dev).

View File

@@ -0,0 +1,153 @@
# Review-Fixes 2026-04-18 — Implementation Plan
> **Quelle:** `docs/superpowers/review/REVIEW-2026-04-18.md` + Sub-Reports.
> **Branch:** `review-fixes-2026-04-18`
> **Goal:** Alle HIGH/MEDIUM Findings aus dem Code-Review adressieren, bewusst verschobene Items dokumentieren.
> **Architecture:** Inkrementelle Refactors, jeder atomar committed + gepusht, Tests nach jedem Wave grün.
> **Tech-Stack:** SvelteKit, TypeScript-strict, Zod, Vitest, better-sqlite3, Service-Worker.
---
## Was wird angegangen (must-do)
| # | Wave | Zeit | Begründung |
|---|------|------|------------|
| 1 | Doku-Fixes (ARCHITECTURE/OPERATIONS/handoff) | 5 min | Hoher Wert, trivialer Aufwand |
| 2 | constants.ts + Image-Endpoint EN + interne Types | 30 min | Alle "Quick-Wins" aus REVIEW |
| 3 | api-helpers.ts (parsePositiveIntParam + validateBody) | 1-2 h | Refactor A — 9+11 Call-Sites |
| 4 | requireProfile() + asyncFetch Wrapper | 1 h | Profile-Guard 4× + fetch-Pattern 5× |
| 5 | Cleanup (yauzl-Doku, baseRecipe-Fixture, Console-Logs) | 30 min | Restliche LOW-Findings |
| 6 | Ingredient-Parser Edge-Cases (Refactor D) | 2-3 h | Locale-Komma, Unicode-Brüche, Bounds |
| 7 | Verifikation (test/check/build, Docker-Smoke) | 30 min | Baseline gegen Regressionen |
| 8 | Re-Review + OPEN-ISSUES-NEXT.md | 1 h | Beweis + Ausblick |
## Was bewusst NICHT angegangen wird (Begründung in OPEN-ISSUES-NEXT.md)
- **Refactor B** (Search-State-Store, halber Tag): Touch von 808-Zeilen-Page + 678-Zeilen-Layout, bricht riskant Frontend ohne UAT. Eigene Phase planen.
- **Refactor C** (RecipeEditor zerlegen): Review sagt explizit "keine Eile, solange niemand sonst drin arbeitet".
- **SearXNG Rate-Limit Recovery**: Größeres Feature, eigene Phase.
- **SW-Zombie-Cleanup Unit-Tests**: Bereits 6 pwa-store-Tests vorhanden, Erweiterung wäre Bonus.
- **JSON-LD Parser Edge-Cases** (Locales): Weniger Käse als Ingredient-Parser-Issues, eigene Iteration.
---
## Wave 1 — Doku-Fixes
**Files:** `docs/ARCHITECTURE.md:55`, `docs/OPERATIONS.md:135`, `docs/superpowers/session-handoff-2026-04-17.md:46`
- [ ] ARCHITECTURE.md: `recipe_ingredient` + `recipe_step``ingredient` + `step`
- [ ] OPERATIONS.md: `IMAGES_PATH``IMAGE_DIR`
- [ ] session-handoff: `/api/recipes/[id]/image` (POST/DELETE) ergänzen
- [ ] Commit `docs(review): Doku-Mismatches korrigiert`
## Wave 2 — Konstanten + Cleanup
**Files:** `src/lib/constants.ts` (neu), `src/routes/+page.svelte`, `src/lib/client/pwa.svelte.ts`, `src/routes/api/recipes/[id]/image/+server.ts`, `src/lib/sw/cache-strategy.ts`, `src/lib/sw/diff-manifest.ts`
- [ ] `src/lib/constants.ts` mit `SW_VERSION_QUERY_TIMEOUT_MS = 1500`, `SW_UPDATE_POLL_INTERVAL_MS = 30 * 60_000`
- [ ] Image-Endpoint: deutsche Fehlermeldungen → englisch (Konsistenz)
- [ ] `RequestShape` / `ManifestDiff`: `export` weg wenn rein intern
- [ ] Test + check, Commit
## Wave 3 — api-helpers.ts (TDD)
**Files:** `src/lib/server/api-helpers.ts` (neu), `tests/unit/api-helpers.test.ts` (neu), `src/lib/types.ts` (ErrorResponse)
### 3a Helper bauen
- [ ] Test: `parsePositiveIntParam("42", "id")` → 42
- [ ] Test: `parsePositiveIntParam("0", "id")` wirft 400
- [ ] Test: `parsePositiveIntParam("abc", "id")` wirft 400
- [ ] Test: `parsePositiveIntParam(null, "id")` wirft 400
- [ ] Test: `validateBody(invalid, schema)` wirft 400 mit issues
- [ ] Test: `validateBody(valid, schema)` returns parsed
- [ ] Implement helpers
- [ ] Tests grün, Commit
### 3b Migration parseId → parsePositiveIntParam (9 Sites)
Files (jeder Endpoint):
- `src/routes/api/recipes/[id]/+server.ts`
- `src/routes/api/recipes/[id]/favorite/+server.ts`
- `src/routes/api/recipes/[id]/rating/+server.ts`
- `src/routes/api/recipes/[id]/cooked/+server.ts`
- `src/routes/api/recipes/[id]/comments/+server.ts`
- `src/routes/api/recipes/[id]/image/+server.ts`
- `src/routes/api/profiles/[id]/+server.ts`
- `src/routes/api/domains/[id]/+server.ts`
- `src/routes/api/wishlist/[recipe_id]/+server.ts`
- [ ] Pro Endpoint: lokales parseId entfernen, Helper importieren
- [ ] Tests grün
- [ ] Commit
### 3c Migration safeParse → validateBody
Files: alle `+server.ts` mit `safeParse`. ErrorResponse-Shape standardisieren.
- [ ] Pro Endpoint umstellen
- [ ] Tests grün
- [ ] Commit
## Wave 4 — Client-Helpers
### 4a requireProfile()
- [ ] Helper in `src/lib/client/profile.svelte.ts` ergänzen
- [ ] 4 Sites in `src/routes/recipes/[id]/+page.svelte` ersetzen
- [ ] Test + Commit
### 4b asyncFetch Wrapper
- [ ] `src/lib/client/api-fetch-wrapper.ts` mit `asyncFetch(url, init, actionTitle)`
- [ ] 5 Sites umstellen: `recipes/[id]/+page.svelte` (2×), `admin/domains/+page.svelte` (2×), `admin/profiles/+page.svelte`
- [ ] Test + Commit
## Wave 5 — Cleanup
- [ ] yauzl: Inline-Kommentar in package.json: "Reserved for Phase 5b ZIP-Backup-Import"
- [ ] baseRecipe Fixture nach `tests/fixtures/recipe.ts` (wenn dupliziert)
- [ ] Console-Logs: per `if (import.meta.env.DEV)` wrappen oder absichtlich-Kommentar
- [ ] Commit
## Wave 6 — Ingredient-Parser Edge-Cases
**Files:** `src/lib/server/parsers/ingredient.ts`, `tests/unit/ingredient.test.ts`
### Tests zuerst (red)
- [ ] Locale-Komma: `"1,5 kg Mehl"` → qty 1.5
- [ ] Unicode-½: `"½ TL Salz"` → qty 0.5
- [ ] Unicode-⅓: `"⅓ Tasse Wasser"` → qty 1/3
- [ ] Unicode-¼: `"¼ kg Zucker"` → qty 0.25
- [ ] Negativ: `"-1 EL Öl"` → wirft / qty=null
- [ ] Null: `"0 g Mehl"` → wirft / qty=null
- [ ] Führende Null: `"0.5 kg"` → 0.5
- [ ] Wissenschaftliche Notation: `"1e3 g"` → wirft / qty=null
### Parser fixen
- [ ] Unicode-Brüche-Map
- [ ] Locale-Komma-Handling (sicher: "1,5" wenn nur 1 Komma + Ziffern drumrum)
- [ ] Bounds: 0 < qty <= 10000 (Zod refinement oder Pre-Check)
- [ ] Tests grün, Commit
## Wave 7 — Verifikation
- [ ] `npm test` — 158+ Tests grün
- [ ] `npm run check` — 0 Errors
- [ ] `npm run build` — erfolgreich
- [ ] Optional: Docker-Smoke `docker compose -f docker-compose.prod.yml up --build`
- [ ] Push aller Commits
## Wave 8 — Re-Review + OPEN-ISSUES-NEXT.md
- [ ] Parallele Explore-Agenten: dead-code, redundancy, structure, docs-vs-code
- [ ] Befunde in `docs/superpowers/review/OPEN-ISSUES-NEXT.md`
- [ ] Bewusst verschobene Items mit Begründung
- [ ] Neue Findings (falls vorhanden)
- [ ] Commit + Push
---
## Erfolgs-Kriterien
1. Tests grün (158+)
2. svelte-check: 0 Errors, 0 Warnings (oder ≤ Baseline)
3. Build erfolgreich
4. Alle 8 Quick-Wins + Refactor A + Refactor D umgesetzt
5. OPEN-ISSUES-NEXT.md vorhanden mit klarer Trennung "verschoben (warum)" vs "neu entdeckt"
6. Branch ready zum Mergen / PR

View File

@@ -0,0 +1,897 @@
# Editor-Split Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use `superpowers:subagent-driven-development` (recommended) or `superpowers:executing-plans` to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Split the monolithic `RecipeEditor.svelte` (628 L) and pull one readability-oriented block out of `RecipeView.svelte` (398 L) by extracting 4 focused Svelte components: `ImageUploadBox`, `IngredientRow`, `StepList`, `TimeDisplay`. No behavior changes, just structure.
**Architecture:** Parent-owned state stays in the parent (`RecipeEditor` still owns `ingredients: DraftIng[]`, `steps: DraftStep[]`). Sub-components receive props + callbacks and render their own template + scoped CSS. Shared draft types land in `src/lib/components/recipe-editor-types.ts` so sub-components and parent agree on the shape. `RecipeView.TimeDisplay` is pure presentational with no state.
**Tech Stack:** Svelte 5 runes (`$props`, `$state`, `$derived`), TypeScript-strict, no new runtime deps.
---
## Why this is worth doing
- `RecipeEditor.svelte:42-89` (Bild-Upload) and `RecipeEditor.svelte:313-334` (Zubereitung) are each self-contained logic-islands with their own state and handlers. Extracting them caps the file a Claude can reason about in one shot.
- `IngredientRow` renders 10 lines of template with 5 ARIA labels and 6 grid-columns — a natural single-responsibility unit.
- `TimeDisplay` is pure formatting; owning it as a component lets future phases (preview, card hover) reuse it.
## What we are NOT doing
- No refactor of `RecipeView`'s tabs / servings-stepper / ingredient-display. Those work fine as-is; roadmap only names the 4 above.
- No component unit tests (kochwas has none for components; the e2e `recipe-detail.spec.ts` still covers View behavior, and edit-flow is manually smoked).
- No `<style global>` extraction. Small CSS duplication (`.add`, `.del` buttons) is accepted.
- No prop-type sharing via `<script module>` blocks. A `.ts` sibling file is simpler.
## Design Snapshot
**Shared types**`src/lib/components/recipe-editor-types.ts`:
```ts
export type DraftIng = {
qty: string;
unit: string;
name: string;
note: string;
};
export type DraftStep = { text: string };
```
**Component APIs (locked before implementation):**
```ts
// ImageUploadBox.svelte
type Props = {
recipeId: number;
imagePath: string | null; // initial value; component owns its own state after
onchange: (path: string | null) => void;
};
// IngredientRow.svelte
type Props = {
ing: DraftIng; // passed by reference — bind:value=ing.* works transparently
idx: number;
total: number; // for "last row? disable move-down"
onmove: (dir: -1 | 1) => void;
onremove: () => void;
};
// StepList.svelte
type Props = {
steps: DraftStep[]; // passed by reference
onadd: () => void;
onremove: (idx: number) => void;
};
// TimeDisplay.svelte
type Props = {
prepTimeMin: number | null;
cookTimeMin: number | null;
totalTimeMin: number | null;
};
```
**Render-wrapping pattern:** The parent keeps the `<section class="block"><h2>…</h2> … </section>` wrappers. Sub-components render bare content (no outer utility-class wrapper), so the parent's scoped `.block` / `h2` styling continues to apply.
---
## Task 1: Extract `ImageUploadBox`
**Files:**
- Create: `src/lib/components/ImageUploadBox.svelte`
- Modify: `src/lib/components/RecipeEditor.svelte`
- [ ] **Step 1: Create the new component**
```svelte
<!-- src/lib/components/ImageUploadBox.svelte -->
<script lang="ts">
import { ImagePlus, ImageOff } from 'lucide-svelte';
import { confirmAction } from '$lib/client/confirm.svelte';
import { asyncFetch } from '$lib/client/api-fetch-wrapper';
import { requireOnline } from '$lib/client/require-online';
type Props = {
recipeId: number;
imagePath: string | null;
onchange: (path: string | null) => void;
};
let { recipeId, imagePath: initial, onchange }: Props = $props();
let imagePath = $state<string | null>(initial);
let uploading = $state(false);
let fileInput: HTMLInputElement | null = $state(null);
const imageSrc = $derived(
imagePath === null
? null
: /^https?:\/\//i.test(imagePath)
? imagePath
: `/images/${imagePath}`
);
async function onFileChosen(event: Event) {
const input = event.target as HTMLInputElement;
const file = input.files?.[0];
input.value = '';
if (!file) return;
if (!requireOnline('Der Bild-Upload')) return;
uploading = true;
try {
const fd = new FormData();
fd.append('file', file);
const res = await asyncFetch(
`/api/recipes/${recipeId}/image`,
{ method: 'POST', body: fd },
'Upload fehlgeschlagen'
);
if (!res) return;
const body = await res.json();
imagePath = body.image_path;
onchange(imagePath);
} finally {
uploading = false;
}
}
async function removeImage() {
if (imagePath === null) return;
const ok = await confirmAction({
title: 'Bild entfernen?',
message: 'Das Rezept wird danach ohne Titelbild angezeigt.',
confirmLabel: 'Entfernen',
destructive: true
});
if (!ok) return;
if (!requireOnline('Das Entfernen')) return;
uploading = true;
try {
const res = await asyncFetch(
`/api/recipes/${recipeId}/image`,
{ method: 'DELETE' },
'Entfernen fehlgeschlagen'
);
if (!res) return;
imagePath = null;
onchange(null);
} finally {
uploading = false;
}
}
</script>
<div class="image-row">
<div class="image-preview" class:empty={!imageSrc}>
{#if imageSrc}
<img src={imageSrc} alt="" />
{:else}
<span class="placeholder">Kein Bild</span>
{/if}
</div>
<div class="image-actions">
<button
class="btn"
type="button"
onclick={() => fileInput?.click()}
disabled={uploading}
>
<ImagePlus size={16} strokeWidth={2} />
<span>{imagePath ? 'Bild ersetzen' : 'Bild hochladen'}</span>
</button>
{#if imagePath}
<button class="btn ghost" type="button" onclick={removeImage} disabled={uploading}>
<ImageOff size={16} strokeWidth={2} />
<span>Entfernen</span>
</button>
{/if}
{#if uploading}
<span class="upload-status">Lade …</span>
{/if}
</div>
<input
bind:this={fileInput}
type="file"
accept="image/jpeg,image/png,image/webp,image/gif,image/avif"
class="file-input"
onchange={onFileChosen}
/>
</div>
<p class="image-hint">Max. 10 MB. JPG, PNG, WebP, GIF oder AVIF.</p>
<style>
.image-row {
display: flex;
gap: 1rem;
align-items: flex-start;
flex-wrap: wrap;
}
.image-preview {
width: 160px;
aspect-ratio: 16 / 10;
border-radius: 10px;
overflow: hidden;
background: #eef3ef;
border: 1px solid #e4eae7;
flex-shrink: 0;
}
.image-preview img {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
.image-preview.empty {
display: grid;
place-items: center;
color: #999;
font-size: 0.85rem;
}
.image-preview .placeholder {
padding: 0 0.5rem;
text-align: center;
}
.image-actions {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
align-items: center;
}
.upload-status {
color: #666;
font-size: 0.9rem;
}
.file-input {
position: absolute;
width: 1px;
height: 1px;
opacity: 0;
pointer-events: none;
}
.image-hint {
margin: 0.6rem 0 0;
color: #888;
font-size: 0.8rem;
}
.btn {
padding: 0.55rem 0.85rem;
border-radius: 10px;
border: 1px solid #cfd9d1;
background: white;
cursor: pointer;
font-family: inherit;
font-size: 0.9rem;
min-height: 40px;
display: inline-flex;
align-items: center;
gap: 0.4rem;
}
.btn.ghost {
color: #666;
}
.btn:disabled {
opacity: 0.6;
cursor: progress;
}
</style>
```
- [ ] **Step 2: Wire up `RecipeEditor.svelte`**
Remove lines 3089 (imagePath/uploading/fileInput state, imageSrc derived, onFileChosen, removeImage).
Remove these imports at the top:
```ts
import { Plus, Trash2, ChevronUp, ChevronDown, ImagePlus, ImageOff } from 'lucide-svelte';
import { confirmAction } from '$lib/client/confirm.svelte';
import { asyncFetch } from '$lib/client/api-fetch-wrapper';
import { requireOnline } from '$lib/client/require-online';
```
Replace with (Task 1 needs only Plus + Trash2 + Chevrons — the image-specific imports move to the sub-component; `confirmAction`/`asyncFetch`/`requireOnline` stay for future tasks):
```ts
import { Plus, Trash2, ChevronUp, ChevronDown } from 'lucide-svelte';
import ImageUploadBox from '$lib/components/ImageUploadBox.svelte';
```
Remove the image-related CSS (`.image-row`, `.image-preview*`, `.image-actions`, `.image-actions .btn`, `.upload-status`, `.file-input`, `.image-hint`, `.image-block` — those live in the sub-component now).
Replace the Bild section in the template:
```svelte
<section class="block">
<h2>Bild</h2>
<ImageUploadBox
recipeId={recipe.id}
imagePath={recipe.image_path}
onchange={(p) => onimagechange?.(p)}
/>
</section>
```
- [ ] **Step 3: Run checks**
```bash
npm run check
npm test
```
Expected: 0 errors, 196/196 tests pass.
- [ ] **Step 4: Manual smoke**
```bash
npm run dev
```
Open any saved recipe → edit → upload an image → verify it shows up and `onimagechange` fires (parent's state updates). Remove the image → confirms the confirm-dialog and removes. Bail out if either flow breaks.
- [ ] **Step 5: Commit**
```bash
git add src/lib/components/ImageUploadBox.svelte src/lib/components/RecipeEditor.svelte
git commit -m "$(cat <<'EOF'
refactor(editor): ImageUploadBox als eigenstaendige Component
Isoliert den Bild-Upload-Flow (File-Input, Preview, Entfernen-Dialog)
aus dem RecipeEditor. Parent haelt nur noch den <section>-Wrapper und
reicht recipe.id + image_path rein, kriegt Aenderungen per onchange
callback zurueck.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
EOF
)"
```
---
## Task 2: Extract types + `IngredientRow`
**Files:**
- Create: `src/lib/components/recipe-editor-types.ts`
- Create: `src/lib/components/IngredientRow.svelte`
- Modify: `src/lib/components/RecipeEditor.svelte`
- [ ] **Step 1: Types file**
```ts
// src/lib/components/recipe-editor-types.ts
export type DraftIng = {
qty: string;
unit: string;
name: string;
note: string;
};
export type DraftStep = { text: string };
```
- [ ] **Step 2: IngredientRow component**
```svelte
<!-- src/lib/components/IngredientRow.svelte -->
<script lang="ts">
import { Trash2, ChevronUp, ChevronDown } from 'lucide-svelte';
import type { DraftIng } from './recipe-editor-types';
type Props = {
ing: DraftIng;
idx: number;
total: number;
onmove: (dir: -1 | 1) => void;
onremove: () => void;
};
let { ing, idx, total, onmove, onremove }: Props = $props();
</script>
<li class="ing-row">
<div class="move">
<button
class="move-btn"
type="button"
aria-label="Zutat nach oben"
disabled={idx === 0}
onclick={() => onmove(-1)}
>
<ChevronUp size={14} strokeWidth={2.5} />
</button>
<button
class="move-btn"
type="button"
aria-label="Zutat nach unten"
disabled={idx === total - 1}
onclick={() => onmove(1)}
>
<ChevronDown size={14} strokeWidth={2.5} />
</button>
</div>
<input class="qty" type="text" bind:value={ing.qty} placeholder="Menge" aria-label="Menge" />
<input class="unit" type="text" bind:value={ing.unit} placeholder="Einheit" aria-label="Einheit" />
<input class="name" type="text" bind:value={ing.name} placeholder="Zutat" aria-label="Zutat" />
<input class="note" type="text" bind:value={ing.note} placeholder="Notiz" aria-label="Notiz" />
<button class="del" type="button" aria-label="Zutat entfernen" onclick={onremove}>
<Trash2 size={16} strokeWidth={2} />
</button>
</li>
<style>
.ing-row {
display: grid;
grid-template-columns: 28px 70px 70px 1fr 1fr 40px;
gap: 0.35rem;
align-items: center;
}
.move {
display: flex;
flex-direction: column;
gap: 2px;
}
.move-btn {
width: 28px;
height: 20px;
border: 1px solid #cfd9d1;
background: white;
border-radius: 6px;
cursor: pointer;
color: #555;
display: inline-flex;
align-items: center;
justify-content: center;
padding: 0;
}
.move-btn:hover:not(:disabled) {
background: #f4f8f5;
}
.move-btn:disabled {
opacity: 0.3;
cursor: not-allowed;
}
.ing-row input {
padding: 0.5rem 0.55rem;
border: 1px solid #cfd9d1;
border-radius: 8px;
font-size: 0.9rem;
min-height: 38px;
font-family: inherit;
min-width: 0;
}
.del {
width: 40px;
height: 40px;
border: 1px solid #f1b4b4;
background: white;
color: #c53030;
border-radius: 8px;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
}
.del:hover {
background: #fdf3f3;
}
@media (max-width: 560px) {
.ing-row {
grid-template-columns: 28px 70px 1fr 40px;
grid-template-areas:
'move qty name del'
'move unit unit del'
'note note note note';
}
.ing-row .move {
grid-area: move;
}
.ing-row .qty {
grid-area: qty;
}
.ing-row .unit {
grid-area: unit;
}
.ing-row .name {
grid-area: name;
}
.ing-row .note {
grid-area: note;
}
.ing-row .del {
grid-area: del;
}
}
</style>
```
- [ ] **Step 3: Wire up `RecipeEditor.svelte`**
Replace the local `DraftIng` / `DraftStep` type declarations (lines 100106) with:
```ts
import type { DraftIng, DraftStep } from '$lib/components/recipe-editor-types';
import IngredientRow from '$lib/components/IngredientRow.svelte';
```
In the template, swap the `<li class="ing-row">` block for:
```svelte
{#each ingredients as ing, idx (idx)}
<IngredientRow
{ing}
{idx}
total={ingredients.length}
onmove={(dir) => moveIngredient(idx, dir)}
onremove={() => removeIngredient(idx)}
/>
{/each}
```
Remove the CSS for `.ing-row`, `.move`, `.move-btn`, `.ing-row input`, `.del`, and the `@media (max-width: 560px)` block — all now live in `IngredientRow.svelte`.
Remove the unused imports `ChevronUp`, `ChevronDown`, `Trash2` from RecipeEditor (they moved to the sub-component, but wait — `Trash2` is also used for step-remove. Keep `Trash2`, remove the two Chevrons).
- [ ] **Step 4: Run checks**
```bash
npm run check
npm test
```
- [ ] **Step 5: Manual smoke**
Open any recipe in edit mode. Add an ingredient, type into all 4 fields, reorder up/down, remove one. Verify save persists the ordering.
- [ ] **Step 6: Commit**
```bash
git add src/lib/components/recipe-editor-types.ts src/lib/components/IngredientRow.svelte src/lib/components/RecipeEditor.svelte
git commit -m "$(cat <<'EOF'
refactor(editor): IngredientRow + shared types
IngredientRow rendert eine einzelne editierbare Zutat-Zeile. DraftIng
und DraftStep sind jetzt in recipe-editor-types.ts, damit Parent und
Sub-Components auf dieselbe Form referenzieren.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
EOF
)"
```
---
## Task 3: Extract `StepList`
**Files:**
- Create: `src/lib/components/StepList.svelte`
- Modify: `src/lib/components/RecipeEditor.svelte`
- [ ] **Step 1: StepList component**
```svelte
<!-- src/lib/components/StepList.svelte -->
<script lang="ts">
import { Plus, Trash2 } from 'lucide-svelte';
import type { DraftStep } from './recipe-editor-types';
type Props = {
steps: DraftStep[];
onadd: () => void;
onremove: (idx: number) => void;
};
let { steps, onadd, onremove }: Props = $props();
</script>
<ol class="step-list">
{#each steps as step, idx (idx)}
<li class="step-row">
<span class="num">{idx + 1}</span>
<textarea
bind:value={step.text}
rows="3"
placeholder="Schritt beschreiben …"
></textarea>
<button class="del" type="button" aria-label="Schritt entfernen" onclick={() => onremove(idx)}>
<Trash2 size={16} strokeWidth={2} />
</button>
</li>
{/each}
</ol>
<button class="add" type="button" onclick={onadd}>
<Plus size={16} strokeWidth={2} />
<span>Schritt hinzufügen</span>
</button>
<style>
.step-list {
list-style: none;
padding: 0;
margin: 0 0 0.6rem;
display: flex;
flex-direction: column;
gap: 0.4rem;
}
.step-row {
display: grid;
grid-template-columns: 32px 1fr 40px;
gap: 0.5rem;
align-items: start;
}
.num {
width: 32px;
height: 32px;
background: #2b6a3d;
color: white;
border-radius: 50%;
display: grid;
place-items: center;
font-weight: 600;
font-size: 0.9rem;
margin-top: 0.25rem;
}
.step-row textarea {
padding: 0.55rem 0.7rem;
border: 1px solid #cfd9d1;
border-radius: 8px;
font-size: 0.95rem;
font-family: inherit;
resize: vertical;
min-height: 70px;
}
.del {
width: 40px;
height: 40px;
border: 1px solid #f1b4b4;
background: white;
color: #c53030;
border-radius: 8px;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
}
.del:hover {
background: #fdf3f3;
}
.add {
display: inline-flex;
align-items: center;
gap: 0.35rem;
padding: 0.55rem 0.9rem;
border: 1px dashed #cfd9d1;
background: white;
color: #2b6a3d;
border-radius: 8px;
cursor: pointer;
font-size: 0.9rem;
font-family: inherit;
}
.add:hover {
background: #f4f8f5;
}
</style>
```
- [ ] **Step 2: Wire up `RecipeEditor.svelte`**
Add import:
```ts
import StepList from '$lib/components/StepList.svelte';
```
Replace the entire Zubereitung `<section class="block">` template block (starting `<section class="block">` with `<h2>Zubereitung</h2>` through the add-step button):
```svelte
<section class="block">
<h2>Zubereitung</h2>
<StepList {steps} onadd={addStep} onremove={removeStep} />
</section>
```
**CSS audit — what stays and what goes in the parent:**
Parent's template after Tasks 13 still contains:
- `<section class="block"><h2>Bild</h2><ImageUploadBox .../></section>` — no `.block` inner styles needed beyond what's in parent.
- `<div class="meta">` — still here. Keep `.meta`, `.field`, `.row`, `.small`, `.lbl`.
- `<section class="block"><h2>Zutaten</h2><ul class="ing-list">{#each ..}<IngredientRow/>{/each}</ul><button class="add">...</button></section>` — still uses `.ing-list` and `.add`.
- `<section class="block"><h2>Zubereitung</h2><StepList/></section>` — no inner CSS.
- `<div class="foot"><button class="btn ghost">...</button><button class="btn primary">...</button></div>` — keeps `.foot`, `.btn`, `.btn.ghost`, `.btn.primary`, `.btn:disabled`.
So parent CSS after Task 3 keeps: `.editor`, `.meta`, `.field`, `.lbl`, `.row`, `.small`, `.block`, `.block h2`, `.ing-list` (the `<ul>` wrapper), `.add` (for "Zutat hinzufügen"), `.foot`, `.btn` and variants.
Drop from parent CSS in Task 3: `.step-list`, `.step-row`, `.num`, `.step-row textarea`, `.del`.
- [ ] **Step 3: Run checks**
```bash
npm run check
npm test
```
- [ ] **Step 4: Manual smoke**
Open any recipe → edit → add a step, type, remove, save. Verify steps persist with correct ordering.
- [ ] **Step 5: Commit**
```bash
git add src/lib/components/StepList.svelte src/lib/components/RecipeEditor.svelte
git commit -m "$(cat <<'EOF'
refactor(editor): StepList als eigenstaendige Component
Zubereitungs-Liste mit Add + Remove als Sub-Component. Parent steuert
nur noch den Wrapper und reicht steps + die zwei Callbacks rein.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
EOF
)"
```
---
## Task 4: Extract `TimeDisplay` (RecipeView)
**Files:**
- Create: `src/lib/components/TimeDisplay.svelte`
- Modify: `src/lib/components/RecipeView.svelte`
- [ ] **Step 1: TimeDisplay component**
```svelte
<!-- src/lib/components/TimeDisplay.svelte -->
<script lang="ts">
type Props = {
prepTimeMin: number | null;
cookTimeMin: number | null;
totalTimeMin: number | null;
};
let { prepTimeMin, cookTimeMin, totalTimeMin }: Props = $props();
const summary = $derived.by(() => {
const parts: string[] = [];
if (prepTimeMin) parts.push(`Vorb. ${prepTimeMin} min`);
if (cookTimeMin) parts.push(`Kochen ${cookTimeMin} min`);
if (!prepTimeMin && !cookTimeMin && totalTimeMin)
parts.push(`Gesamt ${totalTimeMin} min`);
return parts.join(' · ');
});
</script>
{#if summary}
<p class="times">{summary}</p>
{/if}
<style>
.times {
margin: 0 0 0.25rem;
color: #666;
font-size: 0.9rem;
}
</style>
```
- [ ] **Step 2: Wire up `RecipeView.svelte`**
Add import:
```ts
import TimeDisplay from '$lib/components/TimeDisplay.svelte';
```
Remove the local `timeSummary()` function (lines 4552).
Replace the `{#if timeSummary()}<p class="times">...</p>{/if}` block in the template with:
```svelte
<TimeDisplay
prepTimeMin={recipe.prep_time_min}
cookTimeMin={recipe.cook_time_min}
totalTimeMin={recipe.total_time_min}
/>
```
Remove the `.times` CSS from RecipeView (it's in the sub-component now).
- [ ] **Step 3: Run checks**
```bash
npm run check
npm test
```
- [ ] **Step 4: Manual smoke**
Open any recipe → verify the time line still shows the same content (Vorb. / Kochen / Gesamt).
- [ ] **Step 5: Commit**
```bash
git add src/lib/components/TimeDisplay.svelte src/lib/components/RecipeView.svelte
git commit -m "$(cat <<'EOF'
refactor(view): TimeDisplay als eigenstaendige Component
timeSummary-Formatierung in eine wiederverwendbare Component
gezogen. RecipeView liefert nur noch die drei Werte — zukuenftige
Call-Sites (Preview, Hover-Cards) koennen dieselbe Logik reusen.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
EOF
)"
```
---
## Task 5: Self-review + push
- [ ] **Step 1: Line-count audit**
```bash
wc -l src/lib/components/RecipeEditor.svelte src/lib/components/RecipeView.svelte src/lib/components/ImageUploadBox.svelte src/lib/components/IngredientRow.svelte src/lib/components/StepList.svelte src/lib/components/TimeDisplay.svelte
```
Expected shape (approximate, ±10%):
- `RecipeEditor.svelte`: 628 → ~330370
- `RecipeView.svelte`: 398 → ~380
- `ImageUploadBox.svelte`: ~160
- `IngredientRow.svelte`: ~110
- `StepList.svelte`: ~100
- `TimeDisplay.svelte`: ~30
- [ ] **Step 2: Full test + typecheck**
```bash
npm test
npm run check
```
Both green.
- [ ] **Step 3: Git log review**
```bash
git log --oneline main..HEAD
```
Expected 4 commits:
1. `refactor(editor): ImageUploadBox als eigenstaendige Component`
2. `refactor(editor): IngredientRow + shared types`
3. `refactor(editor): StepList als eigenstaendige Component`
4. `refactor(view): TimeDisplay als eigenstaendige Component`
- [ ] **Step 4: Remote E2E after push**
```bash
git push -u origin editor-split
```
CI builds branch-tagged image. After deploy to `kochwas-dev.siegeln.net`:
```bash
npm run test:e2e:remote
```
Expected: 40/42 green (same as Search-State-Store baseline). `recipe-detail.spec.ts` (6 tests) specifically exercises the View side — must be clean.
Manual UAT pass on `https://kochwas-dev.siegeln.net/`:
- Edit a recipe → upload + remove image.
- Add / reorder / remove an ingredient → save → verify persistence on reload.
- Add / remove a step → save → verify.
- Check time-summary rendering on any recipe with prep/cook/total times set.
- [ ] **Step 5: Merge to main**
Once UAT is clean:
```bash
git checkout main
git merge --no-ff editor-split
git push origin main
```
---
## Risk Notes
- **Prop-reference mutability.** `IngredientRow` and `StepList` receive `ing` / `steps` by reference and use `bind:value` on their own `<input>` / `<textarea>` elements. Svelte 5 handles this correctly — writes propagate to the parent's `$state` array. Verified pattern with existing `searchFilterStore` usage and similar bind-through-prop in older Svelte 5 components in this codebase.
- **Confirm-dialog scope.** `ImageUploadBox` imports `confirmAction` directly rather than using a prop-callback. Consistent with the rest of the codebase (`confirmAction` is a global).
- **Scoped CSS duplication.** `.del` and `.add` button styles exist in multiple sub-components. Accepted — the alternative (global button classes) is out of scope for this phase.
- **No component unit tests.** Risk: a structural mistake (bad prop passing, missing callback wiring) wouldn't be caught by logic-layer tests. Mitigation: manual smoke test + `npm run check` type-safety + existing e2e coverage on RecipeView side.
## Deferred — NOT in this plan
- **Component unit tests with `@testing-library/svelte`:** Would add Vitest+browser setup. Worth doing in a separate phase once the project acquires a second component-refactor candidate.
- **Edit-flow E2E spec:** `tests/e2e/remote/recipe-edit.spec.ts` would cover the editor end-to-end. Valuable, but out of scope here — this phase is structural extraction, not test coverage expansion.
- **Extract `RecipeHero` / `ServingsStepper` / `TabSwitcher` from RecipeView:** Not on the roadmap. Add to a future phase if RecipeView grows further.

View File

@@ -0,0 +1,634 @@
# Ingredient Sections Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Zutaten können im Editor in benannte Sektionen (z. B. „Für den Teig", „Für die Füllung") gruppiert werden; in der View werden die Sektionen als Überschriften über den zugehörigen Zutatenblöcken gerendert.
**Architecture:** Eine neue nullable Spalte `section_heading` auf `ingredient`. Ist sie gesetzt, startet an dieser Zeile eine neue Sektion — alle folgenden Zutaten gehören dazu bis zur nächsten Zeile mit gesetzter `section_heading`. Ordnung bleibt `position`. Keine neue Tabelle, keine zweite Ordnungsachse, Scaler/FTS/Importer bleiben unverändert im Verhalten (nur Type-Passthrough). Inline-Button „Abschnitt hinzufügen" erscheint im Editor vor jeder Zutatenzeile und am Listenende.
**Tech Stack:** better-sqlite3 Migration, TypeScript-strict, Svelte 5 runes, vitest.
**Scope-Entscheidungen (vom User bestätigt):**
- Sektionen **nur für Zutaten**, nicht für Zubereitungsschritte.
- „Abschnitt hinzufügen"-Button inline vor jeder Zeile (plus einer am Listenende).
- Keine Import-Extraction — JSON-LD hat keine Sektionen, Emmikochteinfach rendert sie nur im HTML. Später via HTML-Parse möglich, aber out-of-scope.
---
### Task 1: Migration + Type-Erweiterung + parseIngredient-Sites
**Files:**
- Create: `src/lib/server/db/migrations/012_ingredient_section.sql`
- Modify: `src/lib/types.ts` (Ingredient type)
- Modify: `src/lib/server/parsers/ingredient.ts` (3 return sites)
- Test: `tests/unit/ingredient.test.ts` (bereits existierend, muss grün bleiben)
**Warum zusammen:** Nach der Type-Änderung schlägt `svelte-check` überall fehl, wo ein `Ingredient`-Literal gebaut wird. `parseIngredient` hat 3 solcher Stellen und ist vom selben Commit abhängig, sonst wird der Build rot.
- [ ] **Step 1: Migration schreiben**
Create `src/lib/server/db/migrations/012_ingredient_section.sql`:
```sql
-- Nullable — alte Zeilen behalten NULL, neue dürfen eine Überschrift haben.
-- Rendering-Regel: Ist section_heading gesetzt (nicht NULL und nicht leer),
-- startet an dieser Zeile eine neue Sektion mit diesem Titel.
ALTER TABLE ingredient ADD COLUMN section_heading TEXT;
```
- [ ] **Step 2: Ingredient-Type erweitern**
Modify `src/lib/types.ts`:
```ts
export type Ingredient = {
position: number;
quantity: number | null;
unit: string | null;
name: string;
note: string | null;
raw_text: string;
section_heading: string | null;
};
```
- [ ] **Step 3: parseIngredient-Return-Sites aktualisieren**
Modify `src/lib/server/parsers/ingredient.ts`:
Alle drei `return { position, ... raw_text: rawText };`-Literale (Zeilen 108, 115, 119) bekommen `section_heading: null` am Ende. Beispiel für Zeile 108:
```ts
return { position, quantity, unit, name, note, raw_text: rawText, section_heading: null };
```
Analog für Zeilen 115 und 119.
- [ ] **Step 4: Bestehende Unit-Tests grün**
Run: `npm run test -- ingredient.test.ts`
Expected: PASS (Tests prüfen nur vorhandene Felder, neues Feld stört nicht).
- [ ] **Step 5: Svelte-Check muss noch rot sein**
Run: `npm run check`
Expected: FAIL mit Fehlern in `repository.ts` (Select-Statement ohne `section_heading`). Das ist erwartet — wird in Task 2 behoben. Nicht hier fixen.
- [ ] **Step 6: Commit**
```bash
git add src/lib/types.ts src/lib/server/db/migrations/012_ingredient_section.sql src/lib/server/parsers/ingredient.ts
git commit -m "feat(schema): ingredient.section_heading (Migration 012 + Type)"
```
---
### Task 2: Repository-Layer Persistenz
**Files:**
- Modify: `src/lib/server/recipes/repository.ts` (insertRecipe, replaceIngredients, getRecipeById)
- Test: `tests/integration/recipe-repository.test.ts`
**Warum jetzt:** Nach Task 1 ist der Type-Vertrag aufgemacht. Die DB muss das Feld lesen und schreiben, sonst gehen Sektionen beim Save/Load verloren.
- [ ] **Step 1: Failing test für Roundtrip**
Add to `tests/integration/recipe-repository.test.ts` inside `describe('recipe repository', ...)`:
```ts
it('persistiert section_heading und gibt es beim Laden zurück', () => {
const db = openInMemoryForTest();
const recipe = baseRecipe({
title: 'Torte',
ingredients: [
{ position: 1, quantity: 200, unit: 'g', name: 'Mehl', note: null, raw_text: '200 g Mehl', section_heading: 'Für den Teig' },
{ position: 2, quantity: 100, unit: 'g', name: 'Zucker', note: null, raw_text: '100 g Zucker', section_heading: null },
{ position: 3, quantity: 300, unit: 'g', name: 'Beeren', note: null, raw_text: '300 g Beeren', section_heading: 'Für die Füllung' }
]
});
const id = insertRecipe(db, recipe);
const loaded = getRecipeById(db, id);
expect(loaded!.ingredients[0].section_heading).toBe('Für den Teig');
expect(loaded!.ingredients[1].section_heading).toBeNull();
expect(loaded!.ingredients[2].section_heading).toBe('Für die Füllung');
});
it('replaceIngredients persistiert section_heading', () => {
const db = openInMemoryForTest();
const id = insertRecipe(db, baseRecipe({ title: 'X' }));
replaceIngredients(db, id, [
{ position: 1, quantity: null, unit: null, name: 'A', note: null, raw_text: 'A', section_heading: 'Kopf' }
]);
const loaded = getRecipeById(db, id);
expect(loaded!.ingredients[0].section_heading).toBe('Kopf');
});
```
- [ ] **Step 2: Test laufen — muss fehlschlagen**
Run: `npm run test -- recipe-repository.test.ts`
Expected: FAIL — `section_heading` kommt als `undefined` zurück, weil SQL-SELECT es nicht holt.
- [ ] **Step 3: INSERT-Statements erweitern**
Modify `src/lib/server/recipes/repository.ts`:
In `insertRecipe` (line ~66): Spalte + Parameter anhängen.
```ts
const insIng = db.prepare(
`INSERT INTO ingredient(recipe_id, position, quantity, unit, name, note, raw_text, section_heading)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
);
for (const ing of recipe.ingredients) {
insIng.run(id, ing.position, ing.quantity, ing.unit, ing.name, ing.note, ing.raw_text, ing.section_heading);
}
```
In `replaceIngredients` (line ~217): gleiche Änderung.
```ts
const ins = db.prepare(
`INSERT INTO ingredient(recipe_id, position, quantity, unit, name, note, raw_text, section_heading)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
);
for (const ing of ingredients) {
ins.run(recipeId, ing.position, ing.quantity, ing.unit, ing.name, ing.note, ing.raw_text, ing.section_heading);
}
```
- [ ] **Step 4: SELECT-Statement erweitern**
In `getRecipeById` (line ~105):
```ts
const ingredients = db
.prepare(
`SELECT position, quantity, unit, name, note, raw_text, section_heading
FROM ingredient WHERE recipe_id = ? ORDER BY position`
)
.all(id) as Ingredient[];
```
- [ ] **Step 5: Tests grün**
Run: `npm run test -- recipe-repository.test.ts`
Expected: PASS.
- [ ] **Step 6: Volle Suite + svelte-check**
Run: `npm test && npm run check`
Expected: Beides PASS. `svelte-check` ist jetzt auf Repo-Ebene typ-clean; View/Editor noch nicht berührt, deren Nutzung von `Ingredient` bleibt (Feld darf fehlen, weil der Type optional wirkt? — Nein, es ist `string | null`, also **pflicht**. Falls `check` rot wird, liegt es an Importer/Scaler-Aufrufern, die `Ingredient`-Literale bauen. Das ist dann Task 3.)
- [ ] **Step 7: Commit**
```bash
git add src/lib/server/recipes/repository.ts tests/integration/recipe-repository.test.ts
git commit -m "feat(db): section_heading roundtrip in recipe-repository"
```
---
### Task 3: Importer-Passthrough + Scaler-Test
**Files:**
- Modify: `src/lib/recipes/scaler.ts` (nur falls Test rot — siehe unten)
- Test: `tests/unit/scaler.test.ts`
- Test: evtl. `tests/integration/importer.test.ts`
**Warum:** parseIngredient setzt `section_heading: null` (Task 1). Das reicht für den Importer — keine JSON-LD-Extraction. Aber der Scaler ruft `.map((i) => ({ ...i, quantity: ... }))` auf; das Spread erhält `section_heading` automatisch. Wir fügen nur einen Regressions-Test hinzu, dass das stimmt.
- [ ] **Step 1: Scaler-Regressions-Test**
Add to `tests/unit/scaler.test.ts`:
```ts
it('preserves section_heading through scaling', () => {
const input: Ingredient[] = [
{ position: 1, quantity: 200, unit: 'g', name: 'Mehl', note: null, raw_text: '200 g Mehl', section_heading: 'Teig' },
{ position: 2, quantity: null, unit: null, name: 'Ei', note: null, raw_text: 'Ei', section_heading: null }
];
const scaled = scaleIngredients(input, 2);
expect(scaled[0].section_heading).toBe('Teig');
expect(scaled[1].section_heading).toBeNull();
expect(scaled[0].quantity).toBe(400);
});
```
- [ ] **Step 2: Test laufen**
Run: `npm run test -- scaler.test.ts`
Expected: PASS (weil `...i` das Feld durchreicht).
Falls FAIL: In `src/lib/recipes/scaler.ts` das `.map` prüfen — es sollte `...i` spreaden und nur `quantity` überschreiben. Bei Abweichung angleichen.
- [ ] **Step 3: Importer-Roundtrip-Test (Bolognese-Fixture)**
Prüfen, dass Importer für Emmi-Fixture `section_heading: null` auf allen Zutaten liefert. Der existierende `importer.test.ts` sollte automatisch grün bleiben (parseIngredient setzt das Feld auf null), aber wir schauen kurz nach:
Run: `npm run test -- importer.test.ts`
Expected: PASS.
- [ ] **Step 4: Commit**
```bash
git add tests/unit/scaler.test.ts
git commit -m "test(scaler): section_heading ueberlebt Skalierung"
```
---
### Task 4: IngredientRow — Heading-Anzeige + Inline Insert-Button
**Files:**
- Modify: `src/lib/components/recipe-editor-types.ts`
- Modify: `src/lib/components/IngredientRow.svelte`
- Test: neue Svelte-Component-Tests via vitest-browser — **ausgenommen**: wir haben keine Svelte-Component-Unit-Tests im Repo. Stattdessen decken E2E + manuelle Verifikation ab. Das ist konsistent mit der bestehenden Praxis.
**Verhalten:**
- `DraftIng` bekommt `section_heading: string | null` (immer gesetzt, aber nullable).
- Hat eine Zeile `section_heading` als String (auch leer), wird oberhalb der Row ein `<input>` für den Titel gerendert plus ein kleiner „Sektion entfernen"-Button.
- Hat eine Zeile `section_heading === null`, wird ein dezenter `<button class="add-section">Abschnitt hinzufügen</button>` **über** der Row gerendert.
- IngredientRow bekommt Callbacks `onaddSection`, `onremoveSection` — Parent verwaltet das Array.
- [ ] **Step 1: DraftIng-Typ erweitern**
Modify `src/lib/components/recipe-editor-types.ts`:
```ts
export type DraftIng = {
qty: string;
unit: string;
name: string;
note: string;
section_heading: string | null;
};
export type DraftStep = { text: string };
```
- [ ] **Step 2: IngredientRow erweitern — Props**
Modify `src/lib/components/IngredientRow.svelte` Script-Block:
```svelte
<script lang="ts">
import { Trash2, ChevronUp, ChevronDown, Plus, X } from 'lucide-svelte';
import type { DraftIng } from './recipe-editor-types';
type Props = {
ing: DraftIng;
idx: number;
total: number;
onmove: (dir: -1 | 1) => void;
onremove: () => void;
onaddSection: () => void;
onremoveSection: () => void;
};
let { ing, idx, total, onmove, onremove, onaddSection, onremoveSection }: Props = $props();
</script>
```
- [ ] **Step 3: IngredientRow-Template — Section-Block + Add-Button**
Replace the existing `<li class="ing-row">…</li>` with:
```svelte
{#if ing.section_heading === null}
<li class="section-insert">
<button type="button" class="add-section" onclick={onaddSection}>
<Plus size={12} strokeWidth={2.5} />
<span>Abschnitt hinzufügen</span>
</button>
</li>
{:else}
<li class="section-heading-row">
<input
class="section-heading"
type="text"
bind:value={ing.section_heading}
placeholder="Sektion, z. B. Für den Teig""
aria-label="Sektionsüberschrift"
/>
<button
type="button"
class="section-remove"
aria-label="Sektion entfernen"
onclick={onremoveSection}
>
<X size={14} strokeWidth={2.5} />
</button>
</li>
{/if}
<li class="ing-row">
<div class="move">
<!-- unchanged -->
<button class="move-btn" type="button" aria-label="Zutat nach oben" disabled={idx === 0} onclick={() => onmove(-1)}>
<ChevronUp size={14} strokeWidth={2.5} />
</button>
<button class="move-btn" type="button" aria-label="Zutat nach unten" disabled={idx === total - 1} onclick={() => onmove(1)}>
<ChevronDown size={14} strokeWidth={2.5} />
</button>
</div>
<input class="qty" type="text" bind:value={ing.qty} placeholder="Menge" aria-label="Menge" />
<input class="unit" type="text" bind:value={ing.unit} placeholder="Einheit" aria-label="Einheit" />
<input class="name" type="text" bind:value={ing.name} placeholder="Zutat" aria-label="Zutat" />
<input class="note" type="text" bind:value={ing.note} placeholder="Notiz" aria-label="Notiz" />
<button class="del" type="button" aria-label="Zutat entfernen" onclick={onremove}>
<Trash2 size={16} strokeWidth={2} />
</button>
</li>
```
**Hinweis:** Wir rendern pro Row zwei `<li>`: optional einen Sektions-Block (Insert-Button ODER Heading-Input), plus die bestehende Zutaten-Row. Das passt in die `<ul class="ing-list">` des Parents — semantisch unsauber (nicht-Zutat-`<li>` in Zutatenliste), aber praktikabel; alternativ könnte IngredientRow auf `<div>` umgestellt werden, das wäre aber ein Parent-Umbau. Wir bleiben bei `<li>` und geben dem Section-`<li>` `list-style: none` via CSS.
- [ ] **Step 4: Styles für Section-UI**
Add to `<style>`-Block in `IngredientRow.svelte`:
```css
.section-insert {
display: flex;
justify-content: center;
list-style: none;
margin: -0.2rem 0 0.1rem;
opacity: 0;
transition: opacity 0.15s;
}
.ing-list:hover .section-insert,
.section-insert:focus-within {
opacity: 1;
}
.add-section {
display: inline-flex;
align-items: center;
gap: 0.25rem;
padding: 0.2rem 0.55rem;
border: 1px dashed #cfd9d1;
background: white;
color: #2b6a3d;
border-radius: 999px;
cursor: pointer;
font-size: 0.75rem;
font-family: inherit;
}
.add-section:hover {
background: #f4f8f5;
}
.section-heading-row {
display: grid;
grid-template-columns: 1fr 32px;
gap: 0.35rem;
list-style: none;
margin-top: 0.4rem;
}
.section-heading {
padding: 0.45rem 0.7rem;
border: 1px solid #cfd9d1;
border-radius: 8px;
font-size: 0.95rem;
font-weight: 600;
color: #2b6a3d;
font-family: inherit;
background: #f4f8f5;
}
.section-remove {
width: 32px;
height: 38px;
border: 1px solid #cfd9d1;
background: white;
border-radius: 8px;
color: #666;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
}
.section-remove:hover {
background: #fdf3f3;
border-color: #f1b4b4;
color: #c53030;
}
```
**Begründung `opacity: 0` + Hover:** Der Insert-Button erscheint vor **jeder** Zeile — das ist visuelles Rauschen auf statischem Zustand. Fade-in-on-hover hält die Zutatenliste lesbar und macht den Button auf Mouse-Interaktion trotzdem sichtbar. Auf Touch-Geräten ist `:hover` ggf. sticky — das ist OK, weil auf Mobile die Zutatenliste ohnehin explorativ bedient wird. `:focus-within` deckt Keyboard-Navigation ab.
- [ ] **Step 5: svelte-check**
Run: `npm run check`
Expected: FAIL — `RecipeEditor.svelte` gibt die neuen Callbacks `onaddSection` / `onremoveSection` noch nicht rein, und `DraftIng`-Literale im Editor haben noch kein `section_heading`. Wird in Task 5 behoben.
- [ ] **Step 6: Commit**
```bash
git add src/lib/components/IngredientRow.svelte src/lib/components/recipe-editor-types.ts
git commit -m "feat(editor): Sektionsueberschriften in IngredientRow + Insert-Button"
```
---
### Task 5: RecipeEditor — State, Handler, Save-Patch
**Files:**
- Modify: `src/lib/components/RecipeEditor.svelte`
- [ ] **Step 1: DraftIng-Seeding erweitern**
In `RecipeEditor.svelte` Script-Block, `ingredients`-State (line ~40):
```ts
let ingredients = $state<DraftIng[]>(
untrack(() =>
recipe.ingredients.map((i) => ({
qty: i.quantity !== null ? String(i.quantity).replace('.', ',') : '',
unit: i.unit ?? '',
name: i.name,
note: i.note ?? '',
section_heading: i.section_heading
}))
)
);
```
- [ ] **Step 2: addIngredient aktualisieren**
```ts
function addIngredient() {
ingredients = [...ingredients, { qty: '', unit: '', name: '', note: '', section_heading: null }];
}
```
- [ ] **Step 3: Section-Handler einfügen**
```ts
function addSection(idx: number) {
const next = [...ingredients];
next[idx] = { ...next[idx], section_heading: '' };
ingredients = next;
}
function removeSection(idx: number) {
const next = [...ingredients];
next[idx] = { ...next[idx], section_heading: null };
ingredients = next;
}
```
- [ ] **Step 4: save()-Patch erweitern**
In `save()` (line ~86), das `cleanedIngredients`-Mapping:
```ts
const cleanedIngredients: Ingredient[] = ingredients
.filter((i) => i.name.trim())
.map((i, idx) => {
const qty = parseQty(i.qty);
const unit = i.unit.trim() || null;
const name = i.name.trim();
const note = i.note.trim() || null;
const rawParts: string[] = [];
if (qty !== null) rawParts.push(String(qty).replace('.', ','));
if (unit) rawParts.push(unit);
rawParts.push(name);
const heading = i.section_heading === null ? null : (i.section_heading.trim() || null);
return {
position: idx + 1,
quantity: qty,
unit,
name,
note,
raw_text: rawParts.join(' '),
section_heading: heading
};
});
```
**Regel:** Eine leere Sektion (`section_heading === ''` nach Trim) wird beim Speichern zu `null`. Begründung: User tippt „Abschnitt hinzufügen" und lässt das Feld leer → keine unbenannte Sektion in der View. Nur Zeilen mit echtem Titel werden als Sektionsanker persistiert.
- [ ] **Step 5: IngredientRow-Callbacks verdrahten**
In `RecipeEditor.svelte` Template (line ~170):
```svelte
{#each ingredients as ing, idx (idx)}
<IngredientRow
{ing}
{idx}
total={ingredients.length}
onmove={(dir) => moveIngredient(idx, dir)}
onremove={() => removeIngredient(idx)}
onaddSection={() => addSection(idx)}
onremoveSection={() => removeSection(idx)}
/>
{/each}
```
- [ ] **Step 6: svelte-check + Tests**
Run: `npm run check && npm test`
Expected: Beides grün.
- [ ] **Step 7: Commit**
```bash
git add src/lib/components/RecipeEditor.svelte
git commit -m "feat(editor): Sektionen-Handler + save-Patch mit section_heading"
```
---
### Task 6: RecipeView — Sektions-Überschriften rendern
**Files:**
- Modify: `src/lib/components/RecipeView.svelte`
- [ ] **Step 1: Zutatenliste umbauen**
In `RecipeView.svelte` (line ~128), den `<ul class="ing-list">`-Block:
```svelte
<ul class="ing-list">
{#each scaled as ing, i (i)}
{#if ing.section_heading && ing.section_heading.trim()}
<li class="section-heading">{ing.section_heading}</li>
{/if}
<li>
{#if ing.quantity !== null || ing.unit}
<span class="qty">
{formatQty(ing.quantity)}
{#if ing.unit}
{' '}{ing.unit}
{/if}
</span>
{/if}
<span class="name">
{ing.name}
{#if ing.note}<span class="note"> ({ing.note})</span>{/if}
</span>
</li>
{/each}
</ul>
```
**Hinweis:** `<li class="section-heading">` statt `<h3>` — wir sind in einer `<ul>` und dürfen dort nur `<li>` direkt verschachteln. Semantisch ist das OK, Screenreader lesen die Heading-Klasse nicht als Landmark, aber sie liest den Text als normales Listen-Item; für ein Rezept ist das akzeptabel. Alternativ: `<ul>` in mehrere `<section>`s aufsplitten — deutlich komplexer bei gleicher visueller Wirkung; verschoben, bis jemand klagt.
- [ ] **Step 2: Style für .section-heading**
Add to `<style>`-Block in `RecipeView.svelte`:
```css
.ing-list .section-heading {
list-style: none;
font-weight: 600;
color: #2b6a3d;
font-size: 1rem;
margin-top: 0.9rem;
margin-bottom: 0.2rem;
padding: 0.15rem 0;
border-bottom: 1px solid #e4eae7;
}
.ing-list .section-heading:first-child {
margin-top: 0;
}
```
- [ ] **Step 3: Tests + Check**
Run: `npm run check && npm test`
Expected: Beides grün.
- [ ] **Step 4: Dev-Build-Smoke-Test**
Run: `npm run build && npm run preview`
Manuell: Rezept öffnen, editieren, Sektion „Teig" auf Zeile 1 setzen und „Füllung" auf Zeile 3, speichern. Wechsel zu View → beide Überschriften sichtbar, Skalierung ändert nur Mengen. Screenshot ist nice-to-have, nicht Pflicht.
- [ ] **Step 5: Commit**
```bash
git add src/lib/components/RecipeView.svelte
git commit -m "feat(view): Zutaten-Sektionen als Ueberschriften rendern"
```
---
### Task 7: Ship
- [ ] **Step 1: Finale Testsuite**
Run: `npm run check && npm test`
Expected: Beides grün.
- [ ] **Step 2: Push**
```bash
git push -u origin feature/ingredient-sections
```
- [ ] **Step 3: Auf Deploy warten (CI-Image-Build, Pi-Pull)**
User wird manuell signalisieren, wenn deployed.
- [ ] **Step 4: Nach Deploy — Playwright Remote-Smoke**
Run: `npm run test:e2e:remote`
Expected: 42/42 green (unchanged suite, wir haben keine Recipe-Edit-E2E-Tests hinzugefügt).
- [ ] **Step 5: Merge zu main**
Falls E2E grün:
```bash
git checkout main
git merge --no-ff feature/ingredient-sections -m "Merge ingredient-sections — Zutaten-Gruppierung via section_heading"
git push
```
---
## Self-Review-Notiz
- Spec-Coverage: alle drei User-Anforderungen abgedeckt (Inline-Button vor jeder Zeile → Task 4, nur Zutaten → keine Step-Änderungen, Edit-Mode-only → Importer unverändert).
- Type-Konsistenz: `section_heading: string | null` überall einheitlich (Ingredient, DraftIng, Save-Patch).
- Keine Placeholder — alle SQL-/Code-Snippets ausgeschrieben.
- Migrations-Reihenfolge: `012_` nach `011_clear_favicon_for_rerun.sql`.
- FTS-Impact: `section_heading` taucht nicht im FTS-Trigger auf (`001_init.sql` nutzt `name`, `description`, `ingredients_concat`, `tags_concat`). Das ist bewusst so — Sektionstitel sind Organisationshilfen, kein Suchinhalt. User suchen nach „Mehl", nicht nach „Für den Teig".

View File

@@ -0,0 +1,217 @@
# Post-Review Roadmap 2026-04-19
> **Quelle:** `docs/superpowers/review/OPEN-ISSUES-NEXT.md` (Items AI) + UAT `kochwas-dev.siegeln.net` (Branch `review-fixes-2026-04-18`, 2026-04-19).
> **Branch-Status:** Merge-ready — 8 atomare Commits, 184/184 Tests grün, svelte-check 0 Errors, UAT durchgeklickt (Profil, Suche, Rezept-Actions, Wunschliste, Preview, Admin, API-Shapes).
> **Goal:** Die nach dem Review-Branch offenen 9 Items in priorisierte Phasen übersetzen, damit jede einzeln via `/gsd-plan-phase` → `/gsd-execute-phase` abgearbeitet werden kann.
> **Architecture:** Keine Groß-Refactor-Phase, sondern getaktete Einzel-Phasen mit klarem Gate. Reihenfolge folgt Risiko × Wert: erst kleine Wins, dann eine strukturelle Phase (A), dann opportunistische.
> **Tech-Stack:** SvelteKit, TypeScript-strict, Zod, Vitest, Playwright-UAT, better-sqlite3, Service-Worker.
---
## Merge-Entscheidung
**Jetzt mergen.** Der Branch-UAT auf `kochwas-dev` war clean (siehe Session-Log 2026-04-19). Findings aus dem UAT:
- Kommentar-Delete hat keinen UI-Button (MINOR, kein Branch-Regress — Zustand schon vor Refactor so).
- `/preview` ohne `?url=` bleibt im Dauer-Lader (MINOR, harmlos — niemand ruft die Route blank auf).
Beide werden als LOW-Items unten aufgenommen, sind aber **kein Merge-Blocker**.
---
## Tier-Zuordnung
| Tier | Items | Wann | Aufwand total |
|------|-------|------|---------------|
| 1 — Schneller Cleanup-Batch | F, G, H, I | Direkt nach Merge | ~2 h |
| 2 — Phase Search-State-Store | A | Nächster größerer Slot | halber Tag |
| 3 — Phase SearXNG-Recovery | C | Wenn Rate-Limit-Schmerz konkret auftaucht | 12 h |
| 4 — Opportunistisch | B, D, E, + Kommentar-Delete, Preview-Guard | Trigger-basiert | reaktiv |
| 5 — Geparkt | yauzl / Phase 5b | Nur bei explizitem Bedarf | nicht geplant |
---
## Tier 1 — Cleanup-Batch (1 Phase, 4 Items)
**Phasenname-Vorschlag:** `Phase Cleanup-Batch nach Review-Fixes` (via `/gsd-new-phase` oder `/gsd-add-phase`).
Alle vier Items touchen wenige Zeilen, sind LOW/MEDIUM, und lassen sich in 12 Commits pro Item sauber atomar committen. **Gebündelt statt einzeln**, weil Kontext-Overhead pro Einzelphase größer wäre als der Fix.
### Item I — RecipeEditor auf `$derived` umstellen
**Files:** `src/lib/components/RecipeEditor.svelte:28,97102,113,121`, `src/routes/recipes/[id]/+page.svelte:43`
Pattern aktuell: `let foo = recipe.bar` → Svelte-5-Warning, Snapshot-only, bricht bei In-Place-Mutation des Rezepts.
**Plan pro Warnung:**
- [ ] Warning-Site auslesen, beurteilen: soll `foo` Mutations am `recipe` tracken oder bewusst ein Snapshot bleiben?
- [ ] Track-Fall: `let foo = $derived(recipe.bar)`.
- [ ] Snapshot-Fall: Variable umbenennen (z. B. `initialFoo`) und als `$state` deklarieren mit Kommentar `// intentional snapshot`.
- [ ] `npm run check` — 0 Warnings erwartet.
- [ ] `npm test` — grün.
- [ ] Commit: `refactor(editor): RecipeEditor auf $derived umstellen`.
**Gate:** svelte-check 0 Warnings, alle Editor-Flows (Titel, Zutaten, Schritte) per Hand getestet — In-Place-PATCH zeigt aktualisierten Wert.
### Item H — RecipeEditor Bild-Upload/Delete auf `asyncFetch`
**Files:** `src/lib/components/RecipeEditor.svelte:54,83`
**Warum zusammen mit I:** Gleiche Datei, gleicher Touch.
- [ ] Zeile 54 (Upload): `const res = await fetch(...); if (!res.ok) alertAction(...)``await asyncFetch(...)`.
- [ ] Zeile 83 (Delete): dito.
- [ ] Error-Messages beibehalten.
- [ ] Test manuell: Bild hochladen + löschen in einem Test-Rezept.
- [ ] Commit: `refactor(editor): Bild-Upload/Delete auf asyncFetch`.
**Gate:** Bild-Upload + Delete-Flow grün in manuellem Smoke; `npm run check` clean.
### Item F — Inline UI-Constants in `src/lib/theme.ts`
**Files:** Neu `src/lib/theme.ts`, Modify `ConfirmDialog.svelte`, `ProfileSwitcher.svelte`, weitere Call-Sites via `grep`.
- [ ] `grep -rn "z-index:\|border-radius: 999\|setTimeout.*[0-9]{3,4}" src/lib/components src/routes` — Call-Sites auflisten.
- [ ] `src/lib/theme.ts` anlegen mit: `MODAL_Z_INDEX = 1000`, `POPOVER_Z_INDEX = 900`, `PILL_RADIUS = '999px'` (nur Werte, die wirklich mehrfach vorkommen — YAGNI).
- [ ] Call-Sites durchgehen, Inline-Werte durch Import ersetzen.
- [ ] `npm run check` + `npm test`.
- [ ] Commit: `refactor(ui): shared theme constants fuer z-index/radius`.
**Gate:** Keine visuellen Änderungen beim Durchklicken (Confirm-Dialog, Profile-Switcher, Toast, Menü).
### Item G — `requireProfile()` mit optionaler Message
**Files:** `src/lib/client/confirm.svelte.ts` (oder wo `requireProfile` liegt), `src/routes/wishlist/+page.svelte:38`
**Option A — minimal invasiv:** `wishlist/+page.svelte` belassen, Custom-Message-Konstante in der Datei. Dann **nur dokumentieren** im Kommentar der `requireProfile`-Funktion, dass die Wunschliste bewusst eigenständig ist.
**Option B — DRY:** `requireProfile(message?: string): Profile | null` mit Fallback auf Default.
- [ ] **Entscheidung zuerst** — Option A sparsamer, Option B konsistent. Ich empfehle **A**, weil die Custom-Message in der Wunschliste wirklich Kontext ist („um mitzuwünschen"), nicht nur Deko. Aber: wenn B, dann sauber mit Unit-Test.
- [ ] Commit: `refactor(client): requireProfile Custom-Message entscheiden` (je nach Entscheidung).
**Gate:** Wunschliste zeigt beim Klick ohne Profil die korrekte Message; keine anderen Sites verhalten sich anders.
---
## Tier 2 — Phase Search-State-Store (Item A)
**Empfohlener Einstieg:** `/gsd-discuss-phase Search-State-Store` (per OPEN-ISSUES Empfehlung), nicht direkt `/gsd-plan-phase`.
**Warum eigene Phase:** Touch `+page.svelte` (808 L) + `+layout.svelte` (678 L), Reactive-Glue zwischen Header-Search-Dropdown und Home-Search muss 1:1 übernommen werden. **UAT-pflichtig**, weil es keine UI-Tests gibt.
**Scope-Sketch (für die Discuss-Phase):**
- Neu: `src/lib/client/search.svelte.ts` — reaktiver Store mit `query`, `hits`, `loading`, `error`, `hasMore`, `search(q)`, `loadMore()`, `clear()`.
- Debounce (aktuell in `+page.svelte`) in den Store migrieren.
- Web-Fallback-Logik (lokal leer → Web-Suche) beibehalten — Store muss beide Modi kennen (`mode: 'local' | 'web'`).
- `+layout.svelte` Header-Dropdown zuerst migrieren (kleineres Surface), dann `+page.svelte`.
- Duplizierten `$state`-Block entfernen.
**Verifikation pro Wave:**
1. Nach Store-Anlegen: Vitest-Unit-Tests für Store (mocked fetch).
2. Nach Layout-Migration: Browser-UAT Header-Dropdown auf Rezept-Seite + Startseite.
3. Nach Page-Migration: Browser-UAT Live-Suche (lokaler Treffer, Web-Fallback, Empty-State), inkl. Deep-Link `?q=xyz`.
4. Playwright-Script wiederholen (existiert aus 2026-04-19 UAT).
**Gate:** Alle 3 UAT-Pfade clean; `+page.svelte` unter 700 L; `+layout.svelte` unter 600 L; `npm test` + `npm run check` grün.
**Aufwand:** halber Tag (46 h).
---
## Tier 3 — Phase SearXNG-Rate-Limit-Recovery (Item C)
**Trigger:** Wenn konkreter Schmerz (User merkt „Suche liefert komische alte Sachen" oder SearXNG logt 429/403 gehäuft).
**Scope:**
- `src/lib/server/search/searxng.ts`: `lastFailureAt: Map<string, number>` pro Domain.
- Exponentieller Backoff: bei wiederholtem 429/403 → 1 min → 5 min → 30 min (Cap).
- Response-Shape erweitern: `isStale?: boolean` wenn aus Cache nach Fail.
- UI: `src/routes/+page.svelte` Such-Ergebnisheader zeigt „Ergebnisse evtl. veraltet" wenn `isStale`.
**Tests (TDD, Vitest):**
- Simulierter 429 → nächster Call innerhalb 1 min geht nicht raus, Response aus Cache mit `isStale: true`.
- Nach 1 min Wartezeit → Call geht wieder raus.
- Nach erfolgreichem Call → Backoff-Zähler resettet.
**Gate:** Tests grün; manuell: Fake-429 injizieren (z. B. über ENV-Toggle im Dev), UI zeigt Hinweis.
**Aufwand:** 12 h.
---
## Tier 4 — Opportunistisch (Trigger-gesteuert)
Alle Items hier werden **nicht proaktiv** geplant. Sie warten auf ihren Trigger.
### Item B — RecipeEditor/RecipeView in Sub-Components
**Trigger:** Zweite Person arbeitet am Projekt mit, ODER Editor-Bug-Hunt wird unübersichtlich.
**Scope-Sketch:** `IngredientRow.svelte`, `StepList.svelte`, `TimeDisplay.svelte`, `ImageUploadBox.svelte`.
**Vorbedingung:** Item I muss zuerst durch sein (die pre-existing Warnings würden sonst in die Sub-Components wandern).
### Item D — SW Zombie-Cleanup unter Drosselung
**Trigger:** Nächster Service-Worker-Touch (z. B. neue Cache-Strategy oder Chunks-Manifest-Änderung).
**Scope:** Mit DevTools-Throttling-Profil „Slow 3G" durchgehen, prüfen ob der 1500ms-Timeout in `pwa.svelte.ts` False-Positives triggert. Falls ja: Timeout konfigurierbar oder Heuristik verfeinern.
### Item E — JSON-LD Parser Locale-Edge-Cases
**Trigger:** Echter Import-Bug aus dem Alltag.
**Scope:** Gezielter Test für die Fail-URL + Fix. Kein Vorab-Sprint.
### Kommentar-Delete-UI (UAT 2026-04-19)
**Status:** Kommentar-DELETE-Endpoint existiert, aber keine UI-Exposition.
**Vorschlag:** In `src/routes/recipes/[id]/+page.svelte` Kommentar-Liste pro Eintrag ein 🗑-Button für den Autor (`comment.profile_id === profileStore.active?.id`). Mit `confirmAction`-Dialog.
**Trigger:** Erster Wunsch, einen Kommentar loszuwerden.
**Aufwand:** ~30 min.
### Preview-ohne-URL-Guard (UAT 2026-04-19)
**Status:** `/preview` ohne `?url=` bleibt im Dauer-Lader.
**Vorschlag:** `src/routes/preview/+page.svelte` Zeile 33ff.: wenn `u` leer, `errored = 'Kein URL-Parameter gesetzt'` oder Redirect auf `/`. **2-Zeilen-Fix.**
**Trigger:** Bevor jemand die Route bookmarked.
**Aufwand:** 5 min — könnte man auch sofort in Tier 1 reinnehmen, ist aber so trivial, dass es ohne Phase geht.
---
## Tier 5 — Geparkt
### Phase 5b — ZIP-Backup-Restore via `yauzl`
**Status:** Dokumentiert in `ARCHITECTURE.md:33` und `session-handoff-2026-04-17.md`. Dependency bleibt installiert.
**Kein Plan.** Wird erst aktiviert, wenn jemand wirklich ein Backup-ZIP zurückspielen will. Dann: `/gsd-plan-phase Phase-5b-ZIP-Restore`.
---
## Empfohlene Ausführungs-Reihenfolge
1. **Merge** `review-fixes-2026-04-18``main`.
2. **Neuen Branch** `cleanup-batch-post-review` → Tier 1 (Items I + H zusammen in einem Wave, dann F, dann G).
3. **Merge** → Tier 2 Discuss: `/gsd-discuss-phase Search-State-Store`.
4. Tier 2 execution.
5. Tier 3 erst wenn der Trigger da ist, sonst Tier 4 abwarten.
---
## Commit-Stil für alle Phasen
- Deutsch, kleinteilig, eine Idee pro Commit.
- Body erklärt das *Warum* (Reference auf Item-Nummer aus diesem Doc).
- Nach jedem Commit `npm test` + `npm run check` grün.
- Push direkt nach Commit (CI baut Branch-Tag, siehe `docker.yml`).

View File

@@ -0,0 +1,971 @@
# Search-State-Store Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use `superpowers:executing-plans` to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Extract the duplicated live-search state machine from `src/routes/+page.svelte` and `src/routes/+layout.svelte` into a single reusable `SearchStore` class in `src/lib/client/search.svelte.ts`, so both the home search and the header dropdown drive their UI from the same logic.
**Architecture:** Factory-class store (one instance per consumer, like `new SearchStore()` — not a shared singleton). Holds all `$state` fields currently inlined in the Svelte components (query, hits, webHits, searching flags, error, pagination state), plus imperative methods (`runDebounced`, `loadMore`, `reSearch`, `reset`, `captureSnapshot`, `restoreSnapshot`). Consumers keep UI-specific concerns (URL sync, dropdown open/close, snapshot hookup) in their component — the store owns only fetch/pagination/debounce.
**Tech Stack:** Svelte 5 runes (`$state` in class fields), TypeScript-strict, Vitest + jsdom, fetch injection for tests.
---
## Design Snapshot
**API surface (locked before implementation):**
```ts
// src/lib/client/search.svelte.ts
import type { SearchHit } from '$lib/server/recipes/search-local';
import type { WebHit } from '$lib/server/search/searxng';
export type SearchSnapshot = {
query: string;
hits: SearchHit[];
webHits: WebHit[];
searchedFor: string | null;
webError: string | null;
localExhausted: boolean;
webPageno: number;
webExhausted: boolean;
};
export type SearchStoreOptions = {
pageSize?: number; // default 30
debounceMs?: number; // default 300
filterDebounceMs?: number; // default 150 (shorter for filter-change re-search)
minQueryLength?: number; // default 4 (query.trim().length > 3)
filterParam?: () => string; // e.g. () => searchFilterStore.queryParam → "foo,bar" or ""
fetchImpl?: typeof fetch; // injected for tests
};
export class SearchStore {
query = $state('');
hits = $state<SearchHit[]>([]);
webHits = $state<WebHit[]>([]);
searching = $state(false);
webSearching = $state(false);
webError = $state<string | null>(null);
searchedFor = $state<string | null>(null);
localExhausted = $state(false);
webPageno = $state(0);
webExhausted = $state(false);
loadingMore = $state(false);
constructor(opts?: SearchStoreOptions);
/** Call from `$effect(() => { store.query; store.runDebounced(); })`. Handles debounce + race-guard. */
runDebounced(): void;
/** Immediate (no debounce). Used by form `submit`. */
runSearch(q: string): Promise<void>;
/** Filter-change re-search — shorter debounce. */
reSearch(): void;
/** Paginate locally, then fall back to web. Idempotent while in-flight. */
loadMore(): Promise<void>;
/** Clear query + results + cancel any pending debounce (e.g. `afterNavigate`). */
reset(): void;
/** For SvelteKit `Snapshot<>` API. */
captureSnapshot(): SearchSnapshot;
restoreSnapshot(s: SearchSnapshot): void;
}
```
**Behavior invariants (copied 1:1 from the current code — do NOT change):**
- Query threshold: `trim().length > 3` triggers search, `<= 3` clears results.
- Race-guard: after every `await fetch(...)`, bail if `this.query.trim() !== q`.
- When `hits.length === 0` after local search → auto-fire web search page 1.
- `loadMore`: first drains local (offset pagination), then switches to web (pageno pagination).
- Dedup: local by `id`, web by `url`.
- `webError`: keep the message text so UI can render it.
**What stays OUT of the store:**
- URL sync (`history.replaceState` with `?q=`) → stays in `+page.svelte`.
- Dropdown visibility (`navOpen`) → stays in `+layout.svelte`.
- `afterNavigate`-reset wiring → stays in `+layout.svelte`, just calls `store.reset()`.
- SvelteKit `Snapshot<>` wiring → stays in `+page.svelte`, delegates to store.
- Filter-change re-search `$effect` → stays in `+page.svelte`, just calls `store.reSearch()`.
---
## Task 1: Failing Unit Tests for SearchStore
**Files:**
- Create: `tests/unit/search-store.test.ts`
- [ ] **Step 1: Write test file with full behavior coverage (runs red until Task 2)**
```ts
// @vitest-environment jsdom
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { SearchStore } from '../../src/lib/client/search.svelte';
type FetchMock = ReturnType<typeof vi.fn>;
function mockFetch(responses: Array<{ ok?: boolean; status?: number; body: unknown }>): FetchMock {
const calls = [...responses];
return vi.fn(async () => {
const r = calls.shift();
if (!r) throw new Error('fetch called more times than expected');
return {
ok: r.ok ?? true,
status: r.status ?? 200,
json: async () => r.body
} as Response;
});
}
describe('SearchStore', () => {
beforeEach(() => {
vi.useRealTimers();
});
it('keeps results empty while query is <= 3 chars (debounced)', async () => {
vi.useFakeTimers();
const fetchImpl = mockFetch([]);
const store = new SearchStore({ fetchImpl, debounceMs: 50 });
store.query = 'abc';
store.runDebounced();
await vi.advanceTimersByTimeAsync(100);
expect(store.searching).toBe(false);
expect(fetchImpl).not.toHaveBeenCalled();
});
it('fires local search after debounce when query > 3 chars', async () => {
vi.useFakeTimers();
const fetchImpl = mockFetch([
{ body: { hits: [{ id: 1, title: 'Pasta', description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }] } }
]);
const store = new SearchStore({ fetchImpl, debounceMs: 50, pageSize: 30 });
store.query = 'pasta';
store.runDebounced();
expect(store.searching).toBe(true);
await vi.advanceTimersByTimeAsync(100);
await vi.waitFor(() => expect(fetchImpl).toHaveBeenCalled());
expect(fetchImpl.mock.calls[0][0]).toMatch(/\/api\/recipes\/search\?q=pasta&limit=30/);
expect(store.hits).toHaveLength(1);
expect(store.searchedFor).toBe('pasta');
expect(store.localExhausted).toBe(true); // 1 hit < pageSize → exhausted
});
it('falls back to web search when local returns zero hits', async () => {
vi.useFakeTimers();
const fetchImpl = mockFetch([
{ body: { hits: [] } },
{ body: { hits: [{ url: 'https://chefkoch.de/x', title: 'Foo', domain: 'chefkoch.de', snippet: null, thumbnail: null }] } }
]);
const store = new SearchStore({ fetchImpl, debounceMs: 50 });
store.query = 'pizza';
store.runDebounced();
await vi.advanceTimersByTimeAsync(100);
await vi.waitFor(() => expect(store.webHits).toHaveLength(1));
expect(fetchImpl).toHaveBeenCalledTimes(2);
expect(fetchImpl.mock.calls[1][0]).toMatch(/\/api\/recipes\/search\/web\?q=pizza&pageno=1/);
expect(store.webPageno).toBe(1);
});
it('races-guards: stale response discarded when query changed mid-flight', async () => {
vi.useFakeTimers();
const fetchImpl = mockFetch([
{ body: { hits: [{ id: 99, title: 'Stale', description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }] } }
]);
const store = new SearchStore({ fetchImpl, debounceMs: 10 });
store.query = 'stale-query';
store.runDebounced();
await vi.advanceTimersByTimeAsync(15);
store.query = 'different'; // user kept typing
await vi.waitFor(() => expect(fetchImpl).toHaveBeenCalled());
expect(store.hits).toEqual([]); // stale discarded
});
it('loadMore: drains local first (offset pagination)', async () => {
vi.useFakeTimers();
const page1 = Array.from({ length: 30 }, (_, i) => ({ id: i, title: `r${i}`, description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }));
const page2 = Array.from({ length: 5 }, (_, i) => ({ id: i + 30, title: `r${i + 30}`, description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }));
const fetchImpl = mockFetch([
{ body: { hits: page1 } },
{ body: { hits: page2 } }
]);
const store = new SearchStore({ fetchImpl, debounceMs: 10, pageSize: 30 });
store.query = 'meal';
store.runDebounced();
await vi.advanceTimersByTimeAsync(15);
await vi.waitFor(() => expect(store.hits).toHaveLength(30));
expect(store.localExhausted).toBe(false);
await store.loadMore();
expect(store.hits).toHaveLength(35);
expect(fetchImpl.mock.calls[1][0]).toMatch(/offset=30/);
expect(store.localExhausted).toBe(true);
});
it('loadMore: switches to web pagination after local exhausted', async () => {
vi.useFakeTimers();
const local = [{ id: 1, title: 'local', description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }];
const webP1 = [{ url: 'https://a.com', title: 'A', domain: 'a.com', snippet: null, thumbnail: null }];
const webP2 = [{ url: 'https://b.com', title: 'B', domain: 'b.com', snippet: null, thumbnail: null }];
const fetchImpl = mockFetch([
{ body: { hits: local } },
{ body: { hits: webP1 } }, // auto-fallback? No — local has 1 hit, so no fallback.
{ body: { hits: webP2 } }
]);
const store = new SearchStore({ fetchImpl, debounceMs: 10, pageSize: 30 });
store.query = 'soup';
store.runDebounced();
await vi.advanceTimersByTimeAsync(15);
await vi.waitFor(() => expect(store.hits).toHaveLength(1));
expect(store.localExhausted).toBe(true);
await store.loadMore(); // web pageno=1
expect(store.webHits).toHaveLength(1);
await store.loadMore(); // web pageno=2
expect(store.webHits).toHaveLength(2);
expect(store.webPageno).toBe(2);
});
it('web search error sets webError and marks webExhausted', async () => {
vi.useFakeTimers();
const fetchImpl = mockFetch([
{ body: { hits: [] } },
{ ok: false, status: 502, body: { message: 'SearXNG unreachable' } }
]);
const store = new SearchStore({ fetchImpl, debounceMs: 10 });
store.query = 'anything';
store.runDebounced();
await vi.advanceTimersByTimeAsync(15);
await vi.waitFor(() => expect(store.webError).toBe('SearXNG unreachable'));
expect(store.webExhausted).toBe(true);
});
it('reset(): clears query, results, and pending debounce', async () => {
vi.useFakeTimers();
const fetchImpl = mockFetch([]);
const store = new SearchStore({ fetchImpl, debounceMs: 100 });
store.query = 'foobar';
store.runDebounced();
store.reset();
await vi.advanceTimersByTimeAsync(200);
expect(store.query).toBe('');
expect(store.hits).toEqual([]);
expect(fetchImpl).not.toHaveBeenCalled();
});
it('captureSnapshot / restoreSnapshot: round-trips without re-fetching', async () => {
vi.useFakeTimers();
const fetchImpl = mockFetch([]);
const store = new SearchStore({ fetchImpl, debounceMs: 50 });
const snap: SearchSnapshot = {
query: 'lasagne',
hits: [{ id: 7, title: 'Lasagne', description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }],
webHits: [],
searchedFor: 'lasagne',
webError: null,
localExhausted: true,
webPageno: 0,
webExhausted: false
};
store.restoreSnapshot(snap);
expect(store.query).toBe('lasagne');
expect(store.hits).toHaveLength(1);
store.runDebounced(); // should NOT re-fetch after restore
await vi.advanceTimersByTimeAsync(100);
expect(fetchImpl).not.toHaveBeenCalled();
const round = store.captureSnapshot();
expect(round).toEqual(snap);
});
it('filterParam option: gets appended to both local and web requests', async () => {
vi.useFakeTimers();
const fetchImpl = mockFetch([
{ body: { hits: [] } },
{ body: { hits: [] } }
]);
const store = new SearchStore({
fetchImpl,
debounceMs: 10,
filterParam: () => '&domains=chefkoch.de'
});
store.query = 'curry';
store.runDebounced();
await vi.advanceTimersByTimeAsync(15);
await vi.waitFor(() => expect(fetchImpl).toHaveBeenCalledTimes(2));
expect(fetchImpl.mock.calls[0][0]).toMatch(/&domains=chefkoch\.de/);
expect(fetchImpl.mock.calls[1][0]).toMatch(/&domains=chefkoch\.de/);
});
it('reSearch: immediate re-run with current query on filter change', async () => {
vi.useFakeTimers();
let filter = '';
const fetchImpl = mockFetch([
{ body: { hits: [] } },
{ body: { hits: [] } },
{ body: { hits: [{ id: 1, title: 'filtered', description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }] } }
]);
const store = new SearchStore({
fetchImpl,
debounceMs: 10,
filterDebounceMs: 5,
filterParam: () => filter
});
store.query = 'broth';
store.runDebounced();
await vi.advanceTimersByTimeAsync(15);
// Simulate filter change
filter = '&domains=chefkoch.de';
store.reSearch();
await vi.advanceTimersByTimeAsync(10);
await vi.waitFor(() => expect(store.hits).toHaveLength(1));
// Last call should have filter param
const last = fetchImpl.mock.calls.at(-1)?.[0] as string;
expect(last).toMatch(/&domains=chefkoch\.de/);
});
});
```
- [ ] **Step 2: Run tests to verify all fail with "SearchStore is not a constructor" or "Cannot find module"**
```bash
npm test -- search-store.test
```
Expected: 12 tests, all failing because `src/lib/client/search.svelte.ts` doesn't exist yet.
---
## Task 2: Implement SearchStore to pass tests
**Files:**
- Create: `src/lib/client/search.svelte.ts`
- [ ] **Step 1: Scaffold the class + types**
Create `src/lib/client/search.svelte.ts` with this content:
```ts
import type { SearchHit } from '$lib/server/recipes/search-local';
import type { WebHit } from '$lib/server/search/searxng';
export type SearchSnapshot = {
query: string;
hits: SearchHit[];
webHits: WebHit[];
searchedFor: string | null;
webError: string | null;
localExhausted: boolean;
webPageno: number;
webExhausted: boolean;
};
export type SearchStoreOptions = {
pageSize?: number;
debounceMs?: number;
filterDebounceMs?: number;
minQueryLength?: number;
filterParam?: () => string;
fetchImpl?: typeof fetch;
};
export class SearchStore {
query = $state('');
hits = $state<SearchHit[]>([]);
webHits = $state<WebHit[]>([]);
searching = $state(false);
webSearching = $state(false);
webError = $state<string | null>(null);
searchedFor = $state<string | null>(null);
localExhausted = $state(false);
webPageno = $state(0);
webExhausted = $state(false);
loadingMore = $state(false);
private readonly pageSize: number;
private readonly debounceMs: number;
private readonly filterDebounceMs: number;
private readonly minQueryLength: number;
private readonly filterParam: () => string;
private readonly fetchImpl: typeof fetch;
private debounceTimer: ReturnType<typeof setTimeout> | null = null;
private skipNextDebounce = false;
constructor(opts: SearchStoreOptions = {}) {
this.pageSize = opts.pageSize ?? 30;
this.debounceMs = opts.debounceMs ?? 300;
this.filterDebounceMs = opts.filterDebounceMs ?? 150;
this.minQueryLength = opts.minQueryLength ?? 4;
this.filterParam = opts.filterParam ?? (() => '');
this.fetchImpl = opts.fetchImpl ?? ((...a) => fetch(...a));
}
}
```
- [ ] **Step 2: Implement `runDebounced`, `runSearch`, private `runWebSearch`**
Add to the class:
```ts
runDebounced(): void {
// Consumer pattern:
// $effect(() => { store.query; store.runDebounced(); });
// The bare `store.query` read registers the reactive dep; this method
// then reads `this.query` live to kick off / debounce the search.
if (this.debounceTimer) clearTimeout(this.debounceTimer);
if (this.skipNextDebounce) {
this.skipNextDebounce = false;
return;
}
const q = this.query.trim();
if (q.length < this.minQueryLength) {
this.resetResults();
return;
}
this.searching = true;
this.webHits = [];
this.webSearching = false;
this.webError = null;
this.debounceTimer = setTimeout(() => {
void this.runSearch(q);
}, this.debounceMs);
}
async runSearch(q: string): Promise<void> {
this.localExhausted = false;
this.webPageno = 0;
this.webExhausted = false;
try {
const res = await this.fetchImpl(
`/api/recipes/search?q=${encodeURIComponent(q)}&limit=${this.pageSize}${this.filterParam()}`
);
const body = (await res.json()) as { hits: SearchHit[] };
if (this.query.trim() !== q) return;
this.hits = body.hits;
this.searchedFor = q;
if (this.hits.length < this.pageSize) this.localExhausted = true;
if (this.hits.length === 0) {
await this.runWebSearch(q, 1);
}
} finally {
if (this.query.trim() === q) this.searching = false;
}
}
private async runWebSearch(q: string, pageno: number): Promise<void> {
this.webSearching = true;
try {
const res = await this.fetchImpl(
`/api/recipes/search/web?q=${encodeURIComponent(q)}&pageno=${pageno}${this.filterParam()}`
);
if (this.query.trim() !== q) return;
if (!res.ok) {
const err = (await res.json().catch(() => ({}))) as { message?: string };
this.webError = err.message ?? `HTTP ${res.status}`;
this.webExhausted = true;
return;
}
const body = (await res.json()) as { hits: WebHit[] };
this.webHits = pageno === 1 ? body.hits : [...this.webHits, ...body.hits];
this.webPageno = pageno;
if (body.hits.length === 0) this.webExhausted = true;
} finally {
if (this.query.trim() === q) this.webSearching = false;
}
}
```
- [ ] **Step 3: Implement `loadMore`**
```ts
async loadMore(): Promise<void> {
if (this.loadingMore) return;
const q = this.query.trim();
if (!q) return;
this.loadingMore = true;
try {
if (!this.localExhausted) {
const res = await this.fetchImpl(
`/api/recipes/search?q=${encodeURIComponent(q)}&limit=${this.pageSize}&offset=${this.hits.length}${this.filterParam()}`
);
const body = (await res.json()) as { hits: SearchHit[] };
if (this.query.trim() !== q) return;
const more = body.hits;
const seen = new Set(this.hits.map((h) => h.id));
const deduped = more.filter((h) => !seen.has(h.id));
this.hits = [...this.hits, ...deduped];
if (more.length < this.pageSize) this.localExhausted = true;
} else if (!this.webExhausted) {
const nextPage = this.webPageno + 1;
const wasEmpty = this.webHits.length === 0;
if (wasEmpty) this.webSearching = true;
try {
const res = await this.fetchImpl(
`/api/recipes/search/web?q=${encodeURIComponent(q)}&pageno=${nextPage}${this.filterParam()}`
);
if (this.query.trim() !== q) return;
if (!res.ok) {
const err = (await res.json().catch(() => ({}))) as { message?: string };
this.webError = err.message ?? `HTTP ${res.status}`;
this.webExhausted = true;
return;
}
const body = (await res.json()) as { hits: WebHit[] };
const more = body.hits;
const seen = new Set(this.webHits.map((h) => h.url));
const deduped = more.filter((h) => !seen.has(h.url));
if (deduped.length === 0) {
this.webExhausted = true;
} else {
this.webHits = [...this.webHits, ...deduped];
this.webPageno = nextPage;
}
} finally {
if (this.query.trim() === q) this.webSearching = false;
}
}
} finally {
this.loadingMore = false;
}
}
```
- [ ] **Step 4: Implement `reSearch`, `reset`, `resetResults`, snapshot methods**
```ts
reSearch(): void {
const q = this.query.trim();
if (q.length < this.minQueryLength) return;
if (this.debounceTimer) clearTimeout(this.debounceTimer);
this.searching = true;
this.webHits = [];
this.webSearching = false;
this.webError = null;
this.debounceTimer = setTimeout(() => void this.runSearch(q), this.filterDebounceMs);
}
reset(): void {
if (this.debounceTimer) clearTimeout(this.debounceTimer);
this.debounceTimer = null;
this.query = '';
this.resetResults();
}
private resetResults(): void {
this.hits = [];
this.webHits = [];
this.searchedFor = null;
this.searching = false;
this.webSearching = false;
this.webError = null;
this.localExhausted = false;
this.webPageno = 0;
this.webExhausted = false;
}
captureSnapshot(): SearchSnapshot {
return {
query: this.query,
hits: this.hits,
webHits: this.webHits,
searchedFor: this.searchedFor,
webError: this.webError,
localExhausted: this.localExhausted,
webPageno: this.webPageno,
webExhausted: this.webExhausted
};
}
restoreSnapshot(s: SearchSnapshot): void {
this.skipNextDebounce = true;
this.query = s.query;
this.hits = s.hits;
this.webHits = s.webHits;
this.searchedFor = s.searchedFor;
this.webError = s.webError;
this.localExhausted = s.localExhausted;
this.webPageno = s.webPageno;
this.webExhausted = s.webExhausted;
}
```
- [ ] **Step 5: Run tests, iterate until all green**
```bash
npm test -- search-store.test
```
Expected: all 12 tests pass.
- [ ] **Step 6: `npm run check`**
```bash
npm run check
```
Expected: 0 errors, 0 warnings in `search.svelte.ts`.
- [ ] **Step 7: Commit**
```bash
git add src/lib/client/search.svelte.ts tests/unit/search-store.test.ts
git commit -m "feat(search): SearchStore fuer Live-Search mit Web-Fallback
Extrahiert die duplizierte Such-Logik aus +page.svelte und
+layout.svelte in eine gemeinsame Klasse. Pure Datenschicht
mit injizierbarem fetch — UI-Concerns (URL-Sync, Dropdown,
Snapshot) bleiben in den Komponenten."
```
---
## Task 3: Migrate `+layout.svelte` header dropdown
**Why first:** Smaller surface than `+page.svelte`, no snapshot API, no URL sync. If the store is wrong, here we find out with less code at risk.
**Files:**
- Modify: `src/routes/+layout.svelte:20-200`
- [ ] **Step 1: Add import**
At the top of `<script>`:
```ts
import { SearchStore } from '$lib/client/search.svelte';
import { searchFilterStore } from '$lib/client/search-filter.svelte';
```
(Latter is already imported — just confirm.)
- [ ] **Step 2: Replace the 11 `$state` declarations (navQuery, navHits, navWebHits, navSearching, navWebSearching, navWebError, navLocalExhausted, navWebPageno, navWebExhausted, navLoadingMore, debounceTimer) with one store instance.**
Keep these (UI-only): `navOpen`, `navContainer`, `menuOpen`, `menuContainer`.
New:
```ts
const navStore = new SearchStore({
pageSize: 30,
filterParam: () => {
const p = searchFilterStore.queryParam;
return p ? `&domains=${encodeURIComponent(p)}` : '';
}
});
```
Remove the local `filterParam()` helper — the store owns it now.
- [ ] **Step 3: Replace the big `$effect` (lines 52109) with a 3-line `$effect`**
```ts
$effect(() => {
// Bare reads register the reactive deps; then kick the store.
const q = navStore.query;
navStore.runDebounced();
// navOpen follows query length: open while typing, close when cleared.
navOpen = q.trim().length > 3;
});
```
- [ ] **Step 4: Replace `loadMoreNav` function (lines 111159) with a pass-through**
```ts
function loadMoreNav() {
return navStore.loadMore();
}
```
Or inline `onclick={() => navStore.loadMore()}` at the call-site — pick the less disruptive option when looking at the template.
- [ ] **Step 5: Replace `submitNav` (lines 161167)**
```ts
function submitNav(e: SubmitEvent) {
e.preventDefault();
const q = navStore.query.trim();
if (!q) return;
navOpen = false;
void goto(`/?q=${encodeURIComponent(q)}`);
}
```
- [ ] **Step 6: Replace `pickHit` (lines 185190)**
```ts
function pickHit() {
navOpen = false;
navStore.reset();
}
```
- [ ] **Step 7: Update `afterNavigate` (lines 192+)**
```ts
afterNavigate(() => {
navStore.reset();
navOpen = false;
menuOpen = false;
// ... rest of existing body (wishlist refresh etc.)
});
```
- [ ] **Step 8: Update the template**
Every `navQuery``navStore.query`, every `navHits``navStore.hits`, etc. This is a mechanical rename — use find+replace scoped to `src/routes/+layout.svelte` only.
Mapping:
- `navQuery``navStore.query`
- `navHits``navStore.hits`
- `navWebHits``navStore.webHits`
- `navSearching``navStore.searching`
- `navWebSearching``navStore.webSearching`
- `navWebError``navStore.webError`
- `navLocalExhausted``navStore.localExhausted`
- `navWebPageno``navStore.webPageno` (if referenced in template)
- `navWebExhausted``navStore.webExhausted`
- `navLoadingMore``navStore.loadingMore`
`bind:value={navQuery}` on the `<input>``bind:value={navStore.query}`.
- [ ] **Step 9: Run checks**
```bash
npm run check
npm test
```
Both must be clean.
- [ ] **Step 10: Smoke-test dev server manually**
```bash
npm run dev
```
Open a recipe page → type in header dropdown → verify: dropdown opens, shows local hits, falls back to web for unknown query, "+ weitere Ergebnisse" paginates, clicking a hit closes the dropdown, navigating back/forward clears the dropdown.
- [ ] **Step 11: Commit**
```bash
git add src/routes/+layout.svelte
git commit -m "refactor(layout): Header-Dropdown nutzt SearchStore
Ersetzt die 11 lokalen \$state und den Debounce-Effect durch
eine SearchStore-Instanz. Nav-Open-Toggle bleibt lokal, weil
UI-Concern."
```
---
## Task 4: Migrate `+page.svelte` home
**Why after Task 3:** The store is now field-tested. Home adds snapshot + URL sync + filter-change re-search on top.
**Files:**
- Modify: `src/routes/+page.svelte:1-371`
- [ ] **Step 1: Add imports**
```ts
import { SearchStore, type SearchSnapshot } from '$lib/client/search.svelte';
```
- [ ] **Step 2: Remove the duplicated `$state` block (lines 1732)**
Delete: `query`, `hits`, `webHits`, `searching`, `webSearching`, `webError`, `searchedFor`, `localExhausted`, `webPageno`, `webExhausted`, `loadingMore`, `skipNextSearch`, `debounceTimer`.
Keep: `quote`, `recent`, `favorites` (not search-related), and all `all*` state (All-Recipes listing — unrelated to search).
Add:
```ts
const store = new SearchStore({
pageSize: LOCAL_PAGE,
filterParam: () => {
const p = searchFilterStore.queryParam;
return p ? `&domains=${encodeURIComponent(p)}` : '';
}
});
```
Remove the local `filterParam()` helper (lines 224227).
- [ ] **Step 3: Rewire the `Snapshot<>` API (lines 5083)**
```ts
export const snapshot: Snapshot<SearchSnapshot> = {
capture: () => store.captureSnapshot(),
restore: (s) => store.restoreSnapshot(s)
};
```
Delete the old `SearchSnapshot` local type alias (it's now imported).
- [ ] **Step 4: Replace the two search `$effect`s (filter-change + query-change) with two one-liners**
Remove lines 188199 (filter-change effect) and lines 322347 (query-change effect).
Add:
```ts
$effect(() => {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
store.query; // register reactive dep
store.runDebounced();
});
$effect(() => {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
searchFilterStore.active;
store.reSearch();
});
```
- [ ] **Step 5: Keep the URL-sync `$effect` as-is, but read from `store.query`**
```ts
$effect(() => {
if (typeof window === 'undefined') return;
const q = store.query.trim();
const url = new URL(window.location.href);
const current = url.searchParams.get('q') ?? '';
if (q === current) return;
if (q) url.searchParams.set('q', q);
else url.searchParams.delete('q');
history.replaceState(history.state, '', url.toString());
});
```
- [ ] **Step 6: Update `onMount` URL-restore**
```ts
const urlQ = ($page.url.searchParams.get('q') ?? '').trim();
if (urlQ) store.query = urlQ;
```
- [ ] **Step 7: Delete `runSearch` and `loadMore` local functions (lines 229320)**
The store provides both. Template references `loadMore` → change to `store.loadMore()`.
- [ ] **Step 8: Update `submit`**
```ts
function submit(e: SubmitEvent) {
e.preventDefault();
const q = store.query.trim();
if (q.length <= 3) return;
void store.runSearch(q);
}
```
- [ ] **Step 9: Update the template (same mechanical rename as Task 3)**
`query``store.query`, `hits``store.hits`, etc. for all 11 fields.
`bind:value={query}``bind:value={store.query}`.
`activeSearch` derived stays: `const activeSearch = $derived(store.query.trim().length > 3);`
- [ ] **Step 10: Run checks**
```bash
npm run check
npm test
```
- [ ] **Step 11: Verify file is shorter than before**
```bash
wc -l src/routes/+page.svelte
```
Expected: under 700 lines (was 808). Target from roadmap: under 700 L.
```bash
wc -l src/routes/+layout.svelte
```
Expected: under 600 lines (was 681). Target from roadmap: under 600 L.
- [ ] **Step 12: Smoke-test dev manually**
- Type "lasagne" in home → local hits appear.
- Type "pizza margherita" → web fallback.
- Deep-link `/?q=lasagne` → query restored, results visible.
- Navigate to recipe → back → home query + results preserved (snapshot).
- Change domain filter while query is active → results re-fetch with new filter.
- [ ] **Step 13: Commit**
```bash
git add src/routes/+page.svelte
git commit -m "refactor(home): Live-Search auf SearchStore migriert
Entfernt 11 duplizierte \$state, runSearch, loadMore und beide
Debounce-Effekte. URL-Sync, Snapshot und Filter-Re-Search bleiben
hier — aber alle delegieren an den Store."
```
---
## Task 5: Remote E2E smoke (optional — only if CI deploy happens)
**Trigger:** Only run this task if CI builds the `search-state-store` branch and deploys to `kochwas-dev.siegeln.net`. Otherwise skip to Task 6.
**Files:**
- Run: existing `tests/e2e/remote/search.spec.ts`
- [ ] **Step 1: Run remote suite**
```bash
npm run test:e2e:remote -- search.spec.ts
```
Expected: 4/4 pass (existing coverage is sufficient — no new specs needed for a pure refactor).
---
## Task 6: Self-review + merge prep
**Files:**
- Review: all changed files
- [ ] **Step 1: `npm test` full suite**
```bash
npm test
```
Expected: all pass (previous count + 12 new SearchStore tests).
- [ ] **Step 2: `npm run check` full repo**
```bash
npm run check
```
Expected: 0 errors, 0 warnings.
- [ ] **Step 3: `git diff main...HEAD` review**
```bash
git diff main...HEAD --stat
git log main..HEAD --oneline
```
Expected commits:
1. `feat(search): SearchStore fuer Live-Search mit Web-Fallback`
2. `refactor(layout): Header-Dropdown nutzt SearchStore`
3. `refactor(home): Live-Search auf SearchStore migriert`
- [ ] **Step 4: Push branch**
```bash
git push -u origin search-state-store
```
CI builds branch-tagged image → user tests on `kochwas-dev.siegeln.net` → merges to main when clean.
---
## Risk Notes
- **Svelte 5 `$state` in classes:** Standard pattern in this repo (`SearchFilterStore`, `PWAStore`). Works.
- **Two instances of `SearchStore` simultaneously:** Each has its own timer + state. No shared mutable state between them — verified because the store has no static fields.
- **Snapshot restore racing with `runDebounced`:** Handled via `skipNextDebounce` flag. Same mechanism as the current `skipNextSearch` in `+page.svelte`.
- **Filter change on home while query is empty:** `reSearch()` early-exits when `q.length < minQueryLength`. Safe.
- **`afterNavigate` firing during an in-flight search:** `reset()` clears timer and mutates `query`. Any in-flight fetch will race-guard-fail on the next `if (this.query.trim() !== q) return;`. Results get dropped, which is the desired behavior.
## Deferred — NOT in this plan
- **Search-Store-Tests mit echtem Browser-`$effect`:** Would need `@sveltejs/vite-plugin-svelte` test setup with component mount. Current Vitest setup is Node-only. Skip — the injected-fetch unit tests cover the state machine.
- **Shared store instance (singleton) instead of per-consumer:** Rejected during design — would couple home and header search semantically.
- **Web-Hit-Cache im Store:** Out of scope. The roadmap explicitly scopes this phase to state extraction, not perf work.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,166 @@
# Open Issues — Stand nach Review-Fixes
**Datum:** 2026-04-18 (Nacht-Session)
**Branch:** `review-fixes-2026-04-18`
**Baseline:** REVIEW-2026-04-18.md + 4 Sub-Reports vom Morgen
**Tests:** 184/184 grün (Baseline waren 158, +26 neue Tests)
**svelte-check:** 0 Errors, 10 Warnings (alle pre-existing in `RecipeEditor.svelte` / `recipes/[id]/+page.svelte`)
**Build:** `npm run build` erfolgreich
**Smoke-Test:** `npm run preview` + curl auf `/api/health`, `/api/profiles`, `/api/recipes/abc` (400), `/api/wishlist` mit invalider Body (400 + issues) — alle Endpunkte verhalten sich korrekt
---
## Was wurde gemacht (8 Commits)
| Commit | Inhalt | Verifikation |
|---|---|---|
| `2289547` | docs(review): table names, IMAGE_DIR, image endpoints | grep auf alte Namen → 0 |
| `830c740` | refactor(constants): SW-Timing-Konstanten, RequestShape/ManifestDiff intern, Image-Endpoint EN | tests + check grün |
| `739cc2d` | feat(server): api-helpers.ts (parsePositiveIntParam, validateBody, ErrorResponse) | 13 neue Tests |
| `ff293e9` | refactor(api): 13 +server.ts handler auf api-helpers (-67 Zeilen netto) | 171/171 |
| `30a447a` | refactor(client): requireProfile() + asyncFetch wrapper | 5 + 4 Sites umgestellt |
| `60c8352` | docs(searxng): Intent-Kommentar fuer Prod-Logs | — |
| `6d9e79d` | feat(parser): Unicode-Brueche + Mengen-Plausibilitaet | 13 neue Tests |
| `31c6e5c` | refactor(server): IMAGE_DIR/DATABASE_PATH zentralisieren + Doku-Drift | grep auf alte Pattern → 0 |
Net: 31 Files, +626/-272.
### Re-Review per 4 paralleler Explore-Agenten — Beweis
**Dead-Code (HIGH-Confidence):** Alle vorherigen Findings resolved. RequestShape + ManifestDiff sind nur noch interne Types. yauzl ist explizit als Phase 5b markiert (in `session-handoff-2026-04-17.md` und `ARCHITECTURE.md:33`). Kein neuer toter Code durch die Refactors.
**Redundancy (HIGH-Confidence):** 0 verbleibende `function parseId`/`parsePositiveInt`-Definitionen in `src/routes/api/`. 0 verbleibende `safeParse(...) + manueller error(400)`-Blöcke. Der gerade behobene `IMAGE_DIR`-Drift war 6× im Code und 1× in `db/index.ts`. Verbleibende kleine Pattern siehe unten.
**Structure:** Constants-Extraktion + API-Error-Shape-Standardisierung erledigt. Ingredient-Parser-Edge-Cases mit 13 Tests abgesichert. Große Pages bleiben groß (siehe „Bewusst verschoben").
**Docs-vs-Code:** Alle drei Original-Findings behoben. Zwei kleine zusätzliche Mismatches (149→150 Quote-Count, search/-Route gar nicht existent) heute gleich mitgenommen.
---
## ⚠ Verbleibende Items — bewusst verschoben mit Begründung
### A. Refactor B — Search-State-Store extrahieren (HIGH, halber Tag)
**Wo:** `src/routes/+page.svelte` (808 Zeilen, 20+ `$state`-Vars), `src/routes/+layout.svelte` (678 Zeilen, dupliziert das Header-Search-Dropdown).
**Vorschlag:** `src/lib/client/search.svelte.ts` mit `search()`, `loadMore()`, `clear()` und reaktivem `query / hits / loading / error`-Zustand.
**Warum nicht heute:**
1. Touch in zwei der drei größten Files der Codebase (808L + 678L)
2. Bricht Frontend-Verhalten subtil, wenn Reactive-Glue zwischen Layout-Search und Page-Search nicht 1:1 übernommen wird
3. UAT-pflichtig (Live-Suche, Empty-State, Web-Suche-Fallback) — ohne UAT-Slot zu riskant
4. Kein automatisches Test-Sicherheitsnetz für die UI-Layer
**Empfehlung:** Eigene Phase mit `/gsd-discuss-phase` und Smoke-UAT vor dem Mergen. Anschließend `/gsd-execute-phase` mit Browser-Check pro Wave.
---
### B. Refactor C — RecipeEditor / RecipeView in Sub-Components zerlegen (MEDIUM, halber Tag)
**Wo:** `src/lib/components/RecipeEditor.svelte` (630L), `RecipeView.svelte` (398L).
**Kandidaten:** `IngredientRow.svelte`, `StepList.svelte`, `TimeDisplay.svelte`, `ImageUploadBox.svelte`.
**Warum nicht heute:**
- REVIEW-2026-04-18.md sagt explizit: *"Aber: keine Eile, solange niemand sonst drin arbeitet."*
- Solange der Owner allein entwickelt, ist 630L pro Komponente kein Blocker.
- Tests gibt es nur indirekt (über Importer-Tests und Unit-Tests der Parser).
**Empfehlung:** Spätere Phase, falls eine zweite Person mitarbeitet oder wenn Editor-Bug-Hunting zu schwierig wird. Vorher zumindest die 10 pre-existing svelte-check WARNINGs in `RecipeEditor.svelte` fixen — die sind schon flackrige Reactive-Patterns (`$derived` statt `$state` für abgeleitete Werte).
---
### C. SearXNG Rate-Limit Recovery (MEDIUM, 1-2 h)
**Wo:** `src/lib/server/search/searxng.ts`.
**Was fehlt:** Bei 429/403 wird zwar geloggt, aber kein Backoff oder `isStale`-Flag. Folgesuchen liefern alten Cache, der User merkt nichts.
**Empfehlung:** Eigene Phase. Drei mögliche Zutaten: (1) `lastFailureAt`-Map per Domain, (2) exponentieller Backoff, (3) `isStale: boolean` im Response, das die UI als „Ergebnisse evtl. veraltet" anzeigt.
---
### D. Service-Worker Zombie-Cleanup unter Last testen (MEDIUM, 2-3 h)
**Wo:** `src/lib/client/pwa.svelte.ts` Zombie-Heuristik.
**Status:** 6 Unit-Tests existieren bereits (`tests/unit/pwa-store.test.ts`), die beide Pfade abdecken.
**Was offen ist:** Verhalten unter sehr langsamen Netzen (1500ms-Timeout könnte False-Positive triggern). Sehr edge-case, aber im REVIEW-Original als MEDIUM gelistet.
**Empfehlung:** Beim nächsten Service-Worker-Touch mit Throttling-DevTools-Profil testen. Kein eigener Sprint nötig.
---
### E. JSON-LD Parser Edge-Cases (MEDIUM, halbe Phase)
**Wo:** `src/lib/server/parsers/json-ld-recipe.ts` (402L).
**Was abgesichert ist:** Ingredient-Parser-Käfer (Unicode-Brüche, Bounds, Komma-Dezimal) sind heute mit 13 neuen Tests dicht.
**Was offen ist:** JSON-LD selbst hat Edge-Cases — null-Servings, Locale-spezifische Number-Formats, defekte `recipeIngredient`-Arrays.
**Empfehlung:** Wenn beim Importieren ein Bug auftaucht, gezielt einen Test schreiben. Kein Vorab-Sprint.
---
### F. Inline UI-Constants (LOW, 30 min)
**Wo:** `ConfirmDialog.svelte`, `ProfileSwitcher.svelte` etc. mit Hardcoded `z-index`, `border-radius: 999px`, kleinen Timeouts.
**Vorschlag:** `src/lib/theme.ts` mit `MODAL_Z_INDEX`, `POPOVER_Z_INDEX`, `PILL_RADIUS`.
**Warum nicht heute:** LOW-Severity, kein konkreter Bug damit verbunden, betrifft viele Files punktuell.
---
### G. wishlist/+page.svelte:38 — Profil-Guard mit individueller Message (LOW)
**Was:** Eine 7. Stelle hat das Profil-Guard-Pattern, aber mit eigenem Text („um mitzuwünschen"). `requireProfile()` akzeptiert aktuell keine Custom-Message.
**Empfehlung:** Entweder `requireProfile(message?)`-Variante einführen oder das Site so lassen — die Custom-Message ist dort wirklich Kontext-Information.
---
### H. RecipeEditor.svelte:54 + :83 — Bild-Upload/Delete mit inline `if (!res.ok)` (LOW)
**Was:** Image-Upload und -Delete im Editor nutzen noch das Pattern, das `asyncFetch` ersetzen sollte. Der Aufwand wäre 5 Minuten, aber RecipeEditor steckt in den 10 svelte-check-WARNINGs (siehe Refactor B-Notiz) — beim nächsten Touch der Datei mitnehmen.
---
### I. Pre-Existing svelte-check Warnings (10 Stück)
**Wo:** `RecipeEditor.svelte` (9× Zeilen 28, 97-102, 113, 121) + `recipes/[id]/+page.svelte` (1× Zeile 43).
**Was:** Pattern `let foo = recipe.bar` im Top-Level-Script — Svelte 5 will `$derived(recipe.bar)`. Aktuell snapshot-only.
**Risiko:** Bei In-Place-Mutation des Rezepts (z. B. nach PATCH) zeigt der Editor ggf. den alten Wert. **Tests fangen das nicht.**
**Empfehlung:** Kleine Phase „RecipeEditor auf $derived umstellen" — passt gut zur RecipeEditor-Subkomponentenphase (B oben), oder vorab alleine.
---
## 📌 Neu entdeckt in der zweiten Runde — alle behoben
| # | Fund | Severity | Status |
|---|---|---|---|
| 1 | `IMAGE_DIR` 6× dupliziert + `DATABASE_PATH` 2× | HIGH | ✅ `src/lib/server/paths.ts` |
| 2 | `ARCHITECTURE.md:34` — „49 Flachwitze" | MEDIUM | ✅ → 150 |
| 3 | `ARCHITECTURE.md:41``search/`-Route existiert nicht | LOW | ✅ entfernt |
---
## Empfohlene nächste Schritte
1. **PR mergen** sobald lokal abgenickt — der Branch enthält 8 atomische Commits, jeder einzeln revert-bar.
2. **Falls UAT erwünscht:** `npm run build && npm run preview`, dann manuell Profile-Switching, Rezept-Edit, Favoriten-Toggle, Wunschliste, Bild-Upload, Such-Pfade durchklicken. Erwartung: keine Verhaltensänderung gegenüber `main`.
3. **Phase „RecipeEditor reactive cleanup"** für die 10 svelte-check-Warnings (klein) — schließt Item I.
4. **Phase „Search-State-Store"** als nächste größere Phase — schließt Item A und drückt das größte Page-File spürbar runter.
5. yauzl/Phase 5b (ZIP-Backup-Restore) bleibt als ungeplant bis explizit gebraucht.
---
## Code-Quality Snapshot
| Metrik | Vorher | Nachher | Δ |
|---|---|---|---|
| Tests gesamt | 158 | 184 | +26 |
| Tests Files | 23 | 24 | +1 (api-helpers) |
| svelte-check Errors | 0 | 0 | — |
| svelte-check Warnings | 10 | 10 | — (alle pre-existing) |
| Build | ✓ | ✓ | — |
| Größte Datei (recipes/[id]/+page.svelte) | 757 | 725 | -32 |
| Größte Datei (+page.svelte) | 808 | 808 | — |
| API +server.ts Boilerplate | ca. 11 Zeilen pro Handler | ca. 4 Zeilen pro Handler | -64% |
| Duplizierte ENV-Defaults | 8 Sites | 1 Site | -7 |

View File

@@ -0,0 +1,140 @@
# Deep Code Review — Kochwas
**Datum:** 2026-04-18
**Stand:** commit `5283ab9` auf `main`
**Testsuite beim Start:** 158/158 grün
**Scope:** `src/` (~97 Dateien), Migrations, Tests, Docker-Setup, alle Docs unter `docs/`
---
## TL;DR
Der Code ist **gesund**. Keine toten Pfade, keine broken Features, keine strukturellen Fehlentscheidungen. Die vier auffälligsten Themen sind alle **Natural-Growth-Pressure** aus der v1.x-Phase, keine Fehler:
1. **Ein echter Doku-Bug:** `docs/ARCHITECTURE.md:55` sagt `recipe_ingredient` + `recipe_step` — die Tabellen heißen in Wirklichkeit `ingredient` / `step` (siehe `001_init.sql`). 5-Minuten-Fix.
2. **API-Handler duplizieren `parseId`** neunmal. Kandidat #1 für einen `src/lib/server/api-helpers.ts`.
3. **Page-Komponenten sind groß** geworden (`+page.svelte` 808 Zeilen, `recipes/[id]/+page.svelte` 757 Zeilen). Solange du allein dran arbeitest: akzeptabel. Sobald jemand mitprogrammiert: refactor.
4. **`yauzl` / `@types/yauzl` sind installiert, aber nicht importiert.** Reserviert für den noch fehlenden ZIP-Backup-Import. Entweder im Session-Handoff verankert lassen oder als Phase ziehen.
Keine Sicherheits- oder Performance-Probleme im Code-Review aufgetaucht. Keine Reviewer-Korrekturen an der Architektur-Grundlinie (Server/Client-Trennung, Repository-Pattern, Runes-Stores).
---
## Quick-Wins (≤ 30 min pro Stück)
| # | Titel | Aufwand | Wert |
|---|---|---|---|
| 1 | `ARCHITECTURE.md:55` auf `ingredient` + `step` korrigieren | 2 min | hoch (sonst debuggt jemand Geisterschema) |
| 2 | `OPERATIONS.md:135` `IMAGES_PATH``IMAGE_DIR` | 2 min | niedrig, aber trivial |
| 3 | `parseId` zentralisieren (`src/lib/server/api-helpers.ts`) | 20 min | hoch — 9 Call-Sites |
| 4 | Unit-Test für `parseId`-Helper | 10 min | hoch — fängt zukünftige Regressionen |
| 5 | `requireProfile()`-Helper in `recipes/[id]/+page.svelte` (Zeilen 124/143/166/188 räumen 4×7 Zeilen weg) | 15 min | mittel |
| 6 | Timeout-Magic-Numbers nach `src/lib/constants.ts` (1500 ms, 30-min SW-Poll) | 10 min | mittel |
| 7 | Deutsche Fehler-Texte in `api/recipes/[id]/image/+server.ts` englisch ziehen (Konsistenz) | 5 min | kosmetisch |
| 8 | Im Session-Handoff `/api/recipes/[id]/image` (POST/DELETE) nachtragen | 5 min | niedrig |
Summe: unter 90 Minuten — und du hast den Großteil der Haut-Irritationen unten.
---
## Größere Refactor-Kandidaten
### A. API-Endpoints entkoppeln (HIGH, 12 Std)
Extrahiere `src/lib/server/api-helpers.ts` mit:
- `parsePositiveIntParam(raw: string, field: string): number` — wirft via SvelteKit `error(400, …)`
- `validateBody<T>(body: unknown, schema: ZodSchema<T>): T` — ersetzt die `safeParse()`-Loops in 8+ Handlern
- gemeinsame `ErrorResponse`-Shape (aktuell mal `{message}`, mal `{message, issues}`)
Nach dem Helper-Refactor sollten die Handler nur noch echtes Business-Logik enthalten und je 3050 Zeilen kürzer werden.
### B. Search-State aus `+page.svelte` ziehen (HIGH, halber Tag)
`+page.svelte` trägt 20+ `$state`-Variablen (`query`, `hits`, `webHits`, `searching`, `webError` …) und duplizierte Search-UI in `+layout.svelte`. Vorschlag: `src/lib/client/search.svelte.ts` mit `search()`, `loadMore()`, `clear()`. Danach ist das Page-File halbiert und der Layout-Nav-Search nutzt denselben Store.
### C. `RecipeEditor` / `RecipeView` in Sub-Components zerlegen (MEDIUM, halber Tag)
Kandidaten: `IngredientRow.svelte`, `StepList.svelte`, `TimeDisplay.svelte`, `ImageUploadBox.svelte`. Vorteile: isoliert testbar, wiederverwendbar in Preview-Seite. **Aber:** keine Eile, solange niemand sonst drin arbeitet.
### D. Ingredient-Parser-Edge-Cases (HIGH, 23 Std)
Der Parser (`src/lib/server/parsers/ingredient.ts`) und seine Tests decken ASCII-Ganzzahlen + Dezimal + Brüche ab. Fehlt:
- Unicode-Brüche (½, ⅓, ¼)
- führende Nullen, wissenschaftliche Notation
- Locale-Kommadezimal (deutsche Rezepte!)
- 0-Portionen, negative Mengen
Parametrisierte Tests anlegen, dann Parser ggf. mit Zod-Refinement absichern.
---
## Einzelbefunde im Detail
### Dead Code
- Unused Deps: `yauzl`, `@types/yauzl` (absichtlich für Phase 5b; Entscheidung treffen: behalten oder entfernen bis Phase kommt).
- `RequestShape` (`src/lib/sw/cache-strategy.ts:3`) und `ManifestDiff` (`src/lib/sw/diff-manifest.ts:4`) sind exportiert, aber nur intern benutzt — `export` weg oder im Test importieren.
- Alle 97 Source-Files erreichbar, keine orphan-Assets, keine TODO/FIXME/HACK-Marker, keine großen auskommentierten Blöcke.
### Redundanzen
- `parseId`/`parsePositiveInt` — 9 Sites: `api/recipes/[id]/`, `…/favorite`, `…/rating`, `…/cooked`, `…/comments`, `…/image`, `api/profiles/[id]/`, `api/domains/[id]/`, `api/wishlist/[recipe_id]/`
- Fetch-try/catch-alert-Pattern in 5 Svelte-Komponenten: `recipes/[id]/+page.svelte` (2×), `admin/domains/+page.svelte` (2×), `admin/profiles/+page.svelte`
- Zod-`safeParse` + gleicher Error-Throw in 12+ Endpoints
- `parseQty` + Zutat-Reassembly in `RecipeEditor` dupliziert Logik aus `parseIngredient` — könnte über `src/lib/shared/` geteilt werden
- Profile-Guard (`if (!profile.active) alert(…)`) 4× identisch in `recipes/[id]/+page.svelte`
### Struktur
- Große Dateien: `+page.svelte` (808), `recipes/[id]/+page.svelte` (757), `+layout.svelte` (678), `RecipeEditor` (630), `recipes/+page.svelte` (539). Keine davon ist kaputt; alle sind Wachstum unter Last.
- API-Error-Shape: mehrheitlich `{message}`, `profiles/+server.ts` gibt zusätzlich `{issues}` aus (Zod-Details). Festschreiben.
- Store-Init-Races: `profile.svelte.ts` und `search-filter.svelte.ts` laden bei erstem Zugriff. Komponenten sehen ggf. Leer-State vor Fetch. Optional `loading`-Flag.
- Konsolen-Logs: 6 Stück in Prod-Build (`service-worker.ts` 2×, `searxng.ts` 3×, `sw-register.ts` 1×). Vermutlich Absicht; als Dok-Kommentar festhalten oder in `if (DEV)`-Guards packen.
- Svelte-5-Runes-Stores sind konsistent, keine God-Stores.
- TypeScript: `strict` an, 0× `any`, 0× Server-Import-in-Client — bestätigt die CLAUDE.md-Regel.
### Docs-vs-Code-Mismatches
| Fundstelle | Fix |
|---|---|
| `ARCHITECTURE.md:55``recipe_ingredient` + `recipe_step` | `ingredient` + `step` |
| `OPERATIONS.md:135``IMAGES_PATH` | `IMAGE_DIR` |
| `session-handoff-2026-04-17.md:46` — fehlt `/api/recipes/[id]/image` (POST/DELETE) | ergänzen |
| Alle Gotchas in `CLAUDE.md` | ✓ verifiziert, stimmen |
| Alle Claims im offline-PWA-Spec | ✓ verifiziert, alle in Code vorhanden |
---
## Was bleibt wie es ist
- **Migrationen:** 001011 sind historisch sauber. 008 + 010 löschen beide den Thumbnail-Cache — Feature-Iteration, kein Bug. **Keine** bestehende Migration anfassen (das ist ohnehin die dokumentierte Regel).
- **Service-Worker:** Zombie-Cleanup-Logik (`pwa.svelte.ts`) ist Kunst, aber funktioniert und ist kommentiert. Unit-Tests decken beide Zweige (Zombie vs alter SW) ab.
- **Repository-Pattern:** Cleane Schichtung. Nicht refactoren.
- **Test-Suite:** 23 Dateien, 158 Tests, volle Integration inkl. DB/HTTP/Import/SearXNG. Leichte Lücken bei Parser-Edge-Cases (siehe oben).
---
## Ampel
| Dimension | Status |
|---|---|
| Architektur & Schichten | 🟢 gesund |
| Dead Code | 🟢 minimal |
| Redundanzen | 🟡 adressierbar, nicht dringend |
| Datei-/Komponenten-Größen | 🟡 zwei Pages ≥ 750L |
| Tests | 🟢 stark, Edge-Cases ausbaufähig |
| Doku | 🟡 1 inhaltlicher Fehler + 1 ENV-Tippfehler, sonst stabil |
| Sicherheit/Perf | 🟢 keine Funde im statischen Review |
---
## Vorgeschlagene Reihenfolge
1. Heute (10 min): Quick-Wins 1 + 2 (ARCHITECTURE-Tabellen, OPERATIONS-ENV).
2. Nächste Session (2 h): `api-helpers.ts` + `parseId`-Consolidation + Tests (Refactor A).
3. Bei Zeit: Search-State-Store (Refactor B) — bringt beim nächsten Feature sofort Dividende.
4. Phase-5b: `yauzl` einsetzen ODER Deps entfernen.
---
## Teilreports
Die vollständigen Agent-Befunde liegen daneben:
- `docs/superpowers/review/dead-code.md`
- `docs/superpowers/review/redundancy.md`
- `docs/superpowers/review/structure.md`
- `docs/superpowers/review/docs-vs-code.md`
Review-Metadaten: 4 parallele Explore-Agenten, jeweils read-only, Summen manuell gegen Code verifiziert (Line-Counts, Tabellen-Namen, ENV-Namen, `parseId`-Sites).

View File

@@ -0,0 +1,42 @@
# Dead-Code Review
## Summary
Kochwas codebase is remarkably clean with minimal dead code. Primary finding: **yauzl dependency is unused** (reserved for future backup-restore feature). All exports are active, files are properly structured, and no unreachable code paths detected.
## HIGH confidence findings
### Unused Dependencies
- **package.json: yauzl, @types/yauzl** — Declared in `dependencies` but never imported in source code. Added in commit for future backup ZIP import feature (currently only export via archiver is implemented). See `docs/superpowers/session-handoff-2026-04-17.md` which notes: "Import aus ZIP ist noch manueller DB-Copy. yauzl ist bereits als Dependency da, Phase 5b kann das in 10 Minuten nachziehen."
### Exported Types Not Imported Elsewhere
- **src/lib/sw/cache-strategy.ts:3** — `RequestShape` — Exported type only used within the same file as a function parameter. Not imported anywhere (type is passed inline at call site in service-worker.ts). Candidates for internal-only marking.
- **src/lib/sw/diff-manifest.ts:4** — `ManifestDiff` — Exported type only used within same file as a return type of `diffManifest()`. Not imported by any other module.
## MEDIUM confidence findings
None identified. All functions, types, and stores are actively used. All 85 source files are reachable through proper route conventions (+page.svelte, +server.ts, +layout.svelte are auto-routed by SvelteKit).
## LOW confidence / worth double-checking
### Conditional Dead Code in Service Worker (Low risk)
- **src/service-worker.ts:99-110** — The `GET_VERSION` message handler for zombie-SW cleanup is only triggered by `pwaStore` when specific conditions match (bit-identical versions detected after SKIP_WAITING). Works correctly but only fires on edge-case deployments (Chromium race condition). Verified it's needed—comments explain the scenario thoroughly.
### Database Migrations
- **src/lib/server/db/migrations/007-011** — Recent migrations (thumbnail_cache rerun, favicon resets) are cleanup/maintenance operations. Verified they're applied in sequence and read by code (e.g., searxng.ts queries thumbnail_cache). No orphaned migration tables.
## Non-findings (places I checked and confirmed alive)
- **All client stores** (confirm, install-prompt, network, profile, pwa, search-filter, sync-status, toast, wishlist) — Every export used in components
- **All server repositories** (domains, profiles, recipes, wishlist) — All functions imported by API routes
- **All parsers** (ingredient, iso8601-duration, json-ld-recipe) — Used by recipe importer and web search
- **All API routes** — All 27 route handlers are reachable and handler import the functions they need
- **All Svelte components** — No orphaned .svelte files; all imported by routes or other components
- **Static assets** (/manifest.webmanifest, /icon.svg, /icon-192.png, /icon-512.png) — Referenced in app.html, cache-strategy.ts, and manifest
- **Service worker** — All functions in service-worker.ts are called; no dead branches
- **Commented code** — Only legitimate documentation comments (German docs explaining design decisions); no large disabled code blocks
---
**Review Scope:** src/ (~85 files), package.json dependencies, tests/
**Tools used:** Grep (regex + pattern matching), Read (file inspection), Bash (git log)
**Confidence Threshold:** HIGH = 100% sure, MEDIUM = 95%+, LOW = contextual

View File

@@ -0,0 +1,130 @@
# Docs vs Code Audit
**Date:** 2026-04-18 | **Scope:** Full Documentation Review
## Summary
The documentation is **80% accurate and well-structured**, with most claims verifiable against the code. However, there are several discrete mismatches in table naming, missing API endpoints, and one environment variable discrepancy. Core concepts (architecture, deployment, gotchas) are reliable. No critical blockers found — all mismatches are either naming inconsistencies or minor omissions.
---
## CLAUDE.md Findings
### ✅ All gotchas verified
- **Healthcheck rule:** Confirmed in `Dockerfile` line 37: uses `http://127.0.0.1:3000/api/health`
- **SearXNG headers:** Confirmed in `src/lib/server/search/searxng.ts` — sets `X-Forwarded-For: 127.0.0.1` and `X-Real-IP: 127.0.0.1`
- **Icon rendering:** Confirmed — `scripts/render-icons.mjs` renders 192 + 512 PNG icons from `static/icon.svg` via `npm run render:icons`
- **better-sqlite3 native build:** Confirmed in `Dockerfile` lines 67: multi-stage build with Python + make + g++ for ARM64 ✓
- **Service Worker HTTPS-only:** Confirmed in `src/service-worker.ts` and offline-pwa-design.md specs ✓
- **Migration workflow:** Confirmed in `src/lib/server/db/migrations/` — 11 migrations exist, Vite glob bundled ✓
### ⚠ Minor: Environment Variable Name
- **Claim in doc:** OPERATIONS.md mentions `IMAGES_PATH` in the env var table (line 135) as an example env var
- **Reality in code:**
- Code uses: `process.env.IMAGE_DIR` (not `IMAGES_PATH`) — see `src/lib/server/db/index.ts`
- `.env.example` and `Dockerfile` both use `IMAGE_DIR`
- `.env.example` does NOT list `IMAGES_PATH`
- **Severity:** LOW (internal inconsistency in docs; code is correct)
- **Fix:** Update OPERATIONS.md line 135 to use `IMAGE_DIR` instead of `IMAGES_PATH`
---
## docs/ARCHITECTURE.md Findings
### ❌ CRITICAL: Incorrect Table Names
**Claim in doc (line 55):**
> "INSERT in `recipe` + `recipe_ingredient` + `recipe_step` + `recipe_tag`"
**Reality in code:**
- Actual table names in `src/lib/server/db/migrations/001_init.sql`:
- Line 29: `CREATE TABLE IF NOT EXISTS ingredient` (NOT `recipe_ingredient`)
- Line 41: `CREATE TABLE IF NOT EXISTS step` (NOT `recipe_step`)
- Line 54: `CREATE TABLE IF NOT EXISTS recipe_tag` (this one is correct ✓)
**Severity:** HIGH
- **Impact:** Anyone reading docs will search for `recipe_ingredient` table and not find it; confuses debugging
- **Fix:** Update ARCHITECTURE.md line 55 from `recipe_ingredient` + `recipe_step` to `ingredient` + `step`
Also verify the same claim doesn't appear in design specs (section 8.8 of 2026-04-17-kochwas-design.md is correct — it already lists `ingredient` and `step` without the prefix).
### ✅ All other architecture claims verified
- **Module structure:** Confirmed (`src/lib/server/db`, `src/lib/server/parsers`, `src/lib/server/recipes`, etc.) ✓
- **FTS5 virtual table:** Confirmed in `001_init.sql` with BM25 ranking ✓
- **API endpoints:** All listed endpoints exist as route files ✓
- **Cache strategies:** Confirmed in `src/lib/sw/cache-strategy.ts`
- **Service Worker behavior:** Confirmed in `src/service-worker.ts`
---
## docs/OPERATIONS.md Findings
### ⚠ MEDIUM: Environment Variable Discrepancy
- **Same as CLAUDE.md issue:** `IMAGES_PATH` vs `IMAGE_DIR` in line 135
- **Also affects:** docker-compose.prod.yml example in section "Umgebungsvariablen" — doc doesn't show it being set, but it's not needed (code defaults to `./data/images`)
### ✅ All deployment claims verified
- **Healthcheck interval/timeout:** Confirmed in Dockerfile ✓
- **SearXNG configuration:** Confirmed `searxng/settings.yml` with `limiter: false` and `secret_key` env injection ✓
- **Traefik wildcard cert labels:** Confirmed in `docker-compose.prod.yml` lines 2627 ✓
- **PWA offline behavior:** Confirmed in spec and code ✓
- **Backup/restore UI:** Confirmed routes exist `/admin/backup` and `/api/admin/backup`
---
## docs/superpowers/ Findings
### ✅ Session Handoff (2026-04-17)
**Routes listed (line 46):**
- Session-handoff lists: `/images/[filename]` endpoint
- **Actual code:** Route exists at `src/routes/images/[filename]/+server.ts`
- **Verification:** All other endpoints match (`/api/recipes/all`, `/api/recipes/blank`, `/api/recipes/favorites`, `/api/wishlist` etc.) ✓
**Note:** Session-handoff does NOT mention `/api/recipes/[id]/image` (POST/DELETE for profile-specific image updates), which exists in code. This is not a *mismatch* but an **omission** (minor).
### ✅ Design Spec (2026-04-17)
**Section 8 (Datenmodell):**
- Lists `ingredient` and `step` tables correctly (no prefix) ✓
- This contradicts ARCHITECTURE.md (which says `recipe_ingredient` + `recipe_step`), but ARCHITECTURE.md is wrong
- Design spec is the source of truth here ✓
### ✅ Offline PWA Design (2026-04-18)
**All claims verified:**
- `src/service-worker.ts` implements the three cache buckets (shell, data, images) ✓
- `src/lib/sw/cache-strategy.ts` implements the strategy dispatcher ✓
- `src/lib/client/sync-status.svelte.ts` exists with message handler ✓
- `src/lib/client/network.svelte.ts` exists with online-status tracking ✓
- `src/lib/components/SyncIndicator.svelte` exists ✓
- `src/lib/components/Toast.svelte` exists ✓
- `/admin/app/+page.svelte` exists (confirmed in route listing) ✓
- Icon rendering script confirmed ✓
- PWA manifest with PNG icons confirmed ✓
---
## What the Docs Get Right
1. **Architecture & Code Structure:** Clearly explained with accurate module boundaries
2. **Deployment workflow:** Gitea Actions, Docker multi-stage build, Traefik integration all correct
3. **Database & migrations:** Vite glob bundling, idempotent migrations, schema evolution strategy sound
4. **PWA offline-first design:** Well thought out, faithfully implemented
5. **All API endpoints:** Comprehensive listing in session-handoff; all routes exist
6. **Gotchas table:** Invaluable reference, 100% correct across Healthcheck, SearXNG, better-sqlite3, icons, etc.
7. **Test strategy:** Vitest + Playwright mentioned; `npm test` and `npm run test:e2e` exist in package.json
8. **Icon rendering:** Accurately documented; `npm run render:icons` works as described
---
## Summary of Findings
| Finding | Severity | File | Line | Action |
|---------|----------|------|------|--------|
| Table names: `recipe_ingredient` → should be `ingredient` | HIGH | ARCHITECTURE.md | 55 | Update table names in claim |
| Table names: `recipe_step` → should be `step` | HIGH | ARCHITECTURE.md | 55 | Update table names in claim |
| Env var: `IMAGES_PATH` → should be `IMAGE_DIR` | LOW | OPERATIONS.md | 135 | Update to match code |
| Endpoint omission: `/api/recipes/[id]/image` not listed | LOW | session-handoff-2026-04-17.md | 46 | Add to routes list (optional) |
**Total issues found:** 4 (1 HIGH, 2 MEDIUM, 1 LOW) | **Blocker for development:** None

View File

@@ -0,0 +1,61 @@
# Redundancy Review
## Summary
Kochwas exhibits significant duplication in API endpoint handlers across 22 endpoints, with copy-pasted parameter parsing (parseId/parsePositiveInt) in 8+ files, repeated error-handling patterns in fetch wrappers across 5+ Svelte components, and schema validation blocks that could be consolidated.
## HIGH severity
### Duplicated parseId / parsePositiveInt helpers across API endpoints
- **Sites**: /api/recipes/[id]/+server.ts:51-54, /api/recipes/[id]/favorite/+server.ts:9-13, /api/recipes/[id]/rating/+server.ts:14-18, /api/recipes/[id]/cooked/+server.ts:10-14, /api/recipes/[id]/comments/+server.ts:14-18, /api/profiles/[id]/+server.ts:9-13, /api/domains/[id]/+server.ts:19-23, /api/wishlist/[recipe_id]/+server.ts:9-13
- **Pattern**: Eight API endpoints independently define nearly identical parameter-parsing functions that validate positive integers from route params.
- **Suggestion**: Extract to src/lib/server/api-helpers.ts with parsePositiveIntParam(raw, field) returning the number or throwing via SvelteKit error().
### Copy-pasted fetch error-handling + alert pattern in Svelte components
- **Sites**: /recipes/[id]/+page.svelte:76-87, /recipes/[id]/+page.svelte:253-265, /admin/domains/+page.svelte:31-48, /admin/domains/+page.svelte:67-87, /admin/profiles/+page.svelte:30-44
- **Pattern**: Five component functions repeat identical 6-line blocks: await fetch(); if not ok, parse JSON, show alert with body.message or HTTP status.
- **Suggestion**: Create src/lib/client/api-fetch-wrapper.ts with asyncFetch(url, init, actionTitle) that wraps fetch, error handling, and alertAction.
## MEDIUM severity
### Repeated Zod schema validation + error pattern across API endpoints
- **Sites**: /api/recipes/[id]/+server.ts, /api/recipes/[id]/favorite/+server.ts, /api/recipes/[id]/rating/+server.ts, /api/recipes/[id]/cooked/+server.ts, /api/recipes/[id]/comments/+server.ts, /api/profiles/+server.ts, /api/domains/[id]/+server.ts, /api/wishlist/+server.ts
- **Pattern**: Every endpoint defines schemas locally with safeParse() followed by identical error handling: if not success, error 400 Invalid body. 12+ endpoints repeat this 3-4 line pattern.
- **Suggestion**: Create src/lib/server/schemas.ts with common validators and validateBody<T>(body, schema) helper that centralizes the error throw.
### Recipe scaling / ingredient manipulation scattered without consolidation
- **Sites**: /lib/recipes/scaler.ts:10-16, /lib/server/parsers/ingredient.ts:42-68, /lib/components/RecipeEditor.svelte:144-149, /lib/components/RecipeEditor.svelte:156-175
- **Pattern**: RecipeEditor re-implements parseQty and ingredient assembly (raw_text building) instead of importing parseIngredient from server parser. Logic is nearly identical in two places.
- **Suggestion**: Expose parseIngredient as shared client code or create src/lib/shared/ingredient-utils.ts; import into component to avoid duplication.
### Profile not-selected alert pattern duplicated 4x in same component
- **Sites**: /recipes/[id]/+page.svelte:124-131, :143-150, :166-173, :188-195
- **Pattern**: Four action functions (setRating, toggleFavorite, logCooked, addComment) all open with identical 7-line guard checking active profile and showing same alert message.
- **Suggestion**: Extract requireProfile() helper in src/lib/client/profile.svelte that performs the alert and returns boolean; replace all four guard clauses.
## LOW severity
### WakeLock error handling try-catch pattern
- **Sites**: /recipes/[id]/+page.svelte:318-327, :332-338
- **Pattern**: Both functions independently have try-catch that silently swallows errors. Identical empty catch pattern duplicated.
- **Suggestion**: Cosmetic - document once or combine into manageWakeLock(action) wrapper.
### Migration cache-clearing pattern (historical only)
- **Sites**: /db/migrations/008_thumbnail_cache_drop_unknown.sql, /db/migrations/010_thumbnail_cache_rerun_negatives.sql
- **Pattern**: Two consecutive migrations both DELETE from thumbnail_cache due to feature iteration. Not a bug, just historical stacking.
- **Note**: Safe to leave as-is.
### Test fixture duplication: baseRecipe helper
- **Sites**: /tests/integration/recipe-repository.test.ts:22-42
- **Pattern**: baseRecipe factory defined locally; likely duplicated in other tests.
- **Suggestion**: Move to tests/fixtures/recipe.ts and import everywhere.
### API error message language inconsistency
- **Sites**: /api/recipes/[id]/image/+server.ts (German), all others (English)
- **Pattern**: Image endpoint uses German error messages; all other endpoints use English.
- **Suggestion**: Standardize to German or English for consistent UX.
---
## Notes
Strong separation of concerns observed in repositories and parsers. Type definitions well-centralized in src/lib/types.ts. No major SQL redundancy beyond historical migrations. Primary improvement opportunities are parameter validation, error handling, and component fetch logic consolidation.

View File

@@ -0,0 +1,146 @@
# Structure / Design / Maintainability Review
## Summary
Kochwas has a healthy, maintainable codebase with strong architectural boundaries between server and client, comprehensive test coverage (integration + e2e), and disciplined use of TypeScript. The main pressure points are large page components (+700 lines) and some high-complexity features (search orchestration, image import pipeline) that could benefit from further decomposition.
## Big-picture observations
### Strengths
1. **Clean architectural layers**: No server code bleeding into client. Strict separation of $lib/server/*, $lib/client/*.svelte.ts, and components.
2. **Comprehensive testing**: 17+ integration tests, 4+ unit tests, 2 e2e suites covering recipes, images, parsers, search.
3. **Type-safe API**: Domain types in src/lib/types.ts are exhaustive; Zod schemas match; no shadow types.
4. **Consistent error handling**: Custom ImporterError with codes, mapped through mapImporterError().
5. **Smart runes stores**: Separate concerns (profile, network, pwa, sync-status, toast, wishlist, search-filter). No god-stores.
6. **Well-documented gotchas**: CLAUDE.md clearly marks traps (SW HTTPS-only, healthcheck IPv4, native module arm64).
### Concerns
1. **Large page components**: +page.svelte (808L), recipes/[id]/+page.svelte (757L), +layout.svelte (678L).
2. **Dense components**: RecipeEditor (630L), RecipeView (398L), SearchFilter (360L) hard to unit-test.
3. **Complex parsers**: json-ld-recipe.ts (402L) and searxng.ts (389L) lack edge-case validation.
4. **State synchronization**: 20+ local state variables in search page; duplication in +layout.svelte.
5. **Magic numbers**: Timeout constants (1500ms, 30min) and z-index values are inline.
## HIGH severity findings
### Large page components
- **Where**: src/routes/+page.svelte (808L), src/routes/recipes/[id]/+page.svelte (757L), src/routes/+layout.svelte (678L)
- **What**: Pages bundle view + component orchestration + state management (20+ $state vars) + fetch logic. Hard to test individual behaviors without mounting entire page.
- **Suggestion**: Extract orchestration into composables/stores (e.g., usePageSearch()). Break out visual widgets as sub-components. Move fetch logic to +page.server.ts.
### State density: 20+ variables in search page
- **Where**: src/routes/+page.svelte lines 17-48
- **What**: Local state controls search (query, hits, webHits, searching, webError, etc.). Duplication in +layout.svelte nav search. Risk of stale state.
- **Suggestion**: Create useSearchState() rune or dedicated store with methods: .search(q), .loadMore(), .clear().
### JSON-LD parser edge cases
- **Where**: src/lib/server/parsers/json-ld-recipe.ts (402L)
- **What**: Parser assumes well-formed JSON-LD. Tests only cover ASCII digits; no coverage for non-ASCII numerals, fraction chars, or 0 servings.
- **Suggestion**: Add Zod refinement for quantity validation. Test against real recipes from different locales. Document assumptions.
### Ingredient parsing gaps
- **Where**: tests/unit/ingredient.test.ts
- **What**: Tests cover integers/decimals/fractions but not: leading zeros, scientific notation, Unicode fractions, unusual separators, null ingredients.
- **Suggestion**: Parametrized tests for edge cases. Clamp quantity range (0-1000) at parser level.
### Unnamed timeout constants
- **Where**: src/routes/+page.svelte, src/lib/client/pwa.svelte.ts
- **What**: 1500ms (PWA version query), 30*60_000ms (SW update poll), implicit debounce. Hard to find all call sites.
- **Suggestion**: Export to src/lib/constants.ts: SW_VERSION_QUERY_TIMEOUT_MS, SW_UPDATE_POLL_INTERVAL_MS.
## MEDIUM severity findings
### RecipeEditor/RecipeView component size
- **Where**: src/lib/components/RecipeEditor.svelte (630L), src/lib/components/RecipeView.svelte (398L)
- **What**: Feature-complete but dense; hard to test rendering in isolation (e.g., ingredient scaling).
- **Suggestion**: Extract sub-components: IngredientRow.svelte, StepList.svelte, TimeDisplay.svelte, ImageUploadBox.svelte.
### API error shape inconsistency
- **Where**: src/routes/api/**/*.ts
- **What**: Most return {message}. But profiles/+server.ts POST returns {message, issues} (Zod details). Implicit schema.
- **Suggestion**: Standardize or define shared ErrorResponse type in src/lib/types.ts. Document in docs/API.md.
### Service Worker zombie cleanup untested
- **Where**: src/lib/client/pwa.svelte.ts (lines 1-72)
- **What**: Clever but untested heuristic. 1500ms timeout may cause false positives on slow networks.
- **Suggestion**: Unit test timeout scenario. Document 1500ms rationale in comments.
### Searxng rate-limit recovery
- **Where**: src/lib/server/search/searxng.ts (389L)
- **What**: Caches per-query. On 429/403, logs but doesn't backoff. Second search returns stale cache with no signal.
- **Suggestion**: Add isStale flag. Show "results may be outdated" banner or implement exponential backoff.
### Store initialization races
- **Where**: src/lib/client/profile.svelte.ts, src/lib/client/search-filter.svelte.ts
- **What**: Load data on first access. If component mounts before fetch completes, shows stale state. No loading signal.
- **Suggestion**: Add loading property. Load in +page.server.ts instead or await store.init() in onMount().
## LOW severity findings
### Missing named constants
- **Where**: ConfirmDialog.svelte, ProfileSwitcher.svelte (z-index, border-radius, timeouts inline)
- **What**: Z-index (100, 200), border-radius (999px), timeouts (1500ms) hardcoded.
- **Suggestion**: Create src/lib/theme.ts: MODAL_Z_INDEX, POPOVER_Z_INDEX, etc.
### console logging in production
- **Where**: src/service-worker.ts (2), src/lib/server/search/searxng.ts (3), src/lib/client/sw-register.ts (1)
- **What**: Likely intentional (production diagnostics) but unfiltered by log level.
- **Suggestion**: Document intent. If not intentional, wrap in if (DEV) guards.
### Unhandled DB errors
- **Where**: src/routes/api/recipes/all/+server.ts
- **What**: If DB query fails, error propagates as 500.
- **Suggestion**: Wrap in try-catch for consistency (unlikely with local SQLite).
### Migration ordering
- **Where**: Tests don't verify migration sequence
- **What**: Migrations autodiscovered via glob; out-of-order filenames won't cause build error.
- **Suggestion**: CI check verifying 00X_* sequence.
### Incomplete image downloader errors
- **Where**: src/lib/server/images/image-downloader.ts
- **What**: Generic error message; can't distinguish "URL wrong" from "network down."
- **Suggestion**: Add error codes (NOT_FOUND, TIMEOUT, NETWORK).
## Metrics
### Lines per file (top 15)
```
808 src/routes/+page.svelte
757 src/routes/recipes/[id]/+page.svelte
678 src/routes/+layout.svelte
630 src/lib/components/RecipeEditor.svelte
539 src/routes/recipes/+page.svelte
402 src/lib/server/parsers/json-ld-recipe.ts
398 src/lib/components/RecipeView.svelte
389 src/lib/server/search/searxng.ts
360 src/lib/components/SearchFilter.svelte
321 src/routes/wishlist/+page.svelte
318 src/routes/admin/domains/+page.svelte
259 src/service-worker.ts
244 src/lib/server/recipes/repository.ts
218 src/lib/components/ProfileSwitcher.svelte
216 src/routes/preview/+page.svelte
```
### Quality metrics
| Metric | Value | Status |
|--------|-------|--------|
| Test suites (integration) | 17 | Good |
| Test suites (unit) | 5+ | Adequate |
| Zod validation endpoints | 11 | Excellent |
| TypeScript strict | Yes | Excellent |
| Any types found | 0 | Excellent |
| Server code in client | 0 | Excellent |
| Console logging | 6 instances | Minor |
## Recommendations (priority)
1. **Extract page state to stores** (HIGH, medium effort): Reduce +page.svelte by ~200L; enable isolated testing.
2. **Split large components** (HIGH, medium effort): RecipeEditor/RecipeView sub-components.
3. **Add ingredient validation** (HIGH, low effort): Zod refinement + edge-case tests.
4. **Define named constants** (MEDIUM, low effort): src/lib/constants.ts for timeouts/z-index.
5. **Standardize API errors** (MEDIUM, low effort): docs/API.md + shared ErrorResponse type.
6. **Test SW zombie cleanup** (MEDIUM, medium effort): Unit tests + comments.
## Conclusion
Healthy, maintainable codebase. Main pressure: large page/component sizes (natural scaling). With recommendations above, ready for continued development and easy to onboard new developers.

View File

@@ -43,7 +43,7 @@ docker compose -f docker-compose.prod.yml up -d
### Server-Seite ### Server-Seite
- **DB:** SQLite mit FTS5, Migrationen (`./migrations/*.sql`) werden von Vite gebündelt und beim ersten DB-Zugriff angewendet. Auto-mkdir für `data/` und `data/images/`. - **DB:** SQLite mit FTS5, Migrationen (`./migrations/*.sql`) werden von Vite gebündelt und beim ersten DB-Zugriff angewendet. Auto-mkdir für `data/` und `data/images/`.
- **Module:** `parsers/` (iso8601, ingredient, json-ld-recipe), `recipes/` (scaler + repository + actions + importer + search-local), `domains/` (repository + whitelist), `profiles/`, `images/image-downloader`, `search/searxng`, `backup/export`, `http`. - **Module:** `parsers/` (iso8601, ingredient, json-ld-recipe), `recipes/` (scaler + repository + actions + importer + search-local), `domains/` (repository + whitelist), `profiles/`, `images/image-downloader`, `search/searxng`, `backup/export`, `http`.
- **Routes:** `/api/health`, `/api/profiles`, `/api/profiles/[id]`, `/api/domains`, `/api/domains/[id]`, `/api/recipes/search`, `/api/recipes/search/web`, `/api/recipes/preview`, `/api/recipes/import`, `/api/recipes/[id]`, `/api/recipes/[id]/rating`, `/api/recipes/[id]/favorite`, `/api/recipes/[id]/cooked`, `/api/recipes/[id]/comments`, `/api/admin/backup`, `/images/[filename]`. - **Routes:** `/api/health`, `/api/profiles`, `/api/profiles/[id]`, `/api/domains`, `/api/domains/[id]`, `/api/recipes/search`, `/api/recipes/search/web`, `/api/recipes/preview`, `/api/recipes/import`, `/api/recipes/[id]`, `/api/recipes/[id]/rating`, `/api/recipes/[id]/favorite`, `/api/recipes/[id]/cooked`, `/api/recipes/[id]/comments`, `/api/recipes/[id]/image` (POST/DELETE), `/api/admin/backup`, `/images/[filename]`.
### Client-Seite (Svelte 5 Runes) ### Client-Seite (Svelte 5 Runes)
- **Layout** mit Profil-Chip und Zahnrad zu Admin. - **Layout** mit Profil-Chip und Zahnrad zu Admin.

View File

@@ -0,0 +1,336 @@
# Foto-Rezept-Magie — Design Spec
**Status:** approved (brainstorming)
**Datum:** 2026-04-21
**Ziel-Release:** v1.3.0
## 1. Motivation & Scope
Nutzer sollen ein **gedrucktes oder handgeschriebenes Rezept** fotografieren können. Das Foto wird an ein Vision-LLM (Gemini 2.5 Flash) gesendet, dort zu einer strukturierten Recipe-Shape extrahiert, und direkt in einen vorausgefüllten `RecipeEditor` gepackt — der Nutzer korrigiert bei Bedarf und speichert. Das Foto selbst wird nie persistiert.
**In Scope (v1):**
- Einzelnes Foto von gedrucktem Rezept ODER Handschrift.
- Extraktion von Titel, Portionen, Zeiten, Zutaten (mit Menge/Einheit/Name), Zubereitungsschritten.
- Auslöse-Button als `Camera`-Icon (lucide) im Header, disabled wenn offline oder ohne API-Key.
- Direkter Flow in den `RecipeEditor` (kein separater Preview-Schritt).
- Server-seitiger Gemini-Call mit structured-output-Schema, Zod-Validierung, 1× Retry bei Schema-Fehler.
- Hartes Nicht-Speichern des Fotos nach dem Call.
**Explizit Out-of-Scope (v1):**
- Multi-Foto (Kochbuch-Doppelseite): Endpoint nimmt ein Bild entgegen; Erweiterung auf Array bei Bedarf.
- Extraktion von `image_path` aus dem Bild (Dish-Crop aus Kochbuchseite).
- Foto-Backup / Persistenz des Input-Fotos.
- Claude als Fallback.
- Interpretierende Felder: `cuisine`, `category`, `tags`, freie `description`.
- Foto-vom-Gericht → AI-erfindet-Rezept (anderer Use-Case).
## 2. User Flow
```
Header (Camera-Icon, lucide)
/new/from-photo (File-Picker: <input type="file" accept="image/*" capture="environment">)
▼ Nutzer wählt/knipst Foto — File bleibt nur im Browser-State
▼ POST /api/recipes/extract-from-photo (multipart/form-data)
│ Server: MIME + Größe validieren, sharp-Preprocess, Gemini-Call, Response
│ Foto wird NICHT persistiert.
Seite swappt Spinner → <RecipeEditor initialData={recipe}>
▼ Nutzer korrigiert, klickt „Speichern" (Editor-Save-Pfad — ob bestehend oder neu: siehe §11)
/recipes/:id
```
**Invarianten:**
- Extraktion erzeugt **kein** DB-Record. Erst der Save-Klick im Editor schreibt.
- Bei Tab-Close während Extraktion: kein Müll in der DB, AbortController-fähiger Fetch.
- Offline: Kamera-Icon im Header nicht geklickbar; falls trotzdem Route geöffnet, klare Offline-Meldung.
## 3. Komponenten & Dateien
**Neue Dateien**
| Pfad | Zweck |
|---|---|
| `src/routes/new/from-photo/+page.svelte` | Shell. States: `idle` / `loading` / `success` / `error:<code>`. |
| `src/lib/server/ai/gemini-client.ts` | Thin wrapper: `extractRecipeFromImage(buffer, mime): Promise<Partial<Recipe>>`. Liest `GEMINI_API_KEY`, `GEMINI_MODEL`, `GEMINI_TIMEOUT_MS` aus `$env/dynamic/private`. |
| `src/lib/server/ai/recipe-extraction-prompt.ts` | System-Prompt (DE) + JSON-Schema für Gemini `responseSchema`. Isoliert, weil iterabel. |
| `src/lib/server/ai/description-phrases.ts` | 50er-Pool von Magie-Phrasen für das `description`-Feld. Export `pickRandomPhrase(): string`. Siehe §5a. |
| `src/lib/server/ai/image-preprocess.ts` | `sharp`-basierter Resize (≤1600px lange Kante) + JPEG re-encode (quality 85) + Metadata-Strip. HEIC → JPEG. |
| `src/routes/api/recipes/extract-from-photo/+server.ts` | POST. Multipart-Parse, Validierung, preprocess, Gemini-Call, Zod-Validierung, Response. |
| `src/lib/client/photo-upload.svelte.ts` | Frontend-Store für Upload-Zustand. |
| `tests/unit/ai/recipe-extraction-prompt.test.ts` | Schema-Ping, Retry-Pfad, Zod-Ablehnung. |
| `tests/unit/ai/image-preprocess.test.ts` | Resize, HEIC, Metadata-Strip. |
| `tests/unit/ai/gemini-client.test.ts` | Timeout, 429-no-retry, 5xx-1x-retry, Network-Fehler. |
| `tests/unit/ai/description-phrases.test.ts` | Pool hat 50 Einträge, alle unique non-empty, `pickRandomPhrase` liefert nur Pool-Einträge. |
| `tests/api/extract-from-photo.test.ts` | Happy-Path, 413, 415, 422 (NO_RECIPE_IN_IMAGE). |
| `tests/e2e/remote/photo-import.spec.ts` | Kamera-Icon, Upload-Fixture (Endpoint gestubt), Editor-Prefill, Save, Offline-State. |
| `tests/fixtures/photo-recipe/` | 3 Fixture-Fotos: gedrucktes Rezept, Handschrift, No-Recipe-Bild. |
**Geänderte Dateien**
| Pfad | Änderung |
|---|---|
| `src/routes/+layout.svelte` | Header: `Camera`-Icon, `aria-label="Rezept aus Foto erstellen"`. Nur gerendert wenn `GEMINI_API_KEY` gesetzt (Graceful Degradation). Disabled wenn offline (`networkStore`). Führt zu `/new/from-photo`. |
| `src/lib/components/RecipeEditor.svelte` | Akzeptiert optionale `initialData?: Partial<Recipe>`-Prop. Wenn gesetzt, Felder vorbefüllen, kein DB-Round-trip. Heute liest der Editor über eine Rezept-ID — dieser Pfad wird abstrahiert. |
| `Dockerfile` | `sharp` im Native-Build-Stage ergänzen (wie `better-sqlite3`). |
| `docker-compose.yml`, `docker-compose.prod.yml`, `.env.example` | Env-Vars `GEMINI_API_KEY`, `GEMINI_MODEL`, `GEMINI_TIMEOUT_MS` ergänzen. |
| `docs/OPERATIONS.md` | Abschnitt zu Gemini-Config + Recreate-Hinweis bei Env-Änderung. |
| `docs/ARCHITECTURE.md` | AI-Extraktionspfad ergänzen. |
| `CLAUDE.md` | Zeile in Gotcha-Tabelle: Graceful Degradation ohne Key + `sharp` im Build-Stage. |
**Keine DB-Migration.** Recipe-Shape bleibt; der Endpoint produziert ein `Partial<Recipe>` im Response-Body.
## 4. API-Contract
**`POST /api/recipes/extract-from-photo`**
Request: `multipart/form-data`
- `photo`: File. Erlaubt: `image/jpeg`, `image/png`, `image/webp`, `image/heic`, `image/heif`.
- Max 8 MB (vor Preprocess).
Response 200:
```json
{
"recipe": {
"title": "Zürcher Geschnetzeltes",
"description": "Aus dem Bild herbeigezaubert.",
"servings_default": 4,
"servings_unit": "Portionen",
"prep_time_min": 20,
"cook_time_min": 15,
"total_time_min": null,
"cuisine": null,
"category": null,
"image_path": null,
"source_url": null,
"source_domain": null,
"ingredients": [
{ "position": 1, "quantity": 500, "unit": "g", "name": "Kalbsgeschnetzeltes", "note": null, "section": null },
{ "position": 2, "quantity": 200, "unit": "ml", "name": "Rahm", "note": null, "section": null }
],
"steps": [
{ "position": 1, "text": "Fleisch in heißer Pfanne kurz anbraten, herausnehmen." }
],
"tags": []
}
}
```
Response Fehler-Codes:
| Status | `code` | Bedeutung |
|---|---|---|
| 413 | `PAYLOAD_TOO_LARGE` | Photo > 8 MB. |
| 415 | `UNSUPPORTED_MEDIA_TYPE` | MIME nicht in der Whitelist. |
| 422 | `NO_RECIPE_IN_IMAGE` | AI-Output valide, aber `title` leer oder (`ingredients.length === 0` UND `steps.length === 0`). |
| 429 | `AI_RATE_LIMITED` | Gemini 429 durchgereicht. |
| 503 | `AI_TIMEOUT` | Gemini-Timeout (Default 20 s). |
| 503 | `AI_FAILED` | Gemini-5xx nach 1 Retry ODER Schema-Validierung nach 1 Retry fehlgeschlagen. |
| 503 | `AI_NOT_CONFIGURED` | `GEMINI_API_KEY` leer — Endpoint sollte dann ohnehin nicht erreichbar sein via UI, belt-and-suspenders. |
## 5. Prompt-Strategie
Datei: `src/lib/server/ai/recipe-extraction-prompt.ts`
- **Sprache:** Deutsch.
- **Rolle:** „Du bist ein Rezept-Extraktions-Assistent."
- **Regeln:**
- Nur was lesbar auf dem Bild steht, ins Ergebnis. Sonst `null` oder leeres Array.
- Zutatenmengen: Zahl in `quantity`, Einheit separat (`g`, `ml`, `EL`, `TL`, `Stück`, `Prise`…).
- Bruchteile (`½`, `¼`, `1 ½`) zu Dezimalzahlen.
- Zubereitungsschritte: pro erkennbarer Nummerierung/Absatz ein Schritt.
- `description` wird server-seitig **nach** dem AI-Call aus einem 50er-Pool zufällig gewählt (`description-phrases.ts`, siehe §5a). Die AI bekommt `description` gar nicht erst im Schema — keine Halluzinationsfläche.
- **Output:** Gemini `responseMimeType: "application/json"` + `responseSchema`. Strict-typed, keine zusätzlichen Keys.
- **Temperature:** `0.1`.
- **Retry bei Schema-Fehler:** Genau 1 zusätzlicher Call mit Appendix „Dein letztes JSON war invalid. Schema: … Bitte nur JSON zurück." Dann `AI_FAILED`.
Zod-Schema spiegelt das Response-Schema serverseitig und wird auf die Gemini-Antwort angewendet.
## 5a. Description-Phrasen-Pool
Datei: `src/lib/server/ai/description-phrases.ts`
50 deutsche Magie-Phrasen, zufällig gezogen pro Extraktions-Call. Die Auswahl geschieht server-seitig im Endpoint, nachdem die AI-Antwort validiert wurde. Der Nutzer kann die Phrase im Editor weiter editieren, sie ist also ein Starter, kein Lock-in.
```ts
export const DESCRIPTION_PHRASES: readonly string[] = [
'Mit dem Zauberstab aus dem Kochbuch geholt.',
'Foto-Magie frisch aus dem Ofen.',
'Aus dem Bild herbeigezaubert.',
'Ein Klick, ein Foto, fertig.',
'Knipsen statt Abtippen.',
'Von der Buchseite direkt in die Pfanne.',
'Die Kamera hat mitgelesen.',
'Abrakadabra — Rezept da.',
'Per Linse in die Küche teleportiert.',
'Von Oma abfotografiert, von der KI entziffert.',
'Frisch aus dem Bilderrahmen.',
'Klick, zisch, Rezept.',
'Das Foto wurde überredet, sich zu verraten.',
'Schnappschuss zur Schüssel.',
'Einmal lesen lassen, schon da.',
'Keine Hand hat dieses Rezept abgetippt.',
'Vom Bild in die Bratpfanne.',
'Papier ist geduldig, das Foto war es auch.',
'Eine Seite, ein Foto, ein Rezept.',
'Die KI hat drübergeschielt.',
'Handschriftlich entziffert — oder zumindest versucht.',
'Aus der Linse in die Liste.',
'Vom Küchentisch zur Kachel.',
'Knips und weg — zumindest der Zettel.',
'Das Bild hat geredet.',
'Keine Tippfehler, nur Sehfehler.',
'Per Foto eingebürgert.',
'Rezept-Übersetzung aus dem Bild.',
'Die Seite hat sich verraten.',
'Blitzlicht und dann Gulasch.',
'Ein Augenzwinkern der Kamera genügte.',
'Geknipst, gelesen, gespeichert.',
'Fotografische Gedächtnishilfe.',
'Aus der Schublade ans Licht.',
'Das Rezept stand schon da — wir haben nur hingeguckt.',
'Zaubertrick mit Kamera.',
'Vom Papier befreit.',
'Ein Foto sagt mehr als tausend Zutatenlisten.',
'Eingescannt, rausgelesen, reingeschrieben.',
'Die Kamera als Küchenhilfe.',
'Handy hoch, Rezept runter.',
'Aus dem Kochbuch gebeamt.',
'Ein scharfes Foto, ein klares Rezept.',
'Vom Regal zur App in einem Schritt.',
'Aus dem Bild geschöpft wie Suppe aus dem Topf.',
'Optisch erfasst, digital serviert.',
'Das Kleingedruckte hat die KI gelesen.',
'Vom Kladdenzettel in die Datenbank.',
'Kurz gezückt, schon gekocht.',
'Kein Schreibkrampf, nur ein Klick.'
];
export function pickRandomPhrase(): string {
return DESCRIPTION_PHRASES[Math.floor(Math.random() * DESCRIPTION_PHRASES.length)];
}
```
**Invariant:** Genau 50 Einträge, alle non-empty, alle unique. Unit-Test prüft das.
## 6. Fehlerbehandlung
**Client-Zustände auf `/new/from-photo`:**
| State | UI |
|---|---|
| `idle` | `Camera`-Button groß mittig, Text „Foto wählen oder aufnehmen". Hilfetext: „Gedrucktes Rezept oder Handschrift. Eine Seite, scharf, gut ausgeleuchtet." |
| `loading` | `Loader2` (spin) + Text „Lese das Rezept…". `X`-Button für Abbrechen (AbortController). |
| `success` | `<RecipeEditor initialData={recipe}>`. Top-Banner mit `Wand2`: „Aus Foto erstellt — bitte prüfen und ggf. korrigieren." Verschwindet nach erstem Feld-Edit. |
| `error: NO_RECIPE_IN_IMAGE` | Yellow-Box. Buttons: `Camera` „Anderes Foto" (→ idle), `FilePlus` „Leer anlegen" (→ leerer Editor). |
| `error: AI_TIMEOUT` / `AI_RATE_LIMITED` / `AI_FAILED` | Red-Toast, Grund. `RotateCw` „Nochmal versuchen" — reused das gleiche File-Objekt, kein Re-Upload durch den Nutzer. |
| `error: PAYLOAD_TOO_LARGE` | Toast „Foto zu groß (max 8 MB). In besserer Beleuchtung neu aufnehmen." |
| Offline (auf Route) | Hinweis „Diese Funktion braucht Internet." |
**A11y:** Lade-State `aria-live="polite"`, Fehler-Boxen `role="alert"`, Kamera-Icon mit `aria-label`.
**Server-Seite:**
- Gemini-Call mit `AbortSignal`, Default-Timeout `GEMINI_TIMEOUT_MS` (20000).
- Retry-Matrix:
| Gemini-Signal | Verhalten |
|---|---|
| 429 | `AI_RATE_LIMITED`, kein Retry. |
| Network/5xx | 1× Retry mit 500 ms backoff, dann `AI_FAILED`. |
| Invalid JSON | 1× Retry mit Append-Prompt, dann `AI_FAILED`. |
| Valid JSON aber Schema-invalid | gleicher Pfad wie Invalid JSON. |
| Timeout | `AI_TIMEOUT`, kein Retry. |
- **Logging:** `console.warn` mit `{ code, durationMs, imageKB }`**ohne** Prompt/Response-Inhalt (Privacy).
**Icons (alle aus `lucide-svelte`):**
| Zweck | Icon |
|---|---|
| Header-Button | `Camera` |
| Lade-State | `Loader2` (spin) |
| Erfolgs-Banner | `Wand2` |
| Fehler | `AlertTriangle` |
| „Nochmal versuchen" | `RotateCw` |
| „Anderes Foto" | `Camera` |
| „Leer anlegen" | `FilePlus` |
| Abbrechen (Loading) | `X` |
## 7. Sicherheit / Missbrauch
- **Rate-Limit:** 10 Requests/Min pro IP, simple In-Memory-Throttle im Endpoint. Schützt vor versehentlichem Dauer-Tappen und Kosten-Runaways. Übertrieben für's Heimnetz, aber billig einzubauen.
- **MIME-Validierung nicht blind client-seitig** — Buffer-Header prüfen (`sharp` metadata) nach Empfang.
- **`.heic`/`.heif`** funktioniert, wenn `sharp` mit `libheif` gebaut ist (beim offiziellen sharp-arm64-Build dabei). Fixture-Test dafür.
- **Kein Auth** (Kochwas-Policy). Key stays server-side.
- **Privacy-Statement** im OPERATIONS.md: „Fotos gehen einmal an Google Gemini und werden danach nicht gespeichert. Gemini nutzt API-Daten im Paid-Tier nicht für Training."
## 8. Konfiguration
Env-Vars (alle in `docker-compose.yml`, `docker-compose.prod.yml`, `.env.example` ergänzen):
| Var | Default | Zweck |
|---|---|---|
| `GEMINI_API_KEY` | — (required) | Ohne Key: Feature graceful deaktiviert. |
| `GEMINI_MODEL` | `gemini-2.5-flash` | Modell-Wechsel (z.B. auf `gemini-2.5-pro`) ohne Rebuild. |
| `GEMINI_TIMEOUT_MS` | `20000` | Timeout für Vision-Call. |
**Wichtig:** Env-Änderungen greifen erst nach `docker compose up -d --force-recreate`, nicht nach `restart` (siehe Auto-Memory `project_deploy_env_recreate.md`).
## 9. Testing
**Unit (Vitest, mocked Gemini):**
- `image-preprocess.test.ts`: Resize, HEIC→JPEG, Metadata-Strip, JPEG-Qualität.
- `recipe-extraction-prompt.test.ts`: Prompt enthält Schema; Zod akzeptiert gültige Response; Zod lehnt invalide Response ab; Retry-Logik greift genau 1×.
- `gemini-client.test.ts`: Timeout, 429-no-retry, 5xx-1x-retry, Network-Fehler.
**API (SvelteKit-Endpoint, gemockter Gemini-Client):**
- `tests/api/extract-from-photo.test.ts`: Happy-Path mit Fixture-JPEG; 413 bei >8MB; 415 bei nicht-Bild; 422 bei Titel-OK-aber-0-Ingredients-UND-0-Steps; 503 mit `AI_NOT_CONFIGURED` wenn Key fehlt.
**E2E (Playwright gegen `kochwas-dev.siegeln.net`):**
- `tests/e2e/remote/photo-import.spec.ts`: Kamera-Icon-Klick; File-Upload (Endpoint gestubt, kein echter Gemini-Call); Editor-Prefill; Save; Redirect auf `/recipes/:id`; Kamera-Icon-disabled bei `context.setOffline(true)`.
**Fixtures:** `tests/fixtures/photo-recipe/`: gedruckte Seite, Handschrift-Karte, No-Recipe-Bild.
**Explizit nicht getestet:** Die Gemini-Vision-Qualität selbst. Das ist Model-Verhalten, nicht unser Code. Manuelle Verifikation nach Deploy.
## 10. PWA / Service Worker
- `/new/from-photo` in den Shell-Pre-Cache aufnehmen.
- Feature funktioniert nur online — Offline-State wird bewusst gehandhabt (siehe §6).
- Service-Worker ändert nichts am Extract-Endpoint (keine SW-Cachung für `/api/recipes/extract-from-photo`).
## 11. Offene Kleinigkeiten (in Planung zu entscheiden)
- **Save-Endpoint:** Ob der bestehende Editor-Save-Endpoint das Anlegen eines Rezepts aus Scratch unterstützt, oder ob `insertRecipe` über einen neuen POST `/api/recipes` exponiert werden muss — vor dem Planning prüfen.
- **Sharp Build-Stage:** Verifizieren, dass das offizielle `sharp`-npm-Package auf arm64 mit libheif-Support ausgeliefert wird; andernfalls Build-Stage-Rezept ähnlich zu `better-sqlite3`.
- **Rate-Limit-Impl:** In-Memory-LRU oder Redis-like überflüssig — `Map<ip, {count, resetAt}>` reicht.
## 12. Akzeptanz-Kriterien
- [ ] Kamera-Icon in der Kopfzeile sichtbar, führt zu `/new/from-photo`.
- [ ] Kamera-Icon unsichtbar wenn `GEMINI_API_KEY` leer.
- [ ] Kamera-Icon disabled wenn offline (`networkStore.online === false`).
- [ ] File-Picker öffnet mobile Rückkamera direkt (`capture="environment"`).
- [ ] Gedrucktes Fixture-Rezept wird vom Prompt + Mock-Gemini-Response in gültige Recipe-Shape überführt.
- [ ] Handschrift-Fixture ebenso (Mock).
- [ ] No-Recipe-Fixture → 422 `NO_RECIPE_IN_IMAGE` → UI zeigt Yellow-Box mit beiden Buttons.
- [ ] Editor öffnet mit vorbefüllten Feldern, Nutzer kann editieren, Speichern navigiert zu `/recipes/:id`.
- [ ] Foto-Datei wird nach Request nicht auf Disk gefunden (Test-Assertion im API-Test).
- [ ] Build im Dockerfile-arm64-Stage erfolgreich mit `sharp`.
- [ ] `npm test` + `npm run check` grün.

View File

@@ -0,0 +1,295 @@
# Einkaufsliste — Design-Spec
**Datum**: 2026-04-21
**Status**: Spec, vor Implementierung
## Ziel
Aus Rezepten auf der Wunschliste eine flache, aggregierte Einkaufsliste erzeugen. Die Liste ist haushaltsweit geteilt, mobil-first, im Supermarkt abhakbar. Portionen sind pro Rezept anpassbar. Identische Zutaten (gleicher Name + gleiche Einheit) werden über mehrere Rezepte hinweg summiert.
## Entscheidungen (aus Brainstorming)
| Thema | Entscheidung |
|---|---|
| Sichtbarkeit | Global, eine Liste für alle Profile |
| Portionen | Default `servings_default` beim Hinzufügen; zentral auf der Einkaufslisten-Seite anpassbar |
| Aggregation | Flache Liste, exaktes Matching auf `(LOWER(TRIM(name)), LOWER(TRIM(unit)))`. Keine Fuzzy-Matches — lieber zwei Zeilen als falsche Summen. Rezept-Herkunft pro Zeile sichtbar. |
| Abhaken | Checkbox, durchgestrichen, sortiert ans Ende. Manuelles Cleanup via „Erledigte entfernen" / „Liste leeren" |
| Kopplung | Komplett entkoppelt von Wunschliste und `cooking_log`. Abhaken beeinflusst nur die Einkaufsliste. |
| Header-Badge | Zählt **nicht-abgehakte** aggregierte Zutaten-Zeilen. Versteckt sich bei Count = 0. |
| Manuelle Einträge | Out of scope. Nur rezeptbasiert. |
## Datenmodell
Migration `013_shopping_list.sql`:
```sql
CREATE TABLE shopping_cart_recipe (
recipe_id INTEGER PRIMARY KEY REFERENCES recipe(id) ON DELETE CASCADE,
servings INTEGER NOT NULL,
added_by_profile_id INTEGER REFERENCES profile(id) ON DELETE SET NULL,
added_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE shopping_cart_check (
name_key TEXT NOT NULL,
unit_key TEXT NOT NULL,
checked_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (name_key, unit_key)
);
```
**Derivation-Prinzip**: Die aggregierte Liste wird **nicht materialisiert**. Sie wird bei jedem Lesen aus `shopping_cart_recipe JOIN recipe JOIN ingredient` plus Skalierungs-Faktor berechnet. Vorteil: Rezept-Edits wirken live auf die Liste.
**Abhaken pro aggregierter Zeile**: `(name_key, unit_key)` — nicht pro Rezept-Zutat. Wenn zwei Rezepte beide „Mehl, g" haben, gibt es eine Zeile „400 g Mehl", und ein Haken reicht. Wird eines der Rezepte entfernt, bleibt „200 g Mehl" mit Haken sichtbar.
**Orphan-Checks** (aggregierter Schlüssel ist nicht mehr durch ein Rezept im Cart abgedeckt): Werden nicht aktiv gelöscht, tauchen aber in der Ausgabe von `listShoppingList` nicht auf (der Join erzeugt keine Zeile). Späteres Cleanup optional via `clearCart` / `clearCheckedItems`.
### Aggregations-SQL (Kern)
```sql
SELECT
LOWER(TRIM(i.name)) AS name_key,
LOWER(TRIM(COALESCE(i.unit, ''))) AS unit_key,
MIN(i.name) AS display_name,
MIN(i.unit) AS display_unit,
SUM(i.quantity * cr.servings * 1.0 / r.servings_default) AS total_quantity,
GROUP_CONCAT(DISTINCT r.title) AS from_recipes,
EXISTS(SELECT 1 FROM shopping_cart_check c
WHERE c.name_key = LOWER(TRIM(i.name))
AND c.unit_key = LOWER(TRIM(COALESCE(i.unit, '')))) AS checked
FROM shopping_cart_recipe cr
JOIN recipe r ON r.id = cr.recipe_id
JOIN ingredient i ON i.recipe_id = r.id
GROUP BY name_key, unit_key
ORDER BY checked ASC, display_name COLLATE NOCASE;
```
**Edge Cases**:
- `i.quantity IS NULL``total_quantity` bleibt NULL, UI rendert ohne Mengenangabe.
- `r.servings_default IS NULL` → Division through-by-NULL → `total_quantity` NULL; defensiver: `COALESCE(r.servings_default, cr.servings)` (Faktor = 1, wenn kein Default bekannt).
- `i.unit IS NULL``unit_key = ''`, Anzeige ohne Einheit.
- Rezept hat keine Zutaten (sehr selten) → kein Beitrag zur Liste, Rezept-Chip erscheint trotzdem (Signal: „ups, keine Zutaten").
## Server-Module
### `src/lib/server/shopping/repository.ts`
Neue Typen:
```ts
export type ShoppingCartRecipe = {
recipe_id: number;
title: string;
image_path: string | null;
servings: number;
servings_default: number;
};
export type ShoppingListRow = {
name_key: string;
unit_key: string;
display_name: string;
display_unit: string | null;
total_quantity: number | null;
from_recipes: string; // comma-separated recipe titles
checked: 0 | 1;
};
export type ShoppingListSnapshot = {
recipes: ShoppingCartRecipe[];
rows: ShoppingListRow[];
uncheckedCount: number;
};
```
Funktionen:
- `addRecipeToCart(db, recipeId, profileId, servings?)``INSERT … ON CONFLICT(recipe_id) DO UPDATE SET servings = excluded.servings`. Wenn `servings` fehlt, nimmt `COALESCE(recipe.servings_default, 4)`.
- `removeRecipeFromCart(db, recipeId)`
- `setCartServings(db, recipeId, servings)` — App-seitig validiert: `1 ≤ servings ≤ 50`. SQL-Level `CHECK (servings > 0)` zusätzlich als Sicherheitsnetz.
- `listShoppingList(db) → ShoppingListSnapshot` — liefert Cart-Rezepte, aggregierte Zeilen und `uncheckedCount` in einer Transaktion.
- `toggleCheck(db, nameKey, unitKey, checked: boolean)` — Insert bzw. Delete in `shopping_cart_check`.
- `clearCheckedItems(db)` — transaktional:
1. Aggregation laufen lassen und `recipe_id`s finden, deren sämtliche aggregierten Zeilen abgehakt sind (ein Rezept zählt als „erledigt", wenn all seine `(name_key, unit_key)`-Beiträge in `shopping_cart_check` stehen)
2. Diese Rezepte via `DELETE FROM shopping_cart_recipe WHERE recipe_id IN (…)` entfernen
3. Check-Einträge, die jetzt keinen Bezug mehr haben, mit `DELETE FROM shopping_cart_check WHERE (name_key, unit_key) NOT IN (<aktive Keys nach Step 2>)` aufräumen
- `clearCart(db)``DELETE FROM shopping_cart_recipe; DELETE FROM shopping_cart_check;`
### Routen
| Methode + Pfad | Body/Params | Zweck |
|---|---|---|
| `GET /api/shopping-list` | — | Snapshot holen |
| `POST /api/shopping-list/recipe` | `{ recipe_id, servings?, profile_id? }` | Rezept in Cart; idempotent |
| `PATCH /api/shopping-list/recipe/:recipe_id` | `{ servings }` | Portionen ändern |
| `DELETE /api/shopping-list/recipe/:recipe_id` | — | Rezept raus |
| `POST /api/shopping-list/check` | `{ name_key, unit_key }` | Abhaken |
| `DELETE /api/shopping-list/check` | `{ name_key, unit_key }` | Haken weg |
| `DELETE /api/shopping-list/checked` | — | Erledigte entfernen |
| `DELETE /api/shopping-list` | — | Liste leeren |
Error-Handling: 404 wenn `recipe_id` nicht im Cart (nur bei DELETE/PATCH auf spezifischem Rezept), 400 bei Validation-Fehlern (servings ≤ 0, fehlende Felder), 500 mit JSON-Body `{ message }` bei DB-Fehlern.
## Client-Store
`src/lib/client/shopping-cart.svelte.ts` — analog zu `wishlist.svelte.ts`:
```ts
class ShoppingCartStore {
uncheckedCount = $state(0);
recipeIds = $state<Set<number>>(new Set()); // für „ist dieses Rezept im Cart?"
loaded = $state(false);
async refresh(): Promise<void>;
async addRecipe(recipeId: number): Promise<void>;
async removeRecipe(recipeId: number): Promise<void>;
isInCart(recipeId: number): boolean;
}
```
- `refresh()` ruft `GET /api/shopping-list` auf und extrahiert `recipeIds` + `uncheckedCount` aus dem Snapshot. Ein separater Leichtgewichts-Count-Endpoint ist nicht nötig; der Snapshot ist klein.
- Store wird in `+layout.svelte` beim `onMount` initialisiert (wie `wishlistStore.refresh()`).
- Nach jedem Mutating-Call (add/remove/toggle/clear) wird `refresh()` vom aufrufenden Code getriggert.
## UI
### (a) Wunschlisten-Karte — Relayout
Aktuell drücken zwei rechts-gestapelte Buttons den Titel-Text auf Handys zusammen. Neues Layout:
```
┌──────────┬─────────────────────────────┐
│ │ [Utensils|3] [Cart] [Trash] │ Action-Leiste oben, horizontal
│ Bild │ Titel (fett, 2 Zeilen max) │
│ 96px │ Hendrik, Verena, Leana │ wanted_by + ★
│ │ ★ 4.5 │
└──────────┴─────────────────────────────┘
```
Konkret in `src/routes/wishlist/+page.svelte`:
- `.actions` wird horizontal, als erste Zeile über dem Titel rechts-bündig.
- `source_domain`-Span aus der `.meta`-Zeile entfernt (Platz).
- Neuer Cart-Button zwischen Utensils und Trash:
- Nicht im Cart: neutral (Icon grau), aria-label „In den Einkaufswagen"
- Im Cart: grün gefüllt, Häkchen-Badge unten rechts, aria-label „Aus Einkaufswagen entfernen"
- Alle drei Buttons ≥ 44 × 44 px (mobile Tap-Target).
Vergleichbare Reorg in `src/routes/recipes/[id]/+page.svelte` nötig? — **Nein**. Der Cart-Button erscheint nur auf der Wunschliste. (Begründung: Rezept-Detail hat schon ein volles Action-Menü; das Hinzufügen zum Cart passiert bewusst aus der Wunschlisten-Perspektive.)
### (b) Header-Badge
`src/routes/+layout.svelte` — rechts neben dem bestehenden Kochtopf-Icon:
- Icon `ShoppingCart` aus `lucide-svelte`
- Badge-Kreis oben rechts mit `shoppingCartStore.uncheckedCount`
- Nur sichtbar wenn `uncheckedCount > 0`
- Klick → `goto('/shopping-list')`
- Gleicher Visual-Style wie der CookingPot (Farb-Konsistenz grün)
### (c) Seite `/shopping-list`
Datei: `src/routes/shopping-list/+page.svelte`
```
┌──────────────────────────────────────┐
│ Einkaufsliste │ Header
│ 12 noch zu besorgen · 3 Rezepte │
├──────────────────────────────────────┤
│ [Carbonara 4p- +] [Lasagne 6p- +] … │ Rezept-Chips, horizontal scrollbar
│ │ (Titel + Portions-Stepper + X)
├──────────────────────────────────────┤
│ ☐ 400 g Mehl │
│ aus Carbonara, Lasagne │
│ ☐ 6 Stk Eier │
│ aus Carbonara │
│ … │
│ ☑ 200 g Butter (durchgestrichen) │ Abgehakt, ans Ende
├──────────────────────────────────────┤
│ [Erledigte entfernen] [Liste leeren] │ Sticky Footer
└──────────────────────────────────────┘
```
**Komponenten** (neue Svelte-Dateien):
- `src/lib/components/ShoppingCartChip.svelte` — Rezept-Chip mit Stepper + Remove
- `src/lib/components/ShoppingListRow.svelte` — eine Zutatenzeile mit Checkbox
**Portions-Stepper**: - und + Buttons, mittig die Zahl. Min 1, Max 50 (sanity). Klick sendet PATCH, triggert Store-Refresh → Liste rerendert.
**Zutaten-Reihenfolge**: Erst nicht-abgehakt, dann abgehakt; innerhalb jeder Gruppe alphabetisch (`display_name COLLATE NOCASE`). Abgehakt = durchgestrichen + grauer Text.
**Mengen-Formatierung** (`src/lib/quantity-format.ts`, neu):
- `formatQuantity(q: number | null): string`
- `null``''`
- Ganz-nahe-Ganzzahl (Epsilon 0.01) → Integer
- Sonst auf max. 2 Nachkommastellen, trailing Nullen weg
- Beispiele: `400 → "400"`, `0.5 → "0.5"`, `0.333 → "0.33"`, `null → ""`
**Aktionen im Footer**:
- „Erledigte entfernen" — sichtbar wenn ≥ 1 Check, kein Confirm (reversibel genug)
- „Liste leeren" — Confirm via `confirmAction`: „Komplette Einkaufsliste löschen? Das macht nicht rückgängig."
**Empty State**: Icon `ShoppingCart` (große Version), „Einkaufswagen ist leer", Hint „Lege Rezepte auf der Wunschliste in den Wagen, um sie hier zu sehen."
**Offline-Verhalten**: Wie die Wunschliste — alle Mutating-Calls via `requireOnline()`. Service-Worker cached nichts von `/api/shopping-list/*` (network-only analog zu Wishlist). Die PWA-Seite selbst wird vom SW-Shell-Cache serviert, aber ohne Daten. Offline-Robustheit (local queue + sync) ist **out of scope** für v1.
## Testing
### Unit/Integration-Tests (Vitest, in-memory DB)
- `tests/integration/shopping-repository.test.ts`:
- `addRecipeToCart` idempotent, `ON CONFLICT` überschreibt `servings`
- Aggregation: gleiche `(name_key, unit_key)` summiert; unterschiedliche unit_keys bleiben getrennt
- Portions-Skalierung: `servings_default=4`, `servings=2` → alle Mengen halbiert
- Nulls: `quantity IS NULL``total_quantity IS NULL`; `unit IS NULL``unit_key=''`
- `toggleCheck` persistiert über `listShoppingList`-Aufrufe
- Abgehakt-Status überlebt Entfernen eines Rezepts, solange Schlüssel von einem anderen kommt
- `clearCheckedItems`: entfernt nur vollständig abgehakte Rezepte + räumt Orphan-Checks
- `countUncheckedItems` nach diversen Ops korrekt
- `clearCart` cleant beide Tabellen
- `tests/unit/shopping-cart-store.test.ts`:
- Mock-Fetch, testet refresh-Trigger nach add/remove
- `isInCart(id)` reflektiert aktuellen Zustand
- `uncheckedCount` reactive nach refresh
- `tests/unit/quantity-format.test.ts`:
- `formatQuantity(400) === "400"`
- `formatQuantity(0.5) === "0.5"`
- `formatQuantity(0.333333) === "0.33"`
- `formatQuantity(400.001) === "400"` (Epsilon)
- `formatQuantity(null) === ""`
### E2E-Tests (Playwright, `tests/e2e/remote/shopping.spec.ts`)
**Wichtig**: E2E-Tests laufen gegen `kochwas-dev.siegeln.net` und erfordern einen erfolgreichen Deploy des Features. Werden nach dem Feature-Merge manuell ausgelöst, nicht im Rahmen der Implementierungs-Phase.
Abgedeckt:
- Rezept auf Wunschliste → Cart-Button klicken → Header-Badge erscheint
- Navigation zu `/shopping-list`, Portions-Stepper hoch/runter → Zutatenmengen reagieren
- Zutat abhaken → Badge-Count sinkt, Zeile durchgestrichen, Reload persistiert
- „Erledigte entfernen" → vollständig abgehakte Rezepte weg, teilweise abgehakte bleiben
- „Liste leeren" → Empty-State, Badge verschwindet
- Zwei Rezepte mit gleicher Zutat (Fixture-Setup) → aggregierte Zeile mit Summe
- Cleanup-Fixture entfernt Cart + Checks nach jedem Test
**Nicht getestet**: exakte CSS-Styles, Animationen — visuelle Kontrolle beim Deploy.
## Implementierungs-Reihenfolge (Hinweis für Plan)
1. Migration 013 + Repository + Unit-Tests
2. API-Routen + Integrationstests
3. Client-Store
4. Header-Badge-Icon
5. Wunschlisten-Karte Relayout + Cart-Button
6. Seite `/shopping-list` (Chips → Rows → Footer → Empty State)
7. Quantity-Formatter + Tests
8. Service-Worker network-only für `/api/shopping-list/*`
9. Deploy, dann E2E-Tests nachschieben
## Out of Scope (für v1)
- Manuelle Einträge („Klopapier")
- Supermarkt-Abteilungs-Sortierung
- Offline-Queue (add/check während offline, sync später)
- Synonym/Fuzzy-Matching von Zutaten-Namen (der User harmonisiert langfristig händisch)
- Auto-Kopplung zu `cooking_log` / Wunschliste-Remove
- Teilen per Link / Export

View File

@@ -0,0 +1,244 @@
# Hauptseite: "Zuletzt angesehen" Sort + Collapsible Sections
## Kontext
Die Hauptseite (`src/routes/+page.svelte`) hat heute drei Sektionen — "Deine
Favoriten", "Zuletzt hinzugefügt", "Alle Rezepte" — und vier Sort-Optionen
für "Alle Rezepte" (Name, Bewertung, Zuletzt gekocht, Hinzugefügt). Der
User möchte:
1. Eine fünfte Sort-Option "Zuletzt angesehen" für "Alle Rezepte"
2. "Deine Favoriten" und "Zuletzt hinzugefügt" auf-/zuklappbar machen
Beides reduziert visuelle Last und gibt Zugriff auf "kürzlich
beschäftigte mich" Rezepte ohne Suche.
## Design-Entscheidungen (durch Brainstorming bestätigt)
- **View-Tracking**: zählt sofort beim Laden der Detailseite — kein Threshold
- **Storage**: SQLite, pro Profil (konsistent mit Ratings, Cooked, Wishlist)
- **Collapsibles**: standardmäßig offen, User-Wahl persistiert pro Device
## Sektion 1 — Schema & View-Tracking
### Migration
Neue Datei `src/lib/server/db/migrations/014_recipe_view.sql`
(Numbering: aktuell ist die letzte Migration `013_shopping_list.sql`):
```sql
CREATE TABLE recipe_view (
profile_id INTEGER NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
recipe_id INTEGER NOT NULL REFERENCES recipes(id) ON DELETE CASCADE,
last_viewed_at TEXT NOT NULL DEFAULT (datetime('now')),
PRIMARY KEY (profile_id, recipe_id)
);
CREATE INDEX idx_recipe_view_recent
ON recipe_view(profile_id, last_viewed_at DESC);
```
Idempotent über `INSERT OR REPLACE` — mehrfache Visits ein- und desselben
Profils auf dasselbe Rezept führen nur zur Aktualisierung des Timestamps,
kein Multi-Insert.
Cascade auf beide FKs: löscht ein User ein Rezept oder ein Profil, gehen
zugehörige Views automatisch mit.
### API
Neuer Endpoint `POST /api/recipes/[id]/view`:
```
Request body: { "profile_id": number }
Response: 204 No Content
Errors:
- 400 wenn profile_id fehlt oder kein Number
- 404 wenn Recipe nicht existiert (FK-Violation)
- 404 wenn Profil nicht existiert (FK-Violation)
```
Implementation: einfache `INSERT OR REPLACE` mit den IDs. `last_viewed_at`
nutzt den Default (`datetime('now')`).
### Client-Hook
In `src/routes/recipes/[id]/+page.svelte`, in `onMount`:
```ts
if (profileStore.active) {
void fetch(`/api/recipes/${recipe.id}/view`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ profile_id: profileStore.active.id })
});
}
```
Fire-and-forget, kein UI-Block, kein Error-Handling — wenn der Beacon
fehlschlägt, ist es kein User-Visible-Bug, das nächste View korrigiert
es.
## Sektion 2 — Sort "Zuletzt angesehen"
### Page
In `src/routes/+page.svelte`:
```ts
type AllSort = 'name' | 'rating' | 'cooked' | 'created' | 'viewed';
const ALL_SORTS = [
...,
{ value: 'viewed', label: 'Zuletzt angesehen' }
];
```
### API
`GET /api/recipes/all` bekommt einen optionalen `profile_id`-Query-Param.
Der Endpoint reicht ihn an `listAllRecipesPaginated` durch.
### DB-Layer
`listAllRecipesPaginated` in `src/lib/server/recipes/search-local.ts`
bekommt einen optionalen `profileId: number | null`-Parameter. Wenn
`sort === 'viewed'` UND `profileId !== null`:
```sql
SELECT r.*, ...
FROM recipes r
LEFT JOIN recipe_view v
ON v.recipe_id = r.id AND v.profile_id = :profileId
ORDER BY v.last_viewed_at DESC NULLS LAST,
r.title COLLATE NOCASE ASC
LIMIT :limit OFFSET :offset
```
Bei `sort === 'viewed'` ohne `profileId`: fällt auf alphabetische
Sortierung zurück (kein Crash, sinnvolles Default-Verhalten).
### Reactive Refetch bei Profile-Switch
Auf Home-Page-Ebene: ein `$effect` der auf `profileStore.activeId` lauscht
und — wenn `allSort === 'viewed'``setAllSort('viewed')` retriggert
(forciert Refetch mit neuem profile_id). Sonst (anderer Sort) keine
Aktion, weil andere Sorts nicht profilabhängig sind.
### Snapshot-Kompatibilität
Der existierende `rehydrateAll(sort, count, exhausted)` in `+page.svelte`
muss `profile_id` mitschicken, sonst zeigt der Back-Nav für sort='viewed'
einen anderen Inhalt als vor dem Forward-Klick. Das gleiche gilt für
`loadAllMore` und `setAllSort`.
## Sektion 3 — Auf-/Zuklappbare Sektionen
### State
In `src/routes/+page.svelte`:
```ts
type CollapseKey = 'favorites' | 'recent';
let collapsed = $state<Record<CollapseKey, boolean>>({
favorites: false,
recent: false
});
const STORAGE_KEY = 'kochwas.collapsed.sections';
function toggle(key: CollapseKey) {
collapsed[key] = !collapsed[key];
localStorage.setItem(STORAGE_KEY, JSON.stringify(collapsed));
}
```
In `onMount`: aus localStorage parsen, fehlerhafte JSON ignorieren
(default-state behalten).
### Markup
Pro Sektion:
```svelte
<section class="listing">
<button
class="section-head"
onclick={() => toggle('favorites')}
aria-expanded={!collapsed.favorites}
>
<ChevronDown size={18} class:rotated={collapsed.favorites} />
<h2>Deine Favoriten</h2>
<span class="count">{favorites.length}</span>
</button>
{#if !collapsed.favorites}
<div transition:slide={{ duration: 180 }}>
<ul class="cards"></ul>
</div>
{/if}
</section>
```
### Visual / CSS
- Header `<button>`: transparenter Border, full-width, `display: flex`,
`align-items: center`, `gap: 0.5rem`, `min-height: 44px` (Tap-Target)
- Chevron-Icon (lucide-svelte `ChevronDown`): rotiert auf
`transform: rotate(-90deg)` wenn `.rotated`
- Count-Pill rechts: kleiner grauer Text, hilft zu sehen wie viel hinter
einer zugeklappten Sektion steckt
- Hover: leichter Hintergrund (`#f4f8f5`, wie andere interaktive Elemente)
- Animation: `svelte/transition`'s `slide`, ~180 ms
### Persistenz-Format
```json
{ "favorites": false, "recent": true }
```
Truthy = collapsed. Default-Zustand wenn key fehlt: beide false.
### "Alle Rezepte" bleibt nicht-collapsible
Hauptliste, immer sichtbar — User würde das Scrollen verlieren.
## Test-Strategie
### Schema/Migration
- Migrations-Test (existierendes Pattern in `tests/integration`): nach
`applyMigrations` muss `recipe_view` existieren mit erwarteten
Spalten
### View-Endpoint
- `POST /api/recipes/[id]/view` Integration-Test:
- Erstes POST → Row mit `last_viewed_at` ungefähr `now`
- Zweites POST → gleiche Row, `last_viewed_at` aktualisiert
- POST mit ungültiger profile_id → 404
- POST mit ungültiger recipe_id → 404
- POST ohne profile_id im Body → 400
### Sort-Logik
- Unit-Test für `listAllRecipesPaginated(db, 'viewed', limit, offset, profileId)`:
- Mit Views-Daten: angesehene Rezepte zuerst (DESC nach `last_viewed_at`),
Rest alphabetisch
- Ohne profileId: fallback auf alphabetisch
- Mit profileId aber ohne Views: alle als NULL → alphabetisch
### Collapsibles (manuell oder unit)
- localStorage-Persistenz: Toggle, Reload, gleicher State
- Default-State wenn localStorage leer/corrupt: beide offen
- Ein Unit-Test für eine reine Helper-Funktion (parse/serialize), Markup
ist Snapshot-mässig nicht so wertvoll testbar
## Reihenfolge der Umsetzung
1. Migration + DB-Layer + Sort-Query (`search-local.ts`-Erweiterung)
2. View-Endpoint (`POST /api/recipes/[id]/view`) + Client-Beacon in
`recipes/[id]/+page.svelte`
3. Sort-Option in `+page.svelte` UI + API-Param weiterreichen +
profile_id in `loadAllMore`/`rehydrateAll`/`setAllSort` durchreichen
4. Collapsible-Pattern in `+page.svelte` für Favoriten und Recent
Jede Phase atomar committen + pushen.

836
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{ {
"name": "kochwas", "name": "kochwas",
"version": "0.1.0", "version": "1.4.1",
"private": true, "private": true,
"type": "module", "type": "module",
"scripts": { "scripts": {
@@ -14,7 +14,8 @@
"format": "prettier --write .", "format": "prettier --write .",
"render:icons": "node scripts/render-icons.mjs", "render:icons": "node scripts/render-icons.mjs",
"test:e2e": "playwright test", "test:e2e": "playwright test",
"test:e2e:ui": "playwright test --ui" "test:e2e:ui": "playwright test --ui",
"test:e2e:remote": "playwright test --config=playwright.remote.config.ts"
}, },
"devDependencies": { "devDependencies": {
"@playwright/test": "^1.59.1", "@playwright/test": "^1.59.1",
@@ -34,12 +35,15 @@
"vitest": "^2.1.4" "vitest": "^2.1.4"
}, },
"dependencies": { "dependencies": {
"@google/generative-ai": "^0.24.1",
"@types/archiver": "^7.0.0", "@types/archiver": "^7.0.0",
"@types/yauzl": "^2.10.3", "@types/yauzl": "^2.10.3",
"archiver": "^7.0.1", "archiver": "^7.0.1",
"better-sqlite3": "^11.5.0", "better-sqlite3": "^11.5.0",
"linkedom": "^0.18.5", "linkedom": "^0.18.5",
"lucide-svelte": "^1.0.1", "lucide-svelte": "^1.0.1",
"node-addon-api": "^8.7.0",
"node-gyp": "^12.3.0",
"yauzl": "^3.3.0", "yauzl": "^3.3.0",
"zod": "^3.23.8" "zod": "^3.23.8"
} }

View File

@@ -5,6 +5,7 @@ import { defineConfig } from '@playwright/test';
// Preview-Server (kein Dev-Server, damit der SW registrierbar ist). // Preview-Server (kein Dev-Server, damit der SW registrierbar ist).
export default defineConfig({ export default defineConfig({
testDir: 'tests/e2e', testDir: 'tests/e2e',
testIgnore: ['tests/e2e/remote/**'],
fullyParallel: false, fullyParallel: false,
reporter: 'list', reporter: 'list',
use: { use: {

View File

@@ -0,0 +1,32 @@
import { defineConfig } from '@playwright/test';
// Zweite Playwright-Config fuer E2E-Smoketests gegen ein deployed
// Environment (standardmaessig kochwas-dev.siegeln.net).
//
// Getrennt von playwright.config.ts, weil diese Tests:
// - keinen lokalen Preview-Server starten
// - gegen eine echte Datenbank laufen (daher workers: 1, afterEach-Cleanup)
// - Service-Worker-Lifecycle nicht manipulieren (das macht offline.spec.ts lokal)
//
// Ausfuehrung: npm run test:e2e:remote
// Ziel-URL ueberschreiben: E2E_REMOTE_URL=https://... npm run test:e2e:remote
const BASE_URL = process.env.E2E_REMOTE_URL ?? 'https://kochwas-dev.siegeln.net';
export default defineConfig({
testDir: 'tests/e2e/remote',
fullyParallel: false,
workers: 1,
retries: 0,
reporter: [['list'], ['html', { open: 'never', outputFolder: 'playwright-report-remote' }]],
use: {
baseURL: BASE_URL,
headless: true,
trace: 'retain-on-failure',
screenshot: 'only-on-failure',
// Service-Worker blocken: Diese Suite testet Live-API-Verhalten gegen
// den Server, keine PWA-Features (dafuer offline.spec.ts lokal). Die
// frische SW-Registrierung pro Context akkumulierte im Single-Worker-
// Run Browser-State und crashte Chromium zufaellig nach 20-30 Specs.
serviceWorkers: 'block'
}
});

View File

@@ -1,4 +1,13 @@
use_default_settings: true # Defaults laden, aber Engine-Liste rigoros auf brave eindampfen.
# keep_only ist robuster als einzelne `disabled: true`-Overrides: SearXNGs
# Merge-Semantik für partial overrides (nur name + disabled ohne engine:)
# greift nicht zuverlässig — DDG & Co. wurden trotzdem abgefragt. keep_only
# wirft alles andere vor dem Laden raus, kein Captcha-/403-Log-Lärm mehr.
# Mojeek blockt die Pi-IP mit 403 und ist deshalb draußen.
use_default_settings:
engines:
keep_only:
- brave
server: server:
# Platzhalter wird beim Container-Start per os.path.expandvars aus der # Platzhalter wird beim Container-Start per os.path.expandvars aus der
@@ -31,71 +40,21 @@ outgoing:
ui: ui:
default_locale: de default_locale: de
# Quieten engines that fail on cold start and aren't useful here
enabled_plugins: enabled_plugins:
- 'Hash plugin' - 'Hash plugin'
- 'Tracker URL remover' - 'Tracker URL remover'
- 'Open Access DOI rewrite' - 'Open Access DOI rewrite'
engines: engines:
# Brave mit API-Key: stabiler als der HTML-Scraper, kein Rate-Limit-Spam # Brave Search API (engine: braveapi). Die Engine "brave" ist der
# mehr. Key kommt aus dem BRAVE_API_KEY-Env (.env auf dem Pi, nicht im Repo). # HTML-Scraper von search.brave.com und ignoriert api_key — deshalb
# Fehlt der Key oder ist er leer, fällt Brave bei der ersten Anfrage zurück # hier explizit braveapi, sonst landen wir in Brave-Rate-Limits.
# auf einen 401 — andere Engines laufen normal weiter. # Key kommt aus dem BRAVE_API_KEY-Env (.env auf dem Pi, nicht im Repo),
# expandiert via Python os.path.expandvars im searxng-init-Container.
- name: brave - name: brave
engine: brave engine: braveapi
shortcut: br shortcut: br
categories: [general, web] categories: [general, web]
timeout: 6.0 timeout: 6.0
# Wert wird beim Container-Start durch Python-os.path.expandvars aus der
# BRAVE_API_KEY-Env-Variable eingesetzt (siehe docker-compose.prod.yml
# entrypoint-Override). SearXNG selbst hat kein !env-Tag.
api_key: "${BRAVE_API_KEY}" api_key: "${BRAVE_API_KEY}"
disabled: false disabled: false
# DuckDuckGo: deaktiviert, weil DDG die Pi-IP als Bot erkannt hat und
# bei jeder Anfrage mit CAPTCHA antwortet. Brave (API) + Mojeek decken
# die Websuche zuverlässig ab — DDG-Scraping wäre nur zusätzlicher Lärm.
- name: duckduckgo
disabled: true
# Mojeek: eigener Index, seltener Rate-Limits, ergänzt Brave.
- name: mojeek
engine: mojeek
shortcut: mjk
timeout: 6.0
disabled: false
# Video-/News-Engines abdrehen — wir wollen nur Text-Treffer für Rezeptseiten.
- name: google videos
disabled: true
- name: google news
disabled: true
- name: google images
disabled: true
- name: bing videos
disabled: true
- name: bing news
disabled: true
- name: bing images
disabled: true
- name: karmasearch videos
disabled: true
# Startpage: hat unsere Pi-IP als Bot erkannt und blockt mit Captcha
# (1h suspended_time pro Fehler). Bringt für Rezeptsuche nichts, was
# nicht schon Brave/DDG liefern.
- name: startpage
disabled: true
# Tor-basierte Engines brauchen einen Tor-Proxy im Container — haben
# wir nicht, also harmlos deaktivieren, um Init-Fehler loszuwerden.
- name: ahmia
disabled: true
- name: torch
disabled: true
# Wikidata produziert beim Cold-Start einen KeyError (Init-Bug in der
# aktuellen SearXNG-Version 2026.4). Für Rezeptsuche ohne Mehrwert.
- name: wikidata
disabled: true

View File

@@ -0,0 +1,25 @@
import { alertAction } from '$lib/client/confirm.svelte';
/**
* Fetch wrapper for actions where a non-OK response should pop a modal
* via alertAction(). Returns the Response on 2xx, or null after showing
* the alert. Caller should `if (!res) return;` after the call.
*
* Use this for *interactive* actions (rename, delete, save). For form
* submissions where the error should appear inline next to the field
* (e.g. admin/domains add()), keep manual handling.
*/
export async function asyncFetch(
url: string,
init: RequestInit | undefined,
errorTitle: string
): Promise<Response | null> {
const res = await fetch(url, init);
if (res.ok) return res;
const body = (await res.json().catch(() => null)) as { message?: string } | null;
await alertAction({
title: errorTitle,
message: body?.message ?? `HTTP ${res.status}`
});
return null;
}

View File

@@ -0,0 +1,76 @@
import type { Recipe } from '$lib/types';
export type UploadStatus = 'idle' | 'loading' | 'success' | 'error';
export class PhotoUploadStore {
status = $state<UploadStatus>('idle');
recipe = $state<Recipe | null>(null);
errorCode = $state<string | null>(null);
errorMessage = $state<string | null>(null);
lastFile = $state<File | null>(null);
private controller: AbortController | null = null;
private readonly fetchImpl: typeof fetch;
constructor(opts: { fetchImpl?: typeof fetch } = {}) {
this.fetchImpl = opts.fetchImpl ?? fetch;
}
async upload(file: File): Promise<void> {
this.lastFile = file;
await this.doUpload(file);
}
async retry(): Promise<void> {
if (this.lastFile) await this.doUpload(this.lastFile);
}
reset(): void {
this.status = 'idle';
this.recipe = null;
this.errorCode = null;
this.errorMessage = null;
this.lastFile = null;
this.controller?.abort();
this.controller = null;
}
abort(): void {
this.controller?.abort();
}
private async doUpload(file: File): Promise<void> {
this.status = 'loading';
this.recipe = null;
this.errorCode = null;
this.errorMessage = null;
this.controller = new AbortController();
const fd = new FormData();
fd.append('photo', file);
try {
const res = await this.fetchImpl('/api/recipes/extract-from-photo', {
method: 'POST',
body: fd,
signal: this.controller.signal
});
const body = await res.json().catch(() => ({}));
if (!res.ok) {
this.status = 'error';
this.errorCode = typeof body.code === 'string' ? body.code : 'UNKNOWN';
this.errorMessage =
typeof body.message === 'string' ? body.message : `HTTP ${res.status}`;
return;
}
this.recipe = body.recipe as Recipe;
this.status = 'success';
} catch (e) {
if ((e as Error).name === 'AbortError') {
this.status = 'idle';
return;
}
this.status = 'error';
this.errorCode = 'NETWORK';
this.errorMessage = (e as Error).message;
}
}
}

View File

@@ -1,4 +1,5 @@
import type { Profile } from '$lib/types'; import type { Profile } from '$lib/types';
import { alertAction } from '$lib/client/confirm.svelte';
const STORAGE_KEY = 'kochwas.activeProfileId'; const STORAGE_KEY = 'kochwas.activeProfileId';
@@ -60,3 +61,19 @@ class ProfileStore {
} }
export const profileStore = new ProfileStore(); export const profileStore = new ProfileStore();
/**
* Returns the active profile, or null after showing the standard
* "kein Profil gewählt" dialog. Use as the first line of any per-profile
* action so we don't repeat the guard at every call-site.
*
* `message` ueberschreibt den Default, wenn eine Aktion einen spezifischen
* Hinweis braucht (z. B. „um mitzuwünschen" auf der Wunschliste).
*/
export async function requireProfile(
message = 'Tippe oben rechts auf „Profil wählen", dann klappt die Aktion.'
): Promise<Profile | null> {
if (profileStore.active) return profileStore.active;
await alertAction({ title: 'Kein Profil gewählt', message });
return null;
}

View File

@@ -1,3 +1,5 @@
import { SW_UPDATE_POLL_INTERVAL_MS, SW_VERSION_QUERY_TIMEOUT_MS } from '$lib/constants';
// Service-Worker-Update-Pattern: Workbox-Style Handshake (kein // Service-Worker-Update-Pattern: Workbox-Style Handshake (kein
// skipWaiting im install-Handler, User bestätigt via Toast) mit // skipWaiting im install-Handler, User bestätigt via Toast) mit
// zusätzlichem Zombie-Schutz. // zusätzlichem Zombie-Schutz.
@@ -39,7 +41,7 @@ class PwaStore {
// mitbekommt, wenn er die Seite lange offen lässt ohne zu navigieren. // mitbekommt, wenn er die Seite lange offen lässt ohne zu navigieren.
this.pollTimer = setInterval(() => { this.pollTimer = setInterval(() => {
void this.registration?.update().catch(() => {}); void this.registration?.update().catch(() => {});
}, 30 * 60_000); }, SW_UPDATE_POLL_INTERVAL_MS);
} }
private onUpdateFound(): void { private onUpdateFound(): void {
@@ -97,7 +99,7 @@ class PwaStore {
function queryVersion(sw: ServiceWorker): Promise<string | null> { function queryVersion(sw: ServiceWorker): Promise<string | null> {
return new Promise((resolve) => { return new Promise((resolve) => {
const channel = new MessageChannel(); const channel = new MessageChannel();
const timer = setTimeout(() => resolve(null), 1500); const timer = setTimeout(() => resolve(null), SW_VERSION_QUERY_TIMEOUT_MS);
channel.port1.onmessage = (e) => { channel.port1.onmessage = (e) => {
clearTimeout(timer); clearTimeout(timer);
const v = (e.data as { version?: unknown } | null)?.version; const v = (e.data as { version?: unknown } | null)?.version;

View File

@@ -0,0 +1,78 @@
// Persistent scroll restoration across client navigations.
//
// SvelteKit only restores scroll synchronously after the new page mounts.
// Pages whose content is fetched in onMount/afterNavigate (e.g. home,
// wishlist, shopping-list) are still empty at that point, so the saved
// scrollY can't be reached and the browser clamps to 0.
//
// We patch this by saving scrollY on beforeNavigate (keyed by the URL
// we're leaving — NOT location.pathname, which on popstate is already
// the new URL by the time the callback fires) and re-applying it after
// popstate as soon as the document is tall enough — rAF-polled with a
// hard time budget so we never spin.
const STORAGE_KEY = 'kochwas:scroll';
const POLL_BUDGET_MS = 1500;
const MIN_RESTORE_Y = 40; // ignore noise: don't override a default top scroll
type ScrollMap = Record<string, number>;
function readMap(): ScrollMap {
if (typeof sessionStorage === 'undefined') return {};
try {
const raw = sessionStorage.getItem(STORAGE_KEY);
return raw ? (JSON.parse(raw) as ScrollMap) : {};
} catch {
return {};
}
}
function writeMap(map: ScrollMap): void {
if (typeof sessionStorage === 'undefined') return;
try {
sessionStorage.setItem(STORAGE_KEY, JSON.stringify(map));
} catch {
// quota exceeded — silently drop, scroll memory is best-effort
}
}
function keyFor(url: URL): string {
return url.pathname + url.search;
}
export function recordScroll(fromUrl: URL | null | undefined): void {
if (typeof window === 'undefined') return;
if (!fromUrl) return;
const map = readMap();
map[keyFor(fromUrl)] = window.scrollY;
writeMap(map);
}
export function restoreScroll(
navType: string | null | undefined,
toUrl: URL | null | undefined
): void {
if (typeof window === 'undefined') return;
if (navType !== 'popstate') return;
if (!toUrl) return;
const target = readMap()[keyFor(toUrl)];
if (!target || target < MIN_RESTORE_Y) return;
const start = performance.now();
const step = () => {
const docHeight = document.documentElement.scrollHeight;
const reachable = Math.max(0, docHeight - window.innerHeight);
if (reachable >= target - 4) {
window.scrollTo({ top: target, left: 0, behavior: 'instant' });
return;
}
if (performance.now() - start >= POLL_BUDGET_MS) {
// Best effort — content never grew tall enough; clamp will land us
// at the bottom of what's available.
window.scrollTo({ top: target, left: 0, behavior: 'instant' });
return;
}
requestAnimationFrame(step);
};
requestAnimationFrame(step);
}

View File

@@ -0,0 +1,225 @@
import type { SearchHit } from '$lib/server/recipes/search-local';
import type { WebHit } from '$lib/server/search/searxng';
export type SearchSnapshot = {
query: string;
hits: SearchHit[];
webHits: WebHit[];
searchedFor: string | null;
webError: string | null;
localExhausted: boolean;
webPageno: number;
webExhausted: boolean;
};
export type SearchStoreOptions = {
pageSize?: number;
debounceMs?: number;
filterDebounceMs?: number;
minQueryLength?: number;
webFilterParam?: () => string;
fetchImpl?: typeof fetch;
};
export class SearchStore {
query = $state('');
hits = $state<SearchHit[]>([]);
webHits = $state<WebHit[]>([]);
searching = $state(false);
webSearching = $state(false);
webError = $state<string | null>(null);
searchedFor = $state<string | null>(null);
localExhausted = $state(false);
webPageno = $state(0);
webExhausted = $state(false);
loadingMore = $state(false);
private readonly pageSize: number;
private readonly debounceMs: number;
private readonly filterDebounceMs: number;
private readonly minQueryLength: number;
private readonly webFilterParam: () => string;
private readonly fetchImpl: typeof fetch;
private debounceTimer: ReturnType<typeof setTimeout> | null = null;
private skipNextDebounce = false;
constructor(opts: SearchStoreOptions = {}) {
this.pageSize = opts.pageSize ?? 30;
this.debounceMs = opts.debounceMs ?? 300;
this.filterDebounceMs = opts.filterDebounceMs ?? 150;
this.minQueryLength = opts.minQueryLength ?? 4;
this.webFilterParam = opts.webFilterParam ?? (() => '');
this.fetchImpl = opts.fetchImpl ?? ((...a) => fetch(...a));
}
runDebounced(): void {
if (this.debounceTimer) clearTimeout(this.debounceTimer);
if (this.skipNextDebounce) {
this.skipNextDebounce = false;
return;
}
const q = this.query.trim();
if (q.length < this.minQueryLength) {
this.resetResults();
return;
}
this.searching = true;
this.webHits = [];
this.webSearching = false;
this.webError = null;
this.debounceTimer = setTimeout(() => {
void this.runSearch(q);
}, this.debounceMs);
}
async runSearch(q: string): Promise<void> {
if (this.debounceTimer) clearTimeout(this.debounceTimer);
this.debounceTimer = null;
this.localExhausted = false;
this.webPageno = 0;
this.webExhausted = false;
try {
const res = await this.fetchImpl(
`/api/recipes/search?q=${encodeURIComponent(q)}&limit=${this.pageSize}`
);
const body = (await res.json()) as { hits: SearchHit[] };
if (this.query.trim() !== q) return;
this.hits = body.hits;
this.searchedFor = q;
if (this.hits.length < this.pageSize) this.localExhausted = true;
if (this.hits.length === 0) {
await this.runWebSearch(q, 1);
}
} finally {
if (this.query.trim() === q) this.searching = false;
}
}
private async runWebSearch(q: string, pageno: number): Promise<void> {
this.webSearching = true;
try {
const res = await this.fetchImpl(
`/api/recipes/search/web?q=${encodeURIComponent(q)}&pageno=${pageno}${this.webFilterParam()}`
);
if (this.query.trim() !== q) return;
if (!res.ok) {
const err = (await res.json().catch(() => ({}))) as { message?: string };
this.webError = err.message ?? `HTTP ${res.status}`;
this.webExhausted = true;
return;
}
const body = (await res.json()) as { hits: WebHit[] };
this.webHits = pageno === 1 ? body.hits : [...this.webHits, ...body.hits];
this.webPageno = pageno;
if (body.hits.length === 0) this.webExhausted = true;
} finally {
if (this.query.trim() === q) this.webSearching = false;
}
}
async loadMore(): Promise<void> {
if (this.loadingMore) return;
const q = this.query.trim();
if (!q) return;
this.loadingMore = true;
try {
if (!this.localExhausted) {
const res = await this.fetchImpl(
`/api/recipes/search?q=${encodeURIComponent(q)}&limit=${this.pageSize}&offset=${this.hits.length}`
);
const body = (await res.json()) as { hits: SearchHit[] };
if (this.query.trim() !== q) return;
const more = body.hits;
const seen = new Set(this.hits.map((h) => h.id));
const deduped = more.filter((h) => !seen.has(h.id));
this.hits = [...this.hits, ...deduped];
if (more.length < this.pageSize) this.localExhausted = true;
} else if (!this.webExhausted) {
const nextPage = this.webPageno + 1;
const wasEmpty = this.webHits.length === 0;
if (wasEmpty) this.webSearching = true;
try {
const res = await this.fetchImpl(
`/api/recipes/search/web?q=${encodeURIComponent(q)}&pageno=${nextPage}${this.webFilterParam()}`
);
if (this.query.trim() !== q) return;
if (!res.ok) {
const err = (await res.json().catch(() => ({}))) as { message?: string };
this.webError = err.message ?? `HTTP ${res.status}`;
this.webExhausted = true;
return;
}
const body = (await res.json()) as { hits: WebHit[] };
const more = body.hits;
const seen = new Set(this.webHits.map((h) => h.url));
const deduped = more.filter((h) => !seen.has(h.url));
if (deduped.length === 0) {
this.webExhausted = true;
} else {
this.webHits = [...this.webHits, ...deduped];
this.webPageno = nextPage;
}
} finally {
if (this.query.trim() === q) this.webSearching = false;
}
}
} finally {
this.loadingMore = false;
}
}
reSearch(): void {
const q = this.query.trim();
if (q.length < this.minQueryLength) return;
if (this.debounceTimer) clearTimeout(this.debounceTimer);
this.searching = true;
this.webHits = [];
this.webSearching = false;
this.webError = null;
this.debounceTimer = setTimeout(() => void this.runSearch(q), this.filterDebounceMs);
}
reset(): void {
if (this.debounceTimer) clearTimeout(this.debounceTimer);
this.debounceTimer = null;
this.query = '';
this.resetResults();
}
private resetResults(): void {
this.hits = [];
this.webHits = [];
this.searchedFor = null;
this.searching = false;
this.webSearching = false;
this.webError = null;
this.localExhausted = false;
this.webPageno = 0;
this.webExhausted = false;
}
captureSnapshot(): SearchSnapshot {
return {
query: this.query,
hits: this.hits,
webHits: this.webHits,
searchedFor: this.searchedFor,
webError: this.webError,
localExhausted: this.localExhausted,
webPageno: this.webPageno,
webExhausted: this.webExhausted
};
}
restoreSnapshot(s: SearchSnapshot): void {
this.skipNextDebounce = true;
this.query = s.query;
this.hits = s.hits;
this.webHits = s.webHits;
this.searchedFor = s.searchedFor;
this.webError = s.webError;
this.localExhausted = s.localExhausted;
this.webPageno = s.webPageno;
this.webExhausted = s.webExhausted;
}
}

View File

@@ -0,0 +1,52 @@
type Snapshot = {
recipes: { recipe_id: number }[];
uncheckedCount: number;
};
export class ShoppingCartStore {
uncheckedCount = $state(0);
recipeIds = $state<Set<number>>(new Set());
loaded = $state(false);
private readonly fetchImpl: typeof fetch;
constructor(fetchImpl?: typeof fetch) {
this.fetchImpl = fetchImpl ?? ((...a) => fetch(...a));
}
async refresh(): Promise<void> {
try {
const res = await this.fetchImpl('/api/shopping-list');
if (!res.ok) return;
const body = (await res.json()) as Snapshot;
this.recipeIds = new Set(body.recipes.map((r) => r.recipe_id));
this.uncheckedCount = body.uncheckedCount;
this.loaded = true;
} catch {
// keep last known state on network error
}
}
async addRecipe(recipeId: number): Promise<void> {
const res = await this.fetchImpl('/api/shopping-list/recipe', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ recipe_id: recipeId })
});
// Consume body to avoid leaking response, even if we ignore the payload.
await res.json().catch(() => null);
await this.refresh();
}
async removeRecipe(recipeId: number): Promise<void> {
const res = await this.fetchImpl(`/api/shopping-list/recipe/${recipeId}`, { method: 'DELETE' });
await res.json().catch(() => null);
await this.refresh();
}
isInCart(recipeId: number): boolean {
return this.recipeIds.has(recipeId);
}
}
export const shoppingCartStore = new ShoppingCartStore();

View File

@@ -0,0 +1,190 @@
<script lang="ts">
import { untrack } from 'svelte';
import { ImagePlus, ImageOff } from 'lucide-svelte';
import { confirmAction } from '$lib/client/confirm.svelte';
import { asyncFetch } from '$lib/client/api-fetch-wrapper';
import { requireOnline } from '$lib/client/require-online';
type Props = {
recipeId: number;
imagePath: string | null;
onchange: (path: string | null) => void;
};
let { recipeId, imagePath: initial, onchange }: Props = $props();
let imagePath = $state<string | null>(untrack(() => initial));
let uploading = $state(false);
let fileInput: HTMLInputElement | null = $state(null);
const imageSrc = $derived(
imagePath === null
? null
: /^https?:\/\//i.test(imagePath)
? imagePath
: `/images/${imagePath}`
);
async function onFileChosen(event: Event) {
const input = event.target as HTMLInputElement;
const file = input.files?.[0];
input.value = '';
if (!file) return;
if (!requireOnline('Der Bild-Upload')) return;
uploading = true;
try {
const fd = new FormData();
fd.append('file', file);
const res = await asyncFetch(
`/api/recipes/${recipeId}/image`,
{ method: 'POST', body: fd },
'Upload fehlgeschlagen'
);
if (!res) return;
const body = await res.json();
imagePath = body.image_path;
onchange(imagePath);
} finally {
uploading = false;
}
}
async function removeImage() {
if (imagePath === null) return;
const ok = await confirmAction({
title: 'Bild entfernen?',
message: 'Das Rezept wird danach ohne Titelbild angezeigt.',
confirmLabel: 'Entfernen',
destructive: true
});
if (!ok) return;
if (!requireOnline('Das Entfernen')) return;
uploading = true;
try {
const res = await asyncFetch(
`/api/recipes/${recipeId}/image`,
{ method: 'DELETE' },
'Entfernen fehlgeschlagen'
);
if (!res) return;
imagePath = null;
onchange(null);
} finally {
uploading = false;
}
}
</script>
<div class="image-row">
<div class="image-preview" class:empty={!imageSrc}>
{#if imageSrc}
<img src={imageSrc} alt="" />
{:else}
<span class="placeholder">Kein Bild</span>
{/if}
</div>
<div class="image-actions">
<button
class="btn"
type="button"
onclick={() => fileInput?.click()}
disabled={uploading}
>
<ImagePlus size={16} strokeWidth={2} />
<span>{imagePath ? 'Bild ersetzen' : 'Bild hochladen'}</span>
</button>
{#if imagePath}
<button class="btn ghost" type="button" onclick={removeImage} disabled={uploading}>
<ImageOff size={16} strokeWidth={2} />
<span>Entfernen</span>
</button>
{/if}
{#if uploading}
<span class="upload-status">Lade …</span>
{/if}
</div>
<input
bind:this={fileInput}
type="file"
accept="image/jpeg,image/png,image/webp,image/gif,image/avif"
class="file-input"
onchange={onFileChosen}
/>
</div>
<p class="image-hint">Max. 10 MB. JPG, PNG, WebP, GIF oder AVIF.</p>
<style>
.image-row {
display: flex;
gap: 1rem;
align-items: flex-start;
flex-wrap: wrap;
}
.image-preview {
width: 160px;
aspect-ratio: 16 / 10;
border-radius: 10px;
overflow: hidden;
background: #eef3ef;
border: 1px solid #e4eae7;
flex-shrink: 0;
}
.image-preview img {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
.image-preview.empty {
display: grid;
place-items: center;
color: #999;
font-size: 0.85rem;
}
.image-preview .placeholder {
padding: 0 0.5rem;
text-align: center;
}
.image-actions {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
align-items: center;
}
.upload-status {
color: #666;
font-size: 0.9rem;
}
.file-input {
position: absolute;
width: 1px;
height: 1px;
opacity: 0;
pointer-events: none;
}
.image-hint {
margin: 0.6rem 0 0;
color: #888;
font-size: 0.8rem;
}
.btn {
padding: 0.55rem 0.85rem;
border-radius: 10px;
border: 1px solid #cfd9d1;
background: white;
cursor: pointer;
font-family: inherit;
font-size: 0.9rem;
min-height: 40px;
display: inline-flex;
align-items: center;
gap: 0.4rem;
}
.btn.ghost {
color: #666;
}
.btn:disabled {
opacity: 0.6;
cursor: progress;
}
</style>

View File

@@ -0,0 +1,221 @@
<script lang="ts">
import { Trash2, ChevronUp, ChevronDown, Plus } from 'lucide-svelte';
import type { DraftIng } from './recipe-editor-types';
type Props = {
ing: DraftIng;
idx: number;
total: number;
onmove: (dir: -1 | 1) => void;
onremove: () => void;
onaddSection: () => void;
onremoveSection: () => void;
};
let { ing, idx, total, onmove, onremove, onaddSection, onremoveSection }: Props = $props();
</script>
{#if ing.section_heading === null}
<li class="section-insert">
<button type="button" class="add-section" onclick={onaddSection}>
<Plus size={12} strokeWidth={2.5} />
<span>Abschnitt hinzufügen</span>
</button>
</li>
{:else}
<li class="section-heading-row">
<input
class="section-heading"
type="text"
bind:value={ing.section_heading}
placeholder='Sektion, z. B. Für den Teig"'
aria-label="Sektionsüberschrift"
/>
<button
type="button"
class="section-remove"
aria-label="Sektion entfernen"
onclick={onremoveSection}
>
<Trash2 size={14} strokeWidth={2} />
</button>
</li>
{/if}
<li class="ing-row">
<div class="move">
<button
class="move-btn"
type="button"
aria-label="Zutat nach oben"
disabled={idx === 0}
onclick={() => onmove(-1)}
>
<ChevronUp size={14} strokeWidth={2.5} />
</button>
<button
class="move-btn"
type="button"
aria-label="Zutat nach unten"
disabled={idx === total - 1}
onclick={() => onmove(1)}
>
<ChevronDown size={14} strokeWidth={2.5} />
</button>
</div>
<input class="qty" type="text" bind:value={ing.qty} placeholder="Menge" aria-label="Menge" />
<input class="unit" type="text" bind:value={ing.unit} placeholder="Einheit" aria-label="Einheit" />
<input class="name" type="text" bind:value={ing.name} placeholder="Zutat" aria-label="Zutat" />
<input class="note" type="text" bind:value={ing.note} placeholder="Notiz" aria-label="Notiz" />
<button class="del" type="button" aria-label="Zutat entfernen" onclick={onremove}>
<Trash2 size={16} strokeWidth={2} />
</button>
</li>
<style>
.ing-row {
display: grid;
grid-template-columns: 28px 70px 70px 1fr 1fr 40px;
gap: 0.35rem;
align-items: center;
}
.move {
display: flex;
flex-direction: column;
gap: 2px;
}
.move-btn {
width: 28px;
height: 20px;
border: 1px solid #cfd9d1;
background: white;
border-radius: 6px;
cursor: pointer;
color: #555;
display: inline-flex;
align-items: center;
justify-content: center;
padding: 0;
}
.move-btn:hover:not(:disabled) {
background: #f4f8f5;
}
.move-btn:disabled {
opacity: 0.3;
cursor: not-allowed;
}
.ing-row input {
padding: 0.5rem 0.55rem;
border: 1px solid #cfd9d1;
border-radius: 8px;
font-size: 0.9rem;
min-height: 38px;
font-family: inherit;
min-width: 0;
}
.del {
width: 40px;
height: 40px;
border: 1px solid #f1b4b4;
background: white;
color: #c53030;
border-radius: 8px;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
}
.del:hover {
background: #fdf3f3;
}
@media (max-width: 560px) {
.ing-row {
grid-template-columns: 28px 70px 1fr 40px;
grid-template-areas:
'move qty name del'
'move unit unit del'
'note note note note';
}
.ing-row .move {
grid-area: move;
}
.ing-row .qty {
grid-area: qty;
}
.ing-row .unit {
grid-area: unit;
}
.ing-row .name {
grid-area: name;
}
.ing-row .note {
grid-area: note;
}
.ing-row .del {
grid-area: del;
}
}
.section-insert {
display: flex;
justify-content: center;
list-style: none;
margin: -0.2rem 0 0.1rem;
opacity: 0;
transition: opacity 0.15s;
}
/* Parent-UL liegt im RecipeEditor, daher :global(.ing-list). Ohne das
scopt Svelte die Klasse und der Selector matcht zur Laufzeit nicht. */
:global(.ing-list):hover .section-insert,
.section-insert:focus-within {
opacity: 1;
}
.add-section {
display: inline-flex;
align-items: center;
gap: 0.25rem;
padding: 0.2rem 0.55rem;
border: 1px dashed #cfd9d1;
background: white;
color: #2b6a3d;
border-radius: 999px;
cursor: pointer;
font-size: 0.75rem;
font-family: inherit;
}
.add-section:hover {
background: #f4f8f5;
}
.section-heading-row {
display: grid;
grid-template-columns: 1fr 32px;
gap: 0.35rem;
list-style: none;
margin-top: 0.4rem;
}
.section-heading {
padding: 0.45rem 0.7rem;
border: 1px solid #cfd9d1;
border-radius: 8px;
font-size: 0.95rem;
font-weight: 600;
color: #2b6a3d;
font-family: inherit;
background: #f4f8f5;
}
.section-remove {
width: 32px;
height: 38px;
border: 1px solid #cfd9d1;
background: white;
border-radius: 8px;
color: #666;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
}
.section-remove:hover {
background: #fdf3f3;
border-color: #f1b4b4;
color: #c53030;
}
</style>

View File

@@ -99,7 +99,7 @@
align-items: center; align-items: center;
gap: 0.4rem; gap: 0.4rem;
padding: 0.5rem 0.9rem; padding: 0.5rem 0.9rem;
border-radius: 999px; border-radius: var(--pill-radius);
border: 1px solid #cfd9d1; border: 1px solid #cfd9d1;
background: white; background: white;
font-size: 0.95rem; font-size: 0.95rem;

View File

@@ -1,6 +1,11 @@
<script lang="ts"> <script lang="ts">
import { Plus, Trash2, GripVertical } from 'lucide-svelte'; import { untrack } from 'svelte';
import { Plus } from 'lucide-svelte';
import type { Recipe, Ingredient, Step } from '$lib/types'; import type { Recipe, Ingredient, Step } from '$lib/types';
import ImageUploadBox from '$lib/components/ImageUploadBox.svelte';
import IngredientRow from '$lib/components/IngredientRow.svelte';
import StepList from '$lib/components/StepList.svelte';
import type { DraftIng, DraftStep } from '$lib/components/recipe-editor-types';
type Props = { type Props = {
recipe: Recipe; recipe: Recipe;
@@ -16,43 +21,60 @@
steps: Step[]; steps: Step[];
}) => void | Promise<void>; }) => void | Promise<void>;
oncancel: () => void; oncancel: () => void;
/** Fires whenever the image was uploaded or removed — separate from save,
* because the image is its own endpoint and persists immediately. */
onimagechange?: (image_path: string | null) => void;
}; };
let { recipe, saving = false, onsave, oncancel }: Props = $props(); let { recipe, saving = false, onsave, oncancel, onimagechange }: Props = $props();
let title = $state(recipe.title); // Form-lokaler Zustand: Initialwerte aus dem Prop snapshotten (untrack),
let description = $state(recipe.description ?? ''); // damit User-Edits nicht von prop-Updates ueberschrieben werden.
let servings = $state<number | ''>(recipe.servings_default ?? ''); let title = $state(untrack(() => recipe.title));
let prepMin = $state<number | ''>(recipe.prep_time_min ?? ''); let description = $state(untrack(() => recipe.description ?? ''));
let cookMin = $state<number | ''>(recipe.cook_time_min ?? ''); let servings = $state<number | ''>(untrack(() => recipe.servings_default ?? ''));
let totalMin = $state<number | ''>(recipe.total_time_min ?? ''); let prepMin = $state<number | ''>(untrack(() => recipe.prep_time_min ?? ''));
let cookMin = $state<number | ''>(untrack(() => recipe.cook_time_min ?? ''));
type DraftIng = { let totalMin = $state<number | ''>(untrack(() => recipe.total_time_min ?? ''));
qty: string;
unit: string;
name: string;
note: string;
};
type DraftStep = { text: string };
let ingredients = $state<DraftIng[]>( let ingredients = $state<DraftIng[]>(
recipe.ingredients.map((i) => ({ untrack(() =>
qty: i.quantity !== null ? String(i.quantity).replace('.', ',') : '', recipe.ingredients.map((i) => ({
unit: i.unit ?? '', qty: i.quantity !== null ? String(i.quantity).replace('.', ',') : '',
name: i.name, unit: i.unit ?? '',
note: i.note ?? '' name: i.name,
})) note: i.note ?? '',
section_heading: i.section_heading
}))
)
); );
let steps = $state<DraftStep[]>( let steps = $state<DraftStep[]>(
recipe.steps.map((s) => ({ text: s.text })) untrack(() => recipe.steps.map((s) => ({ text: s.text })))
); );
function addIngredient() { function addIngredient() {
ingredients = [...ingredients, { qty: '', unit: '', name: '', note: '' }]; ingredients = [...ingredients, { qty: '', unit: '', name: '', note: '', section_heading: null }];
} }
function removeIngredient(idx: number) { function removeIngredient(idx: number) {
ingredients = ingredients.filter((_, i) => i !== idx); ingredients = ingredients.filter((_, i) => i !== idx);
} }
function moveIngredient(idx: number, dir: -1 | 1) {
const target = idx + dir;
if (target < 0 || target >= ingredients.length) return;
const next = [...ingredients];
[next[idx], next[target]] = [next[target], next[idx]];
ingredients = next;
}
function addSection(idx: number) {
const next = [...ingredients];
next[idx] = { ...next[idx], section_heading: '' };
ingredients = next;
}
function removeSection(idx: number) {
const next = [...ingredients];
next[idx] = { ...next[idx], section_heading: null };
ingredients = next;
}
function addStep() { function addStep() {
steps = [...steps, { text: '' }]; steps = [...steps, { text: '' }];
} }
@@ -83,13 +105,15 @@
if (qty !== null) rawParts.push(String(qty).replace('.', ',')); if (qty !== null) rawParts.push(String(qty).replace('.', ','));
if (unit) rawParts.push(unit); if (unit) rawParts.push(unit);
rawParts.push(name); rawParts.push(name);
const heading = i.section_heading === null ? null : (i.section_heading.trim() || null);
return { return {
position: idx + 1, position: idx + 1,
quantity: qty, quantity: qty,
unit, unit,
name, name,
note, note,
raw_text: rawParts.join(' ') raw_text: rawParts.join(' '),
section_heading: heading
}; };
}); });
const cleanedSteps: Step[] = steps const cleanedSteps: Step[] = steps
@@ -110,6 +134,21 @@
</script> </script>
<div class="editor"> <div class="editor">
{#if recipe.id !== null}
<section class="block">
<h2>Bild</h2>
<ImageUploadBox
recipeId={recipe.id}
imagePath={recipe.image_path}
onchange={(p) => onimagechange?.(p)}
/>
</section>
{:else}
<section class="block info">
<p class="hint">Bild kannst du nach dem Speichern hinzufügen.</p>
</section>
{/if}
<div class="meta"> <div class="meta">
<label class="field"> <label class="field">
<span class="lbl">Titel</span> <span class="lbl">Titel</span>
@@ -148,16 +187,15 @@
<h2>Zutaten</h2> <h2>Zutaten</h2>
<ul class="ing-list"> <ul class="ing-list">
{#each ingredients as ing, idx (idx)} {#each ingredients as ing, idx (idx)}
<li class="ing-row"> <IngredientRow
<span class="grip" aria-hidden="true"><GripVertical size={16} /></span> {ing}
<input class="qty" type="text" bind:value={ing.qty} placeholder="Menge" aria-label="Menge" /> {idx}
<input class="unit" type="text" bind:value={ing.unit} placeholder="Einheit" aria-label="Einheit" /> total={ingredients.length}
<input class="name" type="text" bind:value={ing.name} placeholder="Zutat" aria-label="Zutat" /> onmove={(dir) => moveIngredient(idx, dir)}
<input class="note" type="text" bind:value={ing.note} placeholder="Notiz" aria-label="Notiz" /> onremove={() => removeIngredient(idx)}
<button class="del" type="button" aria-label="Zutat entfernen" onclick={() => removeIngredient(idx)}> onaddSection={() => addSection(idx)}
<Trash2 size={16} strokeWidth={2} /> onremoveSection={() => removeSection(idx)}
</button> />
</li>
{/each} {/each}
</ul> </ul>
<button class="add" type="button" onclick={addIngredient}> <button class="add" type="button" onclick={addIngredient}>
@@ -168,25 +206,7 @@
<section class="block"> <section class="block">
<h2>Zubereitung</h2> <h2>Zubereitung</h2>
<ol class="step-list"> <StepList {steps} onadd={addStep} onremove={removeStep} />
{#each steps as step, idx (idx)}
<li class="step-row">
<span class="num">{idx + 1}</span>
<textarea
bind:value={step.text}
rows="3"
placeholder="Schritt beschreiben …"
></textarea>
<button class="del" type="button" aria-label="Schritt entfernen" onclick={() => removeStep(idx)}>
<Trash2 size={16} strokeWidth={2} />
</button>
</li>
{/each}
</ol>
<button class="add" type="button" onclick={addStep}>
<Plus size={16} strokeWidth={2} />
<span>Schritt hinzufügen</span>
</button>
</section> </section>
<div class="foot"> <div class="foot">
@@ -257,8 +277,16 @@
margin: 0 0 0.75rem; margin: 0 0 0.75rem;
color: #2b6a3d; color: #2b6a3d;
} }
.ing-list, .block.info {
.step-list { background: #f6faf7;
border: 1px dashed #cfd9d1;
}
.hint {
color: #666;
margin: 0;
font-size: 0.9rem;
}
.ing-list {
list-style: none; list-style: none;
padding: 0; padding: 0;
margin: 0 0 0.6rem; margin: 0 0 0.6rem;
@@ -266,68 +294,6 @@
flex-direction: column; flex-direction: column;
gap: 0.4rem; gap: 0.4rem;
} }
.ing-row {
display: grid;
grid-template-columns: 16px 70px 70px 1fr 90px 40px;
gap: 0.35rem;
align-items: center;
}
.grip {
color: #bbb;
display: inline-flex;
justify-content: center;
}
.ing-row input {
padding: 0.5rem 0.55rem;
border: 1px solid #cfd9d1;
border-radius: 8px;
font-size: 0.9rem;
min-height: 38px;
font-family: inherit;
min-width: 0;
}
.step-row {
display: grid;
grid-template-columns: 32px 1fr 40px;
gap: 0.5rem;
align-items: start;
}
.num {
width: 32px;
height: 32px;
background: #2b6a3d;
color: white;
border-radius: 50%;
display: grid;
place-items: center;
font-weight: 600;
font-size: 0.9rem;
margin-top: 0.25rem;
}
.step-row textarea {
padding: 0.55rem 0.7rem;
border: 1px solid #cfd9d1;
border-radius: 8px;
font-size: 0.95rem;
font-family: inherit;
resize: vertical;
min-height: 70px;
}
.del {
width: 40px;
height: 40px;
border: 1px solid #f1b4b4;
background: white;
color: #c53030;
border-radius: 8px;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
}
.del:hover {
background: #fdf3f3;
}
.add { .add {
display: inline-flex; display: inline-flex;
align-items: center; align-items: center;
@@ -373,31 +339,4 @@
opacity: 0.6; opacity: 0.6;
cursor: progress; cursor: progress;
} }
@media (max-width: 560px) {
.ing-row {
grid-template-columns: 70px 1fr 40px;
grid-template-areas:
'qty name del'
'unit unit del'
'note note note';
}
.grip {
display: none;
}
.ing-row .qty {
grid-area: qty;
}
.ing-row .unit {
grid-area: unit;
}
.ing-row .name {
grid-area: name;
}
.ing-row .note {
grid-area: note;
}
.ing-row .del {
grid-area: del;
}
}
</style> </style>

View File

@@ -1,6 +1,7 @@
<script lang="ts"> <script lang="ts">
import { scaleIngredients } from '$lib/recipes/scaler'; import { scaleIngredients } from '$lib/recipes/scaler';
import type { Recipe } from '$lib/types'; import type { Recipe } from '$lib/types';
import TimeDisplay from '$lib/components/TimeDisplay.svelte';
type Props = { type Props = {
recipe: Recipe; recipe: Recipe;
@@ -41,15 +42,6 @@
if (Number.isInteger(q)) return String(q); if (Number.isInteger(q)) return String(q);
return q.toLocaleString('de-DE', { maximumFractionDigits: 2 }); return q.toLocaleString('de-DE', { maximumFractionDigits: 2 });
} }
function timeSummary(): string {
const parts: string[] = [];
if (recipe.prep_time_min) parts.push(`Vorb. ${recipe.prep_time_min} min`);
if (recipe.cook_time_min) parts.push(`Kochen ${recipe.cook_time_min} min`);
if (!recipe.prep_time_min && !recipe.cook_time_min && recipe.total_time_min)
parts.push(`Gesamt ${recipe.total_time_min} min`);
return parts.join(' · ');
}
</script> </script>
{#if banner} {#if banner}
@@ -79,9 +71,11 @@
{/each} {/each}
{/if} {/if}
</div> </div>
{#if timeSummary()} <TimeDisplay
<p class="times">{timeSummary()}</p> prepTimeMin={recipe.prep_time_min}
{/if} cookTimeMin={recipe.cook_time_min}
totalTimeMin={recipe.total_time_min}
/>
{#if recipe.source_url} {#if recipe.source_url}
<p class="src"> <p class="src">
Quelle: <a href={recipe.source_url} target="_blank" rel="noopener">{recipe.source_domain}</a> Quelle: <a href={recipe.source_url} target="_blank" rel="noopener">{recipe.source_domain}</a>
@@ -133,6 +127,9 @@
</div> </div>
<ul class="ing-list"> <ul class="ing-list">
{#each scaled as ing, i (i)} {#each scaled as ing, i (i)}
{#if ing.section_heading && ing.section_heading.trim()}
<li class="section-heading">{ing.section_heading}</li>
{/if}
<li> <li>
{#if ing.quantity !== null || ing.unit} {#if ing.quantity !== null || ing.unit}
<span class="qty"> <span class="qty">
@@ -204,7 +201,7 @@
.pill { .pill {
padding: 0.15rem 0.55rem; padding: 0.15rem 0.55rem;
background: #eaf4ed; background: #eaf4ed;
border-radius: 999px; border-radius: var(--pill-radius);
font-size: 0.8rem; font-size: 0.8rem;
color: #2b6a3d; color: #2b6a3d;
} }
@@ -212,11 +209,6 @@
font-size: 0.8rem; font-size: 0.8rem;
color: #888; color: #888;
} }
.times {
margin: 0 0 0.25rem;
color: #666;
font-size: 0.9rem;
}
.src { .src {
margin: 0; margin: 0;
font-size: 0.85rem; font-size: 0.85rem;
@@ -292,6 +284,19 @@
padding: 0; padding: 0;
margin: 0; margin: 0;
} }
.ing-list .section-heading {
list-style: none;
font-weight: 700;
color: #2b6a3d;
font-size: 1.2rem;
margin-top: 1.1rem;
margin-bottom: 0.3rem;
padding: 0.2rem 0;
border-bottom: 1px solid #e4eae7;
}
.ing-list .section-heading:first-child {
margin-top: 0;
}
.ing-list li { .ing-list li {
display: flex; display: flex;
gap: 0.75rem; gap: 0.75rem;
@@ -347,7 +352,9 @@
/* Querformat-Tablets und Desktop: Zutaten + Zubereitung nebeneinander, /* Querformat-Tablets und Desktop: Zutaten + Zubereitung nebeneinander,
Tabs ausgeblendet. Zutaten sticky, damit sie beim Scrollen der Tabs ausgeblendet. Zutaten sticky, damit sie beim Scrollen der
Zubereitung oben bleiben. */ Zubereitung oben bleiben.
Schriftgrößen hier bewusst größer — das Rezept wird auf einem 10"-
Tablet beim Kochen aus ~50 cm Abstand gelesen. */
@media (min-width: 820px) { @media (min-width: 820px) {
.tabs { .tabs {
display: none; display: none;
@@ -367,5 +374,30 @@
max-height: calc(100vh - 2rem); max-height: calc(100vh - 2rem);
overflow-y: auto; overflow-y: auto;
} }
.ing-list li {
font-size: 1.2rem;
line-height: 1.5;
padding: 0.85rem 0.25rem;
}
.qty {
min-width: 6rem;
}
.srv-value strong {
font-size: 1.5rem;
}
.srv-value span {
font-size: 1rem;
}
.steps li {
font-size: 1.2rem;
line-height: 1.55;
padding: 1rem 0 1rem 3.4rem;
}
.steps li::before {
width: 2.4rem;
height: 2.4rem;
font-size: 1.1rem;
top: 1rem;
}
} }
</style> </style>

View File

@@ -0,0 +1,75 @@
<script lang="ts">
import { X, Minus, Plus } from 'lucide-svelte';
import type { ShoppingCartRecipe } from '$lib/server/shopping/repository';
let { recipe, onServingsChange, onRemove }: {
recipe: ShoppingCartRecipe;
onServingsChange: (id: number, servings: number) => void;
onRemove: (id: number) => void;
} = $props();
function dec() {
if (recipe.servings > 1) onServingsChange(recipe.recipe_id, recipe.servings - 1);
}
function inc() {
if (recipe.servings < 50) onServingsChange(recipe.recipe_id, recipe.servings + 1);
}
</script>
<div class="chip">
<a class="title" href={`/recipes/${recipe.recipe_id}`}>{recipe.title}</a>
<div class="controls">
<button aria-label="Portion weniger" onclick={dec} disabled={recipe.servings <= 1}>
<Minus size={16} />
</button>
<span class="val" aria-label="Portionen">{recipe.servings}p</span>
<button aria-label="Portion mehr" onclick={inc} disabled={recipe.servings >= 50}>
<Plus size={16} />
</button>
<button aria-label="Rezept aus Einkaufsliste entfernen" class="rm" onclick={() => onRemove(recipe.recipe_id)}>
<X size={16} />
</button>
</div>
</div>
<style>
.chip {
flex: 0 0 auto;
padding: 0.5rem 0.75rem;
background: white;
border: 1px solid #cfd9d1;
border-radius: 14px;
display: flex;
flex-direction: column;
gap: 0.35rem;
min-width: 140px;
}
.title {
color: #2b6a3d;
font-weight: 600;
font-size: 0.92rem;
text-decoration: none;
line-height: 1.2;
max-width: 160px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.controls { display: flex; gap: 0.25rem; align-items: center; }
.controls button {
min-width: 32px;
min-height: 32px;
border-radius: 8px;
border: 1px solid #e4eae7;
background: white;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
color: #444;
}
.controls button:disabled { opacity: 0.4; cursor: not-allowed; }
.controls button.rm { margin-left: auto; }
.controls button.rm:hover { color: #c53030; border-color: #f1b4b4; background: #fdf3f3; }
.val { min-width: 32px; text-align: center; font-weight: 600; color: #444; }
</style>

View File

@@ -0,0 +1,57 @@
<script lang="ts">
import type { ShoppingListRow } from '$lib/server/shopping/repository';
import { formatQuantity } from '$lib/quantity-format';
let { row, onToggle }: {
row: ShoppingListRow;
onToggle: (row: ShoppingListRow, next: boolean) => void;
} = $props();
const qtyStr = $derived(formatQuantity(row.total_quantity));
const hasUnit = $derived(!!row.display_unit && row.display_unit.trim().length > 0);
</script>
<label class="row" class:checked={row.checked}>
<input
type="checkbox"
checked={row.checked === 1}
onchange={(e) => onToggle(row, (e.currentTarget as HTMLInputElement).checked)}
/>
<span class="text">
<span class="name">
{#if qtyStr}
<span class="qty">{qtyStr}{hasUnit ? ` ${row.display_unit}` : ''}</span>
{/if}
{row.display_name}
</span>
<span class="src">aus {row.from_recipes}</span>
</span>
</label>
<style>
.row {
display: flex;
gap: 0.75rem;
align-items: flex-start;
padding: 0.75rem;
border: 1px solid #e4eae7;
border-radius: 10px;
background: white;
cursor: pointer;
min-height: 60px;
}
.row input {
width: 24px;
height: 24px;
margin-top: 0.1rem;
flex-shrink: 0;
accent-color: #2b6a3d;
}
.text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 0.2rem; }
.name { font-size: 1rem; }
.qty { font-weight: 600; margin-right: 0.3rem; }
.src { color: #888; font-size: 0.82rem; }
.row.checked { background: #f6f8f7; }
.row.checked .name,
.row.checked .qty { text-decoration: line-through; color: #888; }
</style>

View File

@@ -0,0 +1,101 @@
<script lang="ts">
import { Plus, Trash2 } from 'lucide-svelte';
import type { DraftStep } from './recipe-editor-types';
type Props = {
steps: DraftStep[];
onadd: () => void;
onremove: (idx: number) => void;
};
let { steps, onadd, onremove }: Props = $props();
</script>
<ol class="step-list">
{#each steps as step, idx (idx)}
<li class="step-row">
<span class="num">{idx + 1}</span>
<textarea
bind:value={step.text}
rows="3"
placeholder="Schritt beschreiben …"
></textarea>
<button class="del" type="button" aria-label="Schritt entfernen" onclick={() => onremove(idx)}>
<Trash2 size={16} strokeWidth={2} />
</button>
</li>
{/each}
</ol>
<button class="add" type="button" onclick={onadd}>
<Plus size={16} strokeWidth={2} />
<span>Schritt hinzufügen</span>
</button>
<style>
.step-list {
list-style: none;
padding: 0;
margin: 0 0 0.6rem;
display: flex;
flex-direction: column;
gap: 0.4rem;
}
.step-row {
display: grid;
grid-template-columns: 32px 1fr 40px;
gap: 0.5rem;
align-items: start;
}
.num {
width: 32px;
height: 32px;
background: #2b6a3d;
color: white;
border-radius: 50%;
display: grid;
place-items: center;
font-weight: 600;
font-size: 0.9rem;
margin-top: 0.25rem;
}
.step-row textarea {
padding: 0.55rem 0.7rem;
border: 1px solid #cfd9d1;
border-radius: 8px;
font-size: 0.95rem;
font-family: inherit;
resize: vertical;
min-height: 70px;
}
.del {
width: 40px;
height: 40px;
border: 1px solid #f1b4b4;
background: white;
color: #c53030;
border-radius: 8px;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
}
.del:hover {
background: #fdf3f3;
}
.add {
display: inline-flex;
align-items: center;
gap: 0.35rem;
padding: 0.55rem 0.9rem;
border: 1px dashed #cfd9d1;
background: white;
color: #2b6a3d;
border-radius: 8px;
cursor: pointer;
font-size: 0.9rem;
font-family: inherit;
}
.add:hover {
background: #f4f8f5;
}
</style>

View File

@@ -77,7 +77,7 @@
padding: 0.3rem 0.65rem; padding: 0.3rem 0.65rem;
background: white; background: white;
border: 1px solid #cfd9d1; border: 1px solid #cfd9d1;
border-radius: 999px; border-radius: var(--pill-radius);
color: #555; color: #555;
font-size: 0.78rem; font-size: 0.78rem;
cursor: pointer; cursor: pointer;

View File

@@ -0,0 +1,30 @@
<script lang="ts">
type Props = {
prepTimeMin: number | null;
cookTimeMin: number | null;
totalTimeMin: number | null;
};
let { prepTimeMin, cookTimeMin, totalTimeMin }: Props = $props();
const summary = $derived.by(() => {
const parts: string[] = [];
if (prepTimeMin) parts.push(`Vorb. ${prepTimeMin} min`);
if (cookTimeMin) parts.push(`Kochen ${cookTimeMin} min`);
if (!prepTimeMin && !cookTimeMin && totalTimeMin)
parts.push(`Gesamt ${totalTimeMin} min`);
return parts.join(' · ');
});
</script>
{#if summary}
<p class="times">{summary}</p>
{/if}
<style>
.times {
margin: 0 0 0.25rem;
color: #666;
font-size: 0.9rem;
}
</style>

View File

@@ -28,7 +28,7 @@
padding: 0.6rem 0.85rem 0.6rem 1.1rem; padding: 0.6rem 0.85rem 0.6rem 1.1rem;
background: #1a1a1a; background: #1a1a1a;
color: white; color: white;
border-radius: 999px; border-radius: var(--pill-radius);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3); box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
z-index: 500; z-index: 500;
max-width: calc(100% - 2rem); max-width: calc(100% - 2rem);
@@ -58,7 +58,7 @@
background: #2b6a3d; background: #2b6a3d;
color: white; color: white;
border: 0; border: 0;
border-radius: 999px; border-radius: var(--pill-radius);
font-size: 0.88rem; font-size: 0.88rem;
cursor: pointer; cursor: pointer;
font-weight: 600; font-weight: 600;
@@ -75,7 +75,7 @@
padding: 4px; padding: 4px;
display: inline-flex; display: inline-flex;
align-items: center; align-items: center;
border-radius: 999px; border-radius: var(--pill-radius);
flex-shrink: 0; flex-shrink: 0;
} }
.dismiss:hover { .dismiss:hover {

View File

@@ -0,0 +1,9 @@
export type DraftIng = {
qty: string;
unit: string;
name: string;
note: string;
section_heading: string | null;
};
export type DraftStep = { text: string };

11
src/lib/constants.ts Normal file
View File

@@ -0,0 +1,11 @@
// Shared timing constants. Keep magic numbers here so callers stay readable
// and the rationale lives next to the value.
// How long to wait for a Service Worker to answer GET_VERSION before
// treating the response as missing. Short on purpose — SWs that take this
// long are likely the Chromium zombie case (see pwa.svelte.ts).
export const SW_VERSION_QUERY_TIMEOUT_MS = 1500;
// Active update check while the page sits open in a tab. 30 minutes is a
// trade-off between being timely and not hammering the server.
export const SW_UPDATE_POLL_INTERVAL_MS = 30 * 60_000;

View File

@@ -0,0 +1,7 @@
export function formatQuantity(q: number | null): string {
if (q === null || q === undefined) return '';
const rounded = Math.round(q);
if (Math.abs(q - rounded) < 0.01) return String(rounded);
// auf max. 2 Nachkommastellen, trailing Nullen raus
return q.toFixed(2).replace(/\.?0+$/, '');
}

View File

@@ -0,0 +1,56 @@
export const DESCRIPTION_PHRASES: readonly string[] = [
'Mit dem Zauberstab aus dem Kochbuch geholt.',
'Foto-Magie frisch aus dem Ofen.',
'Aus dem Bild herbeigezaubert.',
'Ein Klick, ein Foto, fertig.',
'Knipsen statt Abtippen.',
'Von der Buchseite direkt in die Pfanne.',
'Die Kamera hat mitgelesen.',
'Abrakadabra — Rezept da.',
'Per Linse in die Küche teleportiert.',
'Von Oma abfotografiert, von der KI entziffert.',
'Frisch aus dem Bilderrahmen.',
'Klick, zisch, Rezept.',
'Das Foto wurde überredet, sich zu verraten.',
'Schnappschuss zur Schüssel.',
'Einmal lesen lassen, schon da.',
'Keine Hand hat dieses Rezept abgetippt.',
'Vom Bild in die Bratpfanne.',
'Papier ist geduldig, das Foto war es auch.',
'Eine Seite, ein Foto, ein Rezept.',
'Die KI hat drübergeschielt.',
'Handschriftlich entziffert — oder zumindest versucht.',
'Aus der Linse in die Liste.',
'Vom Küchentisch zur Kachel.',
'Knips und weg — zumindest der Zettel.',
'Das Bild hat geredet.',
'Keine Tippfehler, nur Sehfehler.',
'Per Foto eingebürgert.',
'Rezept-Übersetzung aus dem Bild.',
'Die Seite hat sich verraten.',
'Blitzlicht und dann Gulasch.',
'Ein Augenzwinkern der Kamera genügte.',
'Geknipst, gelesen, gespeichert.',
'Fotografische Gedächtnishilfe.',
'Aus der Schublade ans Licht.',
'Das Rezept stand schon da — wir haben nur hingeguckt.',
'Zaubertrick mit Kamera.',
'Vom Papier befreit.',
'Ein Foto sagt mehr als tausend Zutatenlisten.',
'Eingescannt, rausgelesen, reingeschrieben.',
'Die Kamera als Küchenhilfe.',
'Handy hoch, Rezept runter.',
'Aus dem Kochbuch gebeamt.',
'Ein scharfes Foto, ein klares Rezept.',
'Vom Regal zur App in einem Schritt.',
'Aus dem Bild geschöpft wie Suppe aus dem Topf.',
'Optisch erfasst, digital serviert.',
'Das Kleingedruckte hat die KI gelesen.',
'Vom Kladdenzettel in die Datenbank.',
'Kurz gezückt, schon gekocht.',
'Kein Schreibkrampf, nur ein Klick.'
];
export function pickRandomPhrase(): string {
return DESCRIPTION_PHRASES[Math.floor(Math.random() * DESCRIPTION_PHRASES.length)];
}

View File

@@ -0,0 +1,170 @@
import { GoogleGenerativeAI } from '@google/generative-ai';
import { env } from '$env/dynamic/private';
import {
RECIPE_EXTRACTION_SYSTEM_PROMPT,
RECIPE_EXTRACTION_USER_PROMPT,
GEMINI_RESPONSE_SCHEMA,
extractionResponseSchema,
type ExtractionResponse
} from './recipe-extraction-prompt';
export type GeminiErrorCode =
| 'AI_NOT_CONFIGURED'
| 'AI_RATE_LIMITED'
| 'AI_TIMEOUT'
| 'AI_FAILED';
export class GeminiError extends Error {
constructor(
public readonly code: GeminiErrorCode,
message: string
) {
super(message);
this.name = 'GeminiError';
}
}
function getStatus(err: unknown): number | undefined {
if (err && typeof err === 'object' && 'status' in err) {
const s = (err as { status?: unknown }).status;
if (typeof s === 'number') return s;
}
return undefined;
}
function getCfg(): { apiKey: string; model: string; timeoutMs: number } {
const apiKey = env.GEMINI_API_KEY ?? process.env.GEMINI_API_KEY ?? '';
const model =
env.GEMINI_MODEL ?? process.env.GEMINI_MODEL ?? 'gemini-2.5-flash';
const rawTimeout =
env.GEMINI_TIMEOUT_MS ?? process.env.GEMINI_TIMEOUT_MS ?? '20000';
const timeoutMs = Number(rawTimeout) || 20000;
return { apiKey, model, timeoutMs };
}
async function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
return new Promise((resolve, reject) => {
const timer = setTimeout(
() => reject(new GeminiError('AI_TIMEOUT', `Gemini timeout after ${ms} ms`)),
ms
);
promise.then(
(v) => {
clearTimeout(timer);
resolve(v);
},
(e) => {
clearTimeout(timer);
reject(e);
}
);
});
}
async function callGemini(
imageBuffer: Buffer,
mimeType: string,
appendUserNote?: string
): Promise<ExtractionResponse> {
const { apiKey, model: modelId, timeoutMs } = getCfg();
if (!apiKey) {
throw new GeminiError('AI_NOT_CONFIGURED', 'GEMINI_API_KEY is not set');
}
const client = new GoogleGenerativeAI(apiKey);
const model = client.getGenerativeModel({
model: modelId,
systemInstruction: RECIPE_EXTRACTION_SYSTEM_PROMPT,
generationConfig: {
temperature: 0.1,
responseMimeType: 'application/json',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
responseSchema: GEMINI_RESPONSE_SCHEMA as any
}
});
const parts: Array<
{ inlineData: { data: string; mimeType: string } } | { text: string }
> = [
{ inlineData: { data: imageBuffer.toString('base64'), mimeType } },
{ text: RECIPE_EXTRACTION_USER_PROMPT }
];
if (appendUserNote) parts.push({ text: appendUserNote });
const result = await withTimeout(
model.generateContent({ contents: [{ role: 'user', parts }] }),
timeoutMs
);
const text = result.response.text();
let parsed: unknown;
try {
parsed = JSON.parse(text);
} catch {
throw new GeminiError('AI_FAILED', 'Gemini returned non-JSON output');
}
const validated = extractionResponseSchema.safeParse(parsed);
if (!validated.success) {
throw new GeminiError(
'AI_FAILED',
`Schema validation failed: ${validated.error.message}`
);
}
return validated.data;
}
// Public entry: one retry on recoverable failures (5xx or schema-invalid),
// no retry on 429, AI_TIMEOUT, or config errors.
export async function extractRecipeFromImage(
imageBuffer: Buffer,
mimeType: string
): Promise<ExtractionResponse> {
let firstMsg: string | null = null;
try {
return await callGemini(imageBuffer, mimeType);
} catch (e) {
if (e instanceof GeminiError && e.code === 'AI_NOT_CONFIGURED') throw e;
if (e instanceof GeminiError && e.code === 'AI_TIMEOUT') throw e;
const status = getStatus(e);
if (status === 429) throw new GeminiError('AI_RATE_LIMITED', 'Gemini rate limit');
const recoverable =
(e instanceof GeminiError && e.code === 'AI_FAILED') ||
(status !== undefined && status >= 500);
if (!recoverable) {
throw e instanceof GeminiError
? e
: new GeminiError('AI_FAILED', String(e));
}
firstMsg = e instanceof Error ? e.message : String(e);
console.warn(`[gemini-client] first attempt failed, retrying: ${firstMsg}`);
await new Promise((r) => setTimeout(r, 500));
try {
return await callGemini(
imageBuffer,
mimeType,
'Dein vorheriger Output war ungültig. Bitte antworte ausschließlich mit JSON gemäß Schema.'
);
} catch (retryErr) {
const retryMsg = retryErr instanceof Error ? retryErr.message : String(retryErr);
if (retryErr instanceof GeminiError) {
if (retryErr.code === 'AI_FAILED') {
throw new GeminiError(
'AI_FAILED',
`retry failed: ${retryMsg} (first: ${firstMsg})`
);
}
throw retryErr;
}
const retryStatus = getStatus(retryErr);
if (retryStatus === 429)
throw new GeminiError('AI_RATE_LIMITED', 'Gemini rate limit on retry');
throw new GeminiError(
'AI_FAILED',
`retry failed: ${retryMsg} (first: ${firstMsg})`
);
}
}
}

View File

@@ -0,0 +1,54 @@
import type SharpType from 'sharp';
import { createRequire } from 'node:module';
const MAX_EDGE = 1600;
const JPEG_QUALITY = 85;
export type PreprocessedImage = {
buffer: Buffer;
mimeType: 'image/jpeg';
};
// sharp per Node-Runtime-require laden, nicht via ES-Import: adapter-node
// bundelt ES-Imports (auch dynamische, auch mit @vite-ignore) ins Server-
// Bundle, was sharp's internes dynamic-require fuer die Plattform-.node-Binary
// zerstoert. createRequire + require() ist pure Node-Runtime-Logik, die
// Rollup nicht anfasst -- sharp wird regulaer aus node_modules geladen.
const nodeRequire = createRequire(import.meta.url);
let sharpModule: typeof SharpType | null = null;
function loadSharp(): typeof SharpType {
if (!sharpModule) {
sharpModule = nodeRequire('sharp') as typeof SharpType;
}
return sharpModule;
}
// Resize auf max 1600px lange Kante, JPEG re-encode, Metadata strippen.
// sharp liest HEIC/HEIF transparent, wenn libheif im libvips-Build enthalten ist
// (in Alpine's vips-dev + in den offiziellen sharp-Prebuilds).
export async function preprocessImage(input: Buffer): Promise<PreprocessedImage> {
const sharp = loadSharp();
const pipeline = sharp(input, { failOn: 'error' }).rotate(); // respect EXIF orientation
const meta = await pipeline.metadata();
if (!meta.width || !meta.height) {
throw new Error('Unable to read image dimensions');
}
const longEdge = Math.max(meta.width, meta.height);
const resized =
longEdge > MAX_EDGE
? pipeline.resize({
width: meta.width >= meta.height ? MAX_EDGE : undefined,
height: meta.height > meta.width ? MAX_EDGE : undefined,
withoutEnlargement: true
})
: pipeline;
// Default-Verhalten seit sharp 0.33: alle Metadata (EXIF/IPTC/XMP) werden
// gestripped. Nur `.keepMetadata()`/`.keepExif()` würde sie erhalten.
const buffer = await resized
.jpeg({ quality: JPEG_QUALITY, mozjpeg: true })
.toBuffer();
return { buffer, mimeType: 'image/jpeg' };
}

View File

@@ -0,0 +1,21 @@
export type RateLimiter = { check: (key: string) => boolean };
export function createRateLimiter(opts: {
windowMs: number;
max: number;
}): RateLimiter {
const store = new Map<string, { count: number; resetAt: number }>();
return {
check(key: string): boolean {
const now = Date.now();
const entry = store.get(key);
if (!entry || entry.resetAt <= now) {
store.set(key, { count: 1, resetAt: now + opts.windowMs });
return true;
}
if (entry.count >= opts.max) return false;
entry.count += 1;
return true;
}
};
}

View File

@@ -0,0 +1,94 @@
import { z } from 'zod';
import { SchemaType } from '@google/generative-ai';
export const RECIPE_EXTRACTION_SYSTEM_PROMPT = `Du bist ein hochpräziser OCR-Experte für kulinarische Dokumente (Rezepte). Deine Aufgabe ist die Extraktion von Rezeptdaten (Titel, Zutaten, Zubereitungsschritte, Zeiten, Portionen) in valides JSON gemäß dem vorgegebenen Schema.
SPRACHE:
- Die Texte sind ausschließlich auf Deutsch. Nutze deutsches Sprachverständnis (Umlaute ä/ö/ü/ß, deutsche Zutatennamen, deutsche Maßeinheiten) als starken Prior bei der Rekonstruktion unklarer Zeichen. Gib die Ausgabe vollständig auf Deutsch zurück.
LOGIK-REGELN FÜR SCHWER LESBARE TEXTE:
- Handle als "Kontext-Detektiv": Wenn Zeichen unklar sind, nutze kulinarisches Wissen zur Rekonstruktion (z.B. "Pr-se" -> "Prise").
- Bei absoluter Unleserlichkeit eines Wortes: Nutze "[?]".
- Halluziniere keine fehlenden Werte: Wenn eine Mengenangabe komplett fehlt, setze 'quantity' auf null. Was nicht auf dem Bild steht, ist null (oder leeres Array).
FORMATIERUNGS-REGELN:
- Zutaten: quantity (Zahl) separat von unit (String). Brüche (½, ¼, 1 ½) strikt in Dezimalzahlen (0.5, 0.25, 1.5).
- Einheiten: Normalisiere auf (g, ml, l, kg, EL, TL, Stück, Prise, Msp).
- Zubereitungsschritte: pro erkennbarer Nummerierung oder Absatz EIN Schritt.
- Zeit: Alle Angaben strikt in Minuten (Integer). "1 Stunde" = 60.
- Rauschen ignorieren: Keine Werbung, Einleitungstexte oder Bildunterschriften extrahieren.
STRIKTE ANWEISUNG: Gib ausschließlich das rohe JSON-Objekt gemäß Schema zurück. Kein Markdown-Code-Block, kein Einleitungstext, keine Prosa.`;
export const RECIPE_EXTRACTION_USER_PROMPT =
'Analysiere dieses Bild hochauflösend. Extrahiere alle rezeptrelevanten Informationen gemäß deiner System-Instruktion. Achte besonders auf schwache Handschriften oder verblassten Text und stelle sicher, dass die Zuordnung von Menge zu Zutat logisch korrekt ist.';
// Gemini responseSchema (Subset von OpenAPI). Wird an GenerativeModel.generateContent
// übergeben; Gemini respektiert die Struktur und liefert valides JSON.
export const GEMINI_RESPONSE_SCHEMA = {
type: SchemaType.OBJECT,
properties: {
title: { type: SchemaType.STRING, nullable: false },
servings_default: { type: SchemaType.INTEGER, nullable: true },
servings_unit: { type: SchemaType.STRING, nullable: true },
prep_time_min: { type: SchemaType.INTEGER, nullable: true },
cook_time_min: { type: SchemaType.INTEGER, nullable: true },
total_time_min: { type: SchemaType.INTEGER, nullable: true },
ingredients: {
type: SchemaType.ARRAY,
items: {
type: SchemaType.OBJECT,
properties: {
quantity: { type: SchemaType.NUMBER, nullable: true },
unit: { type: SchemaType.STRING, nullable: true },
name: { type: SchemaType.STRING, nullable: false },
note: { type: SchemaType.STRING, nullable: true }
},
required: ['name']
}
},
steps: {
type: SchemaType.ARRAY,
items: {
type: SchemaType.OBJECT,
properties: {
text: { type: SchemaType.STRING, nullable: false }
},
required: ['text']
}
}
},
required: ['title', 'ingredients', 'steps']
} as const;
// Zod-Spiegel des Schemas. .strict() verhindert, dass Gemini zusätzliche Keys
// unbemerkt durchschmuggelt.
const ingredientSchema = z
.object({
quantity: z.number().nullable(),
unit: z.string().max(30).nullable(),
name: z.string().min(1).max(200),
note: z.string().max(300).nullable()
})
.strict();
const stepSchema = z
.object({
text: z.string().min(1).max(4000)
})
.strict();
export const extractionResponseSchema = z
.object({
title: z.string().min(1).max(200),
servings_default: z.number().int().nonnegative().nullable(),
servings_unit: z.string().max(30).nullable(),
prep_time_min: z.number().int().nonnegative().nullable(),
cook_time_min: z.number().int().nonnegative().nullable(),
total_time_min: z.number().int().nonnegative().nullable(),
ingredients: z.array(ingredientSchema),
steps: z.array(stepSchema)
})
.strict();
export type ExtractionResponse = z.infer<typeof extractionResponseSchema>;

View File

@@ -0,0 +1,39 @@
import { error } from '@sveltejs/kit';
import type { ZodSchema } from 'zod';
// Shared error body shape for SvelteKit `error()` calls. `issues` is set
// when validateBody fails so the client can show a precise validation
// hint; everywhere else only `message` is used.
export type ErrorResponse = {
message: string;
issues?: unknown;
};
/**
* Parse a route param (or query param) as a positive integer (>=1).
* Throws SvelteKit `error(400)` with `Missing <field>` when null/undefined,
* or `Invalid <field>` when the value is not an integer >= 1.
*/
export function parsePositiveIntParam(
raw: string | undefined | null,
field: string
): number {
if (raw == null) error(400, { message: `Missing ${field}` });
const n = Number(raw);
if (!Number.isInteger(n) || n <= 0) error(400, { message: `Invalid ${field}` });
return n;
}
/**
* Validate an unknown body against a Zod schema. Throws SvelteKit
* `error(400, { message: 'Invalid body', issues })` on mismatch and returns
* the typed parse result on success. Accepts `null` (the typical result of
* `await request.json().catch(() => null)`).
*/
export function validateBody<T>(body: unknown, schema: ZodSchema<T>): T {
const parsed = schema.safeParse(body);
if (!parsed.success) {
error(400, { message: 'Invalid body', issues: parsed.error.issues });
}
return parsed.data;
}

View File

@@ -1,15 +1,15 @@
import Database from 'better-sqlite3'; import Database from 'better-sqlite3';
import { mkdirSync } from 'node:fs'; import { mkdirSync } from 'node:fs';
import { dirname } from 'node:path'; import { dirname } from 'node:path';
import { DATABASE_PATH, IMAGE_DIR } from '$lib/server/paths';
import { runMigrations } from './migrate'; import { runMigrations } from './migrate';
let instance: Database.Database | null = null; let instance: Database.Database | null = null;
export function getDb(path = process.env.DATABASE_PATH ?? './data/kochwas.db'): Database.Database { export function getDb(path = DATABASE_PATH): Database.Database {
if (instance) return instance; if (instance) return instance;
mkdirSync(dirname(path), { recursive: true }); mkdirSync(dirname(path), { recursive: true });
const imageDir = process.env.IMAGE_DIR ?? './data/images'; mkdirSync(IMAGE_DIR, { recursive: true });
mkdirSync(imageDir, { recursive: true });
instance = new Database(path); instance = new Database(path);
instance.pragma('journal_mode = WAL'); instance.pragma('journal_mode = WAL');
instance.pragma('foreign_keys = ON'); instance.pragma('foreign_keys = ON');

View File

@@ -0,0 +1,7 @@
-- Nullable-Spalte fuer optionale Sektionsueberschriften bei Zutaten. User
-- soll im Editor gruppieren koennen ("Fuer den Teig", "Fuer die Fuellung").
-- Rendering-Regel: Ist section_heading gesetzt (nicht NULL, nicht leer),
-- startet an dieser Zeile eine neue Sektion mit diesem Titel; alle folgenden
-- Zutaten gehoeren dazu, bis die naechste Zeile wieder eine Ueberschrift hat.
-- Ordnung bleibt die bestehende position-Spalte.
ALTER TABLE ingredient ADD COLUMN section_heading TEXT;

View File

@@ -0,0 +1,18 @@
-- Einkaufsliste: haushaltsweit geteilt. shopping_cart_recipe haelt die
-- Rezepte im Wagen (inkl. gewuenschter Portionsgroesse), shopping_cart_check
-- die abgehakten aggregierten Zutaten-Zeilen. Aggregation wird bei jedem
-- Read aus shopping_cart_recipe JOIN ingredient derived — nichts
-- materialisiert, damit Rezept-Edits live durchschlagen.
CREATE TABLE shopping_cart_recipe (
recipe_id INTEGER PRIMARY KEY REFERENCES recipe(id) ON DELETE CASCADE,
servings INTEGER NOT NULL CHECK (servings > 0),
added_by_profile_id INTEGER REFERENCES profile(id) ON DELETE SET NULL,
added_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE shopping_cart_check (
name_key TEXT NOT NULL,
unit_key TEXT NOT NULL,
checked_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (name_key, unit_key)
);

View File

@@ -0,0 +1,10 @@
-- Merkt je Profil, wann ein Rezept zuletzt angesehen wurde.
-- Dient als Basis fuer "Zuletzt gesehen"-Sortierung auf der Startseite.
CREATE TABLE recipe_view (
profile_id INTEGER NOT NULL REFERENCES profile(id) ON DELETE CASCADE,
recipe_id INTEGER NOT NULL REFERENCES recipe(id) ON DELETE CASCADE,
last_viewed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (profile_id, recipe_id)
);
CREATE INDEX idx_recipe_view_recent
ON recipe_view (profile_id, last_viewed_at DESC);

View File

@@ -28,6 +28,42 @@ const FRACTION_MAP: Record<string, number> = {
'3/4': 0.75 '3/4': 0.75
}; };
// Vulgar-Fraction-Codepoints — kommen in deutschsprachigen Rezept-Quellen
// regelmäßig vor (Chefkoch et al. liefern sie vereinzelt, mehr aber bei
// Apple's Food App, Fork etc.).
const UNICODE_FRACTION_MAP: Record<string, number> = {
'\u00BD': 0.5, // ½
'\u00BC': 0.25, // ¼
'\u00BE': 0.75, // ¾
'\u2150': 1 / 7,
'\u2151': 1 / 9,
'\u2152': 1 / 10,
'\u2153': 1 / 3, // ⅓
'\u2154': 2 / 3, // ⅔
'\u2155': 0.2, // ⅕
'\u2156': 0.4, // ⅖
'\u2157': 0.6, // ⅗
'\u2158': 0.8, // ⅘
'\u2159': 1 / 6, // ⅙
'\u215A': 5 / 6, // ⅚
'\u215B': 0.125, // ⅛
'\u215C': 0.375, // ⅜
'\u215D': 0.625, // ⅝
'\u215E': 0.875 // ⅞
};
// Mengen außerhalb dieses Bereichs sind fast sicher ein Parse-Müll
// (z. B. Microformat-Date oder Telefon-Nummer in einem JSON-LD-Quantity-
// Feld). Wir geben null zurück, raw_text bleibt für die UI erhalten.
const MAX_REASONABLE_QTY = 10000;
function clampQuantity(n: number | null): number | null {
if (n === null || !Number.isFinite(n)) return null;
if (n <= 0) return null;
if (n > MAX_REASONABLE_QTY) return null;
return n;
}
function parseQuantity(raw: string): number | null { function parseQuantity(raw: string): number | null {
const trimmed = raw.trim(); const trimmed = raw.trim();
if (FRACTION_MAP[trimmed] !== undefined) return FRACTION_MAP[trimmed]; if (FRACTION_MAP[trimmed] !== undefined) return FRACTION_MAP[trimmed];
@@ -39,6 +75,16 @@ function parseQuantity(raw: string): number | null {
return Number.isFinite(num) ? num : null; return Number.isFinite(num) ? num : null;
} }
// Splits "TL Salz" → unit "TL", name "Salz"; "Zitrone" → unit null, name "Zitrone".
function splitUnitAndName(rest: string): { unit: string | null; name: string } {
const trimmed = rest.trim();
const firstTokenMatch = /^(\S+)\s+(.+)$/.exec(trimmed);
if (firstTokenMatch && UNITS.has(firstTokenMatch[1])) {
return { unit: firstTokenMatch[1], name: firstTokenMatch[2].trim() };
}
return { unit: null, name: trimmed };
}
export function parseIngredient(raw: string, position = 0): Ingredient { export function parseIngredient(raw: string, position = 0): Ingredient {
const rawText = raw.trim(); const rawText = raw.trim();
let working = rawText; let working = rawText;
@@ -51,18 +97,24 @@ export function parseIngredient(raw: string, position = 0): Ingredient {
).trim(); ).trim();
} }
// Unicode-Bruch am Anfang? Dann das eine Zeichen als Menge nehmen
// und den Rest wie üblich in Unit + Name aufteilen.
const firstChar = working.charAt(0);
if (UNICODE_FRACTION_MAP[firstChar] !== undefined) {
const tail = working.slice(1).trimStart();
if (tail.length > 0) {
const quantity = clampQuantity(UNICODE_FRACTION_MAP[firstChar]);
const { unit, name } = splitUnitAndName(tail);
return { position, quantity, unit, name, note, raw_text: rawText, section_heading: null };
}
}
const qtyPattern = /^((?:\d+[.,]?\d*(?:\s*[-]\s*\d+[.,]?\d*)?)|(?:\d+\/\d+))\s+(.+)$/; const qtyPattern = /^((?:\d+[.,]?\d*(?:\s*[-]\s*\d+[.,]?\d*)?)|(?:\d+\/\d+))\s+(.+)$/;
const qtyMatch = qtyPattern.exec(working); const qtyMatch = qtyPattern.exec(working);
if (!qtyMatch) { if (!qtyMatch) {
return { position, quantity: null, unit: null, name: working, note, raw_text: rawText }; return { position, quantity: null, unit: null, name: working, note, raw_text: rawText, section_heading: null };
} }
const quantity = parseQuantity(qtyMatch[1]); const quantity = clampQuantity(parseQuantity(qtyMatch[1]));
let rest = qtyMatch[2].trim(); const { unit, name } = splitUnitAndName(qtyMatch[2]);
let unit: string | null = null; return { position, quantity, unit, name, note, raw_text: rawText, section_heading: null };
const firstTokenMatch = /^(\S+)\s+(.+)$/.exec(rest);
if (firstTokenMatch && UNITS.has(firstTokenMatch[1])) {
unit = firstTokenMatch[1];
rest = firstTokenMatch[2].trim();
}
return { position, quantity, unit, name: rest, note, raw_text: rawText };
} }

6
src/lib/server/paths.ts Normal file
View File

@@ -0,0 +1,6 @@
// Filesystem paths read from env at module load. Centralized so a misset
// env var only causes one place to be wrong, not six. Both defaults match
// the docker-compose volume mounts under `/app/data`.
export const DATABASE_PATH = process.env.DATABASE_PATH ?? './data/kochwas.db';
export const IMAGE_DIR = process.env.IMAGE_DIR ?? './data/images';

View File

@@ -64,11 +64,11 @@ export function insertRecipe(db: Database.Database, recipe: Recipe): number {
const id = Number(info.lastInsertRowid); const id = Number(info.lastInsertRowid);
const insIng = db.prepare( const insIng = db.prepare(
`INSERT INTO ingredient(recipe_id, position, quantity, unit, name, note, raw_text) `INSERT INTO ingredient(recipe_id, position, quantity, unit, name, note, raw_text, section_heading)
VALUES (?, ?, ?, ?, ?, ?, ?)` VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
); );
for (const ing of recipe.ingredients) { for (const ing of recipe.ingredients) {
insIng.run(id, ing.position, ing.quantity, ing.unit, ing.name, ing.note, ing.raw_text); insIng.run(id, ing.position, ing.quantity, ing.unit, ing.name, ing.note, ing.raw_text, ing.section_heading);
} }
const insStep = db.prepare( const insStep = db.prepare(
@@ -104,7 +104,7 @@ export function getRecipeById(db: Database.Database, id: number): Recipe | null
const ingredients = db const ingredients = db
.prepare( .prepare(
`SELECT position, quantity, unit, name, note, raw_text `SELECT position, quantity, unit, name, note, raw_text, section_heading
FROM ingredient WHERE recipe_id = ? ORDER BY position` FROM ingredient WHERE recipe_id = ? ORDER BY position`
) )
.all(id) as Ingredient[]; .all(id) as Ingredient[];
@@ -196,6 +196,17 @@ export function updateRecipeMeta(
db.prepare(`UPDATE recipe SET ${fields.join(', ')} WHERE id = ?`).run(...values, id); db.prepare(`UPDATE recipe SET ${fields.join(', ')} WHERE id = ?`).run(...values, id);
} }
export function updateImagePath(
db: Database.Database,
id: number,
filename: string | null
): void {
db.prepare('UPDATE recipe SET image_path = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?').run(
filename,
id
);
}
export function replaceIngredients( export function replaceIngredients(
db: Database.Database, db: Database.Database,
recipeId: number, recipeId: number,
@@ -204,11 +215,11 @@ export function replaceIngredients(
const tx = db.transaction(() => { const tx = db.transaction(() => {
db.prepare('DELETE FROM ingredient WHERE recipe_id = ?').run(recipeId); db.prepare('DELETE FROM ingredient WHERE recipe_id = ?').run(recipeId);
const ins = db.prepare( const ins = db.prepare(
`INSERT INTO ingredient(recipe_id, position, quantity, unit, name, note, raw_text) `INSERT INTO ingredient(recipe_id, position, quantity, unit, name, note, raw_text, section_heading)
VALUES (?, ?, ?, ?, ?, ?, ?)` VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
); );
for (const ing of ingredients) { for (const ing of ingredients) {
ins.run(recipeId, ing.position, ing.quantity, ing.unit, ing.name, ing.note, ing.raw_text); ins.run(recipeId, ing.position, ing.quantity, ing.unit, ing.name, ing.note, ing.raw_text, ing.section_heading);
} }
refreshFts(db, recipeId); refreshFts(db, recipeId);
}); });

View File

@@ -30,15 +30,12 @@ export function searchLocal(
db: Database.Database, db: Database.Database,
query: string, query: string,
limit = 30, limit = 30,
offset = 0, offset = 0
domains: string[] = []
): SearchHit[] { ): SearchHit[] {
const fts = buildFtsQuery(query); const fts = buildFtsQuery(query);
if (!fts) return []; if (!fts) return [];
// bm25: lower is better. Use weights: title > tags > ingredients > description // bm25: lower is better. Use weights: title > tags > ingredients > description
const hasFilter = domains.length > 0;
const placeholders = hasFilter ? domains.map(() => '?').join(',') : '';
const sql = `SELECT r.id, const sql = `SELECT r.id,
r.title, r.title,
r.description, r.description,
@@ -49,13 +46,9 @@ export function searchLocal(
FROM recipe r FROM recipe r
JOIN recipe_fts f ON f.rowid = r.id JOIN recipe_fts f ON f.rowid = r.id
WHERE recipe_fts MATCH ? WHERE recipe_fts MATCH ?
${hasFilter ? `AND r.source_domain IN (${placeholders})` : ''}
ORDER BY bm25(recipe_fts, 10.0, 0.5, 2.0, 5.0) ORDER BY bm25(recipe_fts, 10.0, 0.5, 2.0, 5.0)
LIMIT ? OFFSET ?`; LIMIT ? OFFSET ?`;
const params = hasFilter return db.prepare(sql).all(fts, limit, offset) as SearchHit[];
? [fts, ...domains, limit, offset]
: [fts, limit, offset];
return db.prepare(sql).all(...params) as SearchHit[];
} }
export function listRecentRecipes( export function listRecentRecipes(
@@ -95,18 +88,44 @@ export function listAllRecipes(db: Database.Database): SearchHit[] {
.all() as SearchHit[]; .all() as SearchHit[];
} }
export type AllRecipesSort = 'name' | 'rating' | 'cooked' | 'created'; export type AllRecipesSort = 'name' | 'rating' | 'cooked' | 'created' | 'viewed';
export function listAllRecipesPaginated( export function listAllRecipesPaginated(
db: Database.Database, db: Database.Database,
sort: AllRecipesSort, sort: AllRecipesSort,
limit: number, limit: number,
offset: number offset: number,
profileId: number | null = null
): SearchHit[] { ): SearchHit[] {
// 'viewed' branch needs a JOIN against recipe_view — diverges from the
// simpler ORDER-BY-only path. We keep it in a separate prepare for
// clarity. Without profileId, fall back to alphabetical so the
// sort-chip still produces a sensible list.
if (sort === 'viewed' && profileId !== null) {
return db
.prepare(
`SELECT r.id,
r.title,
r.description,
r.image_path,
r.source_domain,
(SELECT AVG(stars) FROM rating WHERE recipe_id = r.id) AS avg_stars,
(SELECT MAX(cooked_at) FROM cooking_log WHERE recipe_id = r.id) AS last_cooked_at
FROM recipe r
LEFT JOIN recipe_view v
ON v.recipe_id = r.id AND v.profile_id = ?
ORDER BY CASE WHEN v.last_viewed_at IS NULL THEN 1 ELSE 0 END,
v.last_viewed_at DESC,
r.title COLLATE NOCASE ASC
LIMIT ? OFFSET ?`
)
.all(profileId, limit, offset) as SearchHit[];
}
// NULLS-last-Emulation per CASE-Expression — SQLite unterstützt NULLS LAST // NULLS-last-Emulation per CASE-Expression — SQLite unterstützt NULLS LAST
// zwar seit 3.30, aber der Pi könnte auf einer älteren Version laufen und // zwar seit 3.30, aber der Pi könnte auf einer älteren Version laufen und
// CASE ist überall zuverlässig. // CASE ist überall zuverlässig.
const orderBy: Record<AllRecipesSort, string> = { const orderBy: Record<Exclude<AllRecipesSort, 'viewed'>, string> = {
name: 'r.title COLLATE NOCASE ASC', name: 'r.title COLLATE NOCASE ASC',
rating: rating:
'CASE WHEN (SELECT AVG(stars) FROM rating WHERE recipe_id = r.id) IS NULL THEN 1 ELSE 0 END, ' + 'CASE WHEN (SELECT AVG(stars) FROM rating WHERE recipe_id = r.id) IS NULL THEN 1 ELSE 0 END, ' +
@@ -116,6 +135,8 @@ export function listAllRecipesPaginated(
'(SELECT MAX(cooked_at) FROM cooking_log WHERE recipe_id = r.id) DESC, r.title COLLATE NOCASE ASC', '(SELECT MAX(cooked_at) FROM cooking_log WHERE recipe_id = r.id) DESC, r.title COLLATE NOCASE ASC',
created: 'r.created_at DESC, r.id DESC' created: 'r.created_at DESC, r.id DESC'
}; };
// Without profile, 'viewed' degrades to alphabetical.
const effectiveSort = sort === 'viewed' ? 'name' : sort;
return db return db
.prepare( .prepare(
`SELECT r.id, `SELECT r.id,
@@ -126,7 +147,7 @@ export function listAllRecipesPaginated(
(SELECT AVG(stars) FROM rating WHERE recipe_id = r.id) AS avg_stars, (SELECT AVG(stars) FROM rating WHERE recipe_id = r.id) AS avg_stars,
(SELECT MAX(cooked_at) FROM cooking_log WHERE recipe_id = r.id) AS last_cooked_at (SELECT MAX(cooked_at) FROM cooking_log WHERE recipe_id = r.id) AS last_cooked_at
FROM recipe r FROM recipe r
ORDER BY ${orderBy[sort]} ORDER BY ${orderBy[effectiveSort]}
LIMIT ? OFFSET ?` LIMIT ? OFFSET ?`
) )
.all(limit, offset) as SearchHit[]; .all(limit, offset) as SearchHit[];

View File

@@ -0,0 +1,37 @@
import type Database from 'better-sqlite3';
export function recordView(
db: Database.Database,
profileId: number,
recipeId: number
): void {
// ON CONFLICT DO UPDATE bumps only the timestamp field — avoids the
// DELETE+INSERT that INSERT OR REPLACE performs under the hood, which would
// silently cascade-delete any future FK-referencing children.
db.prepare(
`INSERT INTO recipe_view (profile_id, recipe_id)
VALUES (?, ?)
ON CONFLICT(profile_id, recipe_id) DO UPDATE
SET last_viewed_at = CURRENT_TIMESTAMP`
).run(profileId, recipeId);
}
export type ViewRow = {
profile_id: number;
recipe_id: number;
last_viewed_at: string;
};
export function listViews(
db: Database.Database,
profileId: number
): ViewRow[] {
return db
.prepare(
`SELECT profile_id, recipe_id, last_viewed_at
FROM recipe_view
WHERE profile_id = ?
ORDER BY last_viewed_at DESC`
)
.all(profileId) as ViewRow[];
}

View File

@@ -312,6 +312,10 @@ export async function searchWeb(
// Nur Text-Engines abfragen — SearXNG-Video/Image-Engines (karmasearch etc.) // Nur Text-Engines abfragen — SearXNG-Video/Image-Engines (karmasearch etc.)
// bringen uns für Rezeptseiten nichts und produzieren nur 403-Log-Noise. // bringen uns für Rezeptseiten nichts und produzieren nur 403-Log-Noise.
endpoint.searchParams.set('categories', 'general'); endpoint.searchParams.set('categories', 'general');
// Nur Brave (via API) — Mojeek blockt die Pi-IP mit 403, andere Engines
// sind von SearXNG-Seite durch keep_only ohnehin ausgeknipst. So bleibt
// das Log sauber und kochwas ist unabhängig von der globalen Engine-Liste.
endpoint.searchParams.set('engines', 'brave');
if (pageno > 1) endpoint.searchParams.set('pageno', String(pageno)); if (pageno > 1) endpoint.searchParams.set('pageno', String(pageno));
const body = await fetchText(endpoint.toString(), { const body = await fetchText(endpoint.toString(), {
@@ -361,6 +365,9 @@ export async function searchWeb(
}); });
if (hits.length >= limit) break; if (hits.length >= limit) break;
} }
// Absichtliches Prod-Logging: diese drei [searxng]-Zeilen erlauben "warum
// wurde Domain X gefiltert?" ohne Code-Änderung. Strukturiert genug für
// grep/awk, klein genug für jeden Log-Sammler.
console.log( console.log(
`[searxng] q=${JSON.stringify(trimmed)} pageno=${pageno} domains=${domains.length} raw=${results.length} non_whitelist=${dropNonWhitelist} non_recipe_url=${dropNonRecipeUrl} dup=${dropDup} kept_pre_enrich=${hits.length}` `[searxng] q=${JSON.stringify(trimmed)} pageno=${pageno} domains=${domains.length} raw=${results.length} non_whitelist=${dropNonWhitelist} non_recipe_url=${dropNonRecipeUrl} dup=${dropDup} kept_pre_enrich=${hits.length}`
); );

View File

@@ -0,0 +1,196 @@
import type Database from 'better-sqlite3';
// Fallback when a recipe has no servings_default set — matches the default
// used by RecipeEditor's "new recipe" template.
const DEFAULT_SERVINGS = 4;
export type ShoppingCartRecipe = {
recipe_id: number;
title: string;
image_path: string | null;
servings: number;
servings_default: number;
};
export type ShoppingListRow = {
name_key: string;
unit_key: string;
display_name: string;
display_unit: string | null;
total_quantity: number | null;
from_recipes: string;
checked: 0 | 1;
};
export type ShoppingListSnapshot = {
recipes: ShoppingCartRecipe[];
rows: ShoppingListRow[];
uncheckedCount: number;
};
export function addRecipeToCart(
db: Database.Database,
recipeId: number,
profileId: number | null,
servings?: number
): void {
const row = db
.prepare('SELECT servings_default FROM recipe WHERE id = ?')
.get(recipeId) as { servings_default: number | null } | undefined;
const resolved = servings ?? row?.servings_default ?? DEFAULT_SERVINGS;
// ON CONFLICT updates only servings — added_by_profile_id stays with the
// first profile that added the recipe (household cart, audit trail).
db.prepare(
`INSERT INTO shopping_cart_recipe (recipe_id, servings, added_by_profile_id)
VALUES (?, ?, ?)
ON CONFLICT(recipe_id) DO UPDATE SET servings = excluded.servings`
).run(recipeId, resolved, profileId);
}
export function removeRecipeFromCart(
db: Database.Database,
recipeId: number
): void {
db.prepare('DELETE FROM shopping_cart_recipe WHERE recipe_id = ?').run(recipeId);
}
export function setCartServings(
db: Database.Database,
recipeId: number,
servings: number
): void {
if (!Number.isInteger(servings) || servings <= 0) {
throw new Error(`Invalid servings: ${servings}`);
}
db.prepare(
'UPDATE shopping_cart_recipe SET servings = ? WHERE recipe_id = ?'
).run(servings, recipeId);
}
export function listShoppingList(
db: Database.Database
): ShoppingListSnapshot {
const recipes = db
.prepare(
`SELECT cr.recipe_id, r.title, r.image_path, cr.servings,
COALESCE(r.servings_default, cr.servings) AS servings_default
FROM shopping_cart_recipe cr
JOIN recipe r ON r.id = cr.recipe_id
ORDER BY cr.added_at ASC`
)
.all() as ShoppingCartRecipe[];
const rows = db
.prepare(
`SELECT
LOWER(TRIM(i.name)) AS name_key,
LOWER(TRIM(COALESCE(i.unit, ''))) AS unit_key,
MIN(i.name) AS display_name,
MIN(i.unit) AS display_unit,
SUM(i.quantity * cr.servings * 1.0 / NULLIF(COALESCE(r.servings_default, cr.servings), 0)) AS total_quantity,
GROUP_CONCAT(DISTINCT r.title) AS from_recipes,
EXISTS(
SELECT 1 FROM shopping_cart_check c
WHERE c.name_key = LOWER(TRIM(i.name))
AND c.unit_key = LOWER(TRIM(COALESCE(i.unit, '')))
) AS checked
FROM shopping_cart_recipe cr
JOIN recipe r ON r.id = cr.recipe_id
JOIN ingredient i ON i.recipe_id = r.id
GROUP BY name_key, unit_key
ORDER BY checked ASC, display_name COLLATE NOCASE`
)
.all() as ShoppingListRow[];
const uncheckedCount = rows.reduce((n, r) => n + (r.checked ? 0 : 1), 0);
return { recipes, rows, uncheckedCount };
}
export function toggleCheck(
db: Database.Database,
nameKey: string,
unitKey: string,
checked: boolean
): void {
if (checked) {
db.prepare(
`INSERT INTO shopping_cart_check (name_key, unit_key)
VALUES (?, ?)
ON CONFLICT(name_key, unit_key) DO NOTHING`
).run(nameKey, unitKey);
} else {
db.prepare(
'DELETE FROM shopping_cart_check WHERE name_key = ? AND unit_key = ?'
).run(nameKey, unitKey);
}
}
export function clearCheckedItems(db: Database.Database): void {
const tx = db.transaction(() => {
// Alle aggregierten Zeilen mit checked-Status holen, pro recipe_id gruppieren
// und Rezepte finden, deren Zeilen ALLE abgehakt sind.
const allRows = db
.prepare(
`SELECT
cr.recipe_id,
LOWER(TRIM(i.name)) AS name_key,
LOWER(TRIM(COALESCE(i.unit, ''))) AS unit_key,
EXISTS(
SELECT 1 FROM shopping_cart_check c
WHERE c.name_key = LOWER(TRIM(i.name))
AND c.unit_key = LOWER(TRIM(COALESCE(i.unit, '')))
) AS checked
FROM shopping_cart_recipe cr
JOIN ingredient i ON i.recipe_id = cr.recipe_id`
)
.all() as { recipe_id: number; name_key: string; unit_key: string; checked: 0 | 1 }[];
const perRecipe = new Map<number, { total: number; checked: number }>();
for (const r of allRows) {
const e = perRecipe.get(r.recipe_id) ?? { total: 0, checked: 0 };
e.total += 1;
e.checked += r.checked;
perRecipe.set(r.recipe_id, e);
}
const toRemove: number[] = [];
for (const [id, e] of perRecipe) {
if (e.total > 0 && e.total === e.checked) toRemove.push(id);
}
for (const id of toRemove) {
db.prepare('DELETE FROM shopping_cart_recipe WHERE recipe_id = ?').run(id);
}
// Orphan-Checks raeumen: alle Check-Keys, die jetzt in KEINEM Cart-Rezept
// mehr vorkommen.
const activeKeys = db
.prepare(
`SELECT DISTINCT
LOWER(TRIM(i.name)) AS name_key,
LOWER(TRIM(COALESCE(i.unit, ''))) AS unit_key
FROM shopping_cart_recipe cr
JOIN ingredient i ON i.recipe_id = cr.recipe_id`
)
.all() as { name_key: string; unit_key: string }[];
const activeSet = new Set(activeKeys.map((k) => `${k.name_key} ${k.unit_key}`));
const allChecks = db
.prepare('SELECT name_key, unit_key FROM shopping_cart_check')
.all() as { name_key: string; unit_key: string }[];
const del = db.prepare(
'DELETE FROM shopping_cart_check WHERE name_key = ? AND unit_key = ?'
);
for (const c of allChecks) {
if (!activeSet.has(`${c.name_key} ${c.unit_key}`)) {
del.run(c.name_key, c.unit_key);
}
}
});
tx();
}
export function clearCart(db: Database.Database): void {
const tx = db.transaction(() => {
db.prepare('DELETE FROM shopping_cart_recipe').run();
db.prepare('DELETE FROM shopping_cart_check').run();
});
tx();
}

View File

@@ -1,6 +1,6 @@
export type CacheStrategy = 'shell' | 'swr' | 'images' | 'network-only'; export type CacheStrategy = 'shell' | 'network-first' | 'images' | 'network-only';
export type RequestShape = { url: string; method: string }; type RequestShape = { url: string; method: string };
// Pure function — sole decision-maker for "which strategy for this request?". // Pure function — sole decision-maker for "which strategy for this request?".
// Called by the service worker for every fetch event. // Called by the service worker for every fetch event.
@@ -16,7 +16,9 @@ export function resolveStrategy(req: RequestShape): CacheStrategy {
if ( if (
path === '/api/recipes/import' || path === '/api/recipes/import' ||
path === '/api/recipes/preview' || path === '/api/recipes/preview' ||
path.startsWith('/api/recipes/search/web') path === '/api/recipes/extract-from-photo' ||
path.startsWith('/api/recipes/search/web') ||
path.startsWith('/api/shopping-list')
) { ) {
return 'network-only'; return 'network-only';
} }
@@ -37,6 +39,7 @@ export function resolveStrategy(req: RequestShape): CacheStrategy {
return 'shell'; return 'shell';
} }
// Everything else: recipe pages, API reads, lists — all SWR. // Everything else: recipe pages, API reads, lists — network-first with
return 'swr'; // timeout fallback to cache (handled in service-worker.ts).
return 'network-first';
} }

View File

@@ -1,7 +1,7 @@
// Vergleicht die aktuelle Rezept-ID-Liste (vom Server) mit dem, was // Vergleicht die aktuelle Rezept-ID-Liste (vom Server) mit dem, was
// der Cache schon hat. Der SW nutzt das Delta, um nur Neue zu laden // der Cache schon hat. Der SW nutzt das Delta, um nur Neue zu laden
// und Gelöschte abzuräumen. // und Gelöschte abzuräumen.
export type ManifestDiff = { toAdd: number[]; toRemove: number[] }; type ManifestDiff = { toAdd: number[]; toRemove: number[] };
export function diffManifest(currentIds: number[], cachedIds: number[]): ManifestDiff { export function diffManifest(currentIds: number[], cachedIds: number[]): ManifestDiff {
const current = new Set(currentIds); const current = new Set(currentIds);

View File

@@ -5,6 +5,7 @@ export type Ingredient = {
name: string; name: string;
note: string | null; note: string | null;
raw_text: string; raw_text: string;
section_heading: string | null;
}; };
export type Step = { export type Step = {

View File

@@ -0,0 +1,9 @@
import type { LayoutServerLoad } from './$types';
import { env } from '$env/dynamic/private';
export const load: LayoutServerLoad = () => {
return {
version: env.KOCHWAS_TAG ?? 'dev',
geminiConfigured: Boolean(env.GEMINI_API_KEY)
};
};

View File

@@ -1,10 +1,20 @@
<script lang="ts"> <script lang="ts">
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { page } from '$app/stores'; import { page } from '$app/stores';
import { goto, afterNavigate } from '$app/navigation'; import { goto, afterNavigate, beforeNavigate } from '$app/navigation';
import { Settings, CookingPot, Utensils, Menu, BookOpen, ArrowLeft } from 'lucide-svelte'; import {
Settings,
CookingPot,
Utensils,
Menu,
BookOpen,
ArrowLeft,
Camera,
ShoppingCart
} from 'lucide-svelte';
import { profileStore } from '$lib/client/profile.svelte'; import { profileStore } from '$lib/client/profile.svelte';
import { wishlistStore } from '$lib/client/wishlist.svelte'; import { wishlistStore } from '$lib/client/wishlist.svelte';
import { shoppingCartStore } from '$lib/client/shopping-cart.svelte';
import { pwaStore } from '$lib/client/pwa.svelte'; import { pwaStore } from '$lib/client/pwa.svelte';
import { searchFilterStore } from '$lib/client/search-filter.svelte'; import { searchFilterStore } from '$lib/client/search-filter.svelte';
import ProfileSwitcher from '$lib/components/ProfileSwitcher.svelte'; import ProfileSwitcher from '$lib/components/ProfileSwitcher.svelte';
@@ -17,26 +27,21 @@
import { network } from '$lib/client/network.svelte'; import { network } from '$lib/client/network.svelte';
import { installPrompt } from '$lib/client/install-prompt.svelte'; import { installPrompt } from '$lib/client/install-prompt.svelte';
import { registerServiceWorker } from '$lib/client/sw-register'; import { registerServiceWorker } from '$lib/client/sw-register';
import type { SearchHit } from '$lib/server/recipes/search-local'; import { SearchStore } from '$lib/client/search.svelte';
import type { WebHit } from '$lib/server/search/searxng'; import { recordScroll, restoreScroll } from '$lib/client/scroll-restore';
let { children } = $props(); let { data, children } = $props();
const NAV_PAGE_SIZE = 30; const navStore = new SearchStore({
pageSize: 30,
webFilterParam: () => {
const p = searchFilterStore.queryParam;
return p ? `&domains=${encodeURIComponent(p)}` : '';
}
});
let navQuery = $state('');
let navHits = $state<SearchHit[]>([]);
let navWebHits = $state<WebHit[]>([]);
let navSearching = $state(false);
let navWebSearching = $state(false);
let navWebError = $state<string | null>(null);
let navOpen = $state(false); let navOpen = $state(false);
let navLocalExhausted = $state(false);
let navWebPageno = $state(0);
let navWebExhausted = $state(false);
let navLoadingMore = $state(false);
let navContainer: HTMLElement | undefined = $state(); let navContainer: HTMLElement | undefined = $state();
let debounceTimer: ReturnType<typeof setTimeout> | null = null;
let menuOpen = $state(false); let menuOpen = $state(false);
let menuContainer: HTMLElement | undefined = $state(); let menuContainer: HTMLElement | undefined = $state();
@@ -44,123 +49,21 @@
$page.url.pathname.startsWith('/recipes/') || $page.url.pathname === '/preview' $page.url.pathname.startsWith('/recipes/') || $page.url.pathname === '/preview'
); );
function filterParam(): string {
const p = searchFilterStore.queryParam;
return p ? `&domains=${encodeURIComponent(p)}` : '';
}
$effect(() => { $effect(() => {
const q = navQuery.trim(); // Bare reads register the reactive deps; then kick the store.
if (debounceTimer) clearTimeout(debounceTimer); const q = navStore.query;
if (q.length <= 3) { navStore.runDebounced();
navHits = []; // navOpen follows query length: open while typing, close when cleared.
navWebHits = []; navOpen = q.trim().length > 3;
navSearching = false;
navWebSearching = false;
navWebError = null;
navOpen = false;
navLocalExhausted = false;
navWebPageno = 0;
navWebExhausted = false;
return;
}
navSearching = true;
navWebHits = [];
navWebSearching = false;
navWebError = null;
navOpen = true;
navLocalExhausted = false;
navWebPageno = 0;
navWebExhausted = false;
debounceTimer = setTimeout(async () => {
try {
const res = await fetch(
`/api/recipes/search?q=${encodeURIComponent(q)}&limit=${NAV_PAGE_SIZE}${filterParam()}`
);
const body = await res.json();
if (navQuery.trim() !== q) return;
navHits = body.hits;
if (navHits.length < NAV_PAGE_SIZE) navLocalExhausted = true;
if (navHits.length === 0) {
navWebSearching = true;
try {
const wres = await fetch(
`/api/recipes/search/web?q=${encodeURIComponent(q)}&pageno=1${filterParam()}`
);
if (navQuery.trim() !== q) return;
if (!wres.ok) {
const err = await wres.json().catch(() => ({}));
navWebError = err.message ?? `HTTP ${wres.status}`;
navWebExhausted = true;
} else {
const wbody = await wres.json();
navWebHits = wbody.hits;
navWebPageno = 1;
if (navWebHits.length === 0) navWebExhausted = true;
}
} finally {
if (navQuery.trim() === q) navWebSearching = false;
}
}
} finally {
if (navQuery.trim() === q) navSearching = false;
}
}, 300);
}); });
async function loadMoreNav() { function loadMoreNav() {
if (navLoadingMore) return; return navStore.loadMore();
const q = navQuery.trim();
if (!q) return;
navLoadingMore = true;
try {
if (!navLocalExhausted) {
const res = await fetch(
`/api/recipes/search?q=${encodeURIComponent(q)}&limit=${NAV_PAGE_SIZE}&offset=${navHits.length}${filterParam()}`
);
const body = await res.json();
if (navQuery.trim() !== q) return;
const more = body.hits as SearchHit[];
const seen = new Set(navHits.map((h) => h.id));
const deduped = more.filter((h) => !seen.has(h.id));
navHits = [...navHits, ...deduped];
if (more.length < NAV_PAGE_SIZE) navLocalExhausted = true;
} else if (!navWebExhausted) {
const nextPage = navWebPageno + 1;
navWebSearching = navWebHits.length === 0;
try {
const wres = await fetch(
`/api/recipes/search/web?q=${encodeURIComponent(q)}&pageno=${nextPage}${filterParam()}`
);
if (navQuery.trim() !== q) return;
if (!wres.ok) {
const err = await wres.json().catch(() => ({}));
navWebError = err.message ?? `HTTP ${wres.status}`;
navWebExhausted = true;
return;
}
const wbody = await wres.json();
const more = wbody.hits as WebHit[];
const seen = new Set(navWebHits.map((h) => h.url));
const deduped = more.filter((h) => !seen.has(h.url));
if (deduped.length === 0) {
navWebExhausted = true;
} else {
navWebHits = [...navWebHits, ...deduped];
navWebPageno = nextPage;
}
} finally {
if (navQuery.trim() === q) navWebSearching = false;
}
}
} finally {
navLoadingMore = false;
}
} }
function submitNav(e: SubmitEvent) { function submitNav(e: SubmitEvent) {
e.preventDefault(); e.preventDefault();
const q = navQuery.trim(); const q = navStore.query.trim();
if (!q) return; if (!q) return;
navOpen = false; navOpen = false;
void goto(`/?q=${encodeURIComponent(q)}`); void goto(`/?q=${encodeURIComponent(q)}`);
@@ -184,15 +87,23 @@
function pickHit() { function pickHit() {
navOpen = false; navOpen = false;
navQuery = ''; navStore.reset();
navHits = [];
navWebHits = [];
} }
afterNavigate(() => { function goBack() {
navQuery = ''; if (typeof history !== 'undefined' && history.length > 1) {
navHits = []; history.back();
navWebHits = []; } else {
void goto('/');
}
}
beforeNavigate((nav) => {
recordScroll(nav.from?.url);
});
afterNavigate((nav) => {
navStore.reset();
navOpen = false; navOpen = false;
menuOpen = false; menuOpen = false;
// Badge nach jeder Client-Navigation frisch halten — sonst kann er // Badge nach jeder Client-Navigation frisch halten — sonst kann er
@@ -200,11 +111,14 @@
// auf einem anderen Gerät oder in einem anderen Tab etwas geändert // auf einem anderen Gerät oder in einem anderen Tab etwas geändert
// wurde. // wurde.
void wishlistStore.refresh(); void wishlistStore.refresh();
void shoppingCartStore.refresh();
restoreScroll(nav.type, nav.to?.url);
}); });
onMount(() => { onMount(() => {
profileStore.load(); profileStore.load();
void wishlistStore.refresh(); void wishlistStore.refresh();
void shoppingCartStore.refresh();
void searchFilterStore.load(); void searchFilterStore.load();
void pwaStore.init(); void pwaStore.init();
network.init(); network.init();
@@ -227,11 +141,14 @@
<header class="bar"> <header class="bar">
<div class="bar-inner"> <div class="bar-inner">
{#if $page.url.pathname === '/'} {#if $page.url.pathname === '/'}
<a href="/" class="brand">Kochwas</a> <div class="brand-stack">
<a href="/" class="brand">Kochwas</a>
<span class="version" title="Deployment-Tag">{data.version}</span>
</div>
{:else} {:else}
<a href="/" class="home-back" aria-label="Zurück zur Startseite"> <button type="button" class="home-back" aria-label="Zurück" onclick={goBack}>
<ArrowLeft size={22} strokeWidth={2} /> <ArrowLeft size={22} strokeWidth={2} />
</a> </button>
{/if} {/if}
{#if showHeaderSearch} {#if showHeaderSearch}
<div class="nav-search-wrap" bind:this={navContainer}> <div class="nav-search-wrap" bind:this={navContainer}>
@@ -239,9 +156,9 @@
<SearchFilter inline /> <SearchFilter inline />
<input <input
type="search" type="search"
bind:value={navQuery} bind:value={navStore.query}
onfocus={() => { onfocus={() => {
if (navHits.length > 0 || navQuery.trim().length > 3) navOpen = true; if (navStore.hits.length > 0 || navStore.query.trim().length > 3) navOpen = true;
}} }}
placeholder="Rezept suchen…" placeholder="Rezept suchen…"
autocomplete="off" autocomplete="off"
@@ -251,12 +168,12 @@
</form> </form>
{#if navOpen} {#if navOpen}
<div class="dropdown" role="listbox"> <div class="dropdown" role="listbox">
{#if navSearching && navHits.length === 0 && navWebHits.length === 0} {#if navStore.searching && navStore.hits.length === 0 && navStore.webHits.length === 0}
<SearchLoader scope="local" size="sm" /> <SearchLoader scope="local" size="sm" />
{:else} {:else}
{#if navHits.length > 0} {#if navStore.hits.length > 0}
<ul class="dd-list"> <ul class="dd-list">
{#each navHits as r (r.id)} {#each navStore.hits as r (r.id)}
<li> <li>
<a <a
href={`/recipes/${r.id}`} href={`/recipes/${r.id}`}
@@ -282,14 +199,14 @@
</ul> </ul>
{/if} {/if}
{#if navWebHits.length > 0} {#if navStore.webHits.length > 0}
{#if navHits.length > 0} {#if navStore.hits.length > 0}
<p class="dd-section">Aus dem Internet</p> <p class="dd-section">Aus dem Internet</p>
{:else} {:else}
<p class="dd-section">Keine lokalen Rezepte aus dem Internet:</p> <p class="dd-section">Keine lokalen Rezepte aus dem Internet:</p>
{/if} {/if}
<ul class="dd-list"> <ul class="dd-list">
{#each navWebHits as w (w.url)} {#each navStore.webHits as w (w.url)}
<li> <li>
<a <a
href={`/preview?url=${encodeURIComponent(w.url)}`} href={`/preview?url=${encodeURIComponent(w.url)}`}
@@ -313,23 +230,23 @@
</ul> </ul>
{/if} {/if}
{#if navWebSearching} {#if navStore.webSearching}
<SearchLoader scope="web" size="sm" /> <SearchLoader scope="web" size="sm" />
{:else if navWebError && navWebHits.length === 0} {:else if navStore.webError && navStore.webHits.length === 0}
<p class="dd-status dd-error">Internet-Suche zurzeit nicht möglich.</p> <p class="dd-status dd-error">Internet-Suche zurzeit nicht möglich.</p>
{:else if navHits.length === 0 && navWebHits.length === 0 && !navSearching} {:else if navStore.hits.length === 0 && navStore.webHits.length === 0 && !navStore.searching}
<p class="dd-status">Auch im Internet nichts gefunden.</p> <p class="dd-status">Auch im Internet nichts gefunden.</p>
{/if} {/if}
{#if !(navLocalExhausted && navWebExhausted) && (navHits.length > 0 || navWebHits.length > 0)} {#if !(navStore.localExhausted && navStore.webExhausted) && (navStore.hits.length > 0 || navStore.webHits.length > 0)}
<button <button
class="dd-web" class="dd-web"
type="button" type="button"
onclick={loadMoreNav} onclick={loadMoreNav}
disabled={navLoadingMore || navWebSearching} disabled={navStore.loadingMore || navStore.webSearching}
> >
<span <span
>{navLoadingMore || navWebSearching >{navStore.loadingMore || navStore.webSearching
? 'Lade …' ? 'Lade …'
: '+ weitere Ergebnisse'}</span : '+ weitere Ergebnisse'}</span
> >
@@ -341,6 +258,22 @@
</div> </div>
{/if} {/if}
<div class="bar-right"> <div class="bar-right">
{#if data.geminiConfigured}
<a
href={network.online ? '/new/from-photo' : ''}
class="nav-link magic-link"
class:disabled={!network.online}
aria-label="Rezept aus Foto erstellen"
title={network.online
? 'Rezept aus Foto erstellen'
: 'Offline — braucht Internet'}
onclick={(e) => {
if (!network.online) e.preventDefault();
}}
>
<Camera size={20} strokeWidth={2} />
</a>
{/if}
<a <a
href="/wishlist" href="/wishlist"
class="nav-link wishlist-link" class="nav-link wishlist-link"
@@ -353,6 +286,16 @@
<span class="badge">{wishlistStore.count}</span> <span class="badge">{wishlistStore.count}</span>
{/if} {/if}
</a> </a>
{#if shoppingCartStore.uncheckedCount > 0}
<a
href="/shopping-list"
class="nav-link shopping-link"
aria-label={`Einkaufsliste (${shoppingCartStore.uncheckedCount})`}
>
<ShoppingCart size={20} strokeWidth={2} />
<span class="badge">{shoppingCartStore.uncheckedCount}</span>
</a>
{/if}
<div class="menu-wrap" bind:this={menuContainer}> <div class="menu-wrap" bind:this={menuContainer}>
<button <button
class="nav-link" class="nav-link"
@@ -386,6 +329,9 @@
</main> </main>
<style> <style>
:global(:root) {
--pill-radius: 999px;
}
:global(html, body) { :global(html, body) {
margin: 0; margin: 0;
padding: 0; padding: 0;
@@ -416,6 +362,13 @@
padding: 0.6rem 1rem; padding: 0.6rem 1rem;
position: relative; position: relative;
} }
.brand-stack {
display: flex;
flex-direction: column;
align-items: flex-start;
line-height: 1;
flex-shrink: 0;
}
.brand { .brand {
font-size: 1.15rem; font-size: 1.15rem;
font-weight: 700; font-weight: 700;
@@ -423,16 +376,28 @@
color: #2b6a3d; color: #2b6a3d;
flex-shrink: 0; flex-shrink: 0;
} }
.version {
margin-top: 2px;
font-size: 0.65rem;
color: #9aa8a0;
font-variant-numeric: tabular-nums;
letter-spacing: 0.02em;
}
.home-back { .home-back {
display: inline-flex; display: inline-flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
width: 40px; width: 40px;
height: 40px; height: 40px;
border-radius: 999px; border: 0;
border-radius: var(--pill-radius);
background: transparent;
color: #2b6a3d; color: #2b6a3d;
text-decoration: none; text-decoration: none;
flex-shrink: 0; flex-shrink: 0;
cursor: pointer;
font: inherit;
padding: 0;
} }
.home-back:hover { .home-back:hover {
background: #f4f8f5; background: #f4f8f5;
@@ -621,7 +586,7 @@
justify-content: center; justify-content: center;
width: 40px; width: 40px;
height: 40px; height: 40px;
border-radius: 999px; border-radius: var(--pill-radius);
text-decoration: none; text-decoration: none;
font-size: 1.15rem; font-size: 1.15rem;
position: relative; position: relative;
@@ -629,6 +594,11 @@
.nav-link:hover { .nav-link:hover {
background: #f4f8f5; background: #f4f8f5;
} }
.nav-link.disabled {
color: #999;
pointer-events: none;
cursor: not-allowed;
}
.badge { .badge {
position: absolute; position: absolute;
top: -2px; top: -2px;
@@ -636,7 +606,7 @@
min-width: 18px; min-width: 18px;
height: 18px; height: 18px;
padding: 0 5px; padding: 0 5px;
border-radius: 999px; border-radius: var(--pill-radius);
background: #c53030; background: #c53030;
color: white; color: white;
font-size: 0.7rem; font-size: 0.7rem;
@@ -653,7 +623,7 @@
} }
@media (max-width: 520px) { @media (max-width: 520px) {
/* App-Icon auf engen Screens komplett aus — die Suche bekommt den Platz. */ /* App-Icon auf engen Screens komplett aus — die Suche bekommt den Platz. */
.brand { .brand-stack {
display: none; display: none;
} }
.nav-link { .nav-link {

View File

@@ -1,44 +1,47 @@
<script lang="ts"> <script lang="ts">
import { onMount, tick } from 'svelte'; import { onMount, tick, untrack } from 'svelte';
import { page } from '$app/stores'; import { page } from '$app/stores';
import { CookingPot, X } from 'lucide-svelte'; import { CookingPot, X, ChevronDown } from 'lucide-svelte';
import { slide } from 'svelte/transition';
import type { Snapshot } from './$types'; import type { Snapshot } from './$types';
import type { SearchHit } from '$lib/server/recipes/search-local'; import type { SearchHit } from '$lib/server/recipes/search-local';
import type { WebHit } from '$lib/server/search/searxng';
import { randomQuote } from '$lib/quotes'; import { randomQuote } from '$lib/quotes';
import SearchLoader from '$lib/components/SearchLoader.svelte'; import SearchLoader from '$lib/components/SearchLoader.svelte';
import SearchFilter from '$lib/components/SearchFilter.svelte'; import SearchFilter from '$lib/components/SearchFilter.svelte';
import { profileStore } from '$lib/client/profile.svelte'; import { profileStore } from '$lib/client/profile.svelte';
import { searchFilterStore } from '$lib/client/search-filter.svelte'; import { searchFilterStore } from '$lib/client/search-filter.svelte';
import { requireOnline } from '$lib/client/require-online'; import { requireOnline } from '$lib/client/require-online';
import { SearchStore, type SearchSnapshot } from '$lib/client/search.svelte';
const LOCAL_PAGE = 30; const LOCAL_PAGE = 30;
let query = $state(''); const store = new SearchStore({
pageSize: LOCAL_PAGE,
webFilterParam: () => {
const p = searchFilterStore.queryParam;
return p ? `&domains=${encodeURIComponent(p)}` : '';
}
});
let quote = $state(''); let quote = $state('');
let recent = $state<SearchHit[]>([]); let recent = $state<SearchHit[]>([]);
let favorites = $state<SearchHit[]>([]); let favorites = $state<SearchHit[]>([]);
let hits = $state<SearchHit[]>([]);
let webHits = $state<WebHit[]>([]);
let searching = $state(false);
let webSearching = $state(false);
let webError = $state<string | null>(null);
let searchedFor = $state<string | null>(null);
let localExhausted = $state(false);
let webPageno = $state(0);
let webExhausted = $state(false);
let loadingMore = $state(false);
let skipNextSearch = false;
let debounceTimer: ReturnType<typeof setTimeout> | null = null;
const ALL_PAGE = 10; const ALL_PAGE = 10;
type AllSort = 'name' | 'rating' | 'cooked' | 'created'; type AllSort = 'name' | 'rating' | 'cooked' | 'created' | 'viewed';
const ALL_SORTS: { value: AllSort; label: string }[] = [ const ALL_SORTS: { value: AllSort; label: string }[] = [
{ value: 'name', label: 'Name' }, { value: 'name', label: 'Name' },
{ value: 'rating', label: 'Bewertung' }, { value: 'rating', label: 'Bewertung' },
{ value: 'cooked', label: 'Zuletzt gekocht' }, { value: 'cooked', label: 'Zuletzt gekocht' },
{ value: 'created', label: 'Hinzugefügt' } { value: 'created', label: 'Hinzugefügt' },
{ value: 'viewed', label: 'Zuletzt angesehen' }
]; ];
function buildAllUrl(sort: AllSort, limit: number, offset: number): string {
const profileId = profileStore.active?.id;
const profilePart = profileId ? `&profile_id=${profileId}` : '';
return `/api/recipes/all?sort=${sort}&limit=${limit}&offset=${offset}${profilePart}`;
}
let allRecipes = $state<SearchHit[]>([]); let allRecipes = $state<SearchHit[]>([]);
let allSort = $state<AllSort>('name'); let allSort = $state<AllSort>('name');
let allExhausted = $state(false); let allExhausted = $state(false);
@@ -47,41 +50,68 @@
let allChips: HTMLElement | undefined = $state(); let allChips: HTMLElement | undefined = $state();
let allObserver: IntersectionObserver | null = null; let allObserver: IntersectionObserver | null = null;
type SearchSnapshot = { type CollapseKey = 'favorites' | 'recent';
query: string; const COLLAPSE_STORAGE_KEY = 'kochwas.collapsed.sections';
hits: SearchHit[]; let collapsed = $state<Record<CollapseKey, boolean>>({
webHits: WebHit[]; favorites: false,
searchedFor: string | null; recent: false
webError: string | null; });
localExhausted: boolean;
webPageno: number; function toggleCollapsed(key: CollapseKey) {
webExhausted: boolean; collapsed[key] = !collapsed[key];
if (typeof localStorage !== 'undefined') {
localStorage.setItem(COLLAPSE_STORAGE_KEY, JSON.stringify(collapsed));
}
}
// Snapshot persists across history navigation. We capture not only the
// search store, but also the pagination depth ("user had loaded 60
// recipes via infinite scroll") so on back-nav we can re-hydrate the
// full list in one fetch — otherwise the document is too short and
// scroll-restore can't reach the saved Y position.
//
// SvelteKit calls snapshot.restore AFTER onMount (post-mount tick),
// so a flag-based handoff to onMount won't fire — we trigger
// rehydrateAll directly from restore. onMount still calls
// loadAllMore() for the fresh-mount case; if restore lands first,
// allLoading guards the duplicate fetch, otherwise rehydrateAll's
// larger result simply overwrites loadAllMore's initial 10 items.
type HomeSnapshot = SearchSnapshot & {
allLoaded: number;
allSort: AllSort;
allExhausted: boolean;
}; };
export const snapshot: Snapshot<SearchSnapshot> = { export const snapshot: Snapshot<HomeSnapshot> = {
capture: () => ({ capture: () => ({
query, ...store.captureSnapshot(),
hits, allLoaded: allRecipes.length,
webHits, allSort,
searchedFor, allExhausted
webError,
localExhausted,
webPageno,
webExhausted
}), }),
restore: (v) => { restore: (s) => {
query = v.query; store.restoreSnapshot(s);
hits = v.hits; if (s.allLoaded > 0) {
webHits = v.webHits; allSort = s.allSort;
searchedFor = v.searchedFor; void rehydrateAll(s.allSort, s.allLoaded, s.allExhausted);
webError = v.webError; }
localExhausted = v.localExhausted;
webPageno = v.webPageno;
webExhausted = v.webExhausted;
skipNextSearch = true;
} }
}; };
async function rehydrateAll(sort: AllSort, count: number, exhausted: boolean) {
allLoading = true;
try {
const res = await fetch(buildAllUrl(sort, count, 0));
if (!res.ok) return;
const body = await res.json();
const hits = body.hits as SearchHit[];
allRecipes = hits;
allExhausted = exhausted || hits.length < count;
} finally {
allLoading = false;
}
}
async function loadRecent() { async function loadRecent() {
const res = await fetch('/api/recipes/search'); const res = await fetch('/api/recipes/search');
const body = await res.json(); const body = await res.json();
@@ -92,9 +122,7 @@
if (allLoading || allExhausted) return; if (allLoading || allExhausted) return;
allLoading = true; allLoading = true;
try { try {
const res = await fetch( const res = await fetch(buildAllUrl(allSort, ALL_PAGE, allRecipes.length));
`/api/recipes/all?sort=${allSort}&limit=${ALL_PAGE}&offset=${allRecipes.length}`
);
if (!res.ok) return; if (!res.ok) return;
const body = await res.json(); const body = await res.json();
const more = body.hits as SearchHit[]; const more = body.hits as SearchHit[];
@@ -118,9 +146,7 @@
const chipsBefore = allChips?.getBoundingClientRect().top ?? 0; const chipsBefore = allChips?.getBoundingClientRect().top ?? 0;
allLoading = true; allLoading = true;
try { try {
const res = await fetch( const res = await fetch(buildAllUrl(next, ALL_PAGE, 0));
`/api/recipes/all?sort=${next}&limit=${ALL_PAGE}&offset=0`
);
if (!res.ok) return; if (!res.ok) return;
const body = await res.json(); const body = await res.json();
const hits = body.hits as SearchHit[]; const hits = body.hits as SearchHit[];
@@ -152,14 +178,28 @@
// Restore query from URL so history.back() from preview/recipe // Restore query from URL so history.back() from preview/recipe
// brings the user back to the same search results. // brings the user back to the same search results.
const urlQ = ($page.url.searchParams.get('q') ?? '').trim(); const urlQ = ($page.url.searchParams.get('q') ?? '').trim();
if (urlQ) query = urlQ; if (urlQ) store.query = urlQ;
void loadRecent(); void loadRecent();
void searchFilterStore.load(); void searchFilterStore.load();
const saved = localStorage.getItem('kochwas.allSort'); const saved = localStorage.getItem('kochwas.allSort');
if (saved && ['name', 'rating', 'cooked', 'created'].includes(saved)) { if (saved && ['name', 'rating', 'cooked', 'created', 'viewed'].includes(saved)) {
allSort = saved as AllSort; allSort = saved as AllSort;
} }
// Fresh-mount: kick off the initial 10. On back-nav, snapshot.restore
// also fires rehydrateAll(60); if it lands first, allLoading guards
// this; if loadAllMore lands first, rehydrateAll's larger result
// simply overwrites allRecipes once it resolves.
void loadAllMore(); void loadAllMore();
const rawCollapsed = localStorage.getItem(COLLAPSE_STORAGE_KEY);
if (rawCollapsed) {
try {
const parsed = JSON.parse(rawCollapsed) as Partial<Record<CollapseKey, boolean>>;
if (typeof parsed.favorites === 'boolean') collapsed.favorites = parsed.favorites;
if (typeof parsed.recent === 'boolean') collapsed.recent = parsed.recent;
} catch {
// Corrupt JSON — keep defaults (both open).
}
}
}); });
// IntersectionObserver an den Sentinel hängen — wenn sichtbar, nachladen. // IntersectionObserver an den Sentinel hängen — wenn sichtbar, nachladen.
@@ -188,14 +228,38 @@
$effect(() => { $effect(() => {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions // eslint-disable-next-line @typescript-eslint/no-unused-expressions
searchFilterStore.active; searchFilterStore.active;
const q = query.trim(); store.reSearch();
if (!q || q.length <= 3) return; });
if (debounceTimer) clearTimeout(debounceTimer);
searching = true; // 'viewed' sort depends on the active profile. When the user switches
webHits = []; // profiles, refetch with the new profile_id so the list reflects what
webSearching = false; // the *current* profile has viewed. Other sorts are profile-agnostic
webError = null; // and don't need this.
debounceTimer = setTimeout(() => void runSearch(q), 150); //
// Only `profileStore.active` must be a tracked dep. `allSort` /
// `allLoading` are read inside untrack: otherwise the `allLoading = false`
// write in the fetch-finally would re-trigger the effect and start the
// next fetch → endless loop.
$effect(() => {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
profileStore.active;
untrack(() => {
if (allSort !== 'viewed') return;
if (allLoading) return;
void (async () => {
allLoading = true;
try {
const res = await fetch(buildAllUrl('viewed', ALL_PAGE, 0));
if (!res.ok) return;
const body = await res.json();
const hits = body.hits as SearchHit[];
allRecipes = hits;
allExhausted = hits.length < ALL_PAGE;
} finally {
allLoading = false;
}
})();
});
}); });
// Sync current query back into the URL as ?q=... via replaceState, // Sync current query back into the URL as ?q=... via replaceState,
@@ -203,7 +267,7 @@
// when the user clicks a result or otherwise navigates away. // when the user clicks a result or otherwise navigates away.
$effect(() => { $effect(() => {
if (typeof window === 'undefined') return; if (typeof window === 'undefined') return;
const q = query.trim(); const q = store.query.trim();
const url = new URL(window.location.href); const url = new URL(window.location.href);
const current = url.searchParams.get('q') ?? ''; const current = url.searchParams.get('q') ?? '';
if (q === current) return; if (q === current) return;
@@ -221,138 +285,17 @@
void loadFavorites(active.id); void loadFavorites(active.id);
}); });
function filterParam(): string {
const p = searchFilterStore.queryParam;
return p ? `&domains=${encodeURIComponent(p)}` : '';
}
async function runSearch(q: string) {
localExhausted = false;
webPageno = 0;
webExhausted = false;
try {
const res = await fetch(
`/api/recipes/search?q=${encodeURIComponent(q)}&limit=${LOCAL_PAGE}${filterParam()}`
);
const body = await res.json();
if (query.trim() !== q) return;
hits = body.hits;
searchedFor = q;
if (hits.length < LOCAL_PAGE) localExhausted = true;
if (hits.length === 0) {
// Gar keine lokalen Treffer → erste Web-Seite gleich laden,
// damit der User nicht extra auf „+ weitere" klicken muss.
webSearching = true;
try {
const wres = await fetch(
`/api/recipes/search/web?q=${encodeURIComponent(q)}&pageno=1${filterParam()}`
);
if (query.trim() !== q) return;
if (!wres.ok) {
const err = await wres.json().catch(() => ({}));
webError = err.message ?? `HTTP ${wres.status}`;
webExhausted = true;
} else {
const wbody = await wres.json();
webHits = wbody.hits;
webPageno = 1;
if (wbody.hits.length === 0) webExhausted = true;
}
} finally {
if (query.trim() === q) webSearching = false;
}
}
} finally {
if (query.trim() === q) searching = false;
}
}
async function loadMore() {
if (loadingMore) return;
const q = query.trim();
if (!q) return;
loadingMore = true;
try {
if (!localExhausted) {
// Noch mehr lokale Treffer holen.
const res = await fetch(
`/api/recipes/search?q=${encodeURIComponent(q)}&limit=${LOCAL_PAGE}&offset=${hits.length}${filterParam()}`
);
const body = await res.json();
if (query.trim() !== q) return;
const more = body.hits as SearchHit[];
const seen = new Set(hits.map((h) => h.id));
const deduped = more.filter((h) => !seen.has(h.id));
hits = [...hits, ...deduped];
if (more.length < LOCAL_PAGE) localExhausted = true;
} else if (!webExhausted) {
// Lokale erschöpft → auf Web umschalten / weiterblättern.
const nextPage = webPageno + 1;
webSearching = webHits.length === 0;
try {
const wres = await fetch(
`/api/recipes/search/web?q=${encodeURIComponent(q)}&pageno=${nextPage}${filterParam()}`
);
if (query.trim() !== q) return;
if (!wres.ok) {
const err = await wres.json().catch(() => ({}));
webError = err.message ?? `HTTP ${wres.status}`;
webExhausted = true;
return;
}
const wbody = await wres.json();
const more = wbody.hits as WebHit[];
const seen = new Set(webHits.map((h) => h.url));
const deduped = more.filter((h) => !seen.has(h.url));
if (deduped.length === 0) {
webExhausted = true;
} else {
webHits = [...webHits, ...deduped];
webPageno = nextPage;
}
} finally {
if (query.trim() === q) webSearching = false;
}
}
} finally {
loadingMore = false;
}
}
$effect(() => { $effect(() => {
const q = query.trim(); // eslint-disable-next-line @typescript-eslint/no-unused-expressions
if (debounceTimer) clearTimeout(debounceTimer); store.query; // register reactive dep
if (skipNextSearch) { store.runDebounced();
// Snapshot-Restore hat hits/webHits/searchedFor wiederhergestellt —
// nicht erneut fetchen.
skipNextSearch = false;
return;
}
if (q.length <= 3) {
hits = [];
webHits = [];
searchedFor = null;
searching = false;
webSearching = false;
webError = null;
return;
}
searching = true;
webHits = [];
webSearching = false;
webError = null;
debounceTimer = setTimeout(() => {
void runSearch(q);
}, 300);
}); });
function submit(e: SubmitEvent) { function submit(e: SubmitEvent) {
e.preventDefault(); e.preventDefault();
const q = query.trim(); const q = store.query.trim();
if (q.length <= 3) return; if (q.length <= 3) return;
if (debounceTimer) clearTimeout(debounceTimer); void store.runSearch(q);
searching = true;
void runSearch(q);
} }
async function dismissFromRecent(recipeId: number, e: MouseEvent) { async function dismissFromRecent(recipeId: number, e: MouseEvent) {
@@ -367,7 +310,7 @@
}); });
} }
const activeSearch = $derived(query.trim().length > 3); const activeSearch = $derived(store.query.trim().length > 3);
</script> </script>
<section class="hero"> <section class="hero">
@@ -378,7 +321,7 @@
<SearchFilter inline /> <SearchFilter inline />
<input <input
type="search" type="search"
bind:value={query} bind:value={store.query}
placeholder="Rezept suchen…" placeholder="Rezept suchen…"
autocomplete="off" autocomplete="off"
inputmode="search" inputmode="search"
@@ -390,12 +333,12 @@
{#if activeSearch} {#if activeSearch}
<section class="results"> <section class="results">
{#if searching && hits.length === 0 && webHits.length === 0} {#if store.searching && store.hits.length === 0 && store.webHits.length === 0}
<SearchLoader scope="local" /> <SearchLoader scope="local" />
{:else} {:else}
{#if hits.length > 0} {#if store.hits.length > 0}
<ul class="cards"> <ul class="cards">
{#each hits as r (r.id)} {#each store.hits as r (r.id)}
<li> <li>
<a href={`/recipes/${r.id}`} class="card"> <a href={`/recipes/${r.id}`} class="card">
{#if r.image_path} {#if r.image_path}
@@ -413,20 +356,20 @@
</li> </li>
{/each} {/each}
</ul> </ul>
{:else if searchedFor === query.trim() && !webSearching && webHits.length === 0 && !webError} {:else if store.searchedFor === store.query.trim() && !store.webSearching && store.webHits.length === 0 && !store.webError}
<p class="muted no-local-msg">Keine lokalen Rezepte für „{searchedFor}".</p> <p class="muted no-local-msg">Keine lokalen Rezepte für „{store.searchedFor}".</p>
{/if} {/if}
{#if webHits.length > 0} {#if store.webHits.length > 0}
{#if hits.length > 0} {#if store.hits.length > 0}
<h3 class="sep">Aus dem Internet</h3> <h3 class="sep">Aus dem Internet</h3>
{:else if searchedFor === query.trim()} {:else if store.searchedFor === store.query.trim()}
<p class="muted no-local-msg"> <p class="muted no-local-msg">
Keine lokalen Rezepte für „{searchedFor}" — Ergebnisse aus dem Internet: Keine lokalen Rezepte für „{store.searchedFor}" — Ergebnisse aus dem Internet:
</p> </p>
{/if} {/if}
<ul class="cards"> <ul class="cards">
{#each webHits as w (w.url)} {#each store.webHits as w (w.url)}
<li> <li>
<a class="card" href={`/preview?url=${encodeURIComponent(w.url)}`}> <a class="card" href={`/preview?url=${encodeURIComponent(w.url)}`}>
{#if w.thumbnail} {#if w.thumbnail}
@@ -444,16 +387,16 @@
</ul> </ul>
{/if} {/if}
{#if webSearching} {#if store.webSearching}
<SearchLoader scope="web" /> <SearchLoader scope="web" />
{:else if webError && webHits.length === 0} {:else if store.webError && store.webHits.length === 0}
<p class="error">Internet-Suche zurzeit nicht möglich: {webError}</p> <p class="error">Internet-Suche zurzeit nicht möglich: {store.webError}</p>
{/if} {/if}
{#if searchedFor === query.trim() && !(localExhausted && webExhausted) && !(searching && hits.length === 0)} {#if store.searchedFor === store.query.trim() && !(store.localExhausted && store.webExhausted) && !(store.searching && store.hits.length === 0)}
<div class="more-cta"> <div class="more-cta">
<button class="more-btn" onclick={loadMore} disabled={loadingMore || webSearching}> <button class="more-btn" onclick={() => store.loadMore()} disabled={store.loadingMore || store.webSearching}>
{loadingMore || webSearching ? 'Lade …' : '+ weitere Ergebnisse'} {store.loadingMore || store.webSearching ? 'Lade …' : '+ weitere Ergebnisse'}
</button> </button>
</div> </div>
{/if} {/if}
@@ -462,57 +405,91 @@
{:else} {:else}
{#if profileStore.active && favorites.length > 0} {#if profileStore.active && favorites.length > 0}
<section class="listing"> <section class="listing">
<h2>Deine Favoriten</h2> <button
<ul class="cards"> type="button"
{#each favorites as r (r.id)} class="section-head"
<li class="card-wrap"> onclick={() => toggleCollapsed('favorites')}
<a href={`/recipes/${r.id}`} class="card"> aria-expanded={!collapsed.favorites}
{#if r.image_path} >
<img src={`/images/${r.image_path}`} alt="" loading="lazy" /> <ChevronDown
{:else} size={18}
<div class="placeholder"><CookingPot size={36} /></div> strokeWidth={2.2}
{/if} class={collapsed.favorites ? 'chev rotated' : 'chev'}
<div class="card-body"> />
<div class="title">{r.title}</div> <h2>Deine Favoriten</h2>
{#if r.source_domain} <span class="count">{favorites.length}</span>
<div class="domain">{r.source_domain}</div> </button>
{/if} {#if !collapsed.favorites}
</div> <div transition:slide={{ duration: 180 }}>
</a> <ul class="cards">
</li> {#each favorites as r (r.id)}
{/each} <li class="card-wrap">
</ul> <a href={`/recipes/${r.id}`} class="card">
{#if r.image_path}
<img src={`/images/${r.image_path}`} alt="" loading="lazy" />
{:else}
<div class="placeholder"><CookingPot size={36} /></div>
{/if}
<div class="card-body">
<div class="title">{r.title}</div>
{#if r.source_domain}
<div class="domain">{r.source_domain}</div>
{/if}
</div>
</a>
</li>
{/each}
</ul>
</div>
{/if}
</section> </section>
{/if} {/if}
{#if recent.length > 0} {#if recent.length > 0}
<section class="listing"> <section class="listing">
<h2>Zuletzt hinzugefügt</h2> <button
<ul class="cards"> type="button"
{#each recent as r (r.id)} class="section-head"
<li class="card-wrap"> onclick={() => toggleCollapsed('recent')}
<a href={`/recipes/${r.id}`} class="card"> aria-expanded={!collapsed.recent}
{#if r.image_path} >
<img src={`/images/${r.image_path}`} alt="" loading="lazy" /> <ChevronDown
{:else} size={18}
<div class="placeholder"><CookingPot size={36} /></div> strokeWidth={2.2}
{/if} class={collapsed.recent ? 'chev rotated' : 'chev'}
<div class="card-body"> />
<div class="title">{r.title}</div> <h2>Zuletzt hinzugefügt</h2>
{#if r.source_domain} <span class="count">{recent.length}</span>
<div class="domain">{r.source_domain}</div> </button>
{/if} {#if !collapsed.recent}
</div> <div transition:slide={{ duration: 180 }}>
</a> <ul class="cards">
<button {#each recent as r (r.id)}
class="dismiss" <li class="card-wrap">
aria-label="Aus Zuletzt-hinzugefügt entfernen" <a href={`/recipes/${r.id}`} class="card">
onclick={(e) => dismissFromRecent(r.id, e)} {#if r.image_path}
> <img src={`/images/${r.image_path}`} alt="" loading="lazy" />
<X size={16} strokeWidth={2.5} /> {:else}
</button> <div class="placeholder"><CookingPot size={36} /></div>
</li> {/if}
{/each} <div class="card-body">
</ul> <div class="title">{r.title}</div>
{#if r.source_domain}
<div class="domain">{r.source_domain}</div>
{/if}
</div>
</a>
<button
class="dismiss"
aria-label="Aus Zuletzt-hinzugefügt entfernen"
onclick={(e) => dismissFromRecent(r.id, e)}
>
<X size={16} strokeWidth={2.5} />
</button>
</li>
{/each}
</ul>
</div>
{/if}
</section> </section>
{/if} {/if}
<section class="listing"> <section class="listing">
@@ -632,6 +609,49 @@
color: #444; color: #444;
margin: 0 0 0.75rem; margin: 0 0 0.75rem;
} }
.section-head {
display: flex;
align-items: center;
gap: 0.5rem;
width: 100%;
padding: 0.4rem 0.25rem;
background: transparent;
border: 0;
border-radius: 8px;
text-align: left;
cursor: pointer;
font-family: inherit;
color: inherit;
min-height: 44px;
margin-bottom: 0.4rem;
}
.section-head:hover {
background: #f4f8f5;
}
.section-head:focus-visible {
outline: 2px solid #2b6a3d;
outline-offset: 2px;
}
.section-head h2 {
margin: 0;
font-size: 1.05rem;
color: #444;
font-weight: 600;
}
.section-head .count {
margin-left: auto;
color: #888;
font-size: 0.85rem;
font-variant-numeric: tabular-nums;
}
.section-head :global(.chev) {
color: #2b6a3d;
flex-shrink: 0;
transition: transform 180ms;
}
.section-head :global(.chev.rotated) {
transform: rotate(-90deg);
}
.listing-head { .listing-head {
display: flex; display: flex;
align-items: center; align-items: center;
@@ -653,7 +673,7 @@
padding: 0.4rem 0.85rem; padding: 0.4rem 0.85rem;
background: white; background: white;
border: 1px solid #cfd9d1; border: 1px solid #cfd9d1;
border-radius: 999px; border-radius: var(--pill-radius);
color: #2b6a3d; color: #2b6a3d;
font-size: 0.88rem; font-size: 0.88rem;
cursor: pointer; cursor: pointer;
@@ -760,7 +780,7 @@
right: 0.4rem; right: 0.4rem;
width: 28px; width: 28px;
height: 28px; height: 28px;
border-radius: 999px; border-radius: var(--pill-radius);
border: 0; border: 0;
background: rgba(255, 255, 255, 0.9); background: rgba(255, 255, 255, 0.9);
color: #444; color: #444;

View File

@@ -42,7 +42,7 @@
padding: 0.5rem 0.95rem 0.5rem 0.8rem; padding: 0.5rem 0.95rem 0.5rem 0.8rem;
background: white; background: white;
border: 1px solid #e4eae7; border: 1px solid #e4eae7;
border-radius: 999px; border-radius: var(--pill-radius);
text-decoration: none; text-decoration: none;
color: #444; color: #444;
font-size: 0.95rem; font-size: 0.95rem;

View File

@@ -2,7 +2,8 @@
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { Pencil, Check, X, Globe } from 'lucide-svelte'; import { Pencil, Check, X, Globe } from 'lucide-svelte';
import type { AllowedDomain } from '$lib/types'; import type { AllowedDomain } from '$lib/types';
import { confirmAction, alertAction } from '$lib/client/confirm.svelte'; import { confirmAction } from '$lib/client/confirm.svelte';
import { asyncFetch } from '$lib/client/api-fetch-wrapper';
import { requireOnline } from '$lib/client/require-online'; import { requireOnline } from '$lib/client/require-online';
let domains = $state<AllowedDomain[]>([]); let domains = $state<AllowedDomain[]>([]);
@@ -64,22 +65,19 @@
if (!requireOnline('Das Speichern')) return; if (!requireOnline('Das Speichern')) return;
saving = true; saving = true;
try { try {
const res = await fetch(`/api/domains/${d.id}`, { const res = await asyncFetch(
method: 'PATCH', `/api/domains/${d.id}`,
headers: { 'content-type': 'application/json' }, {
body: JSON.stringify({ method: 'PATCH',
domain: editDomain.trim(), headers: { 'content-type': 'application/json' },
display_name: editLabel.trim() || null body: JSON.stringify({
}) domain: editDomain.trim(),
}); display_name: editLabel.trim() || null
if (!res.ok) { })
const body = await res.json().catch(() => ({})); },
await alertAction({ 'Speichern fehlgeschlagen'
title: 'Speichern fehlgeschlagen', );
message: body.message ?? `HTTP ${res.status}` if (!res) return;
});
return;
}
cancelEdit(); cancelEdit();
await load(); await load();
} finally { } finally {

View File

@@ -1,6 +1,7 @@
<script lang="ts"> <script lang="ts">
import { profileStore } from '$lib/client/profile.svelte'; import { profileStore } from '$lib/client/profile.svelte';
import { confirmAction, alertAction } from '$lib/client/confirm.svelte'; import { confirmAction } from '$lib/client/confirm.svelte';
import { asyncFetch } from '$lib/client/api-fetch-wrapper';
import { requireOnline } from '$lib/client/require-online'; import { requireOnline } from '$lib/client/require-online';
let newName = $state(''); let newName = $state('');
@@ -27,19 +28,16 @@
const next = prompt('Neuer Name:', currentName); const next = prompt('Neuer Name:', currentName);
if (!next || next === currentName) return; if (!next || next === currentName) return;
if (!requireOnline('Das Umbenennen')) return; if (!requireOnline('Das Umbenennen')) return;
const res = await fetch(`/api/profiles/${id}`, { const res = await asyncFetch(
method: 'PATCH', `/api/profiles/${id}`,
headers: { 'content-type': 'application/json' }, {
body: JSON.stringify({ name: next.trim() }) method: 'PATCH',
}); headers: { 'content-type': 'application/json' },
if (!res.ok) { body: JSON.stringify({ name: next.trim() })
const body = await res.json().catch(() => ({})); },
await alertAction({ 'Umbenennen fehlgeschlagen'
title: 'Umbenennen fehlgeschlagen', );
message: body.message ?? `HTTP ${res.status}` if (!res) return;
});
return;
}
await profileStore.load(); await profileStore.load();
} }
@@ -187,7 +185,7 @@
padding: 0.15rem 0.5rem; padding: 0.15rem 0.5rem;
background: #eaf4ed; background: #eaf4ed;
color: #2b6a3d; color: #2b6a3d;
border-radius: 999px; border-radius: var(--pill-radius);
font-size: 0.75rem; font-size: 0.75rem;
} }
.actions { .actions {

View File

@@ -1,12 +1,10 @@
import type { RequestHandler } from './$types'; import type { RequestHandler } from './$types';
import { createBackupStream, backupFilename } from '$lib/server/backup/export'; import { createBackupStream, backupFilename } from '$lib/server/backup/export';
import { DATABASE_PATH, IMAGE_DIR } from '$lib/server/paths';
import { Readable } from 'node:stream'; import { Readable } from 'node:stream';
const DB_PATH = process.env.DATABASE_PATH ?? './data/kochwas.db';
const IMAGE_DIR = process.env.IMAGE_DIR ?? './data/images';
export const GET: RequestHandler = async () => { export const GET: RequestHandler = async () => {
const archive = createBackupStream({ dbPath: DB_PATH, imagesDir: IMAGE_DIR }); const archive = createBackupStream({ dbPath: DATABASE_PATH, imagesDir: IMAGE_DIR });
const filename = backupFilename(); const filename = backupFilename();
return new Response(Readable.toWeb(archive) as ReadableStream, { return new Response(Readable.toWeb(archive) as ReadableStream, {
status: 200, status: 200,

View File

@@ -1,9 +1,11 @@
import type { RequestHandler } from './$types'; import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit'; import { json, error, isHttpError } from '@sveltejs/kit';
import { z } from 'zod'; import { z } from 'zod';
import { getDb } from '$lib/server/db'; import { getDb } from '$lib/server/db';
import { validateBody } from '$lib/server/api-helpers';
import { addDomain, listDomains, setDomainFavicon } from '$lib/server/domains/repository'; import { addDomain, listDomains, setDomainFavicon } from '$lib/server/domains/repository';
import { ensureFavicons, fetchAndStoreFavicon } from '$lib/server/domains/favicons'; import { ensureFavicons, fetchAndStoreFavicon } from '$lib/server/domains/favicons';
import { IMAGE_DIR } from '$lib/server/paths';
const CreateSchema = z.object({ const CreateSchema = z.object({
domain: z.string().min(3).max(253), domain: z.string().min(3).max(253),
@@ -11,8 +13,6 @@ const CreateSchema = z.object({
added_by_profile_id: z.number().int().positive().nullable().optional() added_by_profile_id: z.number().int().positive().nullable().optional()
}); });
const IMAGE_DIR = process.env.IMAGE_DIR ?? './data/images';
export const GET: RequestHandler = async () => { export const GET: RequestHandler = async () => {
const db = getDb(); const db = getDb();
// Favicons lazy nachziehen — beim zweiten Aufruf gibt es nichts mehr zu tun. // Favicons lazy nachziehen — beim zweiten Aufruf gibt es nichts mehr zu tun.
@@ -21,16 +21,14 @@ export const GET: RequestHandler = async () => {
}; };
export const POST: RequestHandler = async ({ request }) => { export const POST: RequestHandler = async ({ request }) => {
const body = await request.json().catch(() => null); const data = validateBody(await request.json().catch(() => null), CreateSchema);
const parsed = CreateSchema.safeParse(body);
if (!parsed.success) error(400, { message: 'Invalid body' });
try { try {
const db = getDb(); const db = getDb();
const d = addDomain( const d = addDomain(
db, db,
parsed.data.domain, data.domain,
parsed.data.display_name ?? null, data.display_name ?? null,
parsed.data.added_by_profile_id ?? null data.added_by_profile_id ?? null
); );
// Favicon direkt nach dem Insert mitziehen, damit die Antwort schon das // Favicon direkt nach dem Insert mitziehen, damit die Antwort schon das
// Icon enthält — der POST ist eh ein interaktiver Admin-Vorgang. // Icon enthält — der POST ist eh ein interaktiver Admin-Vorgang.
@@ -41,6 +39,7 @@ export const POST: RequestHandler = async ({ request }) => {
} }
return json(d, { status: 201 }); return json(d, { status: 201 });
} catch (e) { } catch (e) {
if (isHttpError(e)) throw e;
error(409, { message: (e as Error).message }); error(409, { message: (e as Error).message });
} }
}; };

View File

@@ -1,35 +1,27 @@
import type { RequestHandler } from './$types'; import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit'; import { json, error, isHttpError } from '@sveltejs/kit';
import { z } from 'zod'; import { z } from 'zod';
import { getDb } from '$lib/server/db'; import { getDb } from '$lib/server/db';
import { parsePositiveIntParam, validateBody } from '$lib/server/api-helpers';
import { import {
removeDomain, removeDomain,
updateDomain, updateDomain,
setDomainFavicon setDomainFavicon
} from '$lib/server/domains/repository'; } from '$lib/server/domains/repository';
import { fetchAndStoreFavicon } from '$lib/server/domains/favicons'; import { fetchAndStoreFavicon } from '$lib/server/domains/favicons';
import { IMAGE_DIR } from '$lib/server/paths';
const IMAGE_DIR = process.env.IMAGE_DIR ?? './data/images';
const UpdateSchema = z.object({ const UpdateSchema = z.object({
domain: z.string().min(3).max(253).optional(), domain: z.string().min(3).max(253).optional(),
display_name: z.string().max(100).nullable().optional() display_name: z.string().max(100).nullable().optional()
}); });
function parseId(raw: string): number {
const id = Number(raw);
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
return id;
}
export const PATCH: RequestHandler = async ({ params, request }) => { export const PATCH: RequestHandler = async ({ params, request }) => {
const id = parseId(params.id!); const id = parsePositiveIntParam(params.id, 'id');
const body = await request.json().catch(() => null); const data = validateBody(await request.json().catch(() => null), UpdateSchema);
const parsed = UpdateSchema.safeParse(body);
if (!parsed.success) error(400, { message: 'Invalid body' });
try { try {
const db = getDb(); const db = getDb();
const updated = updateDomain(db, id, parsed.data); const updated = updateDomain(db, id, data);
if (!updated) error(404, { message: 'Not found' }); if (!updated) error(404, { message: 'Not found' });
// Wenn updateDomain favicon_path genullt hat (Domain geändert), frisch laden. // Wenn updateDomain favicon_path genullt hat (Domain geändert), frisch laden.
if (updated.favicon_path === null) { if (updated.favicon_path === null) {
@@ -41,12 +33,14 @@ export const PATCH: RequestHandler = async ({ params, request }) => {
} }
return json(updated); return json(updated);
} catch (e) { } catch (e) {
// HTTP-Errors aus error() durchreichen, sonst landet ein 404 als 409.
if (isHttpError(e)) throw e;
error(409, { message: (e as Error).message }); error(409, { message: (e as Error).message });
} }
}; };
export const DELETE: RequestHandler = async ({ params }) => { export const DELETE: RequestHandler = async ({ params }) => {
const id = parseId(params.id!); const id = parsePositiveIntParam(params.id, 'id');
removeDomain(getDb(), id); removeDomain(getDb(), id);
return json({ ok: true }); return json({ ok: true });
}; };

View File

@@ -1,7 +1,8 @@
import type { RequestHandler } from './$types'; import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit'; import { json, error, isHttpError } from '@sveltejs/kit';
import { z } from 'zod'; import { z } from 'zod';
import { getDb } from '$lib/server/db'; import { getDb } from '$lib/server/db';
import { validateBody } from '$lib/server/api-helpers';
import { createProfile, listProfiles } from '$lib/server/profiles/repository'; import { createProfile, listProfiles } from '$lib/server/profiles/repository';
const CreateSchema = z.object({ const CreateSchema = z.object({
@@ -14,15 +15,12 @@ export const GET: RequestHandler = async () => {
}; };
export const POST: RequestHandler = async ({ request }) => { export const POST: RequestHandler = async ({ request }) => {
const body = await request.json().catch(() => null); const data = validateBody(await request.json().catch(() => null), CreateSchema);
const parsed = CreateSchema.safeParse(body);
if (!parsed.success) {
error(400, { message: 'Invalid body', issues: parsed.error.issues });
}
try { try {
const p = createProfile(getDb(), parsed.data.name, parsed.data.avatar_emoji ?? null); const p = createProfile(getDb(), data.name, data.avatar_emoji ?? null);
return json(p, { status: 201 }); return json(p, { status: 201 });
} catch (e) { } catch (e) {
if (isHttpError(e)) throw e;
error(409, { message: (e as Error).message }); error(409, { message: (e as Error).message });
} }
}; };

View File

@@ -1,28 +1,21 @@
import type { RequestHandler } from './$types'; import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit'; import { json } from '@sveltejs/kit';
import { z } from 'zod'; import { z } from 'zod';
import { getDb } from '$lib/server/db'; import { getDb } from '$lib/server/db';
import { parsePositiveIntParam, validateBody } from '$lib/server/api-helpers';
import { deleteProfile, renameProfile } from '$lib/server/profiles/repository'; import { deleteProfile, renameProfile } from '$lib/server/profiles/repository';
const RenameSchema = z.object({ name: z.string().min(1).max(50) }); const RenameSchema = z.object({ name: z.string().min(1).max(50) });
function parseId(raw: string): number {
const id = Number(raw);
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
return id;
}
export const PATCH: RequestHandler = async ({ params, request }) => { export const PATCH: RequestHandler = async ({ params, request }) => {
const id = parseId(params.id!); const id = parsePositiveIntParam(params.id, 'id');
const body = await request.json().catch(() => null); const data = validateBody(await request.json().catch(() => null), RenameSchema);
const parsed = RenameSchema.safeParse(body); renameProfile(getDb(), id, data.name);
if (!parsed.success) error(400, { message: 'Invalid body' });
renameProfile(getDb(), id, parsed.data.name);
return json({ ok: true }); return json({ ok: true });
}; };
export const DELETE: RequestHandler = async ({ params }) => { export const DELETE: RequestHandler = async ({ params }) => {
const id = parseId(params.id!); const id = parsePositiveIntParam(params.id, 'id');
deleteProfile(getDb(), id); deleteProfile(getDb(), id);
return json({ ok: true }); return json({ ok: true });
}; };

View File

@@ -0,0 +1,61 @@
import type { RequestHandler } from './$types';
import { json } from '@sveltejs/kit';
import { z } from 'zod';
import { getDb } from '$lib/server/db';
import { validateBody } from '$lib/server/api-helpers';
import { insertRecipe } from '$lib/server/recipes/repository';
const IngredientSchema = z.object({
position: z.number().int().nonnegative(),
quantity: z.number().nullable(),
unit: z.string().max(30).nullable(),
name: z.string().min(1).max(200),
note: z.string().max(300).nullable(),
raw_text: z.string().max(500),
section_heading: z.string().max(200).nullable()
});
const StepSchema = z.object({
position: z.number().int().positive(),
text: z.string().min(1).max(4000)
});
const CreateRecipeSchema = z.object({
title: z.string().min(1).max(200),
description: z.string().max(2000).nullable(),
servings_default: z.number().int().nonnegative().nullable(),
servings_unit: z.string().max(30).nullable(),
prep_time_min: z.number().int().nonnegative().nullable(),
cook_time_min: z.number().int().nonnegative().nullable(),
total_time_min: z.number().int().nonnegative().nullable(),
ingredients: z.array(IngredientSchema),
steps: z.array(StepSchema)
});
// Anlegen eines kompletten Rezepts aus Scratch. Wird vom Foto-Import-Flow
// genutzt, nachdem der Nutzer im Editor die AI-Extraktion geprüft/korrigiert
// und auf Speichern getippt hat. Der bestehende /api/recipes/blank-Endpoint
// bleibt für den „leer anlegen"-Flow unverändert.
export const POST: RequestHandler = async ({ request }) => {
const body = await request.json().catch(() => null);
const p = validateBody(body, CreateRecipeSchema);
const id = insertRecipe(getDb(), {
id: null,
title: p.title,
description: p.description,
source_url: null,
source_domain: null,
image_path: null,
servings_default: p.servings_default,
servings_unit: p.servings_unit,
prep_time_min: p.prep_time_min,
cook_time_min: p.cook_time_min,
total_time_min: p.total_time_min,
cuisine: null,
category: null,
ingredients: p.ingredients,
steps: p.steps,
tags: []
});
return json({ id }, { status: 201 });
};

View File

@@ -2,6 +2,7 @@ import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit'; import { json, error } from '@sveltejs/kit';
import { z } from 'zod'; import { z } from 'zod';
import { getDb } from '$lib/server/db'; import { getDb } from '$lib/server/db';
import { parsePositiveIntParam, validateBody } from '$lib/server/api-helpers';
import { import {
deleteRecipe, deleteRecipe,
getRecipeById, getRecipeById,
@@ -23,7 +24,8 @@ const IngredientSchema = z.object({
unit: z.string().max(30).nullable(), unit: z.string().max(30).nullable(),
name: z.string().min(1).max(200), name: z.string().min(1).max(200),
note: z.string().max(300).nullable(), note: z.string().max(300).nullable(),
raw_text: z.string().max(500) raw_text: z.string().max(500),
section_heading: z.string().max(200).nullable()
}); });
const StepSchema = z.object({ const StepSchema = z.object({
@@ -48,14 +50,8 @@ const PatchSchema = z
}) })
.refine((v) => Object.keys(v).length > 0, { message: 'Empty patch' }); .refine((v) => Object.keys(v).length > 0, { message: 'Empty patch' });
function parseId(raw: string): number {
const id = Number(raw);
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
return id;
}
export const GET: RequestHandler = async ({ params }) => { export const GET: RequestHandler = async ({ params }) => {
const id = parseId(params.id!); const id = parsePositiveIntParam(params.id, 'id');
const db = getDb(); const db = getDb();
const recipe = getRecipeById(db, id); const recipe = getRecipeById(db, id);
if (!recipe) error(404, { message: 'Recipe not found' }); if (!recipe) error(404, { message: 'Recipe not found' });
@@ -68,12 +64,10 @@ export const GET: RequestHandler = async ({ params }) => {
}; };
export const PATCH: RequestHandler = async ({ params, request }) => { export const PATCH: RequestHandler = async ({ params, request }) => {
const id = parseId(params.id!); const id = parsePositiveIntParam(params.id, 'id');
const body = await request.json().catch(() => null); const body = await request.json().catch(() => null);
const parsed = PatchSchema.safeParse(body); const p = validateBody(body, PatchSchema);
if (!parsed.success) error(400, { message: 'Invalid body' });
const db = getDb(); const db = getDb();
const p = parsed.data;
// Spezielle Kurz-Updates (bleiben als Sonderfall, weil sie FTS triggern // Spezielle Kurz-Updates (bleiben als Sonderfall, weil sie FTS triggern
// bzw. andere Tabellen mitpflegen). // bzw. andere Tabellen mitpflegen).
if (p.title !== undefined && Object.keys(p).length === 1) { if (p.title !== undefined && Object.keys(p).length === 1) {
@@ -121,7 +115,7 @@ export const PATCH: RequestHandler = async ({ params, request }) => {
}; };
export const DELETE: RequestHandler = async ({ params }) => { export const DELETE: RequestHandler = async ({ params }) => {
const id = parseId(params.id!); const id = parsePositiveIntParam(params.id, 'id');
deleteRecipe(getDb(), id); deleteRecipe(getDb(), id);
return json({ ok: true }); return json({ ok: true });
}; };

View File

@@ -1,7 +1,8 @@
import type { RequestHandler } from './$types'; import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit'; import { json } from '@sveltejs/kit';
import { z } from 'zod'; import { z } from 'zod';
import { getDb } from '$lib/server/db'; import { getDb } from '$lib/server/db';
import { parsePositiveIntParam, validateBody } from '$lib/server/api-helpers';
import { addComment, deleteComment, listComments } from '$lib/server/recipes/actions'; import { addComment, deleteComment, listComments } from '$lib/server/recipes/actions';
const Schema = z.object({ const Schema = z.object({
@@ -11,30 +12,20 @@ const Schema = z.object({
const DeleteSchema = z.object({ comment_id: z.number().int().positive() }); const DeleteSchema = z.object({ comment_id: z.number().int().positive() });
function parseId(raw: string): number {
const id = Number(raw);
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
return id;
}
export const GET: RequestHandler = async ({ params }) => { export const GET: RequestHandler = async ({ params }) => {
const id = parseId(params.id!); const id = parsePositiveIntParam(params.id, 'id');
return json(listComments(getDb(), id)); return json(listComments(getDb(), id));
}; };
export const POST: RequestHandler = async ({ params, request }) => { export const POST: RequestHandler = async ({ params, request }) => {
const id = parseId(params.id!); const id = parsePositiveIntParam(params.id, 'id');
const body = await request.json().catch(() => null); const data = validateBody(await request.json().catch(() => null), Schema);
const parsed = Schema.safeParse(body); const cid = addComment(getDb(), id, data.profile_id, data.text);
if (!parsed.success) error(400, { message: 'Invalid body' });
const cid = addComment(getDb(), id, parsed.data.profile_id, parsed.data.text);
return json({ id: cid }, { status: 201 }); return json({ id: cid }, { status: 201 });
}; };
export const DELETE: RequestHandler = async ({ request }) => { export const DELETE: RequestHandler = async ({ request }) => {
const body = await request.json().catch(() => null); const data = validateBody(await request.json().catch(() => null), DeleteSchema);
const parsed = DeleteSchema.safeParse(body); deleteComment(getDb(), data.comment_id);
if (!parsed.success) error(400, { message: 'Invalid body' });
deleteComment(getDb(), parsed.data.comment_id);
return json({ ok: true }); return json({ ok: true });
}; };

View File

@@ -1,25 +1,18 @@
import type { RequestHandler } from './$types'; import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit'; import { json } from '@sveltejs/kit';
import { z } from 'zod'; import { z } from 'zod';
import { getDb } from '$lib/server/db'; import { getDb } from '$lib/server/db';
import { parsePositiveIntParam, validateBody } from '$lib/server/api-helpers';
import { logCooked } from '$lib/server/recipes/actions'; import { logCooked } from '$lib/server/recipes/actions';
import { removeFromWishlistForAll } from '$lib/server/wishlist/repository'; import { removeFromWishlistForAll } from '$lib/server/wishlist/repository';
const Schema = z.object({ profile_id: z.number().int().positive() }); const Schema = z.object({ profile_id: z.number().int().positive() });
function parseId(raw: string): number {
const id = Number(raw);
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
return id;
}
export const POST: RequestHandler = async ({ params, request }) => { export const POST: RequestHandler = async ({ params, request }) => {
const id = parseId(params.id!); const id = parsePositiveIntParam(params.id, 'id');
const body = await request.json().catch(() => null); const data = validateBody(await request.json().catch(() => null), Schema);
const parsed = Schema.safeParse(body);
if (!parsed.success) error(400, { message: 'Invalid body' });
const db = getDb(); const db = getDb();
const entry = logCooked(db, id, parsed.data.profile_id); const entry = logCooked(db, id, data.profile_id);
// Wenn das Rezept heute gekocht wurde, ist der Wunsch erfüllt — für alle // Wenn das Rezept heute gekocht wurde, ist der Wunsch erfüllt — für alle
// Profile raus aus der Wunschliste. Client nutzt den removed_from_wishlist- // Profile raus aus der Wunschliste. Client nutzt den removed_from_wishlist-
// Flag, um den lokalen State (Badge, Button) ohne Reload zu aktualisieren. // Flag, um den lokalen State (Badge, Button) ohne Reload zu aktualisieren.

View File

@@ -1,31 +1,22 @@
import type { RequestHandler } from './$types'; import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit'; import { json } from '@sveltejs/kit';
import { z } from 'zod'; import { z } from 'zod';
import { getDb } from '$lib/server/db'; import { getDb } from '$lib/server/db';
import { parsePositiveIntParam, validateBody } from '$lib/server/api-helpers';
import { addFavorite, removeFavorite } from '$lib/server/recipes/actions'; import { addFavorite, removeFavorite } from '$lib/server/recipes/actions';
const Schema = z.object({ profile_id: z.number().int().positive() }); const Schema = z.object({ profile_id: z.number().int().positive() });
function parseId(raw: string): number {
const id = Number(raw);
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
return id;
}
export const PUT: RequestHandler = async ({ params, request }) => { export const PUT: RequestHandler = async ({ params, request }) => {
const id = parseId(params.id!); const id = parsePositiveIntParam(params.id, 'id');
const body = await request.json().catch(() => null); const data = validateBody(await request.json().catch(() => null), Schema);
const parsed = Schema.safeParse(body); addFavorite(getDb(), id, data.profile_id);
if (!parsed.success) error(400, { message: 'Invalid body' });
addFavorite(getDb(), id, parsed.data.profile_id);
return json({ ok: true }); return json({ ok: true });
}; };
export const DELETE: RequestHandler = async ({ params, request }) => { export const DELETE: RequestHandler = async ({ params, request }) => {
const id = parseId(params.id!); const id = parsePositiveIntParam(params.id, 'id');
const body = await request.json().catch(() => null); const data = validateBody(await request.json().catch(() => null), Schema);
const parsed = Schema.safeParse(body); removeFavorite(getDb(), id, data.profile_id);
if (!parsed.success) error(400, { message: 'Invalid body' });
removeFavorite(getDb(), id, parsed.data.profile_id);
return json({ ok: true }); return json({ ok: true });
}; };

View File

@@ -0,0 +1,56 @@
import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit';
import { createHash } from 'node:crypto';
import { existsSync } from 'node:fs';
import { mkdir, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { getDb } from '$lib/server/db';
import { parsePositiveIntParam } from '$lib/server/api-helpers';
import { getRecipeById, updateImagePath } from '$lib/server/recipes/repository';
import { IMAGE_DIR } from '$lib/server/paths';
const MAX_BYTES = 10 * 1024 * 1024;
const EXT_BY_MIME: Record<string, string> = {
'image/jpeg': '.jpg',
'image/jpg': '.jpg',
'image/png': '.png',
'image/webp': '.webp',
'image/gif': '.gif',
'image/avif': '.avif'
};
export const POST: RequestHandler = async ({ params, request }) => {
const id = parsePositiveIntParam(params.id, 'id');
const db = getDb();
if (!getRecipeById(db, id)) error(404, { message: 'Recipe not found' });
const form = await request.formData().catch(() => null);
const file = form?.get('file');
if (!(file instanceof File)) error(400, { message: 'Field "file" missing' });
if (file.size === 0) error(400, { message: 'Empty file' });
if (file.size > MAX_BYTES) error(413, { message: 'Image too large (max 10 MB)' });
const mime = file.type.toLowerCase();
const ext = EXT_BY_MIME[mime];
if (!ext) error(415, { message: `Image format ${file.type || 'unknown'} not supported` });
const buf = Buffer.from(await file.arrayBuffer());
const hash = createHash('sha256').update(buf).digest('hex');
const filename = `${hash}${ext}`;
const target = join(IMAGE_DIR, filename);
if (!existsSync(target)) {
await mkdir(IMAGE_DIR, { recursive: true });
await writeFile(target, buf);
}
updateImagePath(db, id, filename);
return json({ ok: true, image_path: filename });
};
export const DELETE: RequestHandler = ({ params }) => {
const id = parsePositiveIntParam(params.id, 'id');
const db = getDb();
if (!getRecipeById(db, id)) error(404, { message: 'Recipe not found' });
updateImagePath(db, id, null);
return json({ ok: true });
};

View File

@@ -1,7 +1,8 @@
import type { RequestHandler } from './$types'; import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit'; import { json } from '@sveltejs/kit';
import { z } from 'zod'; import { z } from 'zod';
import { getDb } from '$lib/server/db'; import { getDb } from '$lib/server/db';
import { parsePositiveIntParam, validateBody } from '$lib/server/api-helpers';
import { clearRating, setRating } from '$lib/server/recipes/actions'; import { clearRating, setRating } from '$lib/server/recipes/actions';
const Schema = z.object({ const Schema = z.object({
@@ -11,26 +12,16 @@ const Schema = z.object({
const DeleteSchema = z.object({ profile_id: z.number().int().positive() }); const DeleteSchema = z.object({ profile_id: z.number().int().positive() });
function parseId(raw: string): number {
const id = Number(raw);
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
return id;
}
export const PUT: RequestHandler = async ({ params, request }) => { export const PUT: RequestHandler = async ({ params, request }) => {
const id = parseId(params.id!); const id = parsePositiveIntParam(params.id, 'id');
const body = await request.json().catch(() => null); const data = validateBody(await request.json().catch(() => null), Schema);
const parsed = Schema.safeParse(body); setRating(getDb(), id, data.profile_id, data.stars);
if (!parsed.success) error(400, { message: 'Invalid body' });
setRating(getDb(), id, parsed.data.profile_id, parsed.data.stars);
return json({ ok: true }); return json({ ok: true });
}; };
export const DELETE: RequestHandler = async ({ params, request }) => { export const DELETE: RequestHandler = async ({ params, request }) => {
const id = parseId(params.id!); const id = parsePositiveIntParam(params.id, 'id');
const body = await request.json().catch(() => null); const data = validateBody(await request.json().catch(() => null), DeleteSchema);
const parsed = DeleteSchema.safeParse(body); clearRating(getDb(), id, data.profile_id);
if (!parsed.success) error(400, { message: 'Invalid body' });
clearRating(getDb(), id, parsed.data.profile_id);
return json({ ok: true }); return json({ ok: true });
}; };

View File

@@ -0,0 +1,27 @@
import type { RequestHandler } from './$types';
import { z } from 'zod';
import { error } from '@sveltejs/kit';
import { getDb } from '$lib/server/db';
import { validateBody, parsePositiveIntParam } from '$lib/server/api-helpers';
import { recordView } from '$lib/server/recipes/views';
const Schema = z.object({
profile_id: z.number().int().positive()
});
export const POST: RequestHandler = async ({ params, request }) => {
const recipeId = parsePositiveIntParam(params.id, 'id');
const body = validateBody(await request.json().catch(() => null), Schema);
try {
recordView(getDb(), body.profile_id, recipeId);
} catch (e) {
// FK violation (unknown profile or recipe) → 404
if (e instanceof Error && /FOREIGN KEY constraint failed/i.test(e.message)) {
error(404, { message: 'Recipe or profile not found' });
}
throw e;
}
return new Response(null, { status: 204 });
};

View File

@@ -6,13 +6,30 @@ import {
type AllRecipesSort type AllRecipesSort
} from '$lib/server/recipes/search-local'; } from '$lib/server/recipes/search-local';
const VALID_SORTS = new Set<AllRecipesSort>(['name', 'rating', 'cooked', 'created']); const VALID_SORTS = new Set<AllRecipesSort>([
'name',
'rating',
'cooked',
'created',
'viewed'
]);
function parseProfileId(raw: string | null): number | null {
if (!raw) return null;
const n = Number(raw);
return Number.isInteger(n) && n > 0 ? n : null;
}
export const GET: RequestHandler = async ({ url }) => { export const GET: RequestHandler = async ({ url }) => {
const sortRaw = (url.searchParams.get('sort') ?? 'name') as AllRecipesSort; const sortRaw = (url.searchParams.get('sort') ?? 'name') as AllRecipesSort;
if (!VALID_SORTS.has(sortRaw)) error(400, { message: 'Invalid sort' }); if (!VALID_SORTS.has(sortRaw)) error(400, { message: 'Invalid sort' });
const limit = Math.min(50, Math.max(1, Number(url.searchParams.get('limit') ?? 10))); // Cap is 200 (not 10's typical paging step) to support snapshot-based
// pagination restore on /+page.svelte: when the user navigates back
// after deep infinite-scroll, we re-hydrate the full loaded count in
// one round-trip so document height matches and scroll-restore lands.
const limit = Math.min(200, Math.max(1, Number(url.searchParams.get('limit') ?? 10)));
const offset = Math.max(0, Number(url.searchParams.get('offset') ?? 0)); const offset = Math.max(0, Number(url.searchParams.get('offset') ?? 0));
const hits = listAllRecipesPaginated(getDb(), sortRaw, limit, offset); const profileId = parseProfileId(url.searchParams.get('profile_id'));
const hits = listAllRecipesPaginated(getDb(), sortRaw, limit, offset, profileId);
return json({ sort: sortRaw, limit, offset, hits }); return json({ sort: sortRaw, limit, offset, hits });
}; };

View File

@@ -0,0 +1,183 @@
import type { RequestHandler } from './$types';
import { json } from '@sveltejs/kit';
import { extractRecipeFromImage, GeminiError } from '$lib/server/ai/gemini-client';
import { preprocessImage } from '$lib/server/ai/image-preprocess';
import { pickRandomPhrase } from '$lib/server/ai/description-phrases';
import { createRateLimiter } from '$lib/server/ai/rate-limit';
import type { Ingredient, Step } from '$lib/types';
// 20 MB deckt auch Tablet- und iPad-Pro-Fotos ab (oft 10-15 MB JPEG/HEIC).
// Muss zusammen mit BODY_SIZE_LIMIT (docker-compose.prod.yml) hochgezogen werden --
// SvelteKit rejected groessere Bodies frueher und wirft dann undurchsichtige
// "Multipart erwartet"-Fehler.
const MAX_BYTES = 20 * 1024 * 1024;
const ALLOWED_MIME = new Set([
'image/jpeg',
'image/png',
'image/webp',
'image/heic',
'image/heif'
]);
// Singleton-Limiter: 10 Requests/Minute pro IP. Verhindert Kosten-Runaways
// bei versehentlichem Dauer-Tappen.
const limiter = createRateLimiter({ windowMs: 60_000, max: 10 });
function errJson(status: number, code: string, message: string) {
return json({ code, message }, { status });
}
function buildRawText(q: number | null, u: string | null, name: string): string {
const parts: string[] = [];
if (q !== null) parts.push(String(q).replace('.', ','));
if (u) parts.push(u);
parts.push(name);
return parts.join(' ');
}
export const POST: RequestHandler = async ({ request, getClientAddress }) => {
const ip = getClientAddress();
if (!limiter.check(ip)) {
return errJson(
429,
'RATE_LIMITED',
'Zu viele Anfragen — bitte einen Moment warten.'
);
}
// Header-Snapshot fuer Diagnose beim Upload-Parse-Fehler. Wir loggen
// Content-Type, -Length und User-Agent — nichts, was Inhalt verraet.
const contentType = request.headers.get('content-type') ?? '(missing)';
const contentLength = request.headers.get('content-length') ?? '(missing)';
const userAgent = request.headers.get('user-agent')?.slice(0, 120) ?? '(missing)';
let form: FormData;
try {
form = await request.formData();
} catch (e) {
const err = e as Error;
console.warn(
`[extract-from-photo] formData() failed: name=${err.name} msg=${err.message} ` +
`ct="${contentType}" len=${contentLength} ua="${userAgent}"`
);
return errJson(
400,
'BAD_REQUEST',
`Upload konnte nicht gelesen werden (${err.name}: ${err.message}).`
);
}
const photo = form.get('photo');
if (!(photo instanceof Blob)) {
console.warn(
`[extract-from-photo] photo field missing or not a Blob. ct="${contentType}" ` +
`len=${contentLength} fields=${[...form.keys()].join(',')}`
);
return errJson(400, 'BAD_REQUEST', 'Feld "photo" fehlt.');
}
console.info(
`[extract-from-photo] received photo size=${photo.size} mime="${photo.type}" ua="${userAgent}"`
);
if (photo.size > MAX_BYTES) {
return errJson(
413,
'PAYLOAD_TOO_LARGE',
`Foto zu groß (max ${MAX_BYTES / 1024 / 1024} MB).`
);
}
if (!ALLOWED_MIME.has(photo.type)) {
return errJson(
415,
'UNSUPPORTED_MEDIA_TYPE',
`MIME "${photo.type}" nicht unterstützt.`
);
}
const rawBuffer = Buffer.from(await photo.arrayBuffer());
let preprocessed: { buffer: Buffer; mimeType: 'image/jpeg' };
try {
preprocessed = await preprocessImage(rawBuffer);
} catch (e) {
return errJson(
415,
'UNSUPPORTED_MEDIA_TYPE',
`Bild konnte nicht gelesen werden: ${(e as Error).message}`
);
}
const startedAt = Date.now();
let extracted;
try {
extracted = await extractRecipeFromImage(
preprocessed.buffer,
preprocessed.mimeType
);
} catch (e) {
if (e instanceof GeminiError) {
const status =
e.code === 'AI_RATE_LIMITED'
? 429
: e.code === 'AI_TIMEOUT'
? 503
: e.code === 'AI_NOT_CONFIGURED'
? 503
: 503;
// Nur Code + Meta + Error-Message loggen, niemals Prompt/Response-Inhalt.
// e.message enthaelt z.B. Zod-Validierungspfade oder "non-JSON output" --
// kein AI-Content, aber die Diagnose-Info, warum AI_FAILED kam.
console.warn(
`[extract-from-photo] ${e.code} after ${Date.now() - startedAt}ms, ${preprocessed.buffer.byteLength} bytes: ${e.message}`
);
return errJson(status, e.code, 'Die Bild-Analyse ist fehlgeschlagen.');
}
console.warn(`[extract-from-photo] UNEXPECTED ${(e as Error).message}`);
return errJson(503, 'AI_FAILED', 'Die Bild-Analyse ist fehlgeschlagen.');
}
// Minimum-Gültigkeit: Titel + (mind. 1 Zutat ODER mind. 1 Schritt).
if (
!extracted.title.trim() ||
(extracted.ingredients.length === 0 && extracted.steps.length === 0)
) {
return errJson(
422,
'NO_RECIPE_IN_IMAGE',
'Ich konnte kein Rezept im Bild erkennen.'
);
}
const ingredients: Ingredient[] = extracted.ingredients.map((i, idx) => ({
position: idx + 1,
quantity: i.quantity,
unit: i.unit,
name: i.name,
note: i.note,
raw_text: buildRawText(i.quantity, i.unit, i.name),
section_heading: null
}));
const steps: Step[] = extracted.steps.map((s, idx) => ({
position: idx + 1,
text: s.text
}));
return json({
recipe: {
id: null,
title: extracted.title,
description: pickRandomPhrase(),
source_url: null,
source_domain: null,
image_path: null,
servings_default: extracted.servings_default,
servings_unit: extracted.servings_unit,
prep_time_min: extracted.prep_time_min,
cook_time_min: extracted.cook_time_min,
total_time_min: extracted.total_time_min,
cuisine: null,
category: null,
ingredients,
steps,
tags: []
}
});
};

View File

@@ -1,20 +1,18 @@
import type { RequestHandler } from './$types'; import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit'; import { json } from '@sveltejs/kit';
import { z } from 'zod'; import { z } from 'zod';
import { getDb } from '$lib/server/db'; import { getDb } from '$lib/server/db';
import { validateBody } from '$lib/server/api-helpers';
import { importRecipe } from '$lib/server/recipes/importer'; import { importRecipe } from '$lib/server/recipes/importer';
import { mapImporterError } from '$lib/server/errors'; import { mapImporterError } from '$lib/server/errors';
import { IMAGE_DIR } from '$lib/server/paths';
const ImportSchema = z.object({ url: z.string().url() }); const ImportSchema = z.object({ url: z.string().url() });
const IMAGE_DIR = process.env.IMAGE_DIR ?? './data/images';
export const POST: RequestHandler = async ({ request }) => { export const POST: RequestHandler = async ({ request }) => {
const body = await request.json().catch(() => null); const data = validateBody(await request.json().catch(() => null), ImportSchema);
const parsed = ImportSchema.safeParse(body);
if (!parsed.success) error(400, { message: 'Invalid body' });
try { try {
const result = await importRecipe(getDb(), IMAGE_DIR, parsed.data.url); const result = await importRecipe(getDb(), IMAGE_DIR, data.url);
return json({ id: result.id, duplicate: result.duplicate }); return json({ id: result.id, duplicate: result.duplicate });
} catch (e) { } catch (e) {
mapImporterError(e); mapImporterError(e);

View File

@@ -7,13 +7,9 @@ export const GET: RequestHandler = async ({ url }) => {
const q = url.searchParams.get('q')?.trim() ?? ''; const q = url.searchParams.get('q')?.trim() ?? '';
const limit = Math.min(Number(url.searchParams.get('limit') ?? 30), 100); const limit = Math.min(Number(url.searchParams.get('limit') ?? 30), 100);
const offset = Math.max(0, Number(url.searchParams.get('offset') ?? 0)); const offset = Math.max(0, Number(url.searchParams.get('offset') ?? 0));
const domains = (url.searchParams.get('domains') ?? '')
.split(',')
.map((d) => d.trim())
.filter(Boolean);
const hits = const hits =
q.length >= 1 q.length >= 1
? searchLocal(getDb(), q, limit, offset, domains) ? searchLocal(getDb(), q, limit, offset)
: offset === 0 : offset === 0
? listRecentRecipes(getDb(), limit) ? listRecentRecipes(getDb(), limit)
: []; : [];

View File

@@ -0,0 +1,13 @@
import type { RequestHandler } from './$types';
import { json } from '@sveltejs/kit';
import { getDb } from '$lib/server/db';
import { clearCart, listShoppingList } from '$lib/server/shopping/repository';
export const GET: RequestHandler = async () => {
return json(listShoppingList(getDb()));
};
export const DELETE: RequestHandler = async () => {
clearCart(getDb());
return json({ ok: true });
};

View File

@@ -0,0 +1,23 @@
import type { RequestHandler } from './$types';
import { json } from '@sveltejs/kit';
import { z } from 'zod';
import { getDb } from '$lib/server/db';
import { validateBody } from '$lib/server/api-helpers';
import { toggleCheck } from '$lib/server/shopping/repository';
const CheckSchema = z.object({
name_key: z.string().min(1).max(200),
unit_key: z.string().max(50) // kann leer sein
});
export const POST: RequestHandler = async ({ request }) => {
const data = validateBody(await request.json().catch(() => null), CheckSchema);
toggleCheck(getDb(), data.name_key, data.unit_key, true);
return json({ ok: true });
};
export const DELETE: RequestHandler = async ({ request }) => {
const data = validateBody(await request.json().catch(() => null), CheckSchema);
toggleCheck(getDb(), data.name_key, data.unit_key, false);
return json({ ok: true });
};

View File

@@ -0,0 +1,9 @@
import type { RequestHandler } from './$types';
import { json } from '@sveltejs/kit';
import { getDb } from '$lib/server/db';
import { clearCheckedItems } from '$lib/server/shopping/repository';
export const DELETE: RequestHandler = async () => {
clearCheckedItems(getDb());
return json({ ok: true });
};

View File

@@ -0,0 +1,18 @@
import type { RequestHandler } from './$types';
import { json } from '@sveltejs/kit';
import { z } from 'zod';
import { getDb } from '$lib/server/db';
import { validateBody } from '$lib/server/api-helpers';
import { addRecipeToCart } from '$lib/server/shopping/repository';
const AddSchema = z.object({
recipe_id: z.number().int().positive(),
servings: z.number().int().min(1).max(50).optional(),
profile_id: z.number().int().positive().optional()
});
export const POST: RequestHandler = async ({ request }) => {
const data = validateBody(await request.json().catch(() => null), AddSchema);
addRecipeToCart(getDb(), data.recipe_id, data.profile_id ?? null, data.servings);
return json({ ok: true }, { status: 201 });
};

Some files were not shown because too many files have changed in this diff Show More