All files / src/pages GeneratePage.tsx

80.76% Statements 63/78
64.15% Branches 34/53
60.86% Functions 14/23
85.71% Lines 60/70

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237                                                                1x     34x 24x 24x       34x 34x 34x 34x   34x 34x 4x 4x     34x 4x 4x   34x 34x     34x   4x     34x 9x       3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x         3x 2x 2x 2x   1x 1x   3x       34x 34x 34x 34x 3x 3x     34x       34x                     24x 24x                 24x 24x   24x                                     18x 3x   18x                                                     7x 7x 7x                 7x                                                               2x                      
import {useEffect, useState} from 'react'
import type {Dispatch, SetStateAction} from 'react'
import {Outlet, useLocation, useNavigate, useOutletContext} from 'react-router-dom'
import {useTranslation} from 'react-i18next'
import {ChevronRightIcon, PencilSquareIcon} from '@heroicons/react/24/outline'
import type {components} from '../api'
import {Button} from '../components/Button'
import {RecipeCard} from '../components/RecipeCard'
import {TagSelector} from '../components/TagSelector'
import {tagsById} from '../recipeFormat'
import {localizeTagLabel} from '../locales/recipeTagLabels'
import {usePressPulse} from '../usePressPulse'
import {errorMessage} from '../apiError'
import {SessionExpiredError, useApi} from '../useApi'
import {currentLanguage} from '../i18n'
 
// id is stored on the recipe once it is saved
type Recipe = components['schemas']['RecipeInput'] & { id?: number }
type RecipeRequest = components['schemas']['RecipeRequest']
 
export interface RecipeGenerationContext {
	prompt: string
	setPrompt: Dispatch<SetStateAction<string>>
	selectedTags: string[]
	setSelectedTags: Dispatch<SetStateAction<string[]>>
	recipes: Recipe[]
	setRecipes: Dispatch<SetStateAction<Recipe[]>>
	status: string | null
	loading: boolean
	generate: () => void
}
 
const VIEW_ORDER = {options: 0, results: 1, recipe: 2} as const
 
function viewName(pathname: string): keyof typeof VIEW_ORDER {
	if (pathname.startsWith('/generate/results')) return 'results'
	Iif (pathname.startsWith('/generate/recipe')) return 'recipe'
	return 'options'
}
 
export function GenerateFlow() {
	const {t} = useTranslation()
	const apiFetch = useApi()
	const navigate = useNavigate()
	const {pathname} = useLocation()
 
	const [prompt, setPrompt] = useState(() => sessionStorage.getItem('recipe_prompt') ?? '')
	const [selectedTags, setSelectedTags] = useState<string[]>(() => {
		const stored = sessionStorage.getItem('recipe_tags')
		return stored ? (JSON.parse(stored) as string[]) : []
	})
	// keep the last results so the list is restored when returning from a recipe page
	const [recipes, setRecipes] = useState<Recipe[]>(() => {
		const stored = sessionStorage.getItem('generated_recipes')
		return stored ? (JSON.parse(stored) as Recipe[]) : []
	})
	const [status, setStatus] = useState<string | null>(null)
	const [loading, setLoading] = useState(false)
 
	// Confirm the session is still valid
	useEffect(() => {
		// on failure, this will automatically redirect to /login
		apiFetch('/users/profile')
	}, [apiFetch])
 
	useEffect(() => {
		sessionStorage.setItem('generated_recipes', JSON.stringify(recipes))
	}, [recipes])
 
	async function generate() {
		setLoading(true)
		setStatus(t('generate.generatingStatus'))
		setRecipes([])
		sessionStorage.setItem('recipe_prompt', prompt)
		sessionStorage.setItem('recipe_tags', JSON.stringify(selectedTags))
		navigate('/generate/results')
		try {
			const tagLabels = selectedTags.map((id) => tagsById.get(id)?.label).filter(Boolean)
			const fullPrompt = tagLabels.length > 0 ? `${prompt}\n\nPreferences: ${tagLabels.join(', ')}` : prompt
			const body: RecipeRequest = {prompt: fullPrompt, language: currentLanguage()}
			const response = await apiFetch('/ai/recipes', {
				method: 'POST',
				headers: {'content-type': 'application/json'},
				body: JSON.stringify(body),
			})
			if (!response.ok) throw new Error(await errorMessage(response))
			const data = (await response.json()) as Recipe[]
			setRecipes(data)
			setStatus(data.length === 0 ? t('generate.noRecipes') : null)
		} catch (e) {
			Iif (e instanceof SessionExpiredError) return
			setStatus(t('common.error', {message: e instanceof Error ? e.message : String(e)}))
		} finally {
			setLoading(false)
		}
	}
 
	const view = viewName(pathname)
	const [prevView, setPrevView] = useState(view)
	const [slideDirectionBack, setSlideDirectionBack] = useState(false)
	if (view !== prevView) {
		setSlideDirectionBack(VIEW_ORDER[view] < VIEW_ORDER[prevView])
		setPrevView(view)
	}
 
	const context: RecipeGenerationContext = {
		prompt, setPrompt, selectedTags, setSelectedTags, recipes, setRecipes, status, loading, generate,
	}
 
	return (
		<div
			key={view}
			className={`flex flex-col gap-4 ${slideDirectionBack ? 'animate-slide-from-left' : 'animate-slide-from-right'}`}
		>
			<Outlet context={context}/>
		</div>
	)
}
 
export function GeneratePage() {
	const {t} = useTranslation()
	const navigate = useNavigate()
	const {
		prompt,
		setPrompt,
		selectedTags,
		setSelectedTags,
		recipes,
		loading,
		generate
	} = useOutletContext<RecipeGenerationContext>()
	const [generateBtnRef, pulseGenerate] = usePressPulse<HTMLButtonElement>()
 
	return (
		<>
			<div className="flex items-center justify-between gap-3">
				<h2 className="text-lg font-bold">{t('generate.heading')}</h2>
				{recipes.length > 0 && (
					<button
						type="button"
						className="flex shrink-0 items-center gap-1 text-sm text-gray-500 dark:text-neutral-400 cursor-pointer transition-transform duration-100 hover:scale-98"
						onClick={() => navigate('/generate/results')}
					>
						{t('generate.viewRecipes')}
						<ChevronRightIcon className="h-4 w-4"/>
					</button>
				)}
			</div>
			<textarea
				className="w-full min-h-32 border border-gray-300 dark:border-neutral-600 rounded p-3"
				placeholder={t('generate.placeholder')}
				value={prompt}
				onChange={(e) => setPrompt(e.target.value)}
				onFocus={(e) => e.target.select()}
				onKeyDown={(e) => {
					Iif (e.key === 'Enter' && !e.shiftKey) {
						// on Enter: directly submit instead of adding a new line
						e.preventDefault()
						if (!loading && (prompt.trim() !== '' || selectedTags.length > 0)) {
							pulseGenerate()
							generate()
						}
					}
				}}
			/>
 
			<TagSelector selectedTags={selectedTags} onChange={setSelectedTags}/>
 
			<Button
				ref={generateBtnRef}
				type="button"
				className="self-center"
				onClick={generate}
				disabled={loading || (prompt.trim() === '' && selectedTags.length === 0)}
			>
				{loading ? t('generate.generating') : t('generate.generate')}
			</Button>
		</>
	)
}
 
export function GenerateResultsPage() {
	const {t, i18n} = useTranslation()
	const navigate = useNavigate()
	const {prompt, selectedTags, recipes, status, setRecipes} = useOutletContext<RecipeGenerationContext>()
 
	function handleSavedIdChange(index: number, newId: number | undefined) {
		setRecipes((prev) => prev.map((prevRecipe, prevIndex) => (prevIndex === index ? {
			...prevRecipe,
			id: newId
		} : prevRecipe)))
	}
 
	return (
		<>
			<div className="flex items-start justify-between gap-3 rounded-lg border border-gray-200 dark:border-neutral-700 bg-gray-50 dark:bg-neutral-800 p-3">
				<div className="flex min-w-0 flex-col gap-2">
					{prompt.trim() !== '' && <p className="text-sm text-gray-700 dark:text-neutral-200 line-clamp-2">{prompt}</p>}
					{selectedTags.length > 0 && (
						<div className="flex flex-wrap gap-1.5">
							{selectedTags.map((id) => (
								<span key={id}
								      className="rounded-full border border-gray-200 dark:border-neutral-700 bg-white dark:bg-neutral-800 px-2 py-0.5 text-xs text-gray-600 dark:text-neutral-300">
									{localizeTagLabel(id, tagsById.get(id)?.label ?? id, i18n.language)}
								</span>
							))}
						</div>
					)}
					{prompt.trim() === '' && selectedTags.length === 0 && (
						<p className="text-sm text-gray-400 dark:text-neutral-500">{t('generate.noOptions')}</p>
					)}
				</div>
				<button
					type="button"
					className="flex shrink-0 items-center gap-1 self-start text-sm text-gray-500 dark:text-neutral-400 cursor-pointer transition-transform duration-100 hover:scale-98"
					onClick={() => navigate('/generate')}
				>
					<PencilSquareIcon className="h-4 w-4"/>
					{t('generate.edit')}
				</button>
			</div>
 
			{status && <p className="text-gray-600 dark:text-neutral-300">{status}</p>}
 
			{recipes.map((recipe, index) => (
				<RecipeCard
					key={index}
					recipe={recipe}
					recipeId={recipe.id}
					onSavedIdChange={(newId) => handleSavedIdChange(index, newId)}
					onOpen={() => navigate('/generate/recipe', {state: {index}})}
				/>
			))}
		</>
	)
}