Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/web/src/features/auth/authNavigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ describe('authentication return navigation', () => {
{ from: '/recipes/11111111-1111-4111-8111-111111111111/editor' },
{ from: '/recipes/11111111-1111-4111-8111-111111111111/edit/../../x' },
{ from: '//example.com/recipes/11111111-1111-4111-8111-111111111111/edit' },
{ from: '/recipes/11111111-1111-4111-8111-111111111111' },
])('falls back for unsafe or unsupported state %j', (state) => {
expect(getAuthReturnPath(state)).toBe('/');
});
it.each([
'/cookbook?view=saved#recipes',
'/recipes/new',
'/recipes/11111111-1111-4111-8111-111111111111?view=full#ingredients',
'/recipes/11111111-1111-4111-8111-111111111111/edit',
])('preserves protected destination %s', (from) => {
expect(getAuthReturnPath({ from })).toBe(from);
Expand Down
10 changes: 5 additions & 5 deletions apps/web/src/features/auth/authNavigation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// The recipe editor of one recipe id; nothing else under /recipes/.
const EDIT_ROUTE =
/^\/recipes\/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\/edit$/;
// One recipe detail or editor route; nothing else under /recipes/.
const RECIPE_ROUTE =
/^\/recipes\/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(?:\/edit)?$/;

// Only return to known protected routes. Never navigate to a supplied origin.
// Only return to known recipe and protected routes. Never navigate to a supplied origin.
export function getAuthReturnPath(state: unknown): string {
if (typeof state !== 'object' || state === null || !('from' in state))
return '/';
Expand All @@ -11,7 +11,7 @@ export function getAuthReturnPath(state: unknown): string {
const pathname = from.split(/[?#]/)[0] ?? '';
return pathname === '/cookbook' ||
pathname === '/recipes/new' ||
EDIT_ROUTE.test(pathname)
RECIPE_ROUTE.test(pathname)
? from
: '/';
}
10 changes: 8 additions & 2 deletions apps/web/src/features/cookbook/CookbookControls.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
.cookbook-controls__desktop {
display: flex;
flex-wrap: nowrap;
align-items: center;
align-items: flex-start;
gap: var(--space-3);

min-width: 0;
Expand Down Expand Up @@ -61,8 +61,14 @@
width: 15.5rem;
}

.cookbook-controls__custom-time-input {
.cookbook-controls__desktop-control--custom-time {
flex: 0 0 9rem;
width: 9rem;
overflow-wrap: anywhere;
}

.cookbook-controls__custom-time-input {
width: 100%;

background-color: var(--color-surface-raised);
border-color: var(--color-border-input);
Expand Down
156 changes: 99 additions & 57 deletions apps/web/src/features/cookbook/CookbookControls.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import {
clampCustomTime,
customTimeError,
customTimeText,
} from '../../lib/recipeBrowse/browseValidation';
import { useCustomTimeInput } from '../../lib/recipeBrowse/useCustomTimeInput';

import Button from '../../components/ui/Button/Button';
import Field from '../../components/ui/Field/Field';
import FeedbackMessage from '../../components/ui/FeedbackMessage/FeedbackMessage';
import FilterChip from '../../components/ui/FilterChip/FilterChip';
import { Icon } from '../../components/ui/Icon/Icon';
import Radio from '../../components/ui/Radio/Radio';
Expand Down Expand Up @@ -32,23 +39,14 @@ import {
import './CookbookControls.css';

export type CookbookControlsProps = {
navigationKey?: string;
scope: CookbookScope;
browseState: CookbookBrowseState;
onScopeChange: (scope: CookbookScope) => void;
onBrowseStateChange: (value: CookbookBrowseState) => void;
onFiltersChange: (scope: CookbookScope, value: CookbookBrowseState) => void;
};

function parseCustomMaxMinutes(value: string) {
if (value.trim() === '') {
return null;
}

const parsed = Number(value);

return Number.isInteger(parsed) && parsed > 0 ? parsed : null;
}

function getScopeSelectLabel(scope: CookbookScope) {
switch (scope) {
case 'saved':
Expand All @@ -65,12 +63,20 @@ function getScopeSelectLabel(scope: CookbookScope) {
function CookbookControls({
scope,
browseState,
navigationKey = '',
onScopeChange,
onBrowseStateChange,
onFiltersChange,
}: CookbookControlsProps) {
const [filterSheetOpen, setFilterSheetOpen] = useState(false);
const [sortSheetOpen, setSortSheetOpen] = useState(false);
const [focusMobileTime, setFocusMobileTime] = useState(false);
const desktopTimeRef = useRef<HTMLInputElement>(null);
const mobileTimeRef = useRef<HTMLInputElement>(null);

useEffect(() => {
if (filterSheetOpen && focusMobileTime) mobileTimeRef.current?.focus();
}, [filterSheetOpen, focusMobileTime]);

const [scopeDraft, setScopeDraft] = useState<CookbookScope>(scope);

Expand All @@ -81,7 +87,25 @@ function CookbookControls({
});

const [sortDraft, setSortDraft] = useState<CookbookSort>(browseState.sort);
const [showCustomTimeError, setShowCustomTimeError] = useState(false);
const [mobileTime, setMobileTime] = useState('');
const resetKey = JSON.stringify([navigationKey, browseState, scope]);
const desktopTime = useCustomTimeInput(
browseState.customMaxMinutes,
resetKey,
(minutes, raw) => {
onBrowseStateChange({
...browseState,
cookingTime: 'custom',
customMaxMinutes: minutes,
customMaxMinutesRaw: raw,
});
},
browseState.customMaxMinutesRaw,
);
const committedTimeError =
browseState.cookingTime === 'custom'
? customTimeError(clampCustomTime(customTimeText(browseState)))
: undefined;

const activeFilterCount =
countActiveRecipeFilters(browseState) + (scope === 'all' ? 0 : 1);
Expand All @@ -100,17 +124,13 @@ function CookbookControls({
cookingTime,
customMaxMinutes:
cookingTime === 'custom' ? browseState.customMaxMinutes : null,
customMaxMinutesRaw:
cookingTime === 'custom' ? browseState.customMaxMinutesRaw : undefined,
});
}

function handleDesktopCustomMaxChange(rawValue: string) {
updateBrowseState({
cookingTime: 'custom',
customMaxMinutes: parseCustomMaxMinutes(rawValue),
});
}

function openFilterSheet() {
function openFilterSheet(focusTime = false) {
setFocusMobileTime(focusTime);
setScopeDraft(scope);

setFilterDraft({
Expand All @@ -119,33 +139,40 @@ function CookbookControls({
customMaxMinutes: browseState.customMaxMinutes,
});

setShowCustomTimeError(false);
setMobileTime(customTimeText(browseState));
setFilterSheetOpen(true);
}

function handleDraftCookingTimeChange(cookingTime: RecipeCookingTime) {
if (cookingTime === filterDraft.cookingTime) return;

setFilterDraft((current) => ({
...current,
cookingTime,
customMaxMinutes:
cookingTime === 'custom' ? current.customMaxMinutes : null,
}));

setShowCustomTimeError(false);
setMobileTime('');
}

function handleApplyFilters() {
if (
filterDraft.cookingTime === 'custom' &&
filterDraft.customMaxMinutes === null
) {
setShowCustomTimeError(true);
const clamped = clampCustomTime(mobileTime);
if (filterDraft.cookingTime === 'custom' && customTimeError(clamped)) {
return;
}

onFiltersChange(scopeDraft, {
...browseState,
...filterDraft,
customMaxMinutes:
filterDraft.cookingTime === 'custom' && clamped.trim()
? Number(clamped)
: null,
customMaxMinutesRaw:
filterDraft.cookingTime === 'custom' && customTimeError(mobileTime)
? mobileTime
: undefined,
});

setFilterSheetOpen(false);
Expand All @@ -158,7 +185,15 @@ function CookbookControls({
...DEFAULT_RECIPE_FILTER_STATE,
});

setShowCustomTimeError(false);
setMobileTime('');
}

function editTimeFilter() {
if (desktopTimeRef.current?.getClientRects().length) {
desktopTimeRef.current.focus();
} else {
openFilterSheet(true);
}
}

function openSortSheet() {
Expand Down Expand Up @@ -245,19 +280,25 @@ function CookbookControls({
</div>

{browseState.cookingTime === 'custom' && (
<div className="cookbook-controls__desktop-control">
<div className="cookbook-controls__desktop-control cookbook-controls__desktop-control--custom-time">
<Field
ref={desktopTimeRef}
label="Maximum cooking time"
labelVisuallyHidden
className="cookbook-controls__custom-time-input"
type="number"
min="1"
step="1"
type="text"
inputMode="numeric"
value={browseState.customMaxMinutes ?? ''}
value={desktopTime.text}
error={desktopTime.error}
onBlur={desktopTime.apply}
helpText={
desktopTime.noLimit ? (
<span className="sr-only">No time limit applied.</span>
) : undefined
}
placeholder="Max minutes"
onChange={(event) =>
handleDesktopCustomMaxChange(event.currentTarget.value)
desktopTime.change(event.currentTarget.value)
}
/>
</div>
Expand Down Expand Up @@ -290,7 +331,7 @@ function CookbookControls({
<Button
className="cookbook-controls__mobile-button"
variant="secondary"
onClick={openFilterSheet}
onClick={() => openFilterSheet()}
>
<Icon icon={FilterIcon} size="sm" />

Expand All @@ -310,6 +351,18 @@ function CookbookControls({
</div>
</div>

{committedTimeError && (
<FeedbackMessage tone="error" title="Invalid time filter">
<p>
Recipes cannot be loaded until the maximum cooking time is corrected
or cleared.
</p>
<Button variant="secondary" size="sm" onClick={editTimeFilter}>
Edit time filter
</Button>
</FeedbackMessage>
)}

<Sheet
open={filterSheetOpen}
title="Filter recipes"
Expand Down Expand Up @@ -397,32 +450,21 @@ function CookbookControls({
{filterDraft.cookingTime === 'custom' && (
<div className="cookbook-controls__custom-time">
<Field
ref={mobileTimeRef}
label="Maximum cooking time in minutes"
type="number"
min="1"
step="1"
type="text"
inputMode="numeric"
value={filterDraft.customMaxMinutes ?? ''}
value={mobileTime}
placeholder="e.g. 45"
helpText="Enter a positive whole number of minutes."
error={
showCustomTimeError
? 'Enter a maximum cooking time before applying filters.'
: undefined
helpText={
mobileTime.trim() ? (
'Enter a whole number of minutes.'
) : (
<span className="sr-only">No time limit applied.</span>
)
}
onChange={(event) => {
const customMaxMinutes = parseCustomMaxMinutes(
event.currentTarget.value,
);

setFilterDraft((current) => ({
...current,
cookingTime: 'custom',
customMaxMinutes,
}));

setShowCustomTimeError(false);
}}
error={customTimeError(mobileTime)}
onChange={(event) => setMobileTime(event.currentTarget.value)}
/>
</div>
)}
Expand Down
16 changes: 15 additions & 1 deletion apps/web/src/features/cookbook/CookbookPage.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
.cookbook-page__search-form {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
align-items: end;
align-items: start;
gap: var(--space-3);

width: min(100%, 52rem);
Expand Down Expand Up @@ -76,7 +76,21 @@
grid-template-columns: 1fr;
}

/* Keep the stacked button in place when field feedback appears. */
.cookbook-page__search-form > .field {
display: contents;
}

.cookbook-page__search-form .field__control {
grid-row: 1;
}

.cookbook-page__search-form .field__error {
grid-row: 3;
}

.cookbook-page__search-button {
grid-row: 2;
width: 100%;
}

Expand Down
Loading