Skip to content

feat(ui): random recipe button scoped to the current folder - #554

Merged
LeMyst merged 1 commit into
cooklang:mainfrom
LeMyst:feat/random-recipe
Sep 27, 2026
Merged

LeMyst merged 1 commit into
cooklang:mainfrom
LeMyst:feat/random-recipe

Conversation

@LeMyst

@LeMyst LeMyst commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

Closes #544

What

A dice icon button next to New recipe on the recipe listing opens a random recipe from the folder being browsed, subfolders included. On / the pick spans the whole collection; on /directory/Mains it only picks from Mains/ and below (e.g. Mains/Pasta/…).

How

  • Server — GET /random and GET /random/{*path} (src/server/ui.rs) validate the folder with is_safe_relative_path, walk it with cooklang_find::build_tree, pick one .cook recipe and answer 303 See Other to /recipe/<path>. No recipe list is sent to the page.
    • Menus (.menu) are never picked.
    • A folder with nothing to pick (only menus) answers 404, an unsafe path 400, and a missing folder 404.
    • The Location is percent-encoded per segment, because axum's Redirect refuses non-ASCII headers (e.g. Crème brûlée).
  • Listing — build_recipes_template sets a random_recipe_url only when the subtree holds at least one .cook recipe. The template renders an .icon-btn with a translated aria-label/title only when that URL is set, so the button is hidden for empty or menu-only folders.
  • Static site (cook build web) — the button is hidden: there's no server to redirect, and embedding every recipe path in each listing page felt heavier than this nice-to-have deserves. A client-side pick could be a follow-up.
  • i18n — random-recipe added to all 8 locales.
  • Small refactor — the per-segment URL encoder from build/sitemap.rs moved to util::encode_url_path so the sitemap and the new route share it.
  • Dependency — fastrand (already in the lock file transitively) becomes an optional dependency of the server feature.

Tests

  • tests/random_recipe_test.rs (spawns cook server through common::with_isolated_config) checks that:
    • a pick in a subfolder stays inside it, subfolders included;
    • the root pick covers the whole collection but never a menu;
    • non-ASCII names are encoded, and the picked page loads;
    • the button links to the current folder;
    • a menu-only folder has no button and /random/… answers 404;
    • ..%2F is refused (400) and a missing folder gives 404.
  • tests/build.rs: the static index has no random button.
  • tests/e2e/navigation.spec.ts: clicking the dice on /directory/Breakfast lands on a /recipe/Breakfast/… page.

cargo fmt, cargo clippy --all-targets (no warnings) and cargo test pass. The full chromium Playwright project also passes locally.

🤖 Generated with Claude Code

A dice icon next to "New recipe" on the listing redirects to a random
.cook recipe from the folder being browsed and its subfolders, through
GET /random and GET /random/{*path}. Menus are never picked; the button
is hidden when there is nothing to pick and on static sites, which have
no server to redirect.

The per-segment URL encoder moves from the sitemap to util so the new
redirect can share it: axum's Redirect refuses a non-ASCII Location.

Closes cooklang#544

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@LeMyst
LeMyst marked this pull request as ready for review September 27, 2026 00:43
@LeMyst
LeMyst requested a review from dubadub September 27, 2026 00:43

@dubadub dubadub left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting idea, lets give it a try

@LeMyst
LeMyst merged commit 73b095b into cooklang:main Sep 27, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(ui): random recipe button scoped to the current folder

2 participants